From bc3adaded3f09e6d5fe1c5a18e509aae2490269a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 21 Jun 2021 15:59:57 +0200 Subject: [PATCH] CI/socket examples: Add UDP operation retries, wait until server is up --- .../sockets/udp_server/example_test.py | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/examples/protocols/sockets/udp_server/example_test.py b/examples/protocols/sockets/udp_server/example_test.py index e82311fab7..484c181dab 100644 --- a/examples/protocols/sockets/udp_server/example_test.py +++ b/examples/protocols/sockets/udp_server/example_test.py @@ -27,9 +27,10 @@ def udp_client(address, payload): family_addr, socktype, proto, canonname, addr = res try: sock = socket.socket(family_addr, socket.SOCK_DGRAM) - sock.settimeout(60.0) + sock.settimeout(20.0) except socket.error as msg: - print('Could not create socket: ' + str(msg[0]) + ': ' + msg[1]) + print('Could not create socket') + print(os.strerror(msg.errno)) raise try: sock.sendto(payload.encode(), addr) @@ -37,8 +38,12 @@ def udp_client(address, payload): if not reply: return print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + str(reply)) + except socket.timeout: + print('Socket operation timeout') + return str(None) except socket.error as msg: - print('Error Code : ' + str(msg[0]) + ' Message: ' + msg[1]) + print('Error while sending or receiving data from the socket') + print(os.strerror(msg.errno)) sock.close() raise return reply.decode() @@ -47,6 +52,7 @@ def udp_client(address, payload): @ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') def test_examples_protocol_socket_udpserver(env, extra_data): MESSAGE = 'Data to ESP' + MAX_RETRIES = 3 """ steps: 1. join AP @@ -66,16 +72,28 @@ def test_examples_protocol_socket_udpserver(env, extra_data): ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form) ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0] print('Connected with IPv4={} and IPv6={}'.format(ipv4, ipv6)) + dut1.expect(re.compile(r'Waiting for data'), timeout=10) # test IPv4 - received = udp_client(ipv4, MESSAGE) - if not received == MESSAGE: - raise + for _ in range(MAX_RETRIES): + print('Testing UDP on IPv4...') + received = udp_client(ipv4, MESSAGE) + if received == MESSAGE: + print('OK') + break + else: + raise ValueError('IPv4: Did not receive UDP message after {} retries'.format(MAX_RETRIES)) dut1.expect(MESSAGE) + # test IPv6 - received = udp_client('{}%{}'.format(ipv6, INTERFACE), MESSAGE) - if not received == MESSAGE: - raise + for _ in range(MAX_RETRIES): + print('Testing UDP on IPv6...') + received = udp_client('{}%{}'.format(ipv6, INTERFACE), MESSAGE) + if received == MESSAGE: + print('OK') + break + else: + raise ValueError('IPv6: Did not receive UDP message after {} retries'.format(MAX_RETRIES)) dut1.expect(MESSAGE)