Skip to content

Commit

Permalink
Generic ptest implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Feb 12, 2019
1 parent b4a94f5 commit baa4642
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 202 deletions.
2 changes: 1 addition & 1 deletion test/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test-compile :
$(MAKE) -C c

ptest : test-compile
./ptest.py
./test.py

test : test-compile 01 02 03 04 05 06 07 08 09 10 11 12

Expand Down
79 changes: 3 additions & 76 deletions test/broker/ptest.py → test/broker/test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env python3

import subprocess
import time
import sys
import mosq_test_helper
import ptest

max_running = 10
tests = [
#(ports required, 'path'),
(1, './01-connect-bad-packet.py'),
Expand Down Expand Up @@ -145,75 +143,4 @@
(1, './12-prop-response-topic-correlation-data.py'),
]

minport = 1888
ports = list(range(minport, minport+max_running+1))

def next_test(tests, ports):
if len(tests) == 0 or len(ports) == 0:
return

test = tests.pop()
proc_ports = ()

if len(ports) < test[0]:
tests.insert(0, test)
return None
else:
args = [test[1]]

for i in range(0, test[0]):
proc_port = ports.pop()
proc_ports = proc_ports + (proc_port,)
args.append(str(proc_port))

proc = subprocess.Popen(args)
proc.start_time = time.time()
proc.mosq_port = proc_ports
return proc


def run_tests(tests, ports):
passed = 0
failed = 0

failed_tests = []

running_tests = []
while len(tests) > 0 or len(running_tests) > 0:
if len(running_tests) <= max_running:
t = next_test(tests, ports)
if t is None:
time.sleep(0.1)
else:
running_tests.append(t)

for t in running_tests:
t.poll()
if t.returncode is not None:
running_tests.remove(t)
if isinstance(t.mosq_port, tuple):
for portret in t.mosq_port:
ports.append(portret)
else:
ports.append(t.mosq_port)
t.terminate()
t.wait()
runtime = time.time() - t.start_time
#(stdo, stde) = t.communicate()
if t.returncode == 1:
print("%0.3fs : \033[31m%s\033[0m" % (runtime, t.args[0]))
failed = failed + 1
failed_tests.append(t.args[0])
else:
passed = passed + 1
print("%0.3fs : \033[32m%s\033[0m" % (runtime, t.args[0]))

print("Passed: %d\nFailed: %d\nTotal: %d" % (passed, failed, passed+failed))
if failed > 0:
print("Failing tests:")
for f in failed_tests:
print(f)
sys.exit(1)


run_tests(tests, ports)
ptest.run_tests(tests)
2 changes: 1 addition & 1 deletion test/lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LD_LIBRARY_PATH=../../lib
all :

ptest : test-compile
./ptest.py
./test.py

test : c cpp

Expand Down
124 changes: 0 additions & 124 deletions test/lib/ptest.py

This file was deleted.

71 changes: 71 additions & 0 deletions test/lib/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3

import mosq_test_helper
import ptest

