-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_conn.py
More file actions
440 lines (352 loc) · 15.6 KB
/
test_conn.py
File metadata and controls
440 lines (352 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# pylint: skip-file
from errno import EALREADY, EINPROGRESS, EISCONN, ECONNRESET
import socket
from unittest import mock
import pytest
from kafka.conn import BrokerConnection, ConnectionStates
from kafka.future import Future
from kafka.conn import BrokerConnection, ConnectionStates, SSLWantWriteError, VERSION_CHECKS
from kafka.metrics.metrics import Metrics
from kafka.metrics.stats.sensor import Sensor
from kafka.protocol.api import RequestHeader
from kafka.protocol.group import HeartbeatResponse
from kafka.protocol.metadata import MetadataRequest
from kafka.protocol.produce import ProduceRequest
import kafka.errors as Errors
@pytest.fixture
def dns_lookup(mocker):
return mocker.patch('kafka.conn.dns_lookup',
return_value=[(socket.AF_INET,
None, None, None,
('localhost', 9092))])
@pytest.fixture
def _socket(mocker):
socket = mocker.MagicMock()
socket.connect_ex.return_value = 0
socket.send.side_effect = lambda d: len(d)
socket.recv.side_effect = BlockingIOError("mocked recv")
mocker.patch('socket.socket', return_value=socket)
return socket
@pytest.fixture
def metrics(mocker):
metrics = mocker.MagicMock(Metrics)
metrics.mocked_sensors = {}
def sensor(name, **kwargs):
if name not in metrics.mocked_sensors:
metrics.mocked_sensors[name] = mocker.MagicMock(Sensor)
return metrics.mocked_sensors[name]
metrics.sensor.side_effect = sensor
return metrics
@pytest.fixture
def conn(_socket, dns_lookup, metrics, mocker):
conn = BrokerConnection('localhost', 9092, socket.AF_INET, metrics=metrics)
mocker.patch.object(conn, '_try_api_versions_check', return_value=True)
return conn
@pytest.mark.parametrize("states", [
(([EINPROGRESS, EALREADY], ConnectionStates.CONNECTING),),
(([EALREADY, EALREADY], ConnectionStates.CONNECTING),),
(([0], ConnectionStates.CONNECTED),),
(([EINPROGRESS, EALREADY], ConnectionStates.CONNECTING),
([ECONNRESET], ConnectionStates.DISCONNECTED)),
(([EINPROGRESS, EALREADY], ConnectionStates.CONNECTING),
([EALREADY], ConnectionStates.CONNECTING),
([EISCONN], ConnectionStates.CONNECTED)),
])
def test_connect(_socket, conn, states):
assert conn.state is ConnectionStates.DISCONNECTED
for errno, state in states:
_socket.connect_ex.side_effect = errno
conn.connect()
assert conn.state is state
def test_api_versions_check(_socket, mocker):
conn = BrokerConnection('localhost', 9092, socket.AF_INET)
mocker.patch.object(conn, '_send', return_value=Future())
mocker.patch.object(conn, 'recv', return_value=[])
assert conn._api_versions_future is None
conn.connect()
assert conn._api_versions_future is not None
assert conn.connecting() is True
assert conn.state is ConnectionStates.API_VERSIONS_RECV
assert conn._try_api_versions_check() is False
assert conn.connecting() is True
assert conn.state is ConnectionStates.API_VERSIONS_RECV
conn._api_versions_future = None
conn._check_version_idx = 0
assert conn._try_api_versions_check() is False
assert conn.connecting() is True
conn._check_version_idx = len(VERSION_CHECKS)
conn._api_versions_future = None
assert conn._try_api_versions_check() is False
assert conn.connecting() is False
assert conn.disconnected() is True
def test_api_versions_check_unrecognized(_socket):
conn = BrokerConnection('localhost', 9092, socket.AF_INET, api_version=(0, 0))
with pytest.raises(Errors.UnrecognizedBrokerVersion):
conn.connect()
def test_connect_timeout(_socket, conn):
assert conn.state is ConnectionStates.DISCONNECTED
# Initial connect returns EINPROGRESS
# immediate inline connect returns EALREADY
# second explicit connect returns EALREADY
# third explicit connect returns EALREADY and times out via last_attempt
_socket.connect_ex.side_effect = [EINPROGRESS, EALREADY, EALREADY, EALREADY]
conn.connect()
assert conn.state is ConnectionStates.CONNECTING
conn.connect()
assert conn.state is ConnectionStates.CONNECTING
conn.last_attempt = 0
conn.connect()
assert conn.state is ConnectionStates.DISCONNECTED
def test_blacked_out(conn):
with mock.patch("time.monotonic", return_value=1000):
conn.last_attempt = 0
assert conn.blacked_out() is False
conn.last_attempt = 1000
assert conn.blacked_out() is True
def test_connection_delay(conn, mocker):
mocker.patch.object(conn, '_reconnect_jitter_pct', return_value=1.0)
with mock.patch("time.monotonic", return_value=1000):
conn.last_attempt = 1000
assert conn.connection_delay() == conn.config['reconnect_backoff_ms']
conn.state = ConnectionStates.CONNECTING
assert conn.connection_delay() == conn.config['reconnect_backoff_ms']
conn.state = ConnectionStates.CONNECTED
assert conn.connection_delay() == float('inf')
del conn._gai[:]
conn._update_reconnect_backoff()
conn.state = ConnectionStates.DISCONNECTED
assert conn.connection_delay() == 1.0 * conn.config['reconnect_backoff_ms']
conn.state = ConnectionStates.CONNECTING
assert conn.connection_delay() == 1.0 * conn.config['reconnect_backoff_ms']
conn._update_reconnect_backoff()
conn.state = ConnectionStates.DISCONNECTED
assert conn.connection_delay() == 2.0 * conn.config['reconnect_backoff_ms']
conn.state = ConnectionStates.CONNECTING
assert conn.connection_delay() == 2.0 * conn.config['reconnect_backoff_ms']
conn._update_reconnect_backoff()
conn.state = ConnectionStates.DISCONNECTED
assert conn.connection_delay() == 4.0 * conn.config['reconnect_backoff_ms']
conn.state = ConnectionStates.CONNECTING
assert conn.connection_delay() == 4.0 * conn.config['reconnect_backoff_ms']
def test_connected(conn):
assert conn.connected() is False
conn.state = ConnectionStates.CONNECTED
assert conn.connected() is True
def test_connecting(conn):
assert conn.connecting() is False
conn.state = ConnectionStates.CONNECTING
assert conn.connecting() is True
conn.state = ConnectionStates.CONNECTED
assert conn.connecting() is False
def test_send_disconnected(conn):
conn.state = ConnectionStates.DISCONNECTED
f = conn.send('foobar')
assert f.failed() is True
assert isinstance(f.exception, Errors.KafkaConnectionError)
def test_send_connecting(conn):
conn.state = ConnectionStates.CONNECTING
f = conn.send('foobar')
assert f.failed() is True
assert isinstance(f.exception, Errors.NodeNotReadyError)
def test_send_max_ifr(conn):
conn.state = ConnectionStates.CONNECTED
max_ifrs = conn.config['max_in_flight_requests_per_connection']
for i in range(max_ifrs):
conn.in_flight_requests[i] = 'foo'
f = conn.send('foobar')
assert f.failed() is True
assert isinstance(f.exception, Errors.TooManyInFlightRequests)
def test_send_no_response(_socket, conn):
conn.connect()
assert conn.state is ConnectionStates.CONNECTED
req = ProduceRequest[0](required_acks=0, timeout=0, topics=())
header = RequestHeader(req.API_KEY, req.API_VERSION, 0, conn.config['client_id'])
payload_bytes = len(header.encode()) + len(req.encode())
third = payload_bytes // 3
remainder = payload_bytes % 3
_socket.send.side_effect = [4, third, third, third, remainder]
assert len(conn.in_flight_requests) == 0
f = conn.send(req)
assert f.succeeded() is True
assert f.value is None
assert len(conn.in_flight_requests) == 0
def test_send_response(_socket, conn):
conn.connect()
assert conn.state is ConnectionStates.CONNECTED
req = MetadataRequest[0]([])
header = RequestHeader(req.API_KEY, req.API_VERSION, 0, conn.config['client_id'])
payload_bytes = len(header.encode()) + len(req.encode())
third = payload_bytes // 3
remainder = payload_bytes % 3
_socket.send.side_effect = [4, third, third, third, remainder]
assert len(conn.in_flight_requests) == 0
f = conn.send(req)
assert f.is_done is False
assert len(conn.in_flight_requests) == 1
def test_send_async_request_while_other_request_is_already_in_buffer(_socket, conn, metrics):
conn.connect()
assert conn.state is ConnectionStates.CONNECTED
assert 'node-0.bytes-sent' in metrics.mocked_sensors
bytes_sent_sensor = metrics.mocked_sensors['node-0.bytes-sent']
req1 = MetadataRequest[0](topics='foo')
header1 = RequestHeader(req1.API_KEY, req1.API_VERSION, 0, conn.config['client_id'])
payload_bytes1 = len(header1.encode()) + len(req1.encode())
req2 = MetadataRequest[0]([])
header2 = RequestHeader(req2.API_KEY, req2.API_VERSION, 0, conn.config['client_id'])
payload_bytes2 = len(header2.encode()) + len(req2.encode())
# The first call to the socket will raise a transient SSL exception. This will make the first
# request to be kept in the internal buffer to be sent in the next call of
# send_pending_requests_v2.
_socket.send.side_effect = [SSLWantWriteError, 4 + payload_bytes1, 4 + payload_bytes2]
conn.send(req1, blocking=False)
# This won't send any bytes because of the SSL exception and the request bytes will be kept in
# the buffer.
assert conn.send_pending_requests_v2() is False
assert bytes_sent_sensor.record.call_args_list[0].args == (0,)
conn.send(req2, blocking=False)
# This will send the remaining bytes in the buffer from the first request, but should notice
# that the second request was queued, therefore it should return False.
bytes_sent_sensor.record.reset_mock()
assert conn.send_pending_requests_v2() is False
bytes_sent_sensor.record.assert_called_once_with(4 + payload_bytes1)
bytes_sent_sensor.record.reset_mock()
assert conn.send_pending_requests_v2() is True
bytes_sent_sensor.record.assert_called_once_with(4 + payload_bytes2)
bytes_sent_sensor.record.reset_mock()
assert conn.send_pending_requests_v2() is True
bytes_sent_sensor.record.assert_called_once_with(0)
def test_send_error(_socket, conn):
conn.connect()
assert conn.state is ConnectionStates.CONNECTED
req = MetadataRequest[0]([])
try:
_socket.send.side_effect = ConnectionError
except NameError:
_socket.send.side_effect = socket.error
f = conn.send(req)
assert f.failed() is True
assert isinstance(f.exception, Errors.KafkaConnectionError)
assert _socket.close.call_count == 1
assert conn.state is ConnectionStates.DISCONNECTED
def test_can_send_more(conn):
assert conn.can_send_more() is True
max_ifrs = conn.config['max_in_flight_requests_per_connection']
for i in range(max_ifrs):
assert conn.can_send_more() is True
conn.in_flight_requests[i] = 'foo'
assert conn.can_send_more() is False
def test_recv_disconnected(_socket, conn):
conn.connect()
assert conn.connected()
req = MetadataRequest[0]([])
header = RequestHeader(req.API_KEY, req.API_VERSION, 0, conn.config['client_id'])
payload_bytes = len(header.encode()) + len(req.encode())
_socket.send.side_effect = [4, payload_bytes]
conn.send(req)
# Empty data on recv means the socket is disconnected
_socket.recv.side_effect = None
_socket.recv.return_value = b''
# Attempt to receive should mark connection as disconnected
assert conn.connected(), 'Not connected: %s' % conn.state
conn.recv()
assert conn.disconnected(), 'Not disconnected: %s' % conn.state
def test_recv(_socket, conn):
pass # TODO
def test_close(conn):
pass # TODO
def test_lookup_on_connect():
hostname = 'example.org'
port = 9092
conn = BrokerConnection(hostname, port, socket.AF_UNSPEC)
assert conn.host == hostname
assert conn.port == port
assert conn.afi == socket.AF_UNSPEC
afi1 = socket.AF_INET
sockaddr1 = ('127.0.0.1', 9092)
mock_return1 = [
(afi1, socket.SOCK_STREAM, 6, '', sockaddr1),
]
with mock.patch("socket.getaddrinfo", return_value=mock_return1) as m:
conn.connect()
m.assert_called_once_with(hostname, port, 0, socket.SOCK_STREAM)
assert conn._sock_afi == afi1
assert conn._sock_addr == sockaddr1
conn.close()
afi2 = socket.AF_INET6
sockaddr2 = ('::1', 9092, 0, 0)
mock_return2 = [
(afi2, socket.SOCK_STREAM, 6, '', sockaddr2),
]
with mock.patch("socket.getaddrinfo", return_value=mock_return2) as m:
conn.last_attempt = 0
conn.connect()
m.assert_called_once_with(hostname, port, 0, socket.SOCK_STREAM)
assert conn._sock_afi == afi2
assert conn._sock_addr == sockaddr2
conn.close()
def test_relookup_on_failure():
hostname = 'example.org'
port = 9092
conn = BrokerConnection(hostname, port, socket.AF_UNSPEC)
assert conn.host == hostname
mock_return1 = []
with mock.patch("socket.getaddrinfo", return_value=mock_return1) as m:
last_attempt = conn.last_attempt
conn.connect()
m.assert_called_once_with(hostname, port, 0, socket.SOCK_STREAM)
assert conn.disconnected()
assert conn.last_attempt > last_attempt
afi2 = socket.AF_INET
sockaddr2 = ('127.0.0.2', 9092)
mock_return2 = [
(afi2, socket.SOCK_STREAM, 6, '', sockaddr2),
]
with mock.patch("socket.getaddrinfo", return_value=mock_return2) as m:
conn.last_attempt = 0
conn.connect()
m.assert_called_once_with(hostname, port, 0, socket.SOCK_STREAM)
assert conn._sock_afi == afi2
assert conn._sock_addr == sockaddr2
conn.close()
def test_requests_timed_out(conn):
with mock.patch("time.monotonic", return_value=0):
# No in-flight requests, not timed out
assert not conn.requests_timed_out()
# Single request, timeout_at > now (0)
conn.in_flight_requests[0] = ('foo', 0, 1)
assert not conn.requests_timed_out()
# Add another request w/ timestamp > request_timeout ago
request_timeout = conn.config['request_timeout_ms']
expired_timestamp = 0 - request_timeout - 1
conn.in_flight_requests[1] = ('bar', 0, expired_timestamp)
assert conn.requests_timed_out()
# Drop the expired request and we should be good to go again
conn.in_flight_requests.pop(1)
assert not conn.requests_timed_out()
def test_maybe_throttle(conn):
assert conn.state is ConnectionStates.DISCONNECTED
assert not conn.throttled()
conn.state = ConnectionStates.CONNECTED
assert not conn.throttled()
# No throttle_time_ms attribute
conn._maybe_throttle(HeartbeatResponse[0](error_code=0))
assert not conn.throttled()
with mock.patch("time.monotonic", return_value=1000) as time:
# server-side throttling in v1.0
conn.config['api_version'] = (1, 0)
conn._maybe_throttle(HeartbeatResponse[1](throttle_time_ms=1000, error_code=0))
assert not conn.throttled()
# client-side throttling in v2.0
conn.config['api_version'] = (2, 0)
conn._maybe_throttle(HeartbeatResponse[2](throttle_time_ms=1000, error_code=0))
assert conn.throttled()
time.return_value = 3000
assert not conn.throttled()
def test_host_in_sasl_config():
hostname = 'example.org'
port = 9092
for security_protocol in ('SASL_PLAINTEXT', 'SASL_SSL'):
with mock.patch("kafka.conn.get_sasl_mechanism") as get_sasl_mechanism:
BrokerConnection(hostname, port, socket.AF_UNSPEC, security_protocol=security_protocol)
call_config = get_sasl_mechanism.mock_calls[1].kwargs
assert call_config['host'] == hostname