aboutsummaryrefslogtreecommitdiff
path: root/tests/synch.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-02-20 19:41:55 -0800
committerGarrett D'Amore <garrett@damore.org>2018-02-20 19:41:55 -0800
commitfd871e6ce9de54f81f00918d4c7e3f3f4336b6d3 (patch)
treef7758c6430a696f797763d21d5a3d53ea950b561 /tests/synch.c
parent03b9fab6cbf8374aca5f5daa2b5b030bcc4cbb85 (diff)
downloadnng-fd871e6ce9de54f81f00918d4c7e3f3f4336b6d3.tar.gz
nng-fd871e6ce9de54f81f00918d4c7e3f3f4336b6d3.tar.bz2
nng-fd871e6ce9de54f81f00918d4c7e3f3f4336b6d3.zip
Introduce 'porting layer' Public API.
This introduces portable primitives for time, random numbers, synchronization primitives, and threading. These are somewhat primitive (least common denominiators), but they can help with writing portable applications, especially our own demo apps.
Diffstat (limited to 'tests/synch.c')
-rw-r--r--tests/synch.c195
1 files changed, 96 insertions, 99 deletions
diff --git a/tests/synch.c b/tests/synch.c
index c9de3deb..de0d7e16 100644
--- a/tests/synch.c
+++ b/tests/synch.c
@@ -9,15 +9,16 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
+//#include "core/nng_impl.h"
#include "nng.h"
+#include "supplemental/util/platform.h"
// Notify tests for verifying condvars.
struct notifyarg {
int did;
nng_duration when;
- nni_mtx mx;
- nni_cv cv;
+ nng_mtx * mx;
+ nng_cv * cv;
};
#ifdef NNG_PLATFORM_POSIX
@@ -31,118 +32,116 @@ notifyafter(void *arg)
{
struct notifyarg *na = arg;
- nni_msleep(na->when);
- nni_mtx_lock(&na->mx);
+ nng_msleep(na->when);
+ nng_mtx_lock(na->mx);
na->did = 1;
- nni_cv_wake(&na->cv);
- nni_mtx_unlock(&na->mx);
+ nng_cv_wake(na->cv);
+ nng_mtx_unlock(na->mx);
}
struct notifyarg arg;
-nni_thr thr;
+nng_thread * thr;
static void
test_sync(void)
{
Convey("Mutexes work", {
- nni_mtx mx;
+ nng_mtx *mx;
- nni_mtx_init(&mx);
+ So(nng_mtx_alloc(&mx) == 0);
+ Reset({ nng_mtx_free(mx); });
Convey("We can lock a mutex", {
- nni_mtx_lock(&mx);
+ nng_mtx_lock(mx);
So(1);
Convey("And we can unlock it", {
- nni_mtx_unlock(&mx);
+ nng_mtx_unlock(mx);
So(1);
Convey("And then lock it again", {
- nni_mtx_lock(&mx);
+ nng_mtx_lock(mx);
So(1);
- nni_mtx_unlock(&mx);
+ nng_mtx_unlock(mx);
So(1);
});
});
Convey("Things block properly", {
- nni_mtx_init(&arg.mx);
- nni_cv_init(&arg.cv, &arg.mx);
- So(nni_thr_init(&thr, notifyafter, &arg) == 0);
+ So(nng_mtx_alloc(&arg.mx) == 0);
+ So(nng_cv_alloc(&arg.cv, arg.mx) == 0);
arg.did = 0;
arg.when = 0;
- nni_mtx_lock(&arg.mx);
- nni_thr_run(&thr);
+ nng_mtx_lock(arg.mx);
+ So(nng_thread_create(
+ &thr, notifyafter, &arg) == 0);
nng_msleep(10);
So(arg.did == 0);
- nni_mtx_unlock(&arg.mx);
+ nng_mtx_unlock(arg.mx);
nng_msleep(10);
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
while (!arg.did) {
- nni_cv_wait(&arg.cv);
+ nng_cv_wait(arg.cv);
}
So(arg.did != 0);
- nni_mtx_unlock(&arg.mx);
- nni_thr_fini(&thr);
- nni_cv_fini(&arg.cv);
- nni_mtx_fini(&arg.mx);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
+ nng_cv_free(arg.cv);
+ nng_mtx_free(arg.mx);
})
});
- Convey("We can finalize it", { nni_mtx_fini(&mx); });
});
Convey("Condition variables work", {
- nni_mtx_init(&arg.mx);
- nni_cv_init(&arg.cv, &arg.mx);
- So(nni_thr_init(&thr, notifyafter, &arg) == 0);
+ So(nng_mtx_alloc(&arg.mx) == 0);
+ So(nng_cv_alloc(&arg.cv, arg.mx) == 0);
Reset({
- nni_cv_fini(&arg.cv);
- nni_mtx_fini(&arg.mx);
- nni_thr_fini(&thr);
+ nng_cv_free(arg.cv);
+ nng_mtx_free(arg.mx);
});
Convey("Notification works", {
arg.did = 0;
arg.when = 10;
- nni_thr_run(&thr);
+ So(nng_thread_create(&thr, notifyafter, &arg) == 0);
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_wait(&arg.cv);
+ nng_cv_wait(arg.cv);
}
- nni_mtx_unlock(&arg.mx);
- nni_thr_wait(&thr);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
So(arg.did == 1);
});
Convey("Timeout works", {
arg.did = 0;
arg.when = 200;
- nni_thr_run(&thr);
- nni_mtx_lock(&arg.mx);
+ So(nng_thread_create(&thr, notifyafter, &arg) == 0);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_until(&arg.cv, nni_clock() + 10);
+ nng_cv_until(arg.cv, nng_clock() + 10);
}
So(arg.did == 0);
- nni_mtx_unlock(&arg.mx);
- nni_thr_wait(&thr);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
});
Convey("Empty timeout is EAGAIN", {
- nni_mtx_lock(&arg.mx);
- So(nni_cv_until(&arg.cv, 0) == NNG_EAGAIN);
- nni_mtx_unlock(&arg.mx);
+ 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;
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_until(&arg.cv, nni_clock() + 10);
+ nng_cv_until(arg.cv, nng_clock() + 10);
}
So(arg.did == 0);
- nni_mtx_unlock(&arg.mx);
+ nng_mtx_unlock(arg.mx);
});
});
}
@@ -157,104 +156,102 @@ test_sync_fallback(void)
{
nni_plat_sync_fallback = 1;
Convey("Mutexes work", {
- nni_mtx mx;
+ nng_mtx *mx;
- nni_mtx_init(&mx);
+ So(nng_mtx_alloc(&mx) == 0);
+ Reset({ nng_mtx_free(mx); });
Convey("We can lock a mutex", {
- nni_mtx_lock(&mx);
+ nng_mtx_lock(mx);
So(1);
Convey("And we can unlock it", {
- nni_mtx_unlock(&mx);
+ nng_mtx_unlock(mx);
So(1);
Convey("And then lock it again", {
- nni_mtx_lock(&mx);
+ nng_mtx_lock(mx);
So(1);
- nni_mtx_unlock(&mx);
+ nng_mtx_unlock(mx);
So(1);
});
});
Convey("Things block properly", {
- nni_mtx_init(&arg.mx);
- nni_cv_init(&arg.cv, &arg.mx);
- So(nni_thr_init(&thr, notifyafter, &arg) == 0);
+ So(nng_mtx_alloc(&arg.mx) == 0);
+ So(nng_cv_alloc(&arg.cv, arg.mx) == 0);
arg.did = 0;
arg.when = 0;
- nni_mtx_lock(&arg.mx);
- nni_thr_run(&thr);
+ nng_mtx_lock(arg.mx);
+ So(nng_thread_create(
+ &thr, notifyafter, &arg) == 0);
nng_msleep(10);
So(arg.did == 0);
- nni_mtx_unlock(&arg.mx);
+ nng_mtx_unlock(arg.mx);
nng_msleep(10);
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
while (!arg.did) {
- nni_cv_wait(&arg.cv);
+ nng_cv_wait(arg.cv);
}
So(arg.did != 0);
- nni_mtx_unlock(&arg.mx);
- nni_thr_fini(&thr);
- nni_cv_fini(&arg.cv);
- nni_mtx_fini(&arg.mx);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
+ nng_cv_free(arg.cv);
+ nng_mtx_free(arg.mx);
})
});
- Convey("We can finalize it", { nni_mtx_fini(&mx); });
});
Convey("Condition variables work", {
- nni_mtx_init(&arg.mx);
- nni_cv_init(&arg.cv, &arg.mx);
- So(nni_thr_init(&thr, notifyafter, &arg) == 0);
+ So(nng_mtx_alloc(&arg.mx) == 0);
+ So(nng_cv_alloc(&arg.cv, arg.mx) == 0);
Reset({
- nni_cv_fini(&arg.cv);
- nni_mtx_fini(&arg.mx);
- nni_thr_fini(&thr);
+ nng_cv_free(arg.cv);
+ nng_mtx_free(arg.mx);
});
Convey("Notification works", {
arg.did = 0;
arg.when = 10;
- nni_thr_run(&thr);
+ So(nng_thread_create(&thr, notifyafter, &arg) == 0);
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_wait(&arg.cv);
+ nng_cv_wait(arg.cv);
}
- nni_mtx_unlock(&arg.mx);
- nni_thr_wait(&thr);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
So(arg.did == 1);
});
Convey("Timeout works", {
arg.did = 0;
arg.when = 200;
- nni_thr_run(&thr);
- nni_mtx_lock(&arg.mx);
+ So(nng_thread_create(&thr, notifyafter, &arg) == 0);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_until(&arg.cv, nni_clock() + 10);
+ nng_cv_until(arg.cv, nng_clock() + 10);
}
So(arg.did == 0);
- nni_mtx_unlock(&arg.mx);
- nni_thr_wait(&thr);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
});
Convey("Empty timeout is EAGAIN", {
- nni_mtx_lock(&arg.mx);
- So(nni_cv_until(&arg.cv, 0) == NNG_EAGAIN);
- nni_mtx_unlock(&arg.mx);
+ 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;
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_until(&arg.cv, nni_clock() + 10);
+ nng_cv_until(arg.cv, nng_clock() + 10);
}
So(arg.did == 0);
- nni_mtx_unlock(&arg.mx);
+ nng_mtx_unlock(arg.mx);
});
});
}
@@ -263,7 +260,6 @@ test_sync_fallback(void)
#endif
TestMain("Synchronization", {
- nni_init();
Convey("Synchronization works", { test_sync(); });
@@ -271,21 +267,22 @@ TestMain("Synchronization", {
ConveyFB("Transform works", {
nni_plat_sync_fallback = 0;
- nni_mtx_init(&arg.mx);
+ So(nng_mtx_alloc(&arg.mx) == 0);
nni_plat_sync_fallback = 1;
- nni_cv_init(&arg.cv, &arg.mx);
- So(nni_thr_init(&thr, notifyafter, &arg) == 0);
+ So(nng_cv_alloc(&arg.cv, arg.mx) == 0);
arg.did = 0;
arg.when = 10;
- nni_thr_run(&thr);
+ So(nng_thread_create(&thr, notifyafter, &arg) == 0);
- nni_mtx_lock(&arg.mx);
+ nng_mtx_lock(arg.mx);
if (!arg.did) {
- nni_cv_wait(&arg.cv);
+ nng_cv_wait(arg.cv);
}
- nni_mtx_unlock(&arg.mx);
- nni_thr_wait(&thr);
+ nng_mtx_unlock(arg.mx);
+ nng_thread_destroy(thr);
So(arg.did == 1);
+ nng_cv_free(arg.cv);
+ nng_mtx_free(arg.mx);
});
})