Skip to content

Commit

Permalink
hil: connection: test golioth_client_destroy()
Browse files Browse the repository at this point in the history
- Add golioth_client_destroy() and subsequent reconnect to test ability to
  destroy and restart the client
- Add a semaphore to block until each new connection is established.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Jul 31, 2024
1 parent cb1150f commit 845f68e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
55 changes: 45 additions & 10 deletions tests/hil/tests/connection/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
LOG_INF("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);
}
9 changes: 7 additions & 2 deletions tests/hil/tests/connection/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 845f68e

Please sign in to comment.