Permalink
Browse files

Add test case for issue 281 written by Johan Ström

Signed-off-by: Mika Fischer <mika.fischer@zoopnet.de>
  • Loading branch information...
1 parent 196c1c0 commit 6fe21d9fbcfeff1be6c1e532aad3481ddb6ce81c @mika-fischer mika-fischer committed Nov 18, 2011
Showing with 97 additions and 0 deletions.
  1. +6 −0 281/2-1/README
  2. +91 −0 281/2-1/zmqtest.c
View
6 281/2-1/README
@@ -0,0 +1,6 @@
+When creating many sockets, zmq hits an assertion indicating that
+a mutex is locked while it is being destroyed.
+
+zmq_test.c demonstrates the issue.
+
+For details, see https://zeromq.jira.com/browse/LIBZMQ-281
View
91 281/2-1/zmqtest.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <zmq.h>
+#include <pthread.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <unistd.h>
+
+void * worker(void *ctx) {
+ int64_t more;
+ size_t more_size = sizeof more;
+ void *s = zmq_socket(ctx, ZMQ_ROUTER);
+ assert(s);
+ assert(zmq_bind(s, "inproc://sock") == 0);
+ printf("Worker ready\n");
+
+ long n=0;
+ while(1) {
+ zmq_msg_t addr;
+ zmq_msg_t empty;
+ zmq_msg_t msg;
+ zmq_msg_init(&addr);
+ zmq_msg_init(&empty);
+ zmq_msg_init(&msg);
+
+ assert(zmq_recv(s, &addr, 0) == 0);
+ assert(zmq_getsockopt(s, ZMQ_RCVMORE, &more, &more_size) == 0);
+ assert(more == 1);
+
+ assert(zmq_recv(s, &empty, 0) == 0);
+ assert(zmq_getsockopt(s, ZMQ_RCVMORE, &more, &more_size) == 0);
+ assert(more == 1);
+
+ assert(zmq_recv(s, &msg, 0) == 0);
+ assert(zmq_getsockopt(s, ZMQ_RCVMORE, &more, &more_size) == 0);
+ assert(more == 0);
+
+ if(++n % 100 == 0)
+ printf("pong %d\n", n);
+ assert(zmq_send(s, &addr, ZMQ_SNDMORE) == 0);
+ assert(zmq_send(s, &empty, ZMQ_SNDMORE) == 0);
+ assert(zmq_send(s, &msg, 0) == 0);
+
+ zmq_msg_close (&addr);
+ zmq_msg_close (&msg);
+ }
+}
+void* hammer(void *ctx) {
+ while(1) {
+ zmq_msg_t msg;
+ void *s = zmq_socket(ctx, ZMQ_REQ);
+ if(!s) {
+ printf("zmq_socket failed %d: %s\n", errno, strerror(errno));
+ assert(s);
+ }
+
+ assert(zmq_connect(s, "inproc://sock") == 0);
+
+ zmq_msg_init(&msg);
+ assert(zmq_msg_init_size (&msg, 6) == 0);
+ memset (zmq_msg_data (&msg), 'A', 6);
+
+ assert(zmq_send(s, &msg, 0) == 0);
+ zmq_msg_close (&msg);
+
+ zmq_msg_init(&msg);
+ assert(zmq_recv(s, &msg, 0) == 0);
+ zmq_msg_close (&msg);
+
+ zmq_close(s);
+ }
+}
+
+
+
+int main() {
+ void *ctx = zmq_init(1);
+ pthread_t workerT, hammerT1, hammerT2;
+
+ pthread_create(&workerT, NULL, worker, ctx);
+ sleep(1);
+
+ pthread_create(&hammerT1, NULL, hammer, ctx);
+ pthread_create(&hammerT2, NULL, hammer, ctx);
+
+ pthread_join(workerT, NULL);
+
+ zmq_term(ctx);
+}
+

0 comments on commit 6fe21d9

Please sign in to comment.