summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2016-12-23 12:15:40 -0800
committerGarrett D'Amore <garrett@damore.org>2016-12-23 12:15:40 -0800
commit667262f214441c66bcaeeae3b1b2ecbecf4aaf64 (patch)
tree9912966cf831c39f45a44eb0fd88fa1b92e35936
parenta12baf41fb17ef51a8b1d0c82e31113454c5beae (diff)
downloadnng-667262f214441c66bcaeeae3b1b2ecbecf4aaf64.tar.gz
nng-667262f214441c66bcaeeae3b1b2ecbecf4aaf64.tar.bz2
nng-667262f214441c66bcaeeae3b1b2ecbecf4aaf64.zip
Fix for incorrect nni_usleep(), found with newly created platform tests.
-rw-r--r--src/platform/posix/posix_clock.c4
-rw-r--r--src/platform/posix/posix_synch.c1
-rw-r--r--tests/CMakeLists.txt5
-rw-r--r--tests/platform.c101
-rw-r--r--tests/sock.c2
5 files changed, 108 insertions, 5 deletions
diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c
index c4206ebe..215f66e6 100644
--- a/src/platform/posix/posix_clock.c
+++ b/src/platform/posix/posix_clock.c
@@ -47,7 +47,9 @@ nni_usleep(nni_duration usec)
/* Do this in a loop, so that interrupts don't actually wake us. */
while (ts.tv_sec || ts.tv_nsec) {
- (void) nanosleep(&ts, &ts);
+ if (nanosleep(&ts, &ts) == 0) {
+ break;
+ }
}
}
diff --git a/src/platform/posix/posix_synch.c b/src/platform/posix/posix_synch.c
index 8b5d3cc8..2fb92915 100644
--- a/src/platform/posix/posix_synch.c
+++ b/src/platform/posix/posix_synch.c
@@ -75,7 +75,6 @@ nni_cond_init(nni_cond *c, nni_mutex *m)
{
if (pthread_cond_init(&c->cv, &nni_cvattr) != 0) {
// In theory could be EAGAIN, but handle like ENOMEM
- nni_free(c, sizeof (*c));
return (NNG_ENOMEM);
}
c->mx = &m->mx;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 8a2bbb5c..e8f6706e 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -35,7 +35,7 @@ if (NNG_TESTS)
list (APPEND all_tests ${NAME})
add_executable (${NAME} ${NAME}.c convey.c)
target_link_libraries (${NAME} ${PROJECT_NAME})
- add_test (NAME ${NAME} COMMAND ${NAME} ${TEST_PORT})
+ add_test (NAME ${NAME} COMMAND ${NAME} -v ${TEST_PORT})
set_tests_properties (${NAME} PROPERTIES TIMEOUT ${TIMEOUT})
math (EXPR TEST_PORT "${TEST_PORT}+10")
endmacro (add_nng_test)
@@ -53,4 +53,5 @@ else ()
endif ()
add_nng_test(list 5)
-add_nng_test(sock 5) \ No newline at end of file
+add_nng_test(platform 5)
+add_nng_test(sock 5)
diff --git a/tests/platform.c b/tests/platform.c
new file mode 100644
index 00000000..392c5c9d
--- /dev/null
+++ b/tests/platform.c
@@ -0,0 +1,101 @@
+//
+// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+//
+// 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 "convey.h"
+#include "nng.h"
+#include "core/nng_impl.h"
+
+#ifndef _WIN32
+#include <sys/time.h>
+#endif
+
+uint64_t getms(void) {
+#ifdef _WIN32
+ return (GetTickCount()) ;
+#else
+ static time_t epoch;
+ struct timeval tv;
+
+ if (epoch == 0) {
+ epoch = time(NULL);
+ }
+ gettimeofday(&tv, NULL);
+
+ if (tv.tv_sec < epoch) {
+ // Broken clock.
+ // This will force all other timing tests to fail
+ return (0);
+ }
+ tv.tv_sec -= epoch;
+ return (((uint64_t)(tv.tv_sec ) * 1000) + (tv.tv_usec / 1000));
+#endif
+}
+
+TestMain("Platform Operations", {
+
+ int rv = nni_init(); // This is required for anything else to work
+ Convey("Platform init worked", {
+ So(rv == 0);
+ })
+ Convey("The clock works", {
+ uint64_t now = getms();
+
+ Convey("usleep works", {
+ nni_usleep(100000);
+
+ So((getms() - now) > 100); // cannot be *shorter*!!
+ So((getms() - now) < 150); // crummy clock resolution?
+ })
+ Convey("times work", {
+ uint64_t msend;
+ int usdelta;
+ int msdelta;
+ nni_time usend;
+ nni_time usnow = nni_clock();
+ nni_usleep(200000);
+ usend = nni_clock();
+ msend = getms();
+
+ So(usend > usnow);
+ So(msend > now);
+ usdelta = (int)((usend - usnow) / 1000);
+ msdelta = (int)((msend - now));
+ So(usdelta > 200);
+ So(usdelta < 220);
+ So(abs(msdelta - usdelta) < 20);
+ })
+ })
+ Convey("Mutexes work", {
+ nni_mutex mx;
+ int rv;
+
+ rv = nni_mutex_init(&mx);
+ So(rv == 0);
+
+ Convey("We can lock a mutex", {
+ nni_mutex_enter(&mx);
+ So(1);
+ Convey("And cannot recursively lock", {
+ rv = nni_mutex_tryenter(&mx);
+ So(rv != 0);
+ })
+ Convey("And we can unlock it", {
+ nni_mutex_exit(&mx);
+ So(1);
+ Convey("And then lock it again", {
+ rv = nni_mutex_tryenter(&mx);
+ So(rv == 0);
+ })
+ })
+ })
+ Convey("We can finalize it", {
+ nni_mutex_fini(&mx);
+ })
+ })
+}) \ No newline at end of file
diff --git a/tests/sock.c b/tests/sock.c
index 4bdc684b..81361f8b 100644
--- a/tests/sock.c
+++ b/tests/sock.c
@@ -11,7 +11,6 @@
#include "nng.h"
TestMain("Socket Operations", {
- ConveySetVerbose();
Convey("We are able to open a PAIR socket", {
int rv;
nng_socket *sock = NULL;
@@ -34,6 +33,7 @@ TestMain("Socket Operations", {
int64_t when = 500000;
uint64_t now;
+ // We cheat to get access to the core's clock.
extern uint64_t nni_clock(void);
now = nni_clock();