Skip to content

Commit b41d0cc

Browse files
committed
increased sleeptime, remove harcoded message[i]: should work on different systems
1 parent 099a575 commit b41d0cc

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

Diff for: test/test_ws.py

+51-48
Original file line numberDiff line numberDiff line change
@@ -14,125 +14,128 @@ def test_list(socketio):
1414
global message
1515
socketio.on('message', message_handler)
1616
socketio.emit('command', 'list')
17-
time.sleep(.1)
18-
# print (message)
19-
assert "list" in message[0]
20-
assert "Ports" in message[1]
21-
assert "Network" in message[2]
17+
time.sleep(.2)
18+
print (message)
19+
assert any("list" in i for i in message)
20+
assert any("Ports" in i for i in message)
21+
assert any("Network" in i for i in message)
2222

2323
# NOTE run the following tests on linux with a board connected to the PC and with this sketch on it: https://gist.github.com/Protoneer/96db95bfb87c3befe46e
2424
@pytest.mark.skipif(
2525
running_on_ci(),
2626
reason="VMs have no serial ports",
2727
)
28-
def test__open_serial_default(socketio):
28+
def test_open_serial_default(socketio):
29+
time.sleep(.2)
2930
global message
3031
message = []
3132
socketio.on('message', message_handler)
3233
socketio.emit('command', 'open /dev/ttyACM0 9600')
33-
time.sleep(.1) # give time to message to be filled
34-
assert "\"IsOpen\": true" in message[2]
34+
time.sleep(.5) # give time to message to be filled
35+
assert any("\"IsOpen\": true" in i for i in message)
3536
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
36-
time.sleep(.1)
37-
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
37+
time.sleep(.2)
38+
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
3839
assert "ciao" in extract_serial_data(message)
3940

4041
# test with a lot of emoji: they can be messed up
41-
message = [] # reinitialize the message buffer
42+
# message = [] # reinitialize the message buffer
4243
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
43-
time.sleep(.1)
44-
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
44+
time.sleep(.2)
45+
print (message)
46+
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
4547
emoji_output = extract_serial_data(message)
46-
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in emoji_output # this is failing because of UTF8 encoding problems
48+
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in emoji_output # this could be failing because of UTF8 encoding problems
4749
message = []
4850
socketio.emit('command', 'close /dev/ttyACM0')
49-
time.sleep(.1)
50-
# print (message)
51-
assert "\"IsOpen\": false," in message[8]
51+
time.sleep(.2)
52+
assert any("\"IsOpen\": false," in i for i in message)
5253

5354
@pytest.mark.skipif(
5455
running_on_ci(),
5556
reason="VMs have no serial ports",
5657
)
57-
def test__open_serial_timed(socketio):
58+
def test_open_serial_timed(socketio):
59+
time.sleep(.2)
5860
global message
5961
message = []
6062
socketio.on('message', message_handler)
6163
socketio.emit('command', 'open /dev/ttyACM0 9600 timed')
62-
time.sleep(.1) # give time to message to be filled
63-
assert "\"IsOpen\": true" in message[2]
64+
time.sleep(.5) # give time to message to be filled
65+
print(message)
66+
assert any("\"IsOpen\": true" in i for i in message)
6467
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
65-
time.sleep(.1)
66-
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
68+
time.sleep(.2)
69+
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
6770
assert "ciao" in extract_serial_data(message)
6871

6972
# test with a lot of emoji: usually they get messed up
7073
message = [] # reinitialize the message buffer
7174
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
72-
time.sleep(.1)
73-
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
74-
emoji_output = extract_serial_data(message)
75-
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in emoji_output
75+
time.sleep(.2)
76+
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
77+
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in extract_serial_data(message)
7678
message = []
7779
socketio.emit('command', 'close /dev/ttyACM0')
78-
time.sleep(.1)
80+
time.sleep(.2)
7981
# print (message)
80-
assert "\"IsOpen\": false," in message[8]
82+
assert any("\"IsOpen\": false," in i for i in message)
8183

8284
@pytest.mark.skipif(
8385
running_on_ci(),
8486
reason="VMs have no serial ports",
8587
)
86-
def test__open_serial_timedraw(socketio):
88+
def test_open_serial_timedraw(socketio):
8789
global message
8890
message = []
8991
socketio.on('message', message_handler)
9092
socketio.emit('command', 'open /dev/ttyACM0 9600 timedraw')
91-
time.sleep(.1) # give time to message to be filled
92-
assert "\"IsOpen\": true" in message[2]
93+
time.sleep(.5) # give time to message to be filled
94+
assert any("\"IsOpen\": true" in i for i in message)
9395
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
94-
time.sleep(.1)
95-
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
96+
time.sleep(.2)
97+
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
9698
assert "ciao" in decode_output(extract_serial_data(message))
9799

98100
# test with a lot of emoji: usually they get messed up
99101
message = [] # reinitialize the message buffer
100102
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
101-
time.sleep(.1)
102-
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
103+
time.sleep(.2)
104+
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
103105
# print (message)
104106
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in decode_output(extract_serial_data(message))
105107
socketio.emit('command', 'close /dev/ttyACM0')
106-
time.sleep(.1)
108+
time.sleep(.2)
107109
# print (message)
108-
assert "\"IsOpen\": false," in message[10]
110+
assert any("\"IsOpen\": false," in i for i in message)
109111

110112
@pytest.mark.skipif(
111113
running_on_ci(),
112114
reason="VMs have no serial ports",
113115
)
114-
def test__open_serial_timedbinary(socketio):
116+
def test_open_serial_timedbinary(socketio):
115117
global message
116118
message = []
117119
socketio.on('message', message_handler)
118120
socketio.emit('command', 'open /dev/ttyACM0 9600 timedbinary')
119-
time.sleep(1) # give time to message to be filled
120-
assert "\"IsOpen\": true" in message[2]
121+
time.sleep(.5) # give time to message to be filled
122+
assert any("\"IsOpen\": true" in i for i in message)
121123
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
122-
time.sleep(.1)
123-
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
124+
time.sleep(.2)
125+
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
126+
print (message)
124127
assert "ciao" in decode_output(extract_serial_data(message))
125128

126129
# test with a lot of emoji: usually they get messed up
127130
message = [] # reinitialize the message buffer
128131
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
129-
time.sleep(.1)
130-
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
132+
time.sleep(.2)
133+
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
131134
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in decode_output(extract_serial_data(message))
132135
socketio.emit('command', 'close /dev/ttyACM0')
133-
time.sleep(.1)
136+
time.sleep(.2)
134137
# print (message)
135-
assert "\"IsOpen\": false," in message[10]
138+
assert any("\"IsOpen\": false," in i for i in message)
136139

137140

138141
# callback called by socketio when a message is received
@@ -147,9 +150,9 @@ def extract_serial_data(msg):
147150
serial_data = ""
148151
for i in msg:
149152
if "{\"P\"" in i:
150-
# print (json.loads(i)["D"])
153+
print (json.loads(i)["D"])
151154
serial_data+=json.loads(i)["D"]
152-
# print("serialdata:"+serial_data)
155+
print("serialdata:"+serial_data)
153156
return serial_data
154157

155158
def decode_output(raw_output):

0 commit comments

Comments
 (0)