From bdbabf5594e2c5dba9f2b52d59e742c506eacd60 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 14 Aug 2018 14:59:31 +0200 Subject: [PATCH] build: when running test, wait until emulator starts properly --- script/test | 1 + script/wait_for_emulator.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100755 script/wait_for_emulator.py diff --git a/script/test b/script/test index 8ef1437fb..b6bc428ff 100755 --- a/script/test +++ b/script/test @@ -12,6 +12,7 @@ if [ "$EMULATOR" = 1 ]; then firmware/trezor.elf & sleep 1 export TREZOR_PATH=udp:127.0.0.1:21324 + "${PYTHON:-python}" script/wait_for_emulator.py fi export TREZOR_TRANSPORT_V1=1 diff --git a/script/wait_for_emulator.py b/script/wait_for_emulator.py new file mode 100755 index 000000000..035104b15 --- /dev/null +++ b/script/wait_for_emulator.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import socket +import sys +import time + +DEFAULT_ADDR = "127.0.0.1:21324" + +if len(sys.argv) > 1: + addr = sys.argv[1] +else: + addr = DEFAULT_ADDR + +host, port = addr.split(":") +SOCK_ADDR = (host, int(port)) + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock.connect(SOCK_ADDR) +sock.settimeout(0) + +start = time.monotonic() +while True: + try: + sock.sendall(b"PINGPING") + r = sock.recv(8) + if r == b"PONGPONG": + break + except Exception: + time.sleep(0.05) +end = time.monotonic() +print("waited for {:.3f}s".format(end - start))