Skip to content

Commit adea690

Browse files
committed
Combine all ptest tests into a single run, for speed.
Old `make ptest` locally: 1m34 New `make ptest` locally: 55s Doesn't work on CI for some reason, so actually disabled for `make ptest`. Use ./run_tests.py instead
1 parent 86a6ef0 commit adea690

File tree

13 files changed

+106
-53
lines changed

13 files changed

+106
-53
lines changed

run_tests.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
3+
import inspect
4+
import os
5+
import sys
6+
7+
sys.path.insert(0, "test")
8+
9+
import ptest
10+
import test.apps.ctrl.test as p_ctrl
11+
import test.apps.db_dump.test as p_db_dump
12+
import test.apps.passwd.test as p_passwd
13+
import test.apps.signal.test as p_signal
14+
import test.broker.test as p_broker
15+
import test.client.test as p_client
16+
import test.lib.test as p_lib
17+
18+
test = ptest.PTest()
19+
test.add_tests(p_client.tests, "test/client")
20+
test.add_tests(p_lib.tests, "test/lib")
21+
test.add_tests(p_ctrl.tests, "test/apps/ctrl")
22+
test.add_tests(p_db_dump.tests, "test/apps/db_dump")
23+
test.add_tests(p_passwd.tests, "test/apps/ctrl")
24+
test.add_tests(p_signal.tests, "test/apps/signal")
25+
test.add_tests(p_broker.tests, "test/broker")
26+
27+
test.run()

test/__init__.py

Whitespace-only changes.

test/apps/ctrl/test.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
4-
import pathlib
3+
import sys
4+
sys.path.insert(0, "../..")
55
import ptest
66

77
tests = [
@@ -11,5 +11,6 @@
1111
(2, './ctrl-dynsec.py'),
1212
]
1313

14-
test = ptest.PTest()
15-
test.run_tests(tests)
14+
if __name__ == "__main__":
15+
test = ptest.PTest()
16+
test.run_tests(tests)

test/apps/db_dump/test.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
3+
import os
44
import pathlib
5+
import sys
6+
sys.path.insert(0, "../..")
57
import ptest
68

79
tests = []
810

9-
for test_file in pathlib.Path('.').glob('db-dump-*.py'):
11+
for test_file in pathlib.Path(os.path.abspath(os.path.dirname(__file__))).glob('db-dump-*.py'):
1012
tests.append((1, test_file.resolve()))
1113

12-
test = ptest.PTest()
13-
test.run_tests(tests)
14+
if __name__ == "__main__":
15+
test = ptest.PTest()
16+
test.run_tests(tests)

test/apps/passwd/test.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
3+
import os
44
import pathlib
5+
import sys
6+
sys.path.insert(0, "../..")
57
import ptest
68

79
tests = []
810

9-
for test_file in pathlib.Path('.').glob('passwd-*.py'):
11+
for test_file in pathlib.Path(os.path.abspath(os.path.dirname(__file__))).glob('passwd-*.py'):
1012
tests.append((1, test_file.resolve()))
1113

12-
test = ptest.PTest()
13-
test.run_tests(tests)
14+
if __name__ == "__main__":
15+
test = ptest.PTest()
16+
test.run_tests(tests)

test/apps/signal/test.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
3+
import os
44
import pathlib
5+
import sys
6+
sys.path.insert(0, "../..")
57
import ptest
68

79
tests = []
810

9-
for test_file in pathlib.Path('.').glob('signal-*.py'):
11+
for test_file in pathlib.Path(os.path.abspath(os.path.dirname(__file__))).glob('signal-*.py'):
1012
tests.append((1, test_file.resolve()))
1113

12-
test = ptest.PTest()
13-
test.run_tests(tests)
14+
if __name__ == "__main__":
15+
test = ptest.PTest()
16+
test.run_tests(tests)

test/broker/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ clean :
1515
test-compile :
1616
$(MAKE) -C c
1717

18-
ptest : test-compile msg_sequence_test
18+
ptest : test-compile
1919
./test.py
2020

2121
test : test-compile msg_sequence_test 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 20 21

test/broker/msg_sequence_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def do_test(hostname, port, protocol):
209209
failed_tests.append(this_test)
210210
sock = mosq_test.client_connect_only(hostname=hostname, port=port, timeout=2, protocol=protocol)
211211
this_test.process_all(sock)
212-
print("\033[32m" + tname + "\033[0m")
212+
#print("\033[32m" + tname + "\033[0m")
213213
succeeded += 1
214214
sock.close()
215215
failed_tests.pop()

test/broker/test.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
3+
import sys
4+
sys.path.insert(0, "..")
45
import ptest
56

67
tests = [
78
#(ports required, 'path'),
9+
(1, './msg_sequence_test.py'),
810
(1, './01-bad-initial-packets.py'),
911
(1, './01-connect-575314.py'),
1012
(1, './01-connect-accept-protocol.py'),
@@ -289,5 +291,6 @@
289291
(1, './21-proxy-v2-ssl-require-tls-success.py'),
290292
]
291293

292-
test = ptest.PTest()
293-
test.run_tests(tests)
294+
if __name__ == "__main__":
295+
test = ptest.PTest()
296+
test.run_tests(tests)

