2012-08-23 22:40:30 +02:00
|
|
|
/*
|
2013-03-12 17:04:51 +01:00
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
2012-08-23 22:40:30 +02: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/>.
|
|
|
|
*/
|
2012-04-21 16:39:19 +02:00
|
|
|
|
|
|
|
#include "../include/zmq.h"
|
2013-08-17 14:43:45 +02:00
|
|
|
#include "../include/zmq_utils.h"
|
2012-08-23 22:40:30 +02:00
|
|
|
#include <string.h>
|
2013-01-31 19:46:22 +01:00
|
|
|
#include <time.h>
|
2013-08-17 14:43:45 +02:00
|
|
|
#include "testutil.hpp"
|
2012-04-21 16:39:19 +02:00
|
|
|
|
2012-08-28 01:10:47 +02:00
|
|
|
int main (void)
|
2012-04-21 16:39:19 +02:00
|
|
|
{
|
2013-08-17 14:43:45 +02:00
|
|
|
setup_test_environment();
|
2012-04-21 16:39:19 +02:00
|
|
|
int rc;
|
|
|
|
char buf[32];
|
|
|
|
const char *ep = "tcp://127.0.0.1:5560";
|
|
|
|
|
|
|
|
// Create infrastructure.
|
2013-01-31 09:10:49 +01:00
|
|
|
void *ctx = zmq_ctx_new ();
|
2012-04-21 16:39:19 +02:00
|
|
|
assert (ctx);
|
|
|
|
void *push = zmq_socket (ctx, ZMQ_PUSH);
|
|
|
|
assert (push);
|
|
|
|
rc = zmq_bind (push, ep);
|
|
|
|
assert (rc == 0);
|
|
|
|
void *pull = zmq_socket (ctx, ZMQ_PULL);
|
|
|
|
assert (pull);
|
|
|
|
rc = zmq_connect (pull, ep);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Pass one message through to ensure the connection is established
|
2012-04-21 16:39:19 +02:00
|
|
|
rc = zmq_send (push, "ABC", 3, 0);
|
|
|
|
assert (rc == 3);
|
|
|
|
rc = zmq_recv (pull, buf, sizeof (buf), 0);
|
|
|
|
assert (rc == 3);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Unbind the listening endpoint
|
2012-04-21 16:39:19 +02:00
|
|
|
rc = zmq_unbind (push, ep);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Allow unbind to settle
|
2013-08-17 14:43:45 +02:00
|
|
|
zmq_sleep(1);
|
2012-04-21 16:39:19 +02:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Check that sending would block (there's no outbound connection)
|
2012-04-21 16:39:19 +02:00
|
|
|
rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
|
|
|
|
assert (rc == -1 && zmq_errno () == EAGAIN);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Clean up
|
2012-04-21 16:39:19 +02:00
|
|
|
rc = zmq_close (pull);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_close (push);
|
|
|
|
assert (rc == 0);
|
2013-01-31 09:10:49 +01:00
|
|
|
rc = zmq_ctx_term (ctx);
|
2012-04-21 16:39:19 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Create infrastructure
|
2013-01-31 09:10:49 +01:00
|
|
|
ctx = zmq_ctx_new ();
|
2012-04-21 16:39:19 +02:00
|
|
|
assert (ctx);
|
|
|
|
push = zmq_socket (ctx, ZMQ_PUSH);
|
|
|
|
assert (push);
|
|
|
|
rc = zmq_connect (push, ep);
|
|
|
|
assert (rc == 0);
|
|
|
|
pull = zmq_socket (ctx, ZMQ_PULL);
|
|
|
|
assert (pull);
|
|
|
|
rc = zmq_bind (pull, ep);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Pass one message through to ensure the connection is established.
|
|
|
|
rc = zmq_send (push, "ABC", 3, 0);
|
|
|
|
assert (rc == 3);
|
|
|
|
rc = zmq_recv (pull, buf, sizeof (buf), 0);
|
|
|
|
assert (rc == 3);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Disconnect the bound endpoint
|
2012-04-21 16:39:19 +02:00
|
|
|
rc = zmq_disconnect (push, ep);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Allow disconnect to settle
|
2013-08-17 14:43:45 +02:00
|
|
|
zmq_sleep(1);
|
2012-04-21 16:39:19 +02:00
|
|
|
|
|
|
|
// Check that sending would block (there's no inbound connections).
|
|
|
|
rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
|
|
|
|
assert (rc == -1 && zmq_errno () == EAGAIN);
|
|
|
|
|
|
|
|
// Clean up.
|
|
|
|
rc = zmq_close (pull);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = zmq_close (push);
|
|
|
|
assert (rc == 0);
|
2013-01-31 09:10:49 +01:00
|
|
|
rc = zmq_ctx_term (ctx);
|
2012-04-21 16:39:19 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|