forked from Jaymon/morp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
morp_test.py
442 lines (352 loc) · 12.5 KB
/
morp_test.py
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
# -*- coding: utf-8 -*-
import logging
import sys
import time
import os
from unittest import TestCase
import inspect
import subprocess
import testdata
#import morp
from morp import Message, Connection, DsnConnection
from morp.interface.sqs import SQS
from morp.interface import get_interfaces
from morp.exception import ReleaseMessage, AckMessage
# configure root logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
log_handler = logging.StreamHandler(stream=sys.stderr)
log_formatter = logging.Formatter('[%(levelname).1s] %(message)s')
log_handler.setFormatter(log_formatter)
logger.addHandler(log_handler)
logger = logging.getLogger('boto3')
logger.setLevel(logging.WARNING)
logger = logging.getLogger('botocore')
logger.setLevel(logging.WARNING)
class Client(object):
"""makes running a captain script nice and easy for easy testing"""
def __init__(self, modulepath, contents):
module_info = testdata.create_module(modulepath, contents=contents)
self.directory = module_info.basedir
self.module = module_info.module
self.message_classes = []
clear_names = {}
for _, message_class in inspect.getmembers(self.module, inspect.isclass):
if issubclass(message_class, Message):
clear_names[message_class.get_name()] = message_class
self.message_classes.append(message_class)
for message_class in list(clear_names.values()):
message_class.clear()
def send(self, **fields):
return self.message_classes[0].create(fields)
def recv(self):
return self.run(self.message_classes[0].name)
def run(self, name, count=1, **options):
python_cmd = subprocess.check_output(["which", "python"]).strip()
cmd = "{} -m morp --count={} --directory={} {}".format(
python_cmd,
count,
self.directory,
name
)
expected_ret_code = options.get('code', 0)
is_py2 = True
is_py3 = False
def get_output_str(output):
if is_py2:
return "\n".join(output)
elif is_py3:
return "\n".join((o.decode("utf-8") for o in output))
output = []
try:
process = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=os.getcwd()
)
for line in iter(process.stdout.readline, b""):
line = line.rstrip()
print(line)
output.append(line)
process.wait()
if process.returncode != expected_ret_code:
raise RuntimeError("cmd returned {} with output: {}".format(
process.returncode,
get_output_str(output)
))
except subprocess.CalledProcessError as e:
raise RuntimeError("cmd returned {} with output: {}".format(e.returncode, e.output))
return get_output_str(output)
class BaseInterfaceTestCase(TestCase):
interface_class = None
def setUp(self):
i = self.get_interface()
n = self.get_name()
i.clear(n)
def get_interface(self):
"""get a connected interface"""
config = DsnConnection(os.environ['MORP_DSN_1'])
i = self.interface_class(config)
i.connect()
self.assertTrue(i.connected)
return i
def get_encrypted_interface(self):
"""get a connected interface"""
config = DsnConnection(os.environ['MORP_DSN_1'])
if testdata.random.randint(0, 1):
key_path = testdata.create_file("/morp.key", testdata.get_ascii(100))
config.options['keyfile'] = key_path
else:
config.options['key'] = testdata.get_ascii(32)
i = self.interface_class(config)
i.connect()
self.assertTrue(i.connected)
return i
def get_name(self):
#return 'morp-test-' + testdata.get_ascii(12)
return 'morp-test-sqs'
def test_msg_lifecycle(self):
i = self.get_encrypted_interface()
im = i.create_msg()
fields = {"foo": 1, "bar": 2}
im.fields = fields
body = im.body
im2 = i.create_msg()
im2.body = body
self.assertEqual(im.fields, im2.fields)
self.assertEqual(im.fields, fields)
class SQSInterfaceTest(BaseInterfaceTestCase):
interface_class = SQS
def test_queue_auto_create(self):
"""SQS queues will auto-create, this just makes sure that works as intended"""
i = self.get_interface()
with i.connection() as connection:
with i.queue(testdata.get_ascii(), connection) as q:
q.delete()
def test_send_count_recv(self):
i = self.get_interface()
n = self.get_name()
d = {'foo': 1, 'bar': 2}
interface_msg = i.create_msg(fields={'foo': 10, 'bar': 20})
i.send(n, interface_msg)
self.assertEqual(1, i.count(n))
# re-connect to receive the message
i2 = self.get_interface()
interface_msg2 = i2.recv(n)
self.assertEqual(interface_msg.fields, interface_msg2.fields)
i2.ack(n, interface_msg2)
time.sleep(2) # 2 seconds made it work consistently
self.assertEqual(0, i.count(n))
def test_recv_timeout(self):
i = self.get_interface()
n = self.get_name()
start = time.time()
i.recv(n, 1)
stop = time.time()
self.assertLessEqual(1.0, stop - start)
def test_send_recv_encrypted(self):
i = self.get_encrypted_interface()
n = self.get_name()
interface_msg = i.create_msg(fields={'foo': 10, 'bar': 20})
i.send(n, interface_msg)
interface_msg2 = i.recv(n)
self.assertEqual(interface_msg.fields, interface_msg2.fields)
i.ack(n, interface_msg2)
class MessageTest(BaseInterfaceTestCase):
interface_class = SQS
def get_name(self):
return 'morp-test-message'
def get_msg(self):
class TMsg(Message):
interface = self.get_interface()
@classmethod
def get_name(cls): return self.get_name()
m = TMsg()
return m
def test_fields(self):
"""just make sure interface_msg doesn't end up in the fields dict"""
m = self.get_msg()
m.interface_msg = 1
self.assertFalse("interface_msg" in m.fields)
def test_backoff(self):
# TODO make this work with a backoff, this test works but doesn't do any
# sort of visibility backoff
m = self.get_msg()
mcls = m.__class__
foo = testdata.get_int()
m.foo = foo
m.send()
count = 0
for x in range(2):
with self.assertRaises(RuntimeError):
with mcls.recv() as m2:
self.assertGreater(m2.interface_msg._count, count)
count = m2.interface_msg._count
raise RuntimeError()
with mcls.recv() as m2:
self.assertGreater(m2.interface_msg._count, count)
self.assertEqual(m2.foo, m.foo)
def test_release(self):
m = self.get_msg()
mcls = m.__class__
m.foo = testdata.get_int()
m.send()
with self.assertRaises(RuntimeError):
with mcls.recv() as m2:
raise RuntimeError()
with mcls.recv() as m2:
self.assertEqual(m2.foo, m.foo)
def test_release_message(self):
m = self.get_msg()
mcls = m.__class__
m.foo = 10
m.send()
with mcls.recv() as m2:
raise ReleaseMessage(2)
with mcls.recv_for(1) as m2:
self.assertEqual(None, m2)
time.sleep(1)
with mcls.recv_for(1) as m2:
self.assertEqual(m.foo, m2.foo)
def test_ack_message(self):
m = self.get_msg()
mcls = m.__class__
m.foo = 10
m.send()
with mcls.recv() as m2:
raise AckMessage()
with mcls.recv_for(timeout=1) as m2:
self.assertEqual(None, m2)
def test_send_recv(self):
m = self.get_msg()
m.foo = 1
m.bar = 2
m.send()
with m.__class__.recv() as m2:
self.assertEqual(m.fields, m2.fields)
def test_send_later(self):
m = self.get_msg()
m.foo = 1
m.bar = 2
m.send_later(2)
with m.__class__.recv_for(1) as m2:
self.assertEqual(None, m2)
time.sleep(1)
with m.__class__.recv_for(1) as m2:
self.assertEqual(m.fields, m2.fields)
def test_recv_block(self):
m = self.get_msg()
m.foo = 10
m.bar = 20
m.send()
with m.__class__.recv() as m2:
self.assertEqual(m.fields, m2.fields)
def test_recv_block_error(self):
m = self.get_msg()
mcls = m.__class__
m.foo = 10
m.send()
kwargs = {
"vtimeout": 1,
"timeout": 2
}
with self.assertRaises(RuntimeError):
with mcls.recv(**kwargs) as m2:
raise RuntimeError()
time.sleep(1.2)
kwargs["ack_on_recv"] = True
with self.assertRaises(RuntimeError):
with mcls.recv(**kwargs) as m2:
raise RuntimeError()
time.sleep(1.2)
with mcls.recv_for(timeout=1) as m2:
self.assertEqual(None, m2)
class ConnectionTest(TestCase):
def test_key(self):
c = Connection()
self.assertEqual("", c.key)
self.assertEqual(c.key, c.key)
key = testdata.get_ascii(100)
c = Connection(options=dict(key=key))
self.assertNotEqual(b"", b"{}".format(c.key))
self.assertEqual(c.key, c.key)
key_path = testdata.create_file("morp.key", testdata.get_ascii(100))
c = Connection(options=dict(keyfile=key_path))
self.assertNotEqual(b"", b"{}".format(c.key))
self.assertEqual(c.key, c.key)
c = Connection(options=dict(key=key, keyfile=key_path))
self.assertNotEqual(b"", b"{}".format(c.key))
c2 = Connection(options=dict(key=key, keyfile=key_path))
self.assertEqual(c.key, c2.key)
def test_dsn_connection(self):
tests = [
(
'path.to.Interface://127.0.0.1:4151',
dict(
hosts=[('127.0.0.1', 4151)],
interface_name="path.to.Interface",
name=''
)
),
(
'module.path.to.Interface://example.com:4161#name',
dict(
hosts=[('example.com', 4161)],
interface_name='module.path.to.Interface',
name="name"
)
),
(
'module.path.to.Interface://example.com:4161?foo=bar&bar=che#name',
dict(
hosts=[('example.com', 4161)],
interface_name='module.path.to.Interface',
options={"foo": "bar", "bar": "che"},
name="name"
)
),
(
"morp.interface.sqs.SQS://AWS_ID:AWS_KEY@?read_lock=120",
dict(
username='AWS_ID',
password='AWS_KEY',
interface_name='morp.interface.sqs.SQS',
options={'read_lock': '120'}
)
),
(
"morp.interface.sqs.SQS://AWS_ID:AWS_KEY@",
dict(
username='AWS_ID',
password='AWS_KEY',
interface_name='morp.interface.sqs.SQS',
options={}
)
)
]
for t in tests:
c = DsnConnection(t[0])
for k, v in list(t[1].items()):
self.assertEqual(v, getattr(c, k))
class CLITest(TestCase):
def test_consume(self):
c = Client("tcli.consume", [
"from morp import Message",
"",
"class Consume(Message):",
" def target(self):",
" print(self.text)"
"",
"class Consume2(Consume):",
" pass",
])
m = c.message_classes[0].create(text="foobar")
r = c.recv()
self.assertTrue(m.text in r)
m = c.message_classes[1].create(text="bazche")
r = c.recv()
self.assertTrue(m.text in r)
# so test runner won't try and run it
del BaseInterfaceTestCase