test/client/test.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
3+
import sys
4+
sys.path.insert(0, "..")
45
import ptest
56

67
tests = [
@@ -47,5 +48,6 @@
4748
(2, './04-rr-qos1-ws.py'),
4849
]
4950

50-
test = ptest.PTest()
51-
test.run_tests(tests)
51+
if __name__ == "__main__":
52+
test = ptest.PTest()
53+
test.run_tests(tests)

test/lib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ all :
1010

1111
check : test
1212

13-
ptest : test-compile msg_sequence_test
13+
ptest : test-compile
1414
./test.py
1515

1616
msg_sequence_test: test-compile-c

test/lib/test.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env python3
22

3-
import mosq_test_helper
3+
import sys
4+
sys.path.insert(0, "..")
45
import ptest
56

67
tests = [
8+
(1, './msg_sequence_test.py'),
79
(1, './01-con-discon-success-v5.py'),
810
(1, './01-con-discon-success.py'),
911
(1, './01-con-discon-will-clear.py'),
@@ -63,5 +65,6 @@
6365
]
6466

6567

66-
test = ptest.PTest()
67-
test.run_tests(tests)
68+
if __name__ == "__main__":
69+
test = ptest.PTest()
70+
test.run_tests(tests)

test/ptest.py

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#!/usr/bin/env python3
22

3+
from pathlib import Path
34
import subprocess
45
import time
56
import sys
67

78
class PTestCase():
8-
def __init__(self, testdef):
9-
self.ports = testdef[0]
10-
self.cmd = str(testdef[1])
9+
def __init__(self, path, ports, cmd, args=None):
10+
self.path = path
11+
self.ports = ports
12+
self.cmd = str(cmd)
1113
self.attempts = 0
12-
try:
13-
if isinstance(testdef[2], (list,)):
14-
self.args = [self.cmd] + testdef[2]
15-
else:
16-
self.args = [self.cmd] + [testdef[2]]
17-
except IndexError:
14+
if args is not None:
15+
self.args = [self.cmd] + args
16+
else:
1817
self.args = [self.cmd]
1918
self.start_time = 0
2019
self.proc = None
@@ -26,12 +25,12 @@ def start(self):
2625
for p in self.mosq_port:
2726
self.run_args.append(str(p))
2827

29-
self.proc = subprocess.Popen(self.run_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
28+
self.proc = subprocess.Popen(self.run_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.path)
3029
self.start_time = time.time()
3130

3231
def print_result(self, col):
3332
cmd = " ".join(self.run_args)
34-
print(f"{self.runtime:0.3f}s : \033[{col}m{cmd}\033[0m")
33+
print(f"{self.runtime:0.3f}s : \033[{col}m{self.path}/{cmd}\033[0m")
3534

3635

3736
class PTest():
@@ -40,51 +39,60 @@ def __init__(self, minport=1888, max_running=20):
4039
self.max_running = 20
4140
self.tests = []
4241

43-
def next_test(self, tests, ports):
44-
if len(tests) == 0 or len(ports) == 0:
42+
def add_tests(self, test_list, path=".", label=""):
43+
for testdef in test_list:
44+
try:
45+
if isinstance(testdef[2], (list,)):
46+
args = testdef[2]
47+
else:
48+
args = [testdef[2]]
49+
except IndexError:
50+
args = None
51+
self.tests.append(PTestCase(path, testdef[0], testdef[1], args))
52+
53+
def _next_test(self, ports):
54+
if len(self.tests) == 0 or len(ports) == 0:
4555
return
4656

47-
test = tests.pop()
57+
test = self.tests.pop()
4858
test.mosq_port = []
4959

5060
if len(ports) < test.ports:
51-
tests.insert(0, test)
61+
self.tests.insert(0, test)
5262
return None
5363
else:
54-
5564
for i in range(0, test.ports):
5665
proc_port = ports.pop()
5766
test.mosq_port.append(proc_port)
5867

5968
test.start()
6069
return test
6170

62-
6371
def run_tests(self, test_list):
72+
self.add_tests(test_list)
73+
self.run()
74+
75+
def run(self):
6476
ports = list(range(self.minport, self.minport+self.max_running+1))
6577
start_time = time.time()
6678
passed = 0
6779
retried = 0
6880
failed = 0
6981

70-
tests = []
71-
for t in test_list:
72-
tests.append(PTestCase(t))
73-
7482
failed_tests = []
7583
running_tests = []
7684
retry_tests = []
77-
while len(tests) > 0 or len(running_tests) > 0 or len(retry_tests) > 0:
85+
while len(self.tests) > 0 or len(running_tests) > 0 or len(retry_tests) > 0:
7886
if len(running_tests) <= self.max_running:
79-
t = self.next_test(tests, ports)
87+
t = self._next_test(ports)
8088
if t is None:
8189
time.sleep(0.1)
8290
else:
8391
running_tests.append(t)
8492

85-
if len(running_tests) == 0 and len(tests) == 0 and len(retry_tests) > 0:
93+
if len(running_tests) == 0 and len(self.tests) == 0 and len(retry_tests) > 0:
8694
# Only retry tests after everything else has finished to reduce load
87-
tests = retry_tests
95+
self.tests = retry_tests
8896
retry_tests = []
8997

9098
for t in running_tests:

0 commit comments

Comments
 (0)