Skip to content

Commit b106f36

Browse files
author
Dana Powers
committed
Move common connection testing code to setUp method
1 parent af899ab commit b106f36

File tree

1 file changed

+45
-98
lines changed

1 file changed

+45
-98
lines changed

test/test_conn.py

Lines changed: 45 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
class ConnTest(unittest2.TestCase):
1313
def setUp(self):
14+
self.config = {
15+
'host': 'localhost',
16+
'port': 9090,
17+
'request_id': 0,
18+
'payload': 'test data'
19+
}
20+
1421
# Mocking socket.create_connection will cause _sock to always be a
1522
# MagicMock()
1623
patcher = mock.patch('socket.create_connection', spec=True)
@@ -20,6 +27,9 @@ def setUp(self):
2027
self.MockCreateConn().sendall.return_value = None
2128
self.addCleanup(patcher.stop)
2229

30+
self.conn = KafkaConnection(self.config['host'], self.config['port'])
31+
socket.create_connection.reset_mock()
32+
2333
def test_collect_hosts__happy_path(self):
2434
hosts = "localhost:1234,localhost"
2535
results = collect_hosts(hosts)
@@ -52,168 +62,105 @@ def test_collect_hosts__with_spaces(self):
5262
]))
5363

5464
def test_send(self):
55-
fake_config = {
56-
'host': 'localhost',
57-
'port': 9090,
58-
'request_id': 0,
59-
'payload': 'test data'
60-
}
61-
62-
assert socket.create_connection is self.MockCreateConn
63-
conn = KafkaConnection(fake_config['host'], fake_config['port'])
64-
socket.create_connection.reset_mock()
65-
conn.send(fake_config['request_id'], fake_config['payload'])
66-
conn._sock.sendall.assert_called_with(fake_config['payload'])
65+
self.conn.send(self.config['request_id'], self.config['payload'])
66+
self.conn._sock.sendall.assert_called_with(self.config['payload'])
6767

6868
def test_init_creates_socket_connection(self):
69-
fake_config = {
70-
'host': 'localhost',
71-
'port': 9090,
72-
}
73-
74-
assert socket.create_connection is self.MockCreateConn
75-
socket.create_connection.reset_mock()
76-
KafkaConnection(fake_config['host'], fake_config['port'])
77-
socket.create_connection.assert_called_with((fake_config['host'], fake_config['port']), DEFAULT_SOCKET_TIMEOUT_SECONDS)
69+
KafkaConnection(self.config['host'], self.config['port'])
70+
socket.create_connection.assert_called_with((self.config['host'], self.config['port']), DEFAULT_SOCKET_TIMEOUT_SECONDS)
7871

7972
def test_init_failure_raises_connection_error(self):
80-
fake_config = {
81-
'host': 'localhost',
82-
'port': 9090,
83-
}
8473

8574
def raise_error(*args):
8675
raise socket.error
8776

8877
assert socket.create_connection is self.MockCreateConn
8978
socket.create_connection.side_effect=raise_error
9079
with self.assertRaises(ConnectionError):
91-
KafkaConnection(fake_config['host'], fake_config['port'])
80+
KafkaConnection(self.config['host'], self.config['port'])
9281

9382
def test_send__reconnects_on_dirty_conn(self):
94-
fake_config = {
95-
'host': 'localhost',
96-
'port': 9090,
97-
'request_id': 0,
98-
'payload': 'test data'
99-
}
10083

101-
# Get a connection (with socket mocked)
102-
assert socket.create_connection is self.MockCreateConn
103-
conn = KafkaConnection(fake_config['host'], fake_config['port'])
104-
105-
# Dirty it
84+
# Dirty the connection
85+
assert self.conn._dirty is False
10686
try:
107-
conn._raise_connection_error()
87+
self.conn._raise_connection_error()
10888
except ConnectionError:
10989
pass
110-
111-
# Reset the socket call counts
112-
socket.create_connection.reset_mock()
113-
self.assertEqual(socket.create_connection.call_count, 0)
90+
assert self.conn._dirty is True
11491

11592
# Now test that sending attempts to reconnect
116-
conn.send(fake_config['request_id'], fake_config['payload'])
93+
self.assertEqual(socket.create_connection.call_count, 0)
94+
self.conn.send(self.config['request_id'], self.config['payload'])
11795
self.assertEqual(socket.create_connection.call_count, 1)
11896

11997
# A second way to dirty it...
120-
conn.close()
98+
self.conn.close()
12199

122100
# Reset the socket call counts
123101
socket.create_connection.reset_mock()
124102
self.assertEqual(socket.create_connection.call_count, 0)
125103

126104
# Now test that sending attempts to reconnect
127-
conn.send(fake_config['request_id'], fake_config['payload'])
105+
self.conn.send(self.config['request_id'], self.config['payload'])
128106
self.assertEqual(socket.create_connection.call_count, 1)
129107

