|
1 |
| -# Copyright 2020 Bruce Merry |
| 1 | +# Copyright 2020, 2024 Bruce Merry |
2 | 2 | #
|
3 | 3 | # This file is part of async-solipsism.
|
4 | 4 | #
|
|
15 | 15 | # You should have received a copy of the GNU General Public License
|
16 | 16 | # along with async-solipsism. If not, see <https://www.gnu.org/licenses/>.
|
17 | 17 |
|
| 18 | +import array |
18 | 19 | import socket
|
| 20 | +import struct |
19 | 21 |
|
20 | 22 | import pytest
|
21 | 23 |
|
@@ -69,3 +71,41 @@ def test_use_after_shutdown(end):
|
69 | 71 | assert sock0.recv(1) == b''
|
70 | 72 | with pytest.raises(BrokenPipeError):
|
71 | 73 | 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