2013-09-12 14:44:44 +01:00
|
|
|
/*
|
2014-01-02 12:00:57 +01:00
|
|
|
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
|
2013-09-12 14:44:44 +01:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testutil.hpp"
|
|
|
|
|
2013-09-12 18:09:37 +01:00
|
|
|
static void pusher (void *ctx)
|
|
|
|
{
|
|
|
|
// Connect first
|
|
|
|
void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (connectSocket);
|
|
|
|
int rc = zmq_connect (connectSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Queue up some data
|
|
|
|
rc = zmq_send_const (connectSocket, "foobar", 6, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
rc = zmq_close (connectSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:10:23 +00:00
|
|
|
void test_bind_before_connect ()
|
2013-09-12 14:44:44 +01:00
|
|
|
{
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
|
|
|
// Bind first
|
|
|
|
void *bindSocket = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (bindSocket);
|
|
|
|
int rc = zmq_bind (bindSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Now connect
|
|
|
|
void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (connectSocket);
|
|
|
|
rc = zmq_connect (connectSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 14:44:44 +01:00
|
|
|
// Queue up some data
|
|
|
|
rc = zmq_send_const (connectSocket, "foobar", 6, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
|
|
|
|
// Read pending message
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
2013-09-12 18:09:37 +01:00
|
|
|
rc = zmq_msg_recv (&msg, bindSocket, 0);
|
2013-09-12 14:44:44 +01:00
|
|
|
assert (rc == 6);
|
|
|
|
void *data = zmq_msg_data (&msg);
|
|
|
|
assert (memcmp ("foobar", data, 6) == 0);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
rc = zmq_close (connectSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_close (bindSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:10:23 +00:00
|
|
|
void test_connect_before_bind ()
|
2013-09-12 14:44:44 +01:00
|
|
|
{
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
|
|
|
// Connect first
|
|
|
|
void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (connectSocket);
|
|
|
|
int rc = zmq_connect (connectSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Queue up some data
|
|
|
|
rc = zmq_send_const (connectSocket, "foobar", 6, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
|
|
|
|
// Now bind
|
|
|
|
void *bindSocket = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (bindSocket);
|
|
|
|
rc = zmq_bind (bindSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 14:44:44 +01:00
|
|
|
// Read pending message
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
2013-09-12 18:09:37 +01:00
|
|
|
rc = zmq_msg_recv (&msg, bindSocket, 0);
|
2013-09-12 14:44:44 +01:00
|
|
|
assert (rc == 6);
|
|
|
|
void *data = zmq_msg_data (&msg);
|
|
|
|
assert (memcmp ("foobar", data, 6) == 0);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
rc = zmq_close (connectSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_close (bindSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:10:23 +00:00
|
|
|
void test_connect_before_bind_pub_sub ()
|
2013-09-12 18:09:37 +01:00
|
|
|
{
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
|
|
|
// Connect first
|
|
|
|
void *connectSocket = zmq_socket (ctx, ZMQ_PUB);
|
|
|
|
assert (connectSocket);
|
|
|
|
int rc = zmq_connect (connectSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Queue up some data, this will be dropped
|
|
|
|
rc = zmq_send_const (connectSocket, "before", 6, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
|
|
|
|
// Now bind
|
|
|
|
void *bindSocket = zmq_socket (ctx, ZMQ_SUB);
|
|
|
|
assert (bindSocket);
|
|
|
|
rc = zmq_setsockopt (bindSocket, ZMQ_SUBSCRIBE, "", 0);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_bind (bindSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 18:09:37 +01:00
|
|
|
// Wait for pub-sub connection to happen
|
2013-11-06 13:30:41 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-09-12 18:09:37 +01:00
|
|
|
|
|
|
|
// Queue up some data, this not will be dropped
|
|
|
|
rc = zmq_send_const (connectSocket, "after", 6, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
|
|
|
|
// Read pending message
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_msg_recv (&msg, bindSocket, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
void *data = zmq_msg_data (&msg);
|
|
|
|
assert (memcmp ("after", data, 5) == 0);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
rc = zmq_close (connectSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_close (bindSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:10:23 +00:00
|
|
|
void test_multiple_connects ()
|
2013-09-12 18:09:37 +01:00
|
|
|
{
|
|
|
|
const unsigned int no_of_connects = 10;
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
|
|
|
int rc;
|
2014-02-12 23:10:23 +00:00
|
|
|
void *connectSocket [no_of_connects];
|
2013-09-12 18:09:37 +01:00
|
|
|
|
|
|
|
// Connect first
|
|
|
|
for (unsigned int i = 0; i < no_of_connects; ++i)
|
|
|
|
{
|
|
|
|
connectSocket [i] = zmq_socket (ctx, ZMQ_PUSH);
|
|
|
|
assert (connectSocket [i]);
|
|
|
|
rc = zmq_connect (connectSocket [i], "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Queue up some data
|
|
|
|
rc = zmq_send_const (connectSocket [i], "foobar", 6, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now bind
|
|
|
|
void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
|
|
|
|
assert (bindSocket);
|
|
|
|
rc = zmq_bind (bindSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 18:09:37 +01:00
|
|
|
for (unsigned int i = 0; i < no_of_connects; ++i)
|
|
|
|
{
|
|
|
|
// Read pending message
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_msg_recv (&msg, bindSocket, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
void *data = zmq_msg_data (&msg);
|
|
|
|
assert (memcmp ("foobar", data, 6) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
for (unsigned int i = 0; i < no_of_connects; ++i)
|
|
|
|
{
|
|
|
|
rc = zmq_close (connectSocket [i]);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = zmq_close (bindSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:10:23 +00:00
|
|
|
void test_multiple_threads ()
|
2013-09-12 18:09:37 +01:00
|
|
|
{
|
2013-09-12 21:07:29 +01:00
|
|
|
const unsigned int no_of_threads = 30;
|
2013-09-12 18:09:37 +01:00
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
void *threads [no_of_threads];
|
|
|
|
|
|
|
|
// Connect first
|
|
|
|
for (unsigned int i = 0; i < no_of_threads; ++i)
|
|
|
|
{
|
|
|
|
threads [i] = zmq_threadstart (&pusher, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now bind
|
|
|
|
void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
|
|
|
|
assert (bindSocket);
|
|
|
|
rc = zmq_bind (bindSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < no_of_threads; ++i)
|
|
|
|
{
|
|
|
|
// Read pending message
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_msg_recv (&msg, bindSocket, 0);
|
|
|
|
assert (rc == 6);
|
|
|
|
void *data = zmq_msg_data (&msg);
|
|
|
|
assert (memcmp ("foobar", data, 6) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
for (unsigned int i = 0; i < no_of_threads; ++i)
|
|
|
|
{
|
|
|
|
zmq_threadclose (threads [i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = zmq_close (bindSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 23:10:23 +00:00
|
|
|
void test_identity ()
|
2013-09-12 21:07:29 +01:00
|
|
|
{
|
|
|
|
// Create the infrastructure
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
void *sc = zmq_socket (ctx, ZMQ_DEALER);
|
|
|
|
assert (sc);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
int rc = zmq_connect (sc, "inproc://a");
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
void *sb = zmq_socket (ctx, ZMQ_ROUTER);
|
|
|
|
assert (sb);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
rc = zmq_bind (sb, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Send 2-part message.
|
|
|
|
rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE);
|
|
|
|
assert (rc == 1);
|
|
|
|
rc = zmq_send (sc, "B", 1, 0);
|
|
|
|
assert (rc == 1);
|
|
|
|
|
|
|
|
// Identity comes first.
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_msg_recv (&msg, sb, 0);
|
|
|
|
assert (rc >= 0);
|
|
|
|
int more = zmq_msg_more (&msg);
|
|
|
|
assert (more == 1);
|
|
|
|
|
|
|
|
// Then the first part of the message body.
|
|
|
|
rc = zmq_msg_recv (&msg, sb, 0);
|
|
|
|
assert (rc == 1);
|
|
|
|
more = zmq_msg_more (&msg);
|
|
|
|
assert (more == 1);
|
|
|
|
|
|
|
|
// And finally, the second part of the message body.
|
|
|
|
rc = zmq_msg_recv (&msg, sb, 0);
|
|
|
|
assert (rc == 1);
|
|
|
|
more = zmq_msg_more (&msg);
|
|
|
|
assert (more == 0);
|
|
|
|
|
|
|
|
// Deallocate the infrastructure.
|
|
|
|
rc = zmq_close (sc);
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
rc = zmq_close (sb);
|
|
|
|
assert (rc == 0);
|
2014-02-12 23:10:23 +00:00
|
|
|
|
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_connect_only ()
|
|
|
|
{
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
|
|
|
void *connectSocket = zmq_socket (ctx, ZMQ_PUSH);
|
|
|
|
assert (connectSocket);
|
|
|
|
int rc = zmq_connect (connectSocket, "inproc://a");
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_close (connectSocket);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2013-09-12 14:44:44 +01:00
|
|
|
int main (void)
|
|
|
|
{
|
2014-02-12 23:10:23 +00:00
|
|
|
setup_test_environment ();
|
2013-09-12 14:44:44 +01:00
|
|
|
|
2013-09-12 21:07:29 +01:00
|
|
|
test_bind_before_connect ();
|
|
|
|
test_connect_before_bind ();
|
|
|
|
test_connect_before_bind_pub_sub ();
|
|
|
|
test_multiple_connects ();
|
|
|
|
test_multiple_threads ();
|
|
|
|
test_identity ();
|
2014-02-12 23:10:23 +00:00
|
|
|
test_connect_only ();
|
2013-09-12 14:44:44 +01:00
|
|
|
|
2013-09-12 18:09:37 +01:00
|
|
|
return 0;
|
2013-09-12 14:44:44 +01:00
|
|
|
}
|