From bcfd9818f29bf7980b18c6c9153f7d607feb9615 Mon Sep 17 00:00:00 2001 From: Suren Gabrielyan Date: Fri, 30 Dec 2022 19:50:22 +0400 Subject: [PATCH] mDNS: Fix example test in CI using multiple retries --- .../protocols/mdns/main/mdns_example_main.c | 42 ++++++++++++++----- examples/protocols/mdns/mdns_example_test.py | 2 +- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index 8f655fdad0..f9f35fe2fa 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -25,10 +25,11 @@ static const char *TAG = "mdns-test"; static char* generate_hostname(void); +static const int RETRY_COUNT = 10; #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1 -static void query_mdns_host_with_gethostbyname(char * host); -static void query_mdns_host_with_getaddrinfo(char * host); +static esp_err_t query_mdns_host_with_gethostbyname(char * host); +static esp_err_t query_mdns_host_with_getaddrinfo(char * host); #endif static void initialise_mdns(void) @@ -116,7 +117,7 @@ static void query_mdns_service(const char * service_name, const char * proto) mdns_query_results_free(results); } -static void query_mdns_host(const char * host_name) +static esp_err_t query_mdns_host(const char * host_name) { ESP_LOGI(TAG, "Query A: %s.local", host_name); @@ -127,13 +128,14 @@ static void query_mdns_host(const char * host_name) if(err){ if(err == ESP_ERR_NOT_FOUND){ ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err)); - return; + return err; } ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err)); - return; + return err; } ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr)); + return ESP_OK; } static void initialise_button(void) @@ -167,11 +169,27 @@ static void check_button(void) static void mdns_example_task(void *pvParameters) { + int i = 0; + const TickType_t xDelay = 1000 / portTICK_PERIOD_MS; #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1 /* Send initial queries that are started by CI tester */ - query_mdns_host("tinytester"); - query_mdns_host_with_gethostbyname("tinytester-lwip.local"); - query_mdns_host_with_getaddrinfo("tinytester-lwip.local"); + while (query_mdns_host("tinytester") != ESP_OK && i != RETRY_COUNT) { + query_mdns_host("tinytester"); + i++; + vTaskDelay(xDelay); + } + i = 0; + while (query_mdns_host_with_gethostbyname("tinytester-lwip.local") != ESP_OK && i != RETRY_COUNT) { + query_mdns_host_with_gethostbyname("tinytester-lwip.local"); + i++; + vTaskDelay(xDelay); + } + i = 0; + while (query_mdns_host_with_getaddrinfo("tinytester-lwip.local") != ESP_OK && i != RETRY_COUNT) { + query_mdns_host_with_getaddrinfo("tinytester-lwip.local"); + i++; + vTaskDelay(xDelay); + } #endif while(1) { @@ -221,7 +239,7 @@ static char* generate_hostname(void) * @brief Executes gethostbyname and displays list of resolved addresses. * Note: This function is used only to test advertised mdns hostnames resolution */ -static void query_mdns_host_with_gethostbyname(char * host) +static esp_err_t query_mdns_host_with_gethostbyname(char * host) { struct hostent *res = gethostbyname(host); if (res) { @@ -230,14 +248,16 @@ static void query_mdns_host_with_gethostbyname(char * host) ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, inet_ntoa(*(struct in_addr *) (res->h_addr_list[i]))); i++; } + return ESP_OK; } + return ESP_FAIL; } /** * @brief Executes getaddrinfo and displays list of resolved addresses. * Note: This function is used only to test advertised mdns hostnames resolution */ -static void query_mdns_host_with_getaddrinfo(char * host) +static esp_err_t query_mdns_host_with_getaddrinfo(char * host) { struct addrinfo hints; struct addrinfo * res; @@ -254,6 +274,8 @@ static void query_mdns_host_with_getaddrinfo(char * host) inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr)); res = res->ai_next; } + return ESP_OK; } + return ESP_FAIL; } #endif diff --git a/examples/protocols/mdns/mdns_example_test.py b/examples/protocols/mdns/mdns_example_test.py index 52651035bb..09368af748 100644 --- a/examples/protocols/mdns/mdns_example_test.py +++ b/examples/protocols/mdns/mdns_example_test.py @@ -78,7 +78,7 @@ def mdns_server(esp_host): print('Received query: {} '.format(dns.__repr__())) sock.sendto(get_dns_answer_to_mdns_lwip(TESTER_NAME_LWIP, dns.id), addr) if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A: - if dns.an[0].name == esp_host + u'.local': + if dns.an[0].name.startswith(esp_host + u'.local'): print('Received answer to esp32-mdns query: {}'.format(dns.__repr__())) esp_answered.set() except socket.timeout: