Skip to content

Commit 2e803db

Browse files
tadeas-vintrlikmichalvasko
authored andcommitted
test BUGFIX remove datarace from test_fd_comm
1 parent 399af30 commit 2e803db

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

tests/test_fd_comm.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct nc_session *server_session;
3939
struct nc_session *client_session;
4040
struct ly_ctx *ctx;
4141
pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER;
42+
pthread_barrier_t barrier;
4243
int glob_state;
4344

4445
struct nc_server_reply *
@@ -349,23 +350,6 @@ test_send_recv_data_11(void **state)
349350
test_send_recv_data();
350351
}
351352

352-
static void
353-
test_notif_clb(struct nc_session *session, const struct lyd_node *UNUSED(envp), const struct lyd_node *op)
354-
{
355-
assert_ptr_equal(session, client_session);
356-
assert_string_equal(op->schema->name, "notificationComplete");
357-
358-
/* client notification received, update state */
359-
pthread_mutex_lock(&state_lock);
360-
while (glob_state != 2) {
361-
pthread_mutex_unlock(&state_lock);
362-
usleep(1000);
363-
pthread_mutex_lock(&state_lock);
364-
}
365-
glob_state = 3;
366-
pthread_mutex_unlock(&state_lock);
367-
}
368-
369353
static void *
370354
server_send_notif_thread(void *arg)
371355
{
@@ -401,16 +385,40 @@ server_send_notif_thread(void *arg)
401385

402386
/* update state */
403387
glob_state = 2;
388+
pthread_barrier_wait(&barrier);
404389
pthread_mutex_unlock(&state_lock);
405390

406391
return NULL;
407392
}
408393

394+
static void *
395+
thread_recv_notif(void *arg)
396+
{
397+
struct nc_session *session = (struct nc_session *)arg;
398+
struct lyd_node *envp;
399+
struct lyd_node *op;
400+
NC_MSG_TYPE msgtype;
401+
402+
pthread_barrier_wait(&barrier);
403+
msgtype = nc_recv_notif(session, 1000, &envp, &op);
404+
assert_int_equal(msgtype, NC_MSG_NOTIF);
405+
assert_string_equal(op->schema->name, "notificationComplete");
406+
407+
lyd_free_tree(envp);
408+
lyd_free_tree(op);
409+
410+
pthread_mutex_lock(&state_lock);
411+
glob_state = 3;
412+
pthread_mutex_unlock(&state_lock);
413+
414+
return (void *)0;
415+
}
416+
409417
static void
410418
test_send_recv_notif(void)
411419
{
412420
int ret;
413-
pthread_t tid;
421+
pthread_t tid[2];
414422
uint64_t msgid;
415423
NC_MSG_TYPE msgtype;
416424
struct nc_rpc *rpc;
@@ -425,8 +433,7 @@ test_send_recv_notif(void)
425433
assert_int_equal(msgtype, NC_MSG_RPC);
426434

427435
/* client subscription */
428-
ret = nc_recv_notif_dispatch(client_session, test_notif_clb);
429-
assert_int_equal(ret, 0);
436+
pthread_create(&tid[0], NULL, thread_recv_notif, client_session);
430437

431438
/* create server */
432439
ps = nc_ps_new();
@@ -437,7 +444,7 @@ test_send_recv_notif(void)
437444
pthread_mutex_lock(&state_lock);
438445
glob_state = 0;
439446
pthread_mutex_unlock(&state_lock);
440-
ret = pthread_create(&tid, NULL, server_send_notif_thread, NULL);
447+
ret = pthread_create(&tid[1], NULL, server_send_notif_thread, NULL);
441448
assert_int_equal(ret, 0);
442449

443450
/* server blocked on RPC */
@@ -450,7 +457,9 @@ test_send_recv_notif(void)
450457
pthread_mutex_unlock(&state_lock);
451458

452459
/* server finished */
453-
ret = pthread_join(tid, NULL);
460+
ret = 0;
461+
ret |= pthread_join(tid[0], NULL);
462+
ret |= pthread_join(tid[1], NULL);
454463
assert_int_equal(ret, 0);
455464
nc_ps_free(ps);
456465

@@ -494,6 +503,8 @@ main(void)
494503
struct lysc_node *node;
495504
const char *nc_features[] = {"candidate", NULL};
496505

506+
pthread_barrier_init(&barrier, NULL, 2);
507+
497508
/* create ctx */
498509
ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx);
499510
assert_non_null(ctx);
@@ -538,6 +549,7 @@ main(void)
538549

539550
nc_server_destroy();
540551
ly_ctx_destroy(ctx);
552+
pthread_barrier_destroy(&barrier);
541553

542554
return ret;
543555
}

0 commit comments

Comments
 (0)