-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnnc_test.py
executable file
·171 lines (143 loc) · 5.21 KB
/
nnc_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
#!/usr/bin/python3
from cgi import test
from re import T
import subprocess
import shlex
import os
import paho.mqtt.client as mqtt
from multiprocessing import Process, Value
import time
import signal
import os
g_port = 1883
g_addr = "127.0.0.1"
g_url = " -h {addr} -p {port} ".format(addr = g_addr, port = g_port)
def cnt_message(cmd, n, pid, message):
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
pid.value = process.pid
while True:
output = process.stdout.readline()
if output.strip() == message:
n.value += 1
def test_clean_session():
clean_session_cmd = shlex.split("mosquitto_sub -t topic {} -q 1".format(g_url))
persist_session_cmd = shlex.split("mosquitto_sub -t topic {} -c -i id -q 1".format(g_url))
pub_cmd = shlex.split("mosquitto_pub -m message {} -t topic -q 1".format(g_url))
# persistence session
process = subprocess.Popen(persist_session_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
process.terminate()
time.sleep(1)
process = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
process.terminate()
time.sleep(1)
cnt = Value('i', 0)
pid = Value('i', 0)
process = Process(target=cnt_message, args=(persist_session_cmd, cnt, pid, "message"))
process.start()
time.sleep(5)
process.terminate()
if cnt.value == 1:
print("clean session test passed!")
else:
print("clean session test failed!")
os.kill(pid.value, signal.SIGKILL)
pid = Value('i', 0)
process = Process(target=cnt_message, args=(clean_session_cmd, cnt, pid, "message"))
process.start()
time.sleep(1)
process.terminate()
os.kill(pid.value, signal.SIGKILL)
def test_retain():
retain_pub_cmd = shlex.split("mosquitto_pub -m message {} -t topic -r".format(g_url))
clean_retain_pub_cmd = shlex.split("mosquitto_pub -n {} -t topic -r".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t topic {}".format(g_url))
process = subprocess.Popen(retain_pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
cnt = Value('i', 0)
pid = Value('i', 0)
process = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "message"))
process.start()
time.sleep(1)
process.terminate()
process = subprocess.Popen(clean_retain_pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
process.terminate()
if cnt.value != 1:
print("Retain test failed!")
return
print("Retain test passed!")
os.kill(pid.value, signal.SIGKILL)
def test_v4_v5():
sub_cmd_v4 = shlex.split("mosquitto_sub -t topic/v4/v5 {} -V 311".format(g_url))
sub_cmd_v5 = shlex.split("mosquitto_sub -t topic/v4/v5 {} -V 5".format(g_url))
pub_cmd_v4 = shlex.split("mosquitto_pub -m message -t topic/v4/v5 {} -V 311".format(g_url))
pub_cmd_v5 = shlex.split("mosquitto_pub -m message -t topic/v4/v5 {} -V 5".format(g_url))
cnt = Value('i', 0)
pid = Value('i', 0)
process = Process(target=cnt_message, args=(sub_cmd_v4, cnt, pid, "message"))
process.start()
time.sleep(1)
process2 = subprocess.Popen(pub_cmd_v5,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
process.terminate()
os.kill(pid.value, signal.SIGKILL)
pid = Value('i', 0)
if cnt.value != 1:
print("V4/V5 test failed!")
return
process = Process(target=cnt_message, args=(sub_cmd_v5, cnt, pid, "message"))
process.start()
time.sleep(1)
process4 = subprocess.Popen(pub_cmd_v4,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
process.terminate()
os.kill(pid.value, signal.SIGKILL)
if cnt.value != 2:
print("V4/V5 test failed!")
return
print("V4/V5 test passed!")
def test_will_topic():
pub_cmd = shlex.split("mosquitto_pub {} -t msg -d -l --will-topic will_topic --will-payload will_payload".format(g_url))
sub_cmd = shlex.split("mosquitto_sub -t will_topic {}".format(g_url))
cnt = Value('i', 0)
pid = Value('i', 0)
process = Process(target=cnt_message, args=(sub_cmd, cnt, pid, "will_payload"))
process.start()
time.sleep(1)
process2 = subprocess.Popen(pub_cmd,
stdout=subprocess.PIPE,
universal_newlines=True)
time.sleep(1)
process2.terminate()
times = 0
while True:
if cnt.value == 1:
print("Will topic test passed!")
break
time.sleep(1)
times += 1
if times == 5:
print("Will topic test failed!")
break
process.terminate()
os.kill(pid.value, signal.SIGKILL)
if __name__=='__main__':
test_will_topic()
test_v4_v5()
test_clean_session()
test_retain()