1
1
#!/usr/bin/env python3
2
2
3
+ from pathlib import Path
3
4
import subprocess
4
5
import time
5
6
import sys
6
7
7
8
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 )
11
13
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 :
18
17
self .args = [self .cmd ]
19
18
self .start_time = 0
20
19
self .proc = None
@@ -26,12 +25,12 @@ def start(self):
26
25
for p in self .mosq_port :
27
26
self .run_args .append (str (p ))
28
27
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 )
30
29
self .start_time = time .time ()
31
30
32
31
def print_result (self , col ):
33
32
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" )
35
34
36
35
37
36
class PTest ():
@@ -40,51 +39,60 @@ def __init__(self, minport=1888, max_running=20):
40
39
self .max_running = 20
41
40
self .tests = []
42
41
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 :
45
55
return
46
56
47
- test = tests .pop ()
57
+ test = self . tests .pop ()
48
58
test .mosq_port = []
49
59
50
60
if len (ports ) < test .ports :
51
- tests .insert (0 , test )
61
+ self . tests .insert (0 , test )
52
62
return None
53
63
else :
54
-
55
64
for i in range (0 , test .ports ):
56
65
proc_port = ports .pop ()
57
66
test .mosq_port .append (proc_port )
58
67
59
68
test .start ()
60
69
return test
61
70
62
-
63
71
def run_tests (self , test_list ):
72
+ self .add_tests (test_list )
73
+ self .run ()
74
+
75
+ def run (self ):
64
76
ports = list (range (self .minport , self .minport + self .max_running + 1 ))
65
77
start_time = time .time ()
66
78
passed = 0
67
79
retried = 0
68
80
failed = 0
69
81
70
- tests = []
71
- for t in test_list :
72
- tests .append (PTestCase (t ))
73
-
74
82
failed_tests = []
75
83
running_tests = []
76
84
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 :
78
86
if len (running_tests ) <= self .max_running :
79
- t = self .next_test ( tests , ports )
87
+ t = self ._next_test ( ports )
80
88
if t is None :
81
89
time .sleep (0.1 )
82
90
else :
83
91
running_tests .append (t )
84
92
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 :
86
94
# Only retry tests after everything else has finished to reduce load
87
- tests = retry_tests
95
+ self . tests = retry_tests
88
96
retry_tests = []
89
97
90
98
for t in running_tests :
0 commit comments