From 04664f0ef79292239428bc7f1b7b1760a0f92f3c Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Tue, 23 Dec 2014 15:20:10 +0100 Subject: [PATCH 1/2] Problem: 'bool' is not defined by default zmq_atomic_counter_dec returned a 'bool' value, yet this isn't defined by standard, so causes compile errors in upstream code. Solution: return an int that can be safely converted to bool if needed by bindings. --- doc/zmq_atomic_counter_dec.txt | 17 +++++++++-------- doc/zmq_atomic_counter_destroy.txt | 11 ++++++----- doc/zmq_atomic_counter_inc.txt | 11 ++++++----- doc/zmq_atomic_counter_new.txt | 11 ++++++----- doc/zmq_atomic_counter_set.txt | 11 ++++++----- doc/zmq_atomic_counter_value.txt | 11 ++++++----- include/zmq.h | 2 +- src/zmq_utils.cpp | 7 ++++--- tests/test_atomics.cpp | 10 +++++----- 9 files changed, 49 insertions(+), 42 deletions(-) diff --git a/doc/zmq_atomic_counter_dec.txt b/doc/zmq_atomic_counter_dec.txt index 164ba950..53a088c4 100644 --- a/doc/zmq_atomic_counter_dec.txt +++ b/doc/zmq_atomic_counter_dec.txt @@ -9,7 +9,7 @@ zmq_atomic_counter_dec - decrement an atomic counter SYNOPSIS -------- -*bool zmq_atomic_counter_dec (void *counter);* +*int zmq_atomic_counter_dec (void *counter);* DESCRIPTION @@ -21,8 +21,8 @@ operations. RETURN VALUE ------------ -The _zmq_atomic_counter_dec()_ function returns true if the counter is -greater than zero after decrementing, or false if the counter reached +The _zmq_atomic_counter_dec()_ function returns 1 if the counter is +greater than zero after decrementing, or zero if the counter reached zero. @@ -36,13 +36,14 @@ assert (zmq_atomic_counter_inc (counter) == 0); assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); +return 0; ---- diff --git a/doc/zmq_atomic_counter_destroy.txt b/doc/zmq_atomic_counter_destroy.txt index c58d1e44..ee7c863f 100644 --- a/doc/zmq_atomic_counter_destroy.txt +++ b/doc/zmq_atomic_counter_destroy.txt @@ -36,13 +36,14 @@ assert (zmq_atomic_counter_inc (counter) == 0); assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); +return 0; ---- diff --git a/doc/zmq_atomic_counter_inc.txt b/doc/zmq_atomic_counter_inc.txt index fccda5f2..6346ea13 100644 --- a/doc/zmq_atomic_counter_inc.txt +++ b/doc/zmq_atomic_counter_inc.txt @@ -35,13 +35,14 @@ assert (zmq_atomic_counter_inc (counter) == 0); assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); +return 0; ---- diff --git a/doc/zmq_atomic_counter_new.txt b/doc/zmq_atomic_counter_new.txt index 38427ba4..7278f2b7 100644 --- a/doc/zmq_atomic_counter_new.txt +++ b/doc/zmq_atomic_counter_new.txt @@ -36,13 +36,14 @@ assert (zmq_atomic_counter_inc (counter) == 0); assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); +return 0; ---- diff --git a/doc/zmq_atomic_counter_set.txt b/doc/zmq_atomic_counter_set.txt index 6df210e2..3b0812f6 100644 --- a/doc/zmq_atomic_counter_set.txt +++ b/doc/zmq_atomic_counter_set.txt @@ -35,13 +35,14 @@ assert (zmq_atomic_counter_inc (counter) == 0); assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); +return 0; ---- diff --git a/doc/zmq_atomic_counter_value.txt b/doc/zmq_atomic_counter_value.txt index 66cd870d..3cd26dec 100644 --- a/doc/zmq_atomic_counter_value.txt +++ b/doc/zmq_atomic_counter_value.txt @@ -34,13 +34,14 @@ assert (zmq_atomic_counter_inc (counter) == 0); assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); -assert (zmq_atomic_counter_dec (counter) == true); -assert (zmq_atomic_counter_dec (counter) == false); +assert (zmq_atomic_counter_dec (counter) == 1); +assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); +return 0; ---- diff --git a/include/zmq.h b/include/zmq.h index f56f838e..61817a03 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -440,7 +440,7 @@ ZMQ_EXPORT int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key); ZMQ_EXPORT void *zmq_atomic_counter_new (void); ZMQ_EXPORT void zmq_atomic_counter_set (void *counter, int value); ZMQ_EXPORT int zmq_atomic_counter_inc (void *counter); -ZMQ_EXPORT bool zmq_atomic_counter_dec (void *counter); +ZMQ_EXPORT int zmq_atomic_counter_dec (void *counter); ZMQ_EXPORT int zmq_atomic_counter_value (void *counter); ZMQ_EXPORT void zmq_atomic_counter_destroy (void **counter_p); diff --git a/src/zmq_utils.cpp b/src/zmq_utils.cpp index a04859d4..852d4ede 100644 --- a/src/zmq_utils.cpp +++ b/src/zmq_utils.cpp @@ -232,11 +232,12 @@ int zmq_atomic_counter_inc (void *counter_) return ((zmq::atomic_counter_t *) counter_)->add (1); } -// Decrement the atomic counter and return true if still > zero +// Decrement the atomic counter and return 1 (if counter >= 1), or +// 0 if counter hit zero. -bool zmq_atomic_counter_dec (void *counter_) +int zmq_atomic_counter_dec (void *counter_) { - return ((zmq::atomic_counter_t *) counter_)->sub (1); + return ((zmq::atomic_counter_t *) counter_)->sub (1)? 1: 0; } // Return actual value of atomic counter diff --git a/tests/test_atomics.cpp b/tests/test_atomics.cpp index a4dfc810..c9abaa9c 100644 --- a/tests/test_atomics.cpp +++ b/tests/test_atomics.cpp @@ -27,12 +27,12 @@ int main (void) assert (zmq_atomic_counter_inc (counter) == 1); assert (zmq_atomic_counter_inc (counter) == 2); assert (zmq_atomic_counter_value (counter) == 3); - assert (zmq_atomic_counter_dec (counter) == true); - assert (zmq_atomic_counter_dec (counter) == true); - assert (zmq_atomic_counter_dec (counter) == false); + assert (zmq_atomic_counter_dec (counter) == 1); + assert (zmq_atomic_counter_dec (counter) == 1); + assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_set (counter, 2); - assert (zmq_atomic_counter_dec (counter) == true); - assert (zmq_atomic_counter_dec (counter) == false); + assert (zmq_atomic_counter_dec (counter) == 1); + assert (zmq_atomic_counter_dec (counter) == 0); zmq_atomic_counter_destroy (&counter); return 0; } From eacd4d9cba87c8e6876839d81056131cca1a0c90 Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Tue, 23 Dec 2014 15:21:22 +0100 Subject: [PATCH 2/2] Problem: test cases were sporadically failing Solution: increase settle time for bind/unbind tests --- tests/test_disconnect_inproc.cpp | 6 ++++-- tests/testutil.hpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_disconnect_inproc.cpp b/tests/test_disconnect_inproc.cpp index 475ce0b0..dd484fcd 100644 --- a/tests/test_disconnect_inproc.cpp +++ b/tests/test_disconnect_inproc.cpp @@ -25,8 +25,10 @@ zmq_msg_init(&msg) && printf("zmq_msg_init: %s\n", zmq_strerror(errno)); \ zmq_msg_init_size (&msg, size + 1) && printf("zmq_msg_init_size: %s\n",zmq_strerror(errno)); \ memcpy(zmq_msg_data(&msg), data, size + 1); -int publicationsReceived = 0; -bool isSubscribed = false; +// TODO: this code fails to meet our style guidelines, and needs rewriting + +static int publicationsReceived = 0; +static bool isSubscribed = false; int main(int, char**) { setup_test_environment(); diff --git a/tests/testutil.hpp b/tests/testutil.hpp index c59578d1..7d9b74c7 100644 --- a/tests/testutil.hpp +++ b/tests/testutil.hpp @@ -27,7 +27,7 @@ // This defines the settle time used in tests; raise this if we // get test failures on slower systems due to binds/connects not // settled. Tested to work reliably at 1 msec on a fast PC. -#define SETTLE_TIME 10 // In msec +#define SETTLE_TIME 50 // In msec #undef NDEBUG #include