forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_libevreactor.py
More file actions
333 lines (262 loc) · 11.6 KB
/
test_libevreactor.py
File metadata and controls
333 lines (262 loc) · 11.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
# Copyright 2013-2017 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.
try:
import unittest2 as unittest
except ImportError:
import unittest # noqa
import errno
import math
from mock import patch, Mock
import os
import weakref
import six
from six import BytesIO
from socket import error as socket_error
from cassandra.connection import (HEADER_DIRECTION_TO_CLIENT,
ConnectionException, ProtocolError)
from cassandra.protocol import (write_stringmultimap, write_int, write_string,
SupportedMessage, ReadyMessage, ServerError)
from cassandra.marshal import uint8_pack, uint32_pack, int32_pack, uint16_pack
from tests import is_monkey_patched
try:
from cassandra.io.libevreactor import _cleanup as libev__cleanup
from cassandra.io.libevreactor import LibevConnection, LibevLoop
except ImportError:
LibevConnection = None # noqa
@patch('socket.socket')
@patch('cassandra.io.libevwrapper.IO')
@patch('cassandra.io.libevwrapper.Prepare')
@patch('cassandra.io.libevwrapper.Async')
@patch('cassandra.io.libevreactor.LibevLoop.maybe_start')
class LibevConnectionTest(unittest.TestCase):
def setUp(self):
if is_monkey_patched():
raise unittest.SkipTest("Can't test libev with monkey patching")
if LibevConnection is None:
raise unittest.SkipTest('libev does not appear to be installed correctly')
LibevConnection.initialize_reactor()
def make_connection(self):
c = LibevConnection('1.2.3.4', cql_version='3.0.1')
c._socket = Mock()
c._socket.send.side_effect = lambda x: len(x)
return c
def make_header_prefix(self, message_class, version=3, stream_id=0):
header = list(map(uint8_pack, [
0xff & (HEADER_DIRECTION_TO_CLIENT | version),
stream_id,
message_class.opcode # opcode
]))
header.insert(1, uint16_pack(0)) # flags (compression)
return six.binary_type().join(header)
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=six.binary_type()):
return header + uint32_pack(len(body)) + body
def test_successful_connection(self, *args):
c = self.make_connection()
# let it write the OptionsMessage
c.handle_write(None, 0)
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage)
options = self.make_options_body()
c._socket.recv.return_value = self.make_msg(header, options)
c.handle_read(None, 0)
# let it write out a StartupMessage
c.handle_write(None, 0)
header = self.make_header_prefix(ReadyMessage, stream_id=1)
c._socket.recv.return_value = self.make_msg(header)
c.handle_read(None, 0)
self.assertTrue(c.connected_event.is_set())
return c
def test_egain_on_buffer_size(self, *args):
# get a connection that's already fully started
c = self.test_successful_connection()
# Testing with v3, minimum supported version
header = six.b('\x03') + uint16_pack(0) + six.b('\x00\x00') + int32_pack(20000)
responses = [
header + (six.b('a') * (4096 - len(header))),
six.b('a') * 4096,
socket_error(errno.EAGAIN),
six.b('a') * 100,
socket_error(errno.EAGAIN)]
def side_effect(*args):
response = responses.pop(0)
if isinstance(response, socket_error):
raise response
else:
return response
c._socket.recv.side_effect = side_effect
c.handle_read(None, 0)
self.assertEqual(c._current_frame.end_pos, 20000 + len(header))
# the EAGAIN prevents it from reading the last 100 bytes
c._iobuf.seek(0, os.SEEK_END)
pos = c._iobuf.tell()
self.assertEqual(pos, 4096 + 4096)
# now tell it to read the last 100 bytes
c.handle_read(None, 0)
c._iobuf.seek(0, os.SEEK_END)
pos = c._iobuf.tell()
self.assertEqual(pos, 4096 + 4096 + 100)
def test_protocol_error(self, *args):
c = self.make_connection()
# let it write the OptionsMessage
c.handle_write(None, 0)
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage, version=0xa4)
options = self.make_options_body()
c._socket.recv.return_value = self.make_msg(header, options)
c.handle_read(None, 0)
# make sure it errored correctly
self.assertTrue(c.is_defunct)
self.assertTrue(c.connected_event.is_set())
self.assertIsInstance(c.last_error, ProtocolError)
def test_error_message_on_startup(self, *args):
c = self.make_connection()
# let it write the OptionsMessage
c.handle_write(None, 0)
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage)
options = self.make_options_body()
c._socket.recv.return_value = self.make_msg(header, options)
c.handle_read(None, 0)
# let it write out a StartupMessage
c.handle_write(None, 0)
header = self.make_header_prefix(ServerError, stream_id=1)
body = self.make_error_body(ServerError.error_code, ServerError.summary)
c._socket.recv.return_value = self.make_msg(header, body)
c.handle_read(None, 0)
# make sure it errored correctly
self.assertTrue(c.is_defunct)
self.assertIsInstance(c.last_error, ConnectionException)
self.assertTrue(c.connected_event.is_set())
def test_socket_error_on_write(self, *args):
c = self.make_connection()
# make the OptionsMessage write fail
c._socket.send.side_effect = socket_error(errno.EIO, "bad stuff!")
c.handle_write(None, 0)
# make sure it errored correctly
self.assertTrue(c.is_defunct)
self.assertIsInstance(c.last_error, socket_error)
self.assertTrue(c.connected_event.is_set())
def test_blocking_on_write(self, *args):
c = self.make_connection()
# make the OptionsMessage write block
c._socket.send.side_effect = socket_error(errno.EAGAIN, "socket busy")
c.handle_write(None, 0)
self.assertFalse(c.is_defunct)
# try again with normal behavior
c._socket.send.side_effect = lambda x: len(x)
c.handle_write(None, 0)
self.assertFalse(c.is_defunct)
self.assertTrue(c._socket.send.call_args is not None)
def test_partial_send(self, *args):
c = self.make_connection()
# only write the first four bytes of the OptionsMessage
write_size = 4
c._socket.send.side_effect = None
c._socket.send.return_value = write_size
c.handle_write(None, 0)
msg_size = 9 # v3+ frame header
expected_writes = int(math.ceil(float(msg_size) / write_size))
size_mod = msg_size % write_size
last_write_size = size_mod if size_mod else write_size
self.assertFalse(c.is_defunct)
self.assertEqual(expected_writes, c._socket.send.call_count)
self.assertEqual(last_write_size, len(c._socket.send.call_args[0][0]))
def test_socket_error_on_read(self, *args):
c = self.make_connection()
# let it write the OptionsMessage
c.handle_write(None, 0)
# read in a SupportedMessage response
c._socket.recv.side_effect = socket_error(errno.EIO, "busy socket")
c.handle_read(None, 0)
# make sure it errored correctly
self.assertTrue(c.is_defunct)
self.assertIsInstance(c.last_error, socket_error)
self.assertTrue(c.connected_event.is_set())
def test_partial_header_read(self, *args):
c = self.make_connection()
header = self.make_header_prefix(SupportedMessage)
options = self.make_options_body()
message = self.make_msg(header, options)
# read in the first byte
c._socket.recv.return_value = message[0:1]
c.handle_read(None, 0)
self.assertEqual(c._iobuf.getvalue(), message[0:1])
c._socket.recv.return_value = message[1:]
c.handle_read(None, 0)
self.assertEqual(six.binary_type(), c._iobuf.getvalue())
# let it write out a StartupMessage
c.handle_write(None, 0)
header = self.make_header_prefix(ReadyMessage, stream_id=1)
c._socket.recv.return_value = self.make_msg(header)
c.handle_read(None, 0)
self.assertTrue(c.connected_event.is_set())
self.assertFalse(c.is_defunct)
def test_partial_message_read(self, *args):
c = self.make_connection()
header = self.make_header_prefix(SupportedMessage)
options = self.make_options_body()
message = self.make_msg(header, options)
# read in the first nine bytes
c._socket.recv.return_value = message[:9]
c.handle_read(None, 0)
self.assertEqual(c._iobuf.getvalue(), message[:9])
# ... then read in the rest
c._socket.recv.return_value = message[9:]
c.handle_read(None, 0)
self.assertEqual(six.binary_type(), c._iobuf.getvalue())
# let it write out a StartupMessage
c.handle_write(None, 0)
header = self.make_header_prefix(ReadyMessage, stream_id=1)
c._socket.recv.return_value = self.make_msg(header)
c.handle_read(None, 0)
self.assertTrue(c.connected_event.is_set())
self.assertFalse(c.is_defunct)
def test_watchers_are_finished(self, *args):
"""
Test for asserting that watchers are closed in LibevConnection
This test simulates a process termination without calling cluster.shutdown(), which would trigger
LibevConnection._libevloop._cleanup. It will check the watchers have been closed
Finally it will restore the LibevConnection reactor so it doesn't affect
the rest of the tests
@since 3.10
@jira_ticket PYTHON-747
@expected_result the watchers are closed
@test_category connection
"""
with patch.object(LibevConnection._libevloop, "_thread"), \
patch.object(LibevConnection._libevloop, "notify"):
self.make_connection()
# We have to make a copy because the connections shouldn't
# be alive when we verify them
live_connections = set(LibevConnection._libevloop._live_conns)
# This simulates the process ending without cluster.shutdown()
# being called, then with atexit _cleanup for libevreactor would
# be called
libev__cleanup(weakref.ref(LibevConnection._libevloop))
for conn in live_connections:
for watcher in (conn._write_watcher, conn._read_watcher):
self.assertTrue(watcher.stop.mock_calls)
LibevConnection._libevloop._shutdown = False