130-
131108
def test_send__failure_sets_dirty_connection(self):
132-
fake_config = {
133-
'host': 'localhost',
134-
'port': 9090,
135-
'request_id': 0,
136-
'payload': 'test data'
137-
}
138109

139110
def raise_error(*args):
140111
raise socket.error
141112

142-
# Get a connection (with socket mocked)
143-
assert socket.create_connection is self.MockCreateConn
144-
conn = KafkaConnection(fake_config['host'], fake_config['port'])
113+
assert self.conn._dirty is False
145114

146-
assert isinstance(conn._sock, mock.Mock)
147-
conn._sock.sendall.side_effect=raise_error
115+
assert isinstance(self.conn._sock, mock.Mock)
116+
self.conn._sock.sendall.side_effect=raise_error
148117
try:
149-
conn.send(fake_config['request_id'], fake_config['payload'])
118+
self.conn.send(self.config['request_id'], self.config['payload'])
150119
except ConnectionError:
151-
self.assertEquals(conn._dirty, True)
120+
self.assertEquals(self.conn._dirty, True)
152121

153122
def test_recv(self):
154-
fake_config = {
155-
'host': 'localhost',
156-
'port': 9090,
157-
'request_id': 0,
158-
'payload': 'some test data',
159-
}
160-
161-
# Get a connection
162-
assert socket.create_connection is self.MockCreateConn
163-
conn = KafkaConnection(fake_config['host'], fake_config['port'])
164123

165124
# A function to mock _read_bytes
166-
conn._mock_sent_size = False
167-
conn._mock_data_sent = 0
125+
self.conn._mock_sent_size = False
126+
self.conn._mock_data_sent = 0
168127
def mock_socket_recv(num_bytes):
169-
if not conn._mock_sent_size:
128+
if not self.conn._mock_sent_size:
170129
assert num_bytes == 4
171-
conn._mock_sent_size = True
172-
return struct.pack('>i', len(fake_config['payload']))
130+
self.conn._mock_sent_size = True
131+
return struct.pack('>i', len(self.config['payload']))
173132

174-
recv_data = struct.pack('>%ds' % num_bytes, fake_config['payload'][conn._mock_data_sent:conn._mock_data_sent+num_bytes])
175-
conn._mock_data_sent += num_bytes
133+
recv_data = struct.pack('>%ds' % num_bytes, self.config['payload'][self.conn._mock_data_sent:self.conn._mock_data_sent+num_bytes])
134+
self.conn._mock_data_sent += num_bytes
176135
return recv_data
177136

178-
with mock.patch.object(conn, '_read_bytes', new=mock_socket_recv):
179-
self.assertEquals(conn.recv(fake_config['request_id']), fake_config['payload'])
137+
with mock.patch.object(self.conn, '_read_bytes', new=mock_socket_recv):
138+
self.assertEquals(self.conn.recv(self.config['request_id']), self.config['payload'])
180139

181140
def test_recv__reconnects_on_dirty_conn(self):
182-
fake_config = {
183-
'host': 'localhost',
184-
'port': 9090,
185-
'request_id': 0,
186-
'payload': 'some test data',
187-
}
188141

189-
# Get a connection
190-
assert socket.create_connection is self.MockCreateConn
191-
conn = KafkaConnection(fake_config['host'], fake_config['port'])
192-
193-
# Dirty it
142+
# Dirty the connection
194143
try:
195-
conn._raise_connection_error()
144+
self.conn._raise_connection_error()
196145
except ConnectionError:
197146
pass
198-
199-
# Reset the socket call counts
200-
socket.create_connection.reset_mock()
201-
self.assertEqual(socket.create_connection.call_count, 0)
147+
assert self.conn._dirty is True
202148

203149
# Now test that recv'ing attempts to reconnect
204-
conn._sock.recv.return_value = fake_config['payload']
205-
conn._read_bytes(len(fake_config['payload']))
150+
self.assertEqual(socket.create_connection.call_count, 0)
151+
self.conn._sock.recv.return_value = self.config['payload']
152+
self.conn._read_bytes(len(self.config['payload']))
206153
self.assertEqual(socket.create_connection.call_count, 1)
207154

208155
# A second way to dirty it...
209-
conn.close()
156+
self.conn.close()
210157

211158
# Reset the socket call counts
212159
socket.create_connection.reset_mock()
213160
self.assertEqual(socket.create_connection.call_count, 0)
214161

215162
# Now test that recv'ing attempts to reconnect
216-
conn._read_bytes(len(fake_config['payload']))
163+
self.conn._read_bytes(len(self.config['payload']))
217164
self.assertEqual(socket.create_connection.call_count, 1)
218165

219166
@unittest2.skip("Not Implemented")

0 commit comments

Comments
 (0)