Skip to content

Commit bbaa9b2

Browse files
committed
test BUGFIX make test thread-safe
1 parent a5fd85c commit bbaa9b2

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

tests/test_fd_comm.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
struct nc_session *server_session;
3939
struct nc_session *client_session;
4040
struct ly_ctx *ctx;
41-
volatile int glob_state;
41+
pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER;
42+
int glob_state;
4243

4344
struct nc_server_reply *
4445
my_get_rpc_clb(struct lyd_node *rpc, struct nc_session *session)
@@ -71,12 +72,16 @@ my_commit_rpc_clb(struct lyd_node *rpc, struct nc_session *session)
7172
assert_ptr_equal(session, server_session);
7273

7374
/* update state */
75+
pthread_mutex_lock(&state_lock);
7476
glob_state = 1;
7577

7678
/* wait until the client receives the notification */
7779
while (glob_state != 3) {
80+
pthread_mutex_unlock(&state_lock);
7881
usleep(100000);
82+
pthread_mutex_lock(&state_lock);
7983
}
84+
pthread_mutex_unlock(&state_lock);
8085

8186
return nc_server_reply_ok();
8287
}
@@ -355,10 +360,14 @@ test_notif_clb(struct nc_session *session, const struct nc_notif *notif)
355360
assert_string_equal(notif->tree->schema->name, "notificationComplete");
356361

357362
/* client notification received, update state */
363+
pthread_mutex_lock(&state_lock);
358364
while (glob_state != 2) {
365+
pthread_mutex_unlock(&state_lock);
359366
usleep(1000);
367+
pthread_mutex_lock(&state_lock);
360368
}
361369
glob_state = 3;
370+
pthread_mutex_unlock(&state_lock);
362371
}
363372

364373
static void *
@@ -371,8 +380,11 @@ server_send_notif_thread(void *arg)
371380
(void)arg;
372381

373382
/* wait for the RPC callback to be called */
383+
pthread_mutex_lock(&state_lock);
374384
while (glob_state != 1) {
385+
pthread_mutex_unlock(&state_lock);
375386
usleep(1000);
387+
pthread_mutex_lock(&state_lock);
376388
}
377389

378390
/* create notif */
@@ -391,6 +403,7 @@ server_send_notif_thread(void *arg)
391403

392404
/* update state */
393405
glob_state = 2;
406+
pthread_mutex_unlock(&state_lock);
394407

395408
return NULL;
396409
}
@@ -423,7 +436,9 @@ test_send_recv_notif(void)
423436
nc_ps_add_session(ps, server_session);
424437

425438
/* server will send a notification */
439+
pthread_mutex_lock(&state_lock);
426440
glob_state = 0;
441+
pthread_mutex_unlock(&state_lock);
427442
ret = pthread_create(&tid, NULL, server_send_notif_thread, NULL);
428443
assert_int_equal(ret, 0);
429444

@@ -432,7 +447,9 @@ test_send_recv_notif(void)
432447
assert_int_equal(ret, NC_PSPOLL_RPC);
433448

434449
/* RPC, notification finished fine */
450+
pthread_mutex_lock(&state_lock);
435451
assert_int_equal(glob_state, 3);
452+
pthread_mutex_unlock(&state_lock);
436453

437454
/* server finished */
438455
ret = pthread_join(tid, NULL);

0 commit comments

Comments
 (0)