From 3f7561417bec08226bcfeb107d94be0dbf71b09e Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 21 Dec 2019 10:20:55 -0800 Subject: fixes #1032 Figure out Darwin bustedness fixes #1035 Convey is awkward -- consider acutest.h This represents a rather large effort towards cleaning up our testing and optional configuration infrastructure. A separate test library is built by default, which is static, and includes some useful utilities design to make it easier to write shorter and more robust (not timing dependent) tests. This also means that we can cover pretty nearly all the tests (protocols etc.) in every case, even if the shipped image will be minimized. Subsystems which are optional can now use a few new macros to configure what they need see nng_sources_if, nng_headers_if, and nng_defines_if. This goes a long way to making the distributed CMakefiles a lot simpler. Additionally, tests for different parts of the tree can now be located outside of the tests/ tree, so that they can be placed next to the code that they are testing. Beyond the enabling work, the work has only begun, but these changes have resolved the most often failing tests for Darwin in the cloud. --- tests/platform.c | 201 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 117 insertions(+), 84 deletions(-) (limited to 'tests/platform.c') diff --git a/tests/platform.c b/tests/platform.c index 7af18029..7546fa7f 100644 --- a/tests/platform.c +++ b/tests/platform.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. +// Copyright 2019 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -8,11 +8,12 @@ // found online at https://opensource.org/licenses/MIT. // +#include "testutil.h" + #include #include -#include "convey.h" -#include "stubs.h" +#include "acutest.h" struct addarg { int cnt; @@ -31,84 +32,116 @@ add(void *arg) nng_mtx_unlock(aa->mx); } -TestMain("Platform Operations", { - // This is required for anything else to work - Convey("The clock works", { - uint64_t now = getms(); - - Convey("usleep works", { - nng_msleep(100); - - So((getms() - now) >= 100); // cannot be *shorter*!! - So((getms() - now) < 200); // crummy clock resolution? - }); - Convey("times work", { - uint64_t msend; - int usdelta; - int msdelta; - nng_time usend; - nng_time usnow = nng_clock(); - nng_msleep(200); - usend = nng_clock(); - msend = getms(); - - So(usend > usnow); - So(msend > now); - usdelta = (int) (usend - usnow); - msdelta = (int) (msend - now); - So(usdelta >= 200); - So(usdelta < 250); // increased tolerance for CIs - So(abs(msdelta - usdelta) < 50); - }); - }); - Convey("Mutexes work", { - static nng_mtx *mx; - - So(nng_mtx_alloc(&mx) == 0); - Reset({ nng_mtx_free(mx); }); - - Convey("We can lock and unlock 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("Threads work", { - static nng_thread *thr; - int rv; - struct addarg aa; - - So(nng_mtx_alloc(&aa.mx) == 0); - So(nng_cv_alloc(&aa.cv, aa.mx) == 0); - Reset({ - nng_mtx_free(aa.mx); - nng_cv_free(aa.cv); - }); - aa.cnt = 0; - - Convey("We can create threads", { - rv = nng_thread_create(&thr, add, &aa); - So(rv == 0); - - Reset({ nng_thread_destroy(thr); }); - - Convey("It ran", { - int val; - nng_mtx_lock(aa.mx); - while ((val = aa.cnt) == 0) { - nng_cv_wait(aa.cv); - } - nng_mtx_unlock(aa.mx); - So(val == 1); - }); - }); - }); -}) +void +test_sleep(void) +{ + uint64_t start, end; + start = testutil_clock(); + nng_msleep(100); + end = testutil_clock(); + TEST_CHECK((end - start) >= 100); + TEST_CHECK((end - start) <= 500); +} + +void +test_clock(void) +{ + uint64_t mstart; + uint64_t msend; + uint64_t usdelta; + uint64_t msdelta; + nng_time usend; + nng_time usnow; + + mstart = testutil_clock(); + usnow = nng_clock(); + nng_msleep(200); + usend = nng_clock(); + msend = testutil_clock(); + + TEST_CHECK(usend > usnow); + TEST_CHECK(msend > mstart); + usdelta = usend - usnow; + msdelta = msend - mstart; + TEST_CHECK(usdelta >= 200); + TEST_CHECK(usdelta < 500); // increased tolerance for CIs + if (msdelta > usdelta) { + TEST_CHECK((msdelta - usdelta) < 50); + } else { + TEST_CHECK((usdelta - msdelta) < 50); + } +} + +void +test_mutex(void) +{ + nng_mtx *mx, *mx2; + + TEST_CHECK(nng_mtx_alloc(&mx) == 0); + nng_mtx_lock(mx); + nng_mtx_unlock(mx); + + nng_mtx_lock(mx); + nng_mtx_unlock(mx); + nng_mtx_free(mx); + + // Verify that the mutexes are not always the same! + TEST_CHECK(nng_mtx_alloc(&mx) == 0); + TEST_CHECK(nng_mtx_alloc(&mx2) == 0); + TEST_CHECK(mx != mx2); + nng_mtx_free(mx); + nng_mtx_free(mx2); +} + +void +test_thread(void) +{ + nng_thread * thr; + int rv; + struct addarg aa; + + TEST_CHECK(nng_mtx_alloc(&aa.mx) == 0); + TEST_CHECK(nng_cv_alloc(&aa.cv, aa.mx) == 0); + aa.cnt = 0; + + TEST_CHECK((rv = nng_thread_create(&thr, add, &aa)) == 0); + nng_thread_destroy(thr); + TEST_CHECK(aa.cnt == 1); + + nng_cv_free(aa.cv); + nng_mtx_free(aa.mx); +} + +void +test_condvar(void) +{ + nng_thread * thr; + int rv; + struct addarg aa; + + TEST_CHECK(nng_mtx_alloc(&aa.mx) == 0); + TEST_CHECK(nng_cv_alloc(&aa.cv, aa.mx) == 0); + aa.cnt = 0; + + TEST_CHECK((rv = nng_thread_create(&thr, add, &aa)) == 0); + + nng_mtx_lock(aa.mx); + while (aa.cnt == 0) { + nng_cv_wait(aa.cv); + } + nng_mtx_unlock(aa.mx); + nng_thread_destroy(thr); + TEST_CHECK(aa.cnt == 1); + + nng_cv_free(aa.cv); + nng_mtx_free(aa.mx); +} + +TEST_LIST = { + { "sleep", test_sleep }, + { "clock", test_clock }, + { "mutex", test_mutex }, + { "thread", test_thread }, + { "condvar", test_condvar }, + { NULL, NULL }, +}; -- cgit v1.2.3-70-g09d2