diff --git a/tests/test_ch.c b/tests/test_ch.c index 3822d395..8e22946d 100644 --- a/tests/test_ch.c +++ b/tests/test_ch.c @@ -38,8 +38,9 @@ struct test_ch_data { pthread_barrier_t *barrier2; }; -char buffer[512]; -char expected[512]; +/* set by the log callback in the call-home client thread, polled by the test thread */ +static ATOMIC_T expected_msg_received; +static ATOMIC_PTR_T expected; int TEST_PORT = 10050, TEST_PORT_2 = 10051, TEST_PORT_3 = 10052, TEST_PORT_4 = 10053; const char *TEST_PORT_STR = "10050", *TEST_PORT_2_STR = "10051", *TEST_PORT_3_STR = "10052", *TEST_PORT_4_STR = "10053"; @@ -47,11 +48,13 @@ const char *TEST_PORT_STR = "10050", *TEST_PORT_2_STR = "10051", *TEST_PORT_3_ST static void test_msg_callback(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg) { + const char *exp = ATOMIC_PTR_LOAD_RELAXED(expected); + (void) level; (void) session; - if (strstr(msg, expected)) { - strncpy(buffer, msg, 511); + if (exp && strstr(msg, exp)) { + ATOMIC_STORE_RELAXED(expected_msg_received, 1); } printf("%s\n", msg); @@ -97,8 +100,8 @@ server_thread_ssh(void *arg) /* set print clb so we get access to messages */ nc_set_print_clb_session(test_msg_callback); - buffer[0] = '\0'; - strcpy(expected, "reconnecting in"); + ATOMIC_STORE_RELAXED(expected_msg_received, 0); + ATOMIC_PTR_STORE_RELAXED(expected, "reconnecting in"); /* prepare data for deleting the call-home client */ ret = nc_server_config_del_ch_client("ch_ssh", &test_data->tree); @@ -123,7 +126,7 @@ server_thread_ssh(void *arg) } else if (ret & (NC_PSPOLL_TIMEOUT | NC_PSPOLL_NOSESSIONS)) { usleep(500); } - } while (!strlen(buffer)); + } while (!ATOMIC_LOAD_RELAXED(expected_msg_received)); /* delete the call-home client, the thread should end */ ret = nc_server_config_setup_data(test_data->tree); @@ -433,8 +436,8 @@ server_thread_delete_while_session(void *arg) struct test_ch_data *test_data = test_ctx->test_data; nc_set_print_clb_session(test_msg_callback); - buffer[0] = '\0'; - strcpy(expected, "thread exit"); + ATOMIC_STORE_RELAXED(expected_msg_received, 0); + ATOMIC_PTR_STORE_RELAXED(expected, "thread exit"); ps = nc_ps_new(); assert_non_null(ps); @@ -464,9 +467,9 @@ server_thread_delete_while_session(void *arg) do { usleep(5000); - } while (!strlen(buffer)); + } while (!ATOMIC_LOAD_RELAXED(expected_msg_received)); - assert_true(strlen(buffer) > 0); + assert_true(ATOMIC_LOAD_RELAXED(expected_msg_received)); pthread_barrier_wait(test_data->barrier2);