forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·117 lines (103 loc) · 3.96 KB
/
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
#!/usr/bin/env python
import time
import random
import re
import subprocess
import signal
import sys
import os
multiplier = float(os.environ.get('SLOWNESS', 1))
def run(cmd, **kwargs):
p = subprocess.Popen(cmd.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs)
p.wait()
out = p.stdout.read()
err = p.stderr.read()
time.sleep(0.2 * multiplier)
return p.returncode, out + '\n' + err
def spawn(cmd, **kwargs):
p = subprocess.Popen(cmd.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs)
time.sleep(0.5 * multiplier)
return p
def wait(p, match):
os.kill(p.pid, signal.SIGINT)
p.wait()
out = p.stdout.read()
err = p.stderr.read()
return bool(re.search(match, out)), out + '\n' + err
def gen(prog, arg="", **kwargs):
Prog = ''.join([w.capitalize() for w in prog.split('_')])
ctx = {
'prog': prog,
'Prog': Prog,
'rubyver': os.environ.get('RUBYVER', '1.8'),
'arg': arg,
'java': kwargs.get('java', Prog),
'dotnet': kwargs.get('dotnet', Prog),
}
return [
('python', './venv/bin/python %(prog)s.py %(arg)s' % ctx),
('perl', 'perl %(prog)s.pl %(arg)s' % ctx),
('erlang', './%(prog)s.erl %(arg)s' % ctx),
('java', 'java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:'
'rabbitmq-client.jar %(java)s %(arg)s' % ctx),
('dotnet', 'env MONO_PATH=lib/bin mono %(dotnet)s.exe %(arg)s' % ctx),
('ruby', 'env RUBYOPT=-rubygems GEM_HOME=gems/gems RUBYLIB=gems/lib '
'ruby%(rubyver)s %(prog)s.rb %(arg)s' % ctx),
('php', 'php %(prog)s.php %(arg)s' % ctx),
('python-puka', './venv/bin/python %(prog)s.py %(arg)s' % ctx),
]
def skip(cwd_cmd, to_skip):
return [(cwd,cmd) for cwd, cmd in cwd_cmd if cwd not in to_skip]
tests = {
'tut1': (gen('send'), gen('receive', java='Recv'), 'Hello World!'),
'tut2': (gen('new_task', arg='%(arg)s'), gen('worker'), '%(arg)s'),
'tut3': (gen('emit_log', arg='%(arg)s'), gen('receive_logs'), '%(arg)s'),
'tut4': (skip(gen('emit_log_direct', arg='%(arg)s %(arg2)s'),
['php']),
skip(gen('receive_logs_direct', arg='%(arg)s'),
['php']),
'%(arg2)s'),
'tut5': (skip(gen('emit_log_topic', arg='%(arg)s.foo %(arg2)s'),
['php']),
skip(gen('receive_logs_topic', arg='%(arg)s.*'),
['php']),
'%(arg2)s'),
'tut6': (skip(gen('rpc_client', java='RPCClient', dotnet='RPCClient'),
['erlang']),
skip(gen('rpc_server', java='RPCServer', dotnet='RPCServer'),
['erlang']),
'fib[(]30[)]'),
}
errors = 0
print " [.] Running tests with SLOWNESS=%r" % (multiplier,)
for test in sorted(tests.keys()):
(send_progs, recv_progs, output_mask) = tests[test]
for scwd, send_cmd in send_progs:
for rcwd, recv_cmd in recv_progs:
ctx = {
'arg': 'rand_%s' % (random.randint(1,100),),
'arg2': 'rand_%s' % (random.randint(1,100),),
}
rcmd = recv_cmd % ctx
scmd = send_cmd % ctx
mask = output_mask % ctx
p = spawn(rcmd, cwd=rcwd)
serror, sout = run(scmd, cwd=scwd)
matched, rout = wait(p, mask)
if matched and serror == 0:
print " [+] %s %-30s ok" % (test, scwd+'/'+rcwd)
else:
print " [!] %s %-30s FAILED" % (test, scwd+'/'+rcwd)
print " [!] %r output (error=%s):\n%s\n" % (scmd, serror,
sout.strip())
print " [!] %r output:\n%s\n" % (rcmd, rout.strip())
errors += 1
if errors:
print " [!] %s tests failed" % (errors,)
sys.exit(errors)