Raise IOError when TCPConnection.read(length) recieves no data.

This commit fixes #24 by simply checking whether the socket received any data at all. If it did not, an IOError is raised.
It might be that we should try to recieve multiple times before the IOError is raised, but this is not implemented in this commit as to avoid bloat.
This commit is contained in:
totokaka
2015-01-03 10:51:19 +01:00
parent 7321fef576
commit 240deb27d4
2 changed files with 10 additions and 1 deletions

View File

@@ -140,7 +140,10 @@ class TCPSocketConnection(Connection):
def read(self, length): def read(self, length):
result = bytearray() result = bytearray()
while len(result) < length: while len(result) < length:
result.extend(self.socket.recv(length - len(result))) new = self.socket.recv(length - len(result))
if len(new) == 0:
raise IOError("Server did not respond with any information!")
result.extend(new)
return result return result
def write(self, data): def write(self, data):

View File

@@ -225,6 +225,12 @@ class TCPSocketConnectionTest(TestCase):
self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA")) self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))
def test_read_empty(self):
self.connection.socket.recv.return_value = bytearray.fromhex("")
with self.assertRaises(IOError):
self.connection.read(2)
def test_write(self): def test_write(self):
self.connection.write(bytearray.fromhex("7FAA")) self.connection.write(bytearray.fromhex("7FAA"))