forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_connection.py
More file actions
613 lines (490 loc) · 23.7 KB
/
Copy pathtest_connection.py
File metadata and controls
613 lines (490 loc) · 23.7 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
import unittest
from io import BytesIO
import time
from threading import Lock
from unittest.mock import Mock, ANY, call, patch
from cassandra import OperationTimedOut
from cassandra.cluster import Cluster
from cassandra.connection import (Connection, HEADER_DIRECTION_TO_CLIENT, ProtocolError,
locally_supported_compressions, ConnectionHeartbeat, _Frame, Timer, TimerManager,
ConnectionException, ConnectionShutdown, DefaultEndPoint, ShardAwarePortGenerator,
_ConnectionIOBuffer)
from cassandra.marshal import uint8_pack, uint32_pack, int32_pack
from cassandra.protocol import (write_stringmultimap, write_int, write_string,
SupportedMessage, ProtocolHandler)
from tests.util import wait_until, assertRegex
import pytest
class ConnectionTest(unittest.TestCase):
def make_connection(self, **kwargs):
c = Connection(DefaultEndPoint('1.2.3.4'), **kwargs)
c._socket = Mock()
c._socket.send.side_effect = lambda x: len(x)
return c
def make_header_prefix(self, message_class, version=Connection.protocol_version, stream_id=0):
return bytes().join(map(uint8_pack, [
0xff & (HEADER_DIRECTION_TO_CLIENT | version),
0, # flags (compression)
0, # MSB for v3+ stream
stream_id,
message_class.opcode # opcode
]))
def make_options_body(self):
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.1'],
'COMPRESSION': []
})
return options_buf.getvalue()
def make_error_body(self, code, msg):
buf = BytesIO()
write_int(buf, code)
write_string(buf, msg)
return buf.getvalue()
def make_msg(self, header, body=""):
return header + uint32_pack(len(body)) + body
def test_connection_endpoint(self):
endpoint = DefaultEndPoint('1.2.3.4')
c = Connection(endpoint)
assert c.endpoint == endpoint
assert c.endpoint.address == endpoint.address
c = Connection(host=endpoint) # kwarg
assert c.endpoint == endpoint
assert c.endpoint.address == endpoint.address
c = Connection('10.0.0.1')
endpoint = DefaultEndPoint('10.0.0.1')
assert c.endpoint == endpoint
assert c.endpoint.address == endpoint.address
def test_bad_protocol_version(self, *args):
c = self.make_connection()
c._requests = Mock()
c.defunct = Mock()
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage, version=0x7f)
options = self.make_options_body()
message = self.make_msg(header, options)
c._iobuf._io_buffer = BytesIO()
c._iobuf.write(message)
c.process_io_buffer()
# make sure it errored correctly
c.defunct.assert_called_once_with(ANY)
args, kwargs = c.defunct.call_args
assert isinstance(args[0], ProtocolError)
def test_negative_body_length(self, *args):
c = self.make_connection()
c._requests = Mock()
c.defunct = Mock()
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage)
message = header + int32_pack(-13)
c._iobuf._io_buffer = BytesIO()
c._iobuf.write(message)
c.process_io_buffer()
# make sure it errored correctly
c.defunct.assert_called_once_with(ANY)
args, kwargs = c.defunct.call_args
assert isinstance(args[0], ProtocolError)
def test_unsupported_cql_version(self, *args):
c = self.make_connection()
c._requests = {0: (c._handle_options_response, ProtocolHandler.decode_message, [])}
c.defunct = Mock()
c.cql_version = "3.0.3"
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['7.8.9'],
'COMPRESSION': []
})
options = options_buf.getvalue()
c.process_msg(_Frame(version=4, flags=0, stream=0, opcode=SupportedMessage.opcode, body_offset=9, end_pos=9 + len(options)), options)
# make sure it errored correctly
c.defunct.assert_called_once_with(ANY)
args, kwargs = c.defunct.call_args
assert isinstance(args[0], ProtocolError)
def test_prefer_lz4_compression(self, *args):
c = self.make_connection()
c._requests = {0: (c._handle_options_response, ProtocolHandler.decode_message, [])}
c.defunct = Mock()
c.cql_version = "3.0.3"
locally_supported_compressions.pop('lz4', None)
locally_supported_compressions.pop('snappy', None)
locally_supported_compressions['lz4'] = ('lz4compress', 'lz4decompress')
locally_supported_compressions['snappy'] = ('snappycompress', 'snappydecompress')
# read in a SupportedMessage response
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.3'],
'COMPRESSION': ['snappy', 'lz4']
})
options = options_buf.getvalue()
c.process_msg(_Frame(version=4, flags=0, stream=0, opcode=SupportedMessage.opcode, body_offset=9, end_pos=9 + len(options)), options)
assert c.decompressor == locally_supported_compressions['lz4'][1]
def test_requested_compression_not_available(self, *args):
c = self.make_connection()
c._requests = {0: (c._handle_options_response, ProtocolHandler.decode_message, [])}
c.defunct = Mock()
# request lz4 compression
c.compression = "lz4"
locally_supported_compressions.pop('lz4', None)
locally_supported_compressions.pop('snappy', None)
locally_supported_compressions['lz4'] = ('lz4compress', 'lz4decompress')
locally_supported_compressions['snappy'] = ('snappycompress', 'snappydecompress')
# the server only supports snappy
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.3'],
'COMPRESSION': ['snappy']
})
options = options_buf.getvalue()
c.process_msg(_Frame(version=4, flags=0, stream=0, opcode=SupportedMessage.opcode, body_offset=9, end_pos=9 + len(options)), options)
# make sure it errored correctly
c.defunct.assert_called_once_with(ANY)
args, kwargs = c.defunct.call_args
assert isinstance(args[0], ProtocolError)
def test_use_requested_compression(self, *args):
c = self.make_connection(protocol_version=4)
c._requests = {0: (c._handle_options_response, ProtocolHandler.decode_message, [])}
c.defunct = Mock()
# request snappy compression
c.compression = "snappy"
locally_supported_compressions.pop('lz4', None)
locally_supported_compressions.pop('snappy', None)
locally_supported_compressions['lz4'] = ('lz4compress', 'lz4decompress')
locally_supported_compressions['snappy'] = ('snappycompress', 'snappydecompress')
# the server only supports snappy
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.3'],
'COMPRESSION': ['snappy', 'lz4']
})
options = options_buf.getvalue()
c.process_msg(_Frame(version=4, flags=0, stream=0, opcode=SupportedMessage.opcode, body_offset=9, end_pos=9 + len(options)), options)
assert c.decompressor == locally_supported_compressions['snappy'][1]
def test_disable_compression(self, *args):
c = self.make_connection()
c._requests = {0: (c._handle_options_response, ProtocolHandler.decode_message)}
c.defunct = Mock()
# disable compression
c.compression = False
locally_supported_compressions.pop('lz4', None)
locally_supported_compressions.pop('snappy', None)
locally_supported_compressions['lz4'] = ('lz4compress', 'lz4decompress')
locally_supported_compressions['snappy'] = ('snappycompress', 'snappydecompress')
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage)
# the server only supports snappy
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.3'],
'COMPRESSION': ['snappy', 'lz4']
})
options = options_buf.getvalue()
message = self.make_msg(header, options)
c.process_msg(message, len(message) - 8)
assert c.decompressor == None
def test_not_implemented(self):
"""
Ensure the following methods throw NIE's. If not, come back and test them.
"""
c = self.make_connection()
with pytest.raises(NotImplementedError):
c.close()
def test_set_keyspace_blocking(self):
c = self.make_connection()
assert c.keyspace == None
c.set_keyspace_blocking(None)
assert c.keyspace == None
c.keyspace = 'ks'
c.set_keyspace_blocking('ks')
assert c.keyspace == 'ks'
def test_set_connection_class(self):
cluster = Cluster(connection_class='test')
assert 'test' == cluster.connection_class
def test_connection_shutdown_includes_last_error(self):
"""
Test that ConnectionShutdown exceptions include the last_error when available.
This helps debug issues like "Bad file descriptor" by showing the original cause.
See https://github.com/scylladb/python-driver/issues/614
"""
c = self.make_connection()
c.lock = Lock()
c._requests = {}
# Simulate the connection becoming defunct with a specific error
original_error = OSError(9, "Bad file descriptor")
c.is_defunct = True
c.last_error = original_error
# send_msg should raise ConnectionShutdown that includes the last_error
with pytest.raises(ConnectionShutdown) as exc_info:
c.send_msg(Mock(), 1, Mock())
# Verify the error message includes the original error
error_message = str(exc_info.value)
assert "is defunct" in error_message
assert "Bad file descriptor" in error_message
def test_connection_shutdown_closed_includes_last_error(self):
"""
Test that ConnectionShutdown exceptions for closed connections include last_error.
"""
c = self.make_connection()
c.lock = Lock()
c._requests = {}
# Simulate the connection being closed with a specific error
original_error = OSError(9, "Bad file descriptor")
c.is_closed = True
c.last_error = original_error
# send_msg should raise ConnectionShutdown that includes the last_error
with pytest.raises(ConnectionShutdown) as exc_info:
c.send_msg(Mock(), 1, Mock())
# Verify the error message includes the original error
error_message = str(exc_info.value)
assert "is closed" in error_message
assert "Bad file descriptor" in error_message
def test_wait_for_responses_shutdown_includes_last_error(self):
"""
Test that wait_for_responses raises ConnectionShutdown with last_error.
"""
c = self.make_connection()
c.lock = Lock()
c._requests = {}
# Simulate the connection being defunct with a specific error
original_error = OSError(9, "Bad file descriptor")
c.is_defunct = True
c.last_error = original_error
# wait_for_responses should raise ConnectionShutdown that includes the last_error
with pytest.raises(ConnectionShutdown) as exc_info:
c.wait_for_responses(Mock())
# Verify the error message includes the original error
error_message = str(exc_info.value)
assert "already closed" in error_message
assert "Bad file descriptor" in error_message
@patch('cassandra.connection.ConnectionHeartbeat._raise_if_stopped')
class ConnectionHeartbeatTest(unittest.TestCase):
@staticmethod
def make_get_holders(len):
holders = []
for _ in range(len):
holder = Mock()
holder.get_connections = Mock(return_value=[])
holders.append(holder)
get_holders = Mock(return_value=holders)
return get_holders
def run_heartbeat(self, get_holders_fun, count=2, interval=0.05, timeout=0.05):
ch = ConnectionHeartbeat(interval, get_holders_fun, timeout=timeout)
# wait until the thread is started
wait_until(lambda: get_holders_fun.call_count > 0, 0.01, 100)
time.sleep(interval * (count-1))
ch.stop()
assert get_holders_fun.call_count
def test_empty_connections(self, *args):
count = 3
get_holders = self.make_get_holders(1)
self.run_heartbeat(get_holders, count)
assert get_holders.call_count >= count-1
assert get_holders.call_count <= count
holder = get_holders.return_value[0]
holder.get_connections.assert_has_calls([call()] * get_holders.call_count)
def test_idle_non_idle(self, *args):
request_id = 999
# connection.send_msg(OptionsMessage(), connection.get_request_id(), self._options_callback)
def send_msg(msg, req_id, msg_callback):
msg_callback(SupportedMessage([], {}))
idle_connection = Mock(spec=Connection, host='localhost',
max_request_id=127,
lock=Lock(),
in_flight=0, is_idle=True,
is_defunct=False, is_closed=False,
get_request_id=lambda: request_id,
send_msg=Mock(side_effect=send_msg))
non_idle_connection = Mock(spec=Connection, in_flight=0, is_idle=False, is_defunct=False, is_closed=False)
get_holders = self.make_get_holders(1)
holder = get_holders.return_value[0]
holder.get_connections.return_value.append(idle_connection)
holder.get_connections.return_value.append(non_idle_connection)
self.run_heartbeat(get_holders)
holder.get_connections.assert_has_calls([call()] * get_holders.call_count)
assert idle_connection.in_flight == 0
assert non_idle_connection.in_flight == 0
idle_connection.send_msg.assert_has_calls([call(ANY, request_id, ANY)] * get_holders.call_count)
assert non_idle_connection.send_msg.call_count == 0
def test_closed_defunct(self, *args):
get_holders = self.make_get_holders(1)
closed_connection = Mock(spec=Connection, in_flight=0, is_idle=False, is_defunct=False, is_closed=True)
defunct_connection = Mock(spec=Connection, in_flight=0, is_idle=False, is_defunct=True, is_closed=False)
holder = get_holders.return_value[0]
holder.get_connections.return_value.append(closed_connection)
holder.get_connections.return_value.append(defunct_connection)
self.run_heartbeat(get_holders)
holder.get_connections.assert_has_calls([call()] * get_holders.call_count)
assert closed_connection.in_flight == 0
assert defunct_connection.in_flight == 0
assert closed_connection.send_msg.call_count == 0
assert defunct_connection.send_msg.call_count == 0
def test_no_req_ids(self, *args):
in_flight = 3
get_holders = self.make_get_holders(1)
max_connection = Mock(spec=Connection, host='localhost',
lock=Lock(),
max_request_id=in_flight - 1, in_flight=in_flight,
is_idle=True, is_defunct=False, is_closed=False)
holder = get_holders.return_value[0]
holder.get_connections.return_value.append(max_connection)
self.run_heartbeat(get_holders)
holder.get_connections.assert_has_calls([call()] * get_holders.call_count)
assert max_connection.in_flight == in_flight
assert max_connection.send_msg.call_count == 0
assert max_connection.send_msg.call_count == 0
max_connection.defunct.assert_has_calls([call(ANY)] * get_holders.call_count)
holder.return_connection.assert_has_calls(
[call(max_connection)] * get_holders.call_count)
def test_unexpected_response(self, *args):
request_id = 999
get_holders = self.make_get_holders(1)
def send_msg(msg, req_id, msg_callback):
msg_callback(object())
connection = Mock(spec=Connection, host='localhost',
max_request_id=127,
lock=Lock(),
in_flight=0, is_idle=True,
is_defunct=False, is_closed=False,
get_request_id=lambda: request_id,
send_msg=Mock(side_effect=send_msg))
holder = get_holders.return_value[0]
holder.get_connections.return_value.append(connection)
self.run_heartbeat(get_holders)
assert connection.in_flight == get_holders.call_count
connection.send_msg.assert_has_calls([call(ANY, request_id, ANY)] * get_holders.call_count)
connection.defunct.assert_has_calls([call(ANY)] * get_holders.call_count)
exc = connection.defunct.call_args_list[0][0][0]
assert isinstance(exc, ConnectionException)
assertRegex(exc.args[0], r'^Received unexpected response to OptionsMessage.*')
holder.return_connection.assert_has_calls(
[call(connection)] * get_holders.call_count)
def test_timeout(self, *args):
request_id = 999
get_holders = self.make_get_holders(1)
def send_msg(msg, req_id, msg_callback):
pass
# we used endpoint=X here because it's a mock and we need connection.endpoint to be set
connection = Mock(spec=Connection, endpoint=DefaultEndPoint('localhost'),
max_request_id=127,
lock=Lock(),
in_flight=0, is_idle=True,
is_defunct=False, is_closed=False,
get_request_id=lambda: request_id,
send_msg=Mock(side_effect=send_msg))
holder = get_holders.return_value[0]
holder.get_connections.return_value.append(connection)
self.run_heartbeat(get_holders)
assert connection.in_flight == get_holders.call_count
connection.send_msg.assert_has_calls([call(ANY, request_id, ANY)] * get_holders.call_count)
connection.defunct.assert_has_calls([call(ANY)] * get_holders.call_count)
exc = connection.defunct.call_args_list[0][0][0]
assert isinstance(exc, OperationTimedOut)
assert exc.errors == 'Connection heartbeat timeout after 0.05 seconds'
assert exc.last_host == DefaultEndPoint('localhost')
holder.return_connection.assert_has_calls(
[call(connection)] * get_holders.call_count)
class TimerTest(unittest.TestCase):
def test_timer_collision(self):
# simple test demonstrating #466
# same timeout, comparison will defer to the Timer object itself
t1 = Timer(0, lambda: None)
t2 = Timer(0, lambda: None)
t2.end = t1.end
tm = TimerManager()
tm.add_timer(t1)
tm.add_timer(t2)
# Prior to #466: "TypeError: unorderable types: Timer() < Timer()"
tm.service_timeouts()
class DefaultEndPointTest(unittest.TestCase):
def test_default_endpoint_properties(self):
endpoint = DefaultEndPoint('10.0.0.1')
assert endpoint.address == '10.0.0.1'
assert endpoint.port == 9042
assert str(endpoint) == '10.0.0.1:9042'
endpoint = DefaultEndPoint('10.0.0.1', 8888)
assert endpoint.address == '10.0.0.1'
assert endpoint.port == 8888
assert str(endpoint) == '10.0.0.1:8888'
def test_endpoint_equality(self):
assert DefaultEndPoint('10.0.0.1') == DefaultEndPoint('10.0.0.1')
assert DefaultEndPoint('10.0.0.1') == DefaultEndPoint('10.0.0.1', 9042)
assert DefaultEndPoint('10.0.0.1') != DefaultEndPoint('10.0.0.2')
assert DefaultEndPoint('10.0.0.1') != DefaultEndPoint('10.0.0.1', 0000)
def test_endpoint_resolve(self):
assert DefaultEndPoint('10.0.0.1').resolve() == ('10.0.0.1', 9042)
assert DefaultEndPoint('10.0.0.1', 3232).resolve() == ('10.0.0.1', 3232)
class TestShardawarePortGenerator(unittest.TestCase):
@patch('random.randrange')
def test_generate_ports_basic(self, mock_randrange):
mock_randrange.return_value = 10005
gen = ShardAwarePortGenerator(10000, 10020)
ports = list(itertools.islice(gen.generate(shard_id=1, total_shards=3), 5))
# Starting from aligned 10005 + shard_id (1), step by 3
assert ports == [10006, 10009, 10012, 10015, 10018]
@patch('random.randrange')
def test_wraps_around_to_start(self, mock_randrange):
mock_randrange.return_value = 10008
gen = ShardAwarePortGenerator(10000, 10020)
ports = list(itertools.islice(gen.generate(shard_id=2, total_shards=4), 5))
# Expected wrap-around from start_port after end_port is exceeded
assert ports == [10010, 10014, 10018, 10002, 10006]
@patch('random.randrange')
def test_all_ports_have_correct_modulo(self, mock_randrange):
mock_randrange.return_value = 10012
total_shards = 5
shard_id = 3
gen = ShardAwarePortGenerator(10000, 10020)
for port in gen.generate(shard_id=shard_id, total_shards=total_shards):
assert port % total_shards == shard_id
@patch('random.randrange')
def test_generate_is_repeatable_with_same_mock(self, mock_randrange):
mock_randrange.return_value = 10010
gen = ShardAwarePortGenerator(10000, 10020)
first_run = list(itertools.islice(gen.generate(0, 2), 5))
second_run = list(itertools.islice(gen.generate(0, 2), 5))
assert first_run == second_run
class ResetBufferTest(unittest.TestCase):
"""Tests for _ConnectionIOBuffer._reset_buffer static method."""
def test_preserves_remaining_data(self):
buf = BytesIO()
buf.write(b"already_consumed_new_data")
buf.seek(17) # position after "already_consumed_"
result = _ConnectionIOBuffer._reset_buffer(buf)
self.assertEqual(result.getvalue(), b"new_data")
# Cursor is at SEEK_END, ready for further writes
self.assertEqual(result.tell(), len(b"new_data"))
def test_empty_remaining(self):
buf = BytesIO()
buf.write(b"all_consumed")
buf.seek(12)
result = _ConnectionIOBuffer._reset_buffer(buf)
self.assertEqual(result.getvalue(), b"")
self.assertEqual(result.tell(), 0)
def test_nothing_consumed(self):
buf = BytesIO()
buf.write(b"all_remaining")
buf.seek(0)
result = _ConnectionIOBuffer._reset_buffer(buf)
self.assertEqual(result.getvalue(), b"all_remaining")
# Cursor is at SEEK_END, ready for further writes
self.assertEqual(result.tell(), len(b"all_remaining"))
def test_new_buffer_is_writable(self):
buf = BytesIO()
buf.write(b"head_tail")
buf.seek(5)
result = _ConnectionIOBuffer._reset_buffer(buf)
result.seek(0, 2) # seek to end
result.write(b"_more")
self.assertEqual(result.getvalue(), b"tail_more")