tests = [
(1, ['./01-con-discon-success.py', 'c/01-con-discon-success.test']),
(1, ['./01-keepalive-pingreq.py', 'c/01-keepalive-pingreq.test']),
(1, ['./01-server-keepalive-pingreq.py', 'c/01-server-keepalive-pingreq.test']),
(1, ['./01-no-clean-session.py', 'c/01-no-clean-session.test']),
(1, ['./01-unpwd-set.py', 'c/01-unpwd-set.test']),
(1, ['./01-will-set.py', 'c/01-will-set.test']),
(1, ['./01-will-unpwd-set.py', 'c/01-will-unpwd-set.test']),
(1, ['./02-subscribe-qos0.py', 'c/02-subscribe-qos0.test']),
(1, ['./02-subscribe-qos1.py', 'c/02-subscribe-qos1.test']),
(1, ['./02-subscribe-qos2.py', 'c/02-subscribe-qos2.test']),
(1, ['./02-unsubscribe.py', 'c/02-unsubscribe.test']),
(1, ['./03-publish-b2c-qos1.py', 'c/03-publish-b2c-qos1.test']),
(1, ['./03-publish-b2c-qos2.py', 'c/03-publish-b2c-qos2.test']),
(1, ['./03-publish-c2b-qos1-len.py', 'c/03-publish-c2b-qos1-len.test']),
(1, ['./03-publish-c2b-qos2-len.py', 'c/03-publish-c2b-qos2-len.test']),
(1, ['./03-publish-b2c-qos2-len.py', 'c/03-publish-b2c-qos2-len.test']),
(1, ['./03-publish-c2b-qos1-disconnect.py', 'c/03-publish-c2b-qos1-disconnect.test']),
(1, ['./03-publish-c2b-qos2-disconnect.py', 'c/03-publish-c2b-qos2-disconnect.test']),
(1, ['./03-publish-c2b-qos2.py', 'c/03-publish-c2b-qos2.test']),
(1, ['./03-publish-c2b-qos1-receive-maximum.py', 'c/03-publish-c2b-qos1-receive-maximum.test']),
(1, ['./03-publish-c2b-qos2-receive-maximum-1.py', 'c/03-publish-c2b-qos2-receive-maximum-1.test']),
(1, ['./03-publish-c2b-qos2-receive-maximum-2.py', 'c/03-publish-c2b-qos2-receive-maximum-2.test']),
(1, ['./03-publish-c2b-qos2-pubrec-error.py', 'c/03-publish-c2b-qos2-pubrec-error.test']),
(1, ['./03-publish-c2b-qos2-maximum-qos-0.py', 'c/03-publish-c2b-qos2-maximum-qos-0.test']),
(1, ['./03-publish-c2b-qos2-maximum-qos-1.py', 'c/03-publish-c2b-qos2-maximum-qos-1.test']),
(1, ['./03-publish-qos0-no-payload.py', 'c/03-publish-qos0-no-payload.test']),
(1, ['./03-publish-qos0.py', 'c/03-publish-qos0.test']),
(1, ['./03-request-response.py', 'c/03-request-response.test']),
(1, ['./03-request-response-correlation.py', 'c/03-request-response-correlation.test']),
(1, ['./04-retain-qos0.py', 'c/04-retain-qos0.test']),
(1, ['./08-ssl-bad-cacert.py', 'c/08-ssl-bad-cacert.test']),
(1, ['./08-ssl-connect-cert-auth-enc.py', 'c/08-ssl-connect-cert-auth-enc.test']),
(1, ['./08-ssl-connect-cert-auth.py', 'c/08-ssl-connect-cert-auth.test']),
(1, ['./08-ssl-connect-no-auth.py', 'c/08-ssl-connect-no-auth.test']),
(1, ['./09-util-topic-tokenise.py', 'c/09-util-topic-tokenise.test']),
(1, ['./11-prop-send-payload-format.py', 'c/11-prop-send-payload-format.test']),
(1, ['./11-prop-send-content-type.py', 'c/11-prop-send-content-type.test']),

(1, ['./01-con-discon-success.py', 'cpp/01-con-discon-success.test']),
(1, ['./01-keepalive-pingreq.py', 'cpp/01-keepalive-pingreq.test']),
(1, ['./01-no-clean-session.py', 'cpp/01-no-clean-session.test']),
(1, ['./01-unpwd-set.py', 'cpp/01-unpwd-set.test']),
(1, ['./01-will-set.py', 'cpp/01-will-set.test']),
(1, ['./01-will-unpwd-set.py', 'cpp/01-will-unpwd-set.test']),
(1, ['./02-subscribe-qos0.py', 'cpp/02-subscribe-qos0.test']),
(1, ['./02-subscribe-qos1.py', 'cpp/02-subscribe-qos1.test']),
(1, ['./02-subscribe-qos2.py', 'cpp/02-subscribe-qos2.test']),
(1, ['./02-unsubscribe.py', 'cpp/02-unsubscribe.test']),
(1, ['./03-publish-b2c-qos1.py', 'cpp/03-publish-b2c-qos1.test']),
(1, ['./03-publish-b2c-qos2.py', 'cpp/03-publish-b2c-qos2.test']),
(1, ['./03-publish-c2b-qos1-disconnect.py', 'cpp/03-publish-c2b-qos1-disconnect.test']),
(1, ['./03-publish-c2b-qos2-disconnect.py', 'cpp/03-publish-c2b-qos2-disconnect.test']),
(1, ['./03-publish-c2b-qos2.py', 'cpp/03-publish-c2b-qos2.test']),
(1, ['./03-publish-qos0-no-payload.py', 'cpp/03-publish-qos0-no-payload.test']),
(1, ['./03-publish-qos0.py', 'cpp/03-publish-qos0.test']),
(1, ['./04-retain-qos0.py', 'cpp/04-retain-qos0.test']),
(1, ['./08-ssl-bad-cacert.py', 'cpp/08-ssl-bad-cacert.test']),
(1, ['./08-ssl-connect-cert-auth-enc.py', 'cpp/08-ssl-connect-cert-auth-enc.test']),
(1, ['./08-ssl-connect-cert-auth.py', 'cpp/08-ssl-connect-cert-auth.test']),
(1, ['./08-ssl-connect-no-auth.py', 'cpp/08-ssl-connect-no-auth.test']),
(1, ['./09-util-topic-tokenise.py', 'cpp/09-util-topic-tokenise.test']),
]


ptest.run_tests(tests)
78 changes: 78 additions & 0 deletions test/ptest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3

import subprocess
import time
import sys

def next_test(tests, ports):
if len(tests) == 0 or len(ports) == 0:
return

test = tests.pop()
proc_ports = ()

if len(ports) < test[0]:
tests.insert(0, test)
return None
else:
if isinstance(test[1], (list,)):
args = test[1]
else:
args = [test[1]]

for i in range(0, test[0]):
proc_port = ports.pop()
proc_ports = proc_ports + (proc_port,)
args.append(str(proc_port))

proc = subprocess.Popen(args)
proc.start_time = time.time()
proc.mosq_port = proc_ports
return proc


def run_tests(tests, minport=1888, max_running=20):
ports = list(range(minport, minport+max_running+1))
start_time = time.time()
passed = 0
failed = 0

failed_tests = []

running_tests = []
while len(tests) > 0 or len(running_tests) > 0:
if len(running_tests) <= max_running:
t = next_test(tests, ports)
if t is None:
time.sleep(0.1)
else:
running_tests.append(t)

for t in running_tests:
t.poll()
if t.returncode is not None:
running_tests.remove(t)
if isinstance(t.mosq_port, tuple):
for portret in t.mosq_port:
ports.append(portret)
else:
ports.append(t.mosq_port)
t.terminate()
t.wait()
runtime = time.time() - t.start_time
#(stdo, stde) = t.communicate()
if t.returncode == 1:
print("%0.3fs : \033[31m%s\033[0m" % (runtime, t.args[0]))
failed = failed + 1
failed_tests.append(t.args[0])
else:
passed = passed + 1
print("%0.3fs : \033[32m%s\033[0m" % (runtime, t.args[0]))

print("Passed: %d\nFailed: %d\nTotal: %d\nTotal time: %0.2f" % (passed, failed, passed+failed, time.time()-start_time))
if failed > 0:
print("Failing tests:")
for f in failed_tests:
print(f)
sys.exit(1)

0 comments on commit baa4642

Please sign in to comment.