Skip to content

Commit

Permalink
increased sleeptime, remove harcoded message[i]: should work on diffe…
Browse files Browse the repository at this point in the history
…rent systems
  • Loading branch information
umbynos committed Jul 30, 2021
1 parent 099a575 commit b41d0cc
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions test/test_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,125 +14,128 @@ def test_list(socketio):
global message
socketio.on('message', message_handler)
socketio.emit('command', 'list')
time.sleep(.1)
# print (message)
assert "list" in message[0]
assert "Ports" in message[1]
assert "Network" in message[2]
time.sleep(.2)
print (message)
assert any("list" in i for i in message)
assert any("Ports" in i for i in message)
assert any("Network" in i for i in message)

# 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
@pytest.mark.skipif(
running_on_ci(),
reason="VMs have no serial ports",
)
def test__open_serial_default(socketio):
def test_open_serial_default(socketio):
time.sleep(.2)
global message
message = []
socketio.on('message', message_handler)
socketio.emit('command', 'open /dev/ttyACM0 9600')
time.sleep(.1) # give time to message to be filled
assert "\"IsOpen\": true" in message[2]
time.sleep(.5) # give time to message to be filled
assert any("\"IsOpen\": true" in i for i in message)
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
assert "ciao" in extract_serial_data(message)

# test with a lot of emoji: they can be messed up
message = [] # reinitialize the message buffer
# message = [] # reinitialize the message buffer
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
time.sleep(.2)
print (message)
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
emoji_output = extract_serial_data(message)
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in emoji_output # this is failing because of UTF8 encoding problems
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in emoji_output # this could be failing because of UTF8 encoding problems
message = []
socketio.emit('command', 'close /dev/ttyACM0')
time.sleep(.1)
# print (message)
assert "\"IsOpen\": false," in message[8]
time.sleep(.2)
assert any("\"IsOpen\": false," in i for i in message)

@pytest.mark.skipif(
running_on_ci(),
reason="VMs have no serial ports",
)
def test__open_serial_timed(socketio):
def test_open_serial_timed(socketio):
time.sleep(.2)
global message
message = []
socketio.on('message', message_handler)
socketio.emit('command', 'open /dev/ttyACM0 9600 timed')
time.sleep(.1) # give time to message to be filled
assert "\"IsOpen\": true" in message[2]
time.sleep(.5) # give time to message to be filled
print(message)
assert any("\"IsOpen\": true" in i for i in message)
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
assert "ciao" in extract_serial_data(message)

# test with a lot of emoji: usually they get messed up
message = [] # reinitialize the message buffer
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
emoji_output = extract_serial_data(message)
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in emoji_output
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in extract_serial_data(message)
message = []
socketio.emit('command', 'close /dev/ttyACM0')
time.sleep(.1)
time.sleep(.2)
# print (message)
assert "\"IsOpen\": false," in message[8]
assert any("\"IsOpen\": false," in i for i in message)

@pytest.mark.skipif(
running_on_ci(),
reason="VMs have no serial ports",
)
def test__open_serial_timedraw(socketio):
def test_open_serial_timedraw(socketio):
global message
message = []
socketio.on('message', message_handler)
socketio.emit('command', 'open /dev/ttyACM0 9600 timedraw')
time.sleep(.1) # give time to message to be filled
assert "\"IsOpen\": true" in message[2]
time.sleep(.5) # give time to message to be filled
assert any("\"IsOpen\": true" in i for i in message)
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
assert "ciao" in decode_output(extract_serial_data(message))

# test with a lot of emoji: usually they get messed up
message = [] # reinitialize the message buffer
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
# print (message)
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in decode_output(extract_serial_data(message))
socketio.emit('command', 'close /dev/ttyACM0')
time.sleep(.1)
time.sleep(.2)
# print (message)
assert "\"IsOpen\": false," in message[10]
assert any("\"IsOpen\": false," in i for i in message)

@pytest.mark.skipif(
running_on_ci(),
reason="VMs have no serial ports",
)
def test__open_serial_timedbinary(socketio):
def test_open_serial_timedbinary(socketio):
global message
message = []
socketio.on('message', message_handler)
socketio.emit('command', 'open /dev/ttyACM0 9600 timedbinary')
time.sleep(1) # give time to message to be filled
assert "\"IsOpen\": true" in message[2]
time.sleep(.5) # give time to message to be filled
assert any("\"IsOpen\": true" in i for i in message)
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"ciao/\"" in message[3]
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
print (message)
assert "ciao" in decode_output(extract_serial_data(message))

# test with a lot of emoji: usually they get messed up
message = [] # reinitialize the message buffer
socketio.emit('command', 'send /dev/ttyACM0 /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"')
time.sleep(.1)
assert "send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in message[0]
time.sleep(.2)
assert any("send /dev/ttyACM0 /\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in i for i in message)
assert "/\"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\"" in decode_output(extract_serial_data(message))
socketio.emit('command', 'close /dev/ttyACM0')
time.sleep(.1)
time.sleep(.2)
# print (message)
assert "\"IsOpen\": false," in message[10]
assert any("\"IsOpen\": false," in i for i in message)


# callback called by socketio when a message is received
Expand All @@ -147,9 +150,9 @@ def extract_serial_data(msg):
serial_data = ""
for i in msg:
if "{\"P\"" in i:
# print (json.loads(i)["D"])
print (json.loads(i)["D"])
serial_data+=json.loads(i)["D"]
# print("serialdata:"+serial_data)
print("serialdata:"+serial_data)
return serial_data

def decode_output(raw_output):
Expand Down

0 comments on commit b41d0cc

Please sign in to comment.