aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-09-09 12:31:22 -0700
committerGarrett D'Amore <garrett@damore.org>2018-09-09 12:59:42 -0700
commit0bdff77c12c61610c7f6bdab499580d0bc688c84 (patch)
treeb3c38de26f270e9df45a86a09ff83bcb5e5b25e0 /tests
parent36000e13fedb8f883a52aa629d434a80046cdfb0 (diff)
downloadnng-0bdff77c12c61610c7f6bdab499580d0bc688c84.tar.gz
nng-0bdff77c12c61610c7f6bdab499580d0bc688c84.tar.bz2
nng-0bdff77c12c61610c7f6bdab499580d0bc688c84.zip
fixes #710 idhash has nasty performance bug
fixes #709 idhash bug on duplicate add
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt2
-rw-r--r--tests/idhash.c90
2 files changed, 90 insertions, 2 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 4c3625b6..031c15cd 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -139,7 +139,7 @@ add_nng_test(errors 2)
add_nng_test1(files 5 NNG_STATIC_LIB)
add_nng_test1(httpclient 60 NNG_SUPP_HTTP)
add_nng_test2(httpserver 30 NNG_STATIC_LIB NNG_SUPP_HTTP)
-add_nng_test1(idhash 5 NNG_STATIC_LIB)
+add_nng_test1(idhash 30 NNG_STATIC_LIB)
add_nng_test1(inproc 5 NNG_TRANSPORT_INPROC)
add_nng_test1(ipc 5 NNG_TRANSPORT_IPC)
add_nng_test1(ipcperms 5 NNG_TRANSPORT_IPC)
diff --git a/tests/idhash.c b/tests/idhash.c
index eb62fd76..72f1a44f 100644
--- a/tests/idhash.c
+++ b/tests/idhash.c
@@ -13,6 +13,77 @@
#include "core/nng_impl.h"
+#define STRESSLOAD 50000
+#define NVALUES 1000
+
+int
+stress(nni_idhash *h, void **values, size_t nvalues, int iter)
+{
+ for (int i = 0; i < iter; i++) {
+ void *x;
+ int v = rand() % nvalues; // Keep it constrained
+
+ switch (rand() & 3) {
+ case 0:
+ x = &values[rand() % nvalues];
+ values[v] = x;
+ if (nni_idhash_insert(h, v, x) != 0) {
+ return (-1);
+ }
+ break;
+
+ case 1:
+ if (values[v] == NULL) {
+ if (nni_idhash_remove(h, v) != NNG_ENOENT) {
+ return (-1);
+ } else {
+ break;
+ }
+ } else {
+ if (nni_idhash_remove(h, v) != 0) {
+ return (-1);
+ }
+ values[v] = NULL;
+ }
+ break;
+ case 2:
+ if (values[v] == NULL) {
+ if (nni_idhash_find(h, v, &x) != NNG_ENOENT) {
+ return (-1);
+ }
+
+ } else {
+ if ((nni_idhash_find(h, v, &x) != 0) ||
+ (x != values[v])) {
+ return (-1);
+ }
+ }
+ break;
+ }
+ }
+ return (0);
+}
+
+int
+poststress(nni_idhash *h, void **values, size_t nvalues)
+{
+ for (size_t i = 0; i < nvalues; i++) {
+ void *x;
+ if (values[i] == NULL) {
+ if ((nni_idhash_find(h, i, &x) != NNG_ENOENT) ||
+ (nni_idhash_remove(h, i) != NNG_ENOENT)) {
+ return (-1);
+ }
+ continue;
+ }
+ if (((nni_idhash_find(h, i, &x) != 0) || (x != values[i])) ||
+ (nni_idhash_remove(h, i) != 0)) {
+ return (-1);
+ }
+ }
+ return (0);
+}
+
Main({
nni_init();
atexit(nni_fini);
@@ -69,7 +140,6 @@ Main({
So(ptr == four);
});
});
-
});
Convey("We cannot find bogus values", {
void *ptr;
@@ -168,4 +238,22 @@ Main({
});
});
});
+
+ Test("Stress it", {
+ void *values[NVALUES];
+
+ Convey("Given a hash", {
+ nni_idhash *h;
+ So(nni_idhash_init(&h) == 0);
+ Reset({ nni_idhash_fini(h); });
+ memset(values, 0, sizeof(values));
+
+ Convey("A stress run works", {
+ So(stress(h, values, NVALUES, STRESSLOAD) ==
+ 0);
+ So(poststress(h, values, NVALUES) == 0);
+ So(nni_idhash_count(h) == 0);
+ });
+ });
+ });
})