Merge pull request #25 from totokaka/master

Raise IOError when TCPConnection.read(length) recieves no data.
This commit is contained in:
Dinner Bone
2015-01-15 16:55:36 +01:00
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"))