Skip to content

Commit

Permalink
Fixed receive_some ignoring max_bytes when reading from buffer
Browse files Browse the repository at this point in the history
Fixes nedbat#31.
  • Loading branch information
agronholm committed Nov 13, 2018
1 parent 3c97c88 commit 419c77e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion anyio/_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def buffered_data(self) -> bytes:

async def receive_some(self, max_bytes: Optional[int]) -> bytes:
if self._buffer:
data, self._buffer = self._buffer, b''
data, self._buffer = self._buffer[:max_bytes], self._buffer[max_bytes:]
return data

return await self._socket.recv(max_bytes)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ async def server():

assert response == b'halb'

@pytest.mark.anyio
async def test_receive_some_from_cache(self):
async def server():
async with await stream_server.accept() as stream:
await stream.receive_until(b'a', 10)
request = await stream.receive_some(1)
await stream.send_all(request + b'\n')

received = None
async with create_task_group() as tg:
async with await create_tcp_server(interface='localhost') as stream_server:
await tg.spawn(server)
async with await connect_tcp('localhost', stream_server.port) as client:
await client.send_all(b'abc')
received = await client.receive_until(b'\n', 3)

assert received == b'b'

@pytest.mark.parametrize('method_name, params', [
('receive_until', [b'\n', 100]),
('receive_exactly', [5])
Expand Down

0 comments on commit 419c77e

Please sign in to comment.