From 0a2a8d4c14543b9b8b1228e6012b05190915bda4 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 30 Nov 2024 21:50:12 -0500 Subject: tests: convert synch test to NUTS. While here we added a test for nng_cv_wake1 to demonstrate it does not fall afoul of the thundering herd. --- tests/CMakeLists.txt | 1 - tests/synch.c | 138 --------------------------------------------------- 2 files changed, 139 deletions(-) delete mode 100644 tests/synch.c (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 272ba78e..1e8ea918 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -130,7 +130,6 @@ add_nng_test1(httpserver 30 NNG_SUPP_HTTP) add_nng_test(ipcsupp 10) add_nng_test(nonblock 60) add_nng_test(scalability 20 ON) -add_nng_test(synch 5) add_nng_test(tcpsupp 10) add_nng_test(wss 30) add_nng_test1(zt 60 NNG_TRANSPORT_ZEROTIER) diff --git a/tests/synch.c b/tests/synch.c deleted file mode 100644 index c0bf18c9..00000000 --- a/tests/synch.c +++ /dev/null @@ -1,138 +0,0 @@ -// -// Copyright 2022 Staysail Systems, Inc. -// Copyright 2024 Capitar IT Group BV -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include - -#include "convey.h" - -// Notify tests for verifying condvars. -struct notifyarg { - int did; - nng_duration when; - nng_mtx *mx; - nng_cv *cv; -}; - -void -notifyafter(void *a) -{ - struct notifyarg *na = a; - - nng_msleep(na->when); - nng_mtx_lock(na->mx); - na->did = 1; - nng_cv_wake(na->cv); - nng_mtx_unlock(na->mx); -} - -struct notifyarg arg; -nng_thread *thr; - -static void -test_sync(void) -{ - Convey("Mutexes work", { - nng_mtx *mx; - - So(nng_mtx_alloc(&mx) == 0); - Reset({ nng_mtx_free(mx); }); - - Convey("We can lock and unlock a mutex", { - nng_mtx_lock(mx); - So(1); - nng_mtx_unlock(mx); - So(1); - Convey("And then lock it again", { - nng_mtx_lock(mx); - So(1); - nng_mtx_unlock(mx); - So(1); - }); - }); - Convey("Things block properly", { - So(nng_mtx_alloc(&arg.mx) == 0); - So(nng_cv_alloc(&arg.cv, arg.mx) == 0); - arg.did = 0; - arg.when = 0; - nng_mtx_lock(arg.mx); - So(nng_thread_create(&thr, notifyafter, &arg) == 0); - nng_msleep(10); - So(arg.did == 0); - nng_mtx_unlock(arg.mx); - nng_msleep(10); - nng_mtx_lock(arg.mx); - while (!arg.did) { - nng_cv_wait(arg.cv); - } - So(arg.did != 0); - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - nng_cv_free(arg.cv); - nng_mtx_free(arg.mx); - }) - }); - - Convey("Condition variables work", { - So(nng_mtx_alloc(&arg.mx) == 0); - So(nng_cv_alloc(&arg.cv, arg.mx) == 0); - - Reset({ - nng_cv_free(arg.cv); - nng_mtx_free(arg.mx); - }); - - Convey("Notification works", { - arg.did = 0; - arg.when = 10; - So(nng_thread_create(&thr, notifyafter, &arg) == 0); - - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_wait(arg.cv); - } - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - So(arg.did == 1); - }); - - Convey("Timeout works", { - arg.did = 0; - arg.when = 200; - So(nng_thread_create(&thr, notifyafter, &arg) == 0); - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_until(arg.cv, nng_clock() + 10); - } - So(arg.did == 0); - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - }); - - Convey("Empty timeout is EAGAIN", { - nng_mtx_lock(arg.mx); - So(nng_cv_until(arg.cv, 0) == NNG_EAGAIN); - nng_mtx_unlock(arg.mx); - }); - - Convey("Not running works", { - arg.did = 0; - arg.when = 1; - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_until(arg.cv, nng_clock() + 10); - } - So(arg.did == 0); - nng_mtx_unlock(arg.mx); - }); - }); -} - -TestMain( - "Synchronization", { Convey("Synchronization works", { test_sync(); }); }) -- cgit v1.2.3-70-g09d2