Skip to content

Commit f862774

Browse files
author
Dana Powers
committed
Add a mock to sock.recv() in the test_conn setUp method. Returns 2 packet payloads, then '', then error
1 parent 1a7f808 commit f862774

File tree

1 file changed

+25
-61
lines changed

1 file changed

+25
-61
lines changed

test/test_conn.py

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,35 @@ def setUp(self):
1313
'host': 'localhost',
1414
'port': 9090,
1515
'request_id': 0,
16-
'payload': 'test data'
16+
'payload': 'test data',
17+
'payload2': 'another packet'
1718
}
1819

1920
# Mocking socket.create_connection will cause _sock to always be a
2021
# MagicMock()
2122
patcher = mock.patch('socket.create_connection', spec=True)
2223
self.MockCreateConn = patcher.start()
24+
self.addCleanup(patcher.stop)
2325

2426
# Also mock socket.sendall() to appear successful
25-
self.MockCreateConn().sendall.return_value = None
26-
self.addCleanup(patcher.stop)
27+
socket.create_connection().sendall.return_value = None
28+
29+
# And mock socket.recv() to return two payloads, then '', then raise
30+
# Note that this currently ignores the num_bytes parameter to sock.recv()
31+
payload_size = len(self.config['payload'])
32+
payload2_size = len(self.config['payload2'])
33+
socket.create_connection().recv.side_effect = [
34+
struct.pack('>i', payload_size),
35+
struct.pack('>%ds' % payload_size, self.config['payload']),
36+
struct.pack('>i', payload2_size),
37+
struct.pack('>%ds' % payload2_size, self.config['payload2']),
38+
''
39+
]
2740

28-
# And mock socket.recv() to return the payload
29-
self.MockCreateConn().recv.return_value = self.config['payload']
41+
# Create a connection object
3042
self.conn = KafkaConnection(self.config['host'], self.config['port'])
43+
44+
# Reset any mock counts caused by __init__
3145
socket.create_connection.reset_mock()
3246

3347
def test_collect_hosts__happy_path(self):
@@ -92,17 +106,6 @@ def test_send__reconnects_on_dirty_conn(self):
92106
self.conn.send(self.config['request_id'], self.config['payload'])
93107
self.assertEqual(socket.create_connection.call_count, 1)
94108

95-
# A second way to dirty it...
96-
self.conn.close()
97-
98-
# Reset the socket call counts
99-
socket.create_connection.reset_mock()
100-
self.assertEqual(socket.create_connection.call_count, 0)
101-
102-
# Now test that sending attempts to reconnect
103-
self.conn.send(self.config['request_id'], self.config['payload'])
104-
self.assertEqual(socket.create_connection.call_count, 1)
105-
106109
def test_send__failure_sets_dirty_connection(self):
107110

108111
def raise_error(*args):
@@ -117,21 +120,7 @@ def raise_error(*args):
117120

118121
def test_recv(self):
119122

120-
# A function to mock _read_bytes
121-
self.conn._mock_sent_size = False
122-
self.conn._mock_data_sent = 0
123-
def mock_socket_recv(num_bytes):
124-
if not self.conn._mock_sent_size:
125-
assert num_bytes == 4
126-
self.conn._mock_sent_size = True
127-
return struct.pack('>i', len(self.config['payload']))
128-
129-
recv_data = struct.pack('>%ds' % num_bytes, self.config['payload'][self.conn._mock_data_sent:self.conn._mock_data_sent+num_bytes])
130-
self.conn._mock_data_sent += num_bytes
131-
return recv_data
132-
133-
with mock.patch.object(self.conn, '_read_bytes', new=mock_socket_recv):
134-
self.assertEquals(self.conn.recv(self.config['request_id']), self.config['payload'])
123+
self.assertEquals(self.conn.recv(self.config['request_id']), self.config['payload'])
135124

136125
def test_recv__reconnects_on_dirty_conn(self):
137126

@@ -143,18 +132,7 @@ def test_recv__reconnects_on_dirty_conn(self):
143132

144133
# Now test that recv'ing attempts to reconnect
145134
self.assertEqual(socket.create_connection.call_count, 0)
146-
self.conn._read_bytes(len(self.config['payload']))
147-
self.assertEqual(socket.create_connection.call_count, 1)
148-
149-
# A second way to dirty it...
150-
self.conn.close()
151-
152-
# Reset the socket call counts
153-
socket.create_connection.reset_mock()
154-
self.assertEqual(socket.create_connection.call_count, 0)
155-
156-
# Now test that recv'ing attempts to reconnect
157-
self.conn._read_bytes(len(self.config['payload']))
135+
self.conn.recv(self.config['request_id'])
158136
self.assertEqual(socket.create_connection.call_count, 1)
159137

160138
def test_recv__failure_sets_dirty_connection(self):
@@ -171,24 +149,10 @@ def raise_error(*args):
171149
self.assertIsNone(self.conn._sock)
172150

173151
def test_recv__doesnt_consume_extra_data_in_stream(self):
174-
data1 = self.config['payload']
175-
size1 = len(data1)
176-
encoded1 = struct.pack('>i%ds' % size1, size1, data1)
177-
data2 = "an extra payload"
178-
size2 = len(data2)
179-
encoded2 = struct.pack('>i%ds' % size2, size2, data2)
180-
181-
self.conn._recv_buffer = encoded1
182-
self.conn._recv_buffer += encoded2
183-
184-
def mock_socket_recv(num_bytes):
185-
data = self.conn._recv_buffer[0:num_bytes]
186-
self.conn._recv_buffer = self.conn._recv_buffer[num_bytes:]
187-
return data
188-
189-
with mock.patch.object(self.conn._sock, 'recv', new=mock_socket_recv):
190-
self.assertEquals(self.conn.recv(self.config['request_id']), self.config['payload'])
191-
self.assertEquals(str(self.conn._recv_buffer), encoded2)
152+
153+
# Here just test that each call to recv will return a single payload
154+
self.assertEquals(self.conn.recv(self.config['request_id']), self.config['payload'])
155+
self.assertEquals(self.conn.recv(self.config['request_id']), self.config['payload2'])
192156

193157
def test_close__object_is_reusable(self):
194158

0 commit comments

Comments
 (0)