Skip to content

Commit 2f067d7

Browse files
committed
Add tests for Socket updates
1 parent 0addc36 commit 2f067d7

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

test/test_socket.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 Bruce Merry
1+
# Copyright 2020, 2024 Bruce Merry
22
#
33
# This file is part of async-solipsism.
44
#
@@ -15,7 +15,9 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with async-solipsism. If not, see <https://www.gnu.org/licenses/>.
1717

18+
import array
1819
import socket
20+
import struct
1921

2022
import pytest
2123

@@ -69,3 +71,41 @@ def test_use_after_shutdown(end):
6971
assert sock0.recv(1) == b''
7072
with pytest.raises(BrokenPipeError):
7173
sock1.send(b'hello')
74+
75+
76+
def test_recv_into_array():
77+
sock1, sock2 = async_solipsism.socketpair()
78+
data = array.array("I", [1, 2, 3])
79+
sock1.send(b"\xDE\xAD\xBE\xEF")
80+
nbytes = sock2.recv_into(data)
81+
assert nbytes == 4
82+
assert data[0] == struct.unpack("I", b"\xDE\xAD\xBE\xEF")[0]
83+
assert data[1] == 2
84+
assert data[2] == 3
85+
86+
87+
def test_recv_into_array_partial():
88+
sock1, sock2 = async_solipsism.socketpair()
89+
data = array.array("I", [1, 2, 3])
90+
sock1.send(b"\xDE\xAD\xBE\xEF\xCA\xFE")
91+
nbytes = sock2.recv_into(data, 4)
92+
assert nbytes == 4
93+
assert data[0] == struct.unpack("I", b"\xDE\xAD\xBE\xEF")[0]
94+
assert data[1] == 2
95+
assert data[2] == 3
96+
97+
98+
def test_write_array():
99+
sock1, sock2 = async_solipsism.socketpair(capacity=100)
100+
data = array.array("I", range(64))
101+
nbytes = sock1.send(data)
102+
assert nbytes == 100
103+
received = sock2.recv(-1)
104+
assert len(received) == 100
105+
assert memoryview(received) == memoryview(data).cast("B")[:100]
106+
107+
108+
@pytest.mark.parametrize("nbytes", [-1, 5])
109+
def test_recv_into_bad_nbytes(sock, nbytes):
110+
with pytest.raises(ValueError):
111+
sock.recv_into(bytearray(4), nbytes=nbytes)

0 commit comments

Comments
 (0)