diff --git a/tests/hil/tests/connection/test.c b/tests/hil/tests/connection/test.c index 33b4c06c0..3c7ce4a0d 100644 --- a/tests/hil/tests/connection/test.c +++ b/tests/hil/tests/connection/test.c @@ -3,22 +3,57 @@ LOG_TAG_DEFINE(test_connect); +static golioth_sys_sem_t _connected_sem = NULL; + +static void on_client_event(struct golioth_client *client, + enum golioth_client_event event, + void *arg) +{ + bool is_connected = (event == GOLIOTH_CLIENT_EVENT_CONNECTED); + if (is_connected) + { + golioth_sys_sem_give(_connected_sem); + } + GLTH_LOGI(TAG, "Golioth client %s", is_connected ? "connected" : "disconnected"); +} + void hil_test_entry(const struct golioth_client_config *config) { + _connected_sem = golioth_sys_sem_create(1, 0); + struct golioth_client *client = golioth_client_create(config); - (void) client; + golioth_client_register_event_callback(client, on_client_event, NULL); + golioth_sys_sem_take(_connected_sem, GOLIOTH_SYS_WAIT_FOREVER); - while (1) - { - golioth_sys_msleep(10 * 1000); + /* Pause to ensure we don't have out-of-order logs */ + golioth_sys_msleep(1 * 1000); - GLTH_LOGI(TAG, "Stopping client"); - golioth_client_stop(client); + GLTH_LOGI(TAG, "Stopping client"); + golioth_client_stop(client); - golioth_sys_msleep(10 * 1000); + golioth_sys_msleep(10 * 1000); - GLTH_LOGI(TAG, "Starting client"); - golioth_client_start(client); - } + GLTH_LOGI(TAG, "Starting client"); + golioth_client_start(client); + + golioth_sys_sem_take(_connected_sem, GOLIOTH_SYS_WAIT_FOREVER); + + /* Pause to ensure we don't have out-of-order logs */ + golioth_sys_msleep(1 * 1000); + + GLTH_LOGI(TAG, "Destroying client"); + golioth_client_destroy(client); + client = NULL; + + golioth_sys_msleep(10 * 1000); + + GLTH_LOGI(TAG, "Starting client"); + client = golioth_client_create(config); + golioth_client_start(client); + + golioth_sys_sem_take(_connected_sem, GOLIOTH_SYS_WAIT_FOREVER); + + /* Pause to ensure logs are show */ + golioth_sys_msleep(1 * 1000); } diff --git a/tests/hil/tests/connection/test_connection.py b/tests/hil/tests/connection/test_connection.py index 0d692f56d..8e8781ebc 100644 --- a/tests/hil/tests/connection/test_connection.py +++ b/tests/hil/tests/connection/test_connection.py @@ -10,7 +10,12 @@ async def test_connect(board, device): # Confirm connection to Golioth assert None != board.wait_for_regex_in_line('Golioth CoAP client connected', timeout_s=120) - # Wait for reconnection - assert None != board.wait_for_regex_in_line('Stopping client', timeout_s=10) + # Wait for reconnection after golioth_client_stop(); + assert None != board.wait_for_regex_in_line('Stopping client', timeout_s=15) + assert None != board.wait_for_regex_in_line('Starting client', timeout_s=120) + assert None != board.wait_for_regex_in_line('Golioth CoAP client connected', timeout_s=120) + + # Wait for reconnection after golioth_client_destroy(); + assert None != board.wait_for_regex_in_line('Destroying client', timeout_s=15) assert None != board.wait_for_regex_in_line('Starting client', timeout_s=120) assert None != board.wait_for_regex_in_line('Golioth CoAP client connected', timeout_s=120)