summaryrefslogtreecommitdiff
path: root/tests/idhash.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-01-21 17:40:04 -0800
committerGarrett D'Amore <garrett@damore.org>2017-01-21 17:40:04 -0800
commit568a84ed2d3d41da5ca64cde15a677237fffd991 (patch)
tree92ee212c0c8f4dc264acd0cef33285bddefd5a93 /tests/idhash.c
parent434cdd9f4e9211b99ba62ff6973e082b90e098f0 (diff)
downloadnng-568a84ed2d3d41da5ca64cde15a677237fffd991.tar.gz
nng-568a84ed2d3d41da5ca64cde15a677237fffd991.tar.bz2
nng-568a84ed2d3d41da5ca64cde15a677237fffd991.zip
Fix leaks in bus, socket leaks, tighten up close-side refcnting.
This does a few things. First it closes some preexisting leaks. Second it tightens the overall close logic so that we automatically discard idhash resources (while keeping numeric values for next id etc. around) when the last socket is closed. This then eliminates the need for applications to ever explicitly terminate resources. It turns out platform-specific resources established at nni_init() time might still be leaked, but it's also the case that we now no longer dynamically allocate anything at platform initialization time. (This presumes that the platform doesn't do so under the hood when creating critical sections or mutexes for example.)
Diffstat (limited to 'tests/idhash.c')
-rw-r--r--tests/idhash.c100
1 files changed, 37 insertions, 63 deletions
diff --git a/tests/idhash.c b/tests/idhash.c
index 600b17ae..50a8d976 100644
--- a/tests/idhash.c
+++ b/tests/idhash.c
@@ -8,86 +8,69 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "core/idhash.c"
Main({
- nni_init();
-
Test("General ID Hash", {
int rv;
Convey("Given an id hash", {
- nni_idhash *h;
+ nni_idhash h;
- rv = nni_idhash_create(&h);
- So(rv == 0);
- So(h->ih_cap == 8);
- So(h->ih_entries != NULL);
- So(h->ih_count == 0);
+ So(nni_idhash_init(&h) == 0);
+ So(nni_idhash_count(&h) == 0);
Reset({
- nni_idhash_destroy(h);
+ nni_idhash_fini(&h);
})
Convey("We can insert an element", {
char *five = "five";
char *four = "four";
- rv = nni_idhash_insert(h, 5, five);
+ rv = nni_idhash_insert(&h, 5, five);
+ So(nni_idhash_count(&h) == 1);
So(rv == 0);
- So(h->ih_load == 1);
- So(h->ih_count == 1);
Convey("And we can find it", {
void *ptr;
- rv = nni_idhash_find(h, 5, &ptr);
+ rv = nni_idhash_find(&h, 5, &ptr);
So(rv == 0);
So(ptr == five);
})
Convey("We can delete it", {
void *ptr;
- rv = nni_idhash_remove(h, 5);
+ rv = nni_idhash_remove(&h, 5);
So(rv == 0);
- rv = nni_idhash_find(h, 5, &ptr);
+ rv = nni_idhash_find(&h, 5, &ptr);
So(rv == NNG_ENOENT);
})
Convey("We can change the value", {
void *ptr;
- rv = nni_idhash_insert(h, 5, four);
- So(rv == 0);
- So(h->ih_count == 1);
- rv = nni_idhash_find(h, 5, &ptr);
- So(rv == 0);
+ So(nni_idhash_insert(&h, 5, four) == 0);
+ So(nni_idhash_count(&h) == 1);
+ So(nni_idhash_find(&h, 5, &ptr) == 0);
So(ptr == four);
})
Convey("We can insert a hash collision", {
void *ptr;
- rv = nni_idhash_insert(h, 13, four);
- So(rv == 0);
- So(h->ih_load == 2);
- So(h->ih_count == 2);
- rv = nni_idhash_find(h, 5, &ptr);
- So(rv == 0);
+ So(nni_idhash_insert(&h, 13, four) == 0);
+ So(nni_idhash_count(&h) == 2);
+ So(nni_idhash_find(&h, 5, &ptr) == 0);
So(ptr == five);
- rv = nni_idhash_find(h, 13, &ptr);
- So(rv == 0);
+ So(nni_idhash_find(&h, 13, &ptr) == 0);
So(ptr == four);
- So(h->ih_entries[5].ihe_skips == 1);
Convey("And delete the intermediate", {
- rv = nni_idhash_remove(h, 5);
- So(rv == 0);
+ So(nni_idhash_remove(&h, 5) == 0);
ptr = NULL;
- rv = nni_idhash_find(h, 13, &ptr);
- So(rv == 0);
+ So(nni_idhash_find(&h, 13, &ptr) == 0);
So(ptr == four);
- So(h->ih_load == 2);
})
})
})
Convey("We cannot find bogus values", {
void *ptr = NULL;
- rv = nni_idhash_find(h, 42, &ptr);
+ rv = nni_idhash_find(&h, 42, &ptr);
So(rv == NNG_ENOENT);
So(ptr == NULL);
})
@@ -103,34 +86,27 @@ Main({
expect[i] = i;
}
Convey("Given an id hash", {
- nni_idhash *h;
+ nni_idhash h;
- rv = nni_idhash_create(&h);
- So(rv == 0);
- So(h->ih_cap == 8);
- So(h->ih_entries != NULL);
- So(h->ih_count == 0);
+ So(nni_idhash_init(&h) == 0);
+ So(nni_idhash_count(&h) == 0);
Reset({
- nni_idhash_destroy(h);
+ nni_idhash_fini(&h);
})
Convey("We can insert 1024 items", {
uint32_t count;
for (i = 0; i < 1024; i++) {
- nni_idhash_insert(h, i, &expect[i]);
+ nni_idhash_insert(&h, i, &expect[i]);
}
- So(nni_idhash_count(h, &count) == 0);
- So(count == 1024);
- So(h->ih_cap = 2048);
- So(h->ih_count == 1024);
+ So(nni_idhash_count(&h) == 1024);
Convey("We can remove them", {
for (i = 0; i < 1024; i++) {
- nni_idhash_remove(h, i);
+ nni_idhash_remove(&h, i);
}
- So(h->ih_count == 0);
- So(h->ih_cap == 8);
+ So(nni_idhash_count(&h) == 0);
})
})
})
@@ -138,36 +114,34 @@ Main({
Test("Dynamic ID generation", {
Convey("Given a small ID hash", {
- nni_idhash *h;
+ nni_idhash h;
int expect[5];
uint32_t id;
int i;
- So(nni_idhash_create(&h) == 0);
+ So(nni_idhash_init(&h) == 0);
Reset({
- nni_idhash_destroy(h);
+ nni_idhash_fini(&h);
})
- nni_idhash_set_limits(h, 10, 13, 10);
+ nni_idhash_set_limits(&h, 10, 13, 10);
So(1);
Convey("We can fill the table", {
for (i = 0; i < 4; i++) {
- So(nni_idhash_alloc(h, &id, &expect[i]) == 0);
+ So(nni_idhash_alloc(&h, &id, &expect[i]) == 0);
So(id == (i + 10));
}
Convey("Adding another fails", {
- So(nni_idhash_alloc(h, &id, &expect[5]) == NNG_ENOMEM);
+ So(nni_idhash_alloc(&h, &id, &expect[5]) == NNG_ENOMEM);
})
Convey("Deleting one lets us reinsert", {
- nni_idhash_remove(h, 11);
- So(nni_idhash_alloc(h, &id, &expect[5]) == 0);
+ nni_idhash_remove(&h, 11);
+ So(nni_idhash_alloc(&h, &id, &expect[5]) == 0);
So(id == 11);
})
})
Convey("We cannot insert bogus values", {
- So(nni_idhash_insert(h, 1, &expect[0]) == NNG_EINVAL);
- So(nni_idhash_insert(h, 100, &expect[0]) == NNG_EINVAL);
+ So(nni_idhash_insert(&h, 1, &expect[0]) == NNG_EINVAL);
+ So(nni_idhash_insert(&h, 100, &expect[0]) == NNG_EINVAL);
})
})
})
-
- nni_fini();
})