1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
//
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Capitar IT Group BV <info@capitar.com>
//
// 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 <nng/nng.h>
#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(); }); })
|