From b26b4df64f73537abf7dddd24038b6f7d7711df4 Mon Sep 17 00:00:00 2001 From: Nico Kemnitz Date: Thu, 10 Jan 2019 10:54:03 -0500 Subject: [PATCH] Fix BufferedSocket capacity calculation --- hyper/common/bufsocket.py | 2 +- test/test_socket.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/hyper/common/bufsocket.py b/hyper/common/bufsocket.py index 854681ca..61733583 100644 --- a/hyper/common/bufsocket.py +++ b/hyper/common/bufsocket.py @@ -54,7 +54,7 @@ def _remaining_capacity(self): """ The maximum number of bytes the buffer could still contain. """ - return self._buffer_size - self._index + return self._buffer_size - self._index - self._bytes_in_buffer @property def _buffer_end(self): diff --git a/test/test_socket.py b/test/test_socket.py index baf22818..6a6437e1 100644 --- a/test/test_socket.py +++ b/test/test_socket.py @@ -239,6 +239,20 @@ def test_socket_fill_resizes_if_needed(self): assert len(b.buffer) == 4 assert b._index == 0 + def test_socket_fill_resizes_if_needed_with_bytes_in_buffer(self): + s = DummySocket() + b = BufferedSocket(s) + + b._index = 982 + b._buffer_view[982:1000] = b'This is incomplete' + b._bytes_in_buffer += 18 + assert len(b.buffer) == 18 + + s.inbound_packets = [b' not anymore'] + b.fill() + assert len(b.buffer) == 30 + assert b._index == 0 + def test_socket_fill_raises_connection_errors(self): s = DummySocket() b = BufferedSocket(s)