// This checks for normal operation - that is pings and pongs being // exchanged normally. There should be an accepted event on the server, // and then no event afterwards. static void test_heartbeat_notimeout (int is_curve_, int client_type_, int server_type_) { int rc; char my_endpoint[MAX_SOCKET_STRING]; void *server, *server_mon; prep_server_socket (1, is_curve_, &server, &server_mon, my_endpoint, MAX_SOCKET_STRING, server_type_); void *client = test_context_socket (client_type_); if (is_curve_) setup_curve (client, 0); rc = zmq_connect (client, my_endpoint); // Give it a sec to connect and handshake msleep (SETTLE_TIME); // By now everything should report as connected rc = get_monitor_event (server_mon); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc); // We should still be connected because pings and pongs are happenin' TEST_ASSERT_EQUAL_INT (-1, get_monitor_event (server_mon)); test_context_socket_close (client); test_context_socket_close (server); test_context_socket_close (server_mon); }
// This checks that peers respect the TTL value in ping messages // We set up a mock ZMTP 3 client and send a ping message with a TLL // to a server that is not doing any heartbeating. Then we sleep, // if the server disconnects the client, then we know the TTL did // its thing correctly. static void test_heartbeat_ttl (void) { int rc, value; // Set up our context and sockets void *ctx = zmq_ctx_new (); assert (ctx); void * server, * server_mon, *client; prep_server_socket(ctx, 0, 0, &server, &server_mon); client = zmq_socket(ctx, ZMQ_DEALER); assert(client != NULL); // Set the heartbeat TTL to 0.1 seconds value = 100; zmq_setsockopt(client, ZMQ_HEARTBEAT_TTL, &value, sizeof(value)); // Set the heartbeat interval to much longer than the TTL so that // the socket times out oon the remote side. value = 250; zmq_setsockopt(client, ZMQ_HEARTBEAT_IVL, &value, sizeof(value)); rc = zmq_connect(client, "tcp://localhost:5556"); assert(rc == 0); // By now everything should report as connected rc = get_monitor_event(server_mon); assert(rc == ZMQ_EVENT_ACCEPTED); msleep(100); // We should have been disconnected rc = get_monitor_event(server_mon); assert(rc == ZMQ_EVENT_DISCONNECTED); rc = zmq_close (server); assert (rc == 0); rc = zmq_close (server_mon); assert (rc == 0); rc = zmq_close (client); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); }
// This checks for a broken TCP connection (or, in this case a stuck one // where the peer never responds to PINGS). There should be an accepted event // then a disconnect event. static void test_heartbeat_timeout (void) { int rc; // Set up our context and sockets void *ctx = zmq_ctx_new (); assert (ctx); void * server, * server_mon; prep_server_socket(ctx, 1, 0, &server, &server_mon); struct sockaddr_in ip4addr; int s; ip4addr.sin_family = AF_INET; ip4addr.sin_port = htons(5556); inet_pton(AF_INET, "127.0.0.1", &ip4addr.sin_addr); s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); rc = connect (s, (struct sockaddr*) &ip4addr, sizeof ip4addr); assert (rc > -1); // Mock a ZMTP 3 client so we can forcibly time out a connection mock_handshake(s); // By now everything should report as connected rc = get_monitor_event(server_mon); assert(rc == ZMQ_EVENT_ACCEPTED); // We should have been disconnected rc = get_monitor_event(server_mon); assert(rc == ZMQ_EVENT_DISCONNECTED); close(s); rc = zmq_close (server); assert (rc == 0); rc = zmq_close (server_mon); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); }
// This checks for a broken TCP connection (or, in this case a stuck one // where the peer never responds to PINGS). There should be an accepted event // then a disconnect event. static void test_heartbeat_timeout (int server_type_, int mock_ping_) { int rc; char my_endpoint[MAX_SOCKET_STRING]; void *server, *server_mon; prep_server_socket (!mock_ping_, 0, &server, &server_mon, my_endpoint, MAX_SOCKET_STRING, server_type_); struct sockaddr_in ip4addr; raw_socket s; ip4addr.sin_family = AF_INET; ip4addr.sin_port = htons (atoi (strrchr (my_endpoint, ':') + 1)); #if defined(ZMQ_HAVE_WINDOWS) && (_WIN32_WINNT < 0x0600) ip4addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); #else inet_pton (AF_INET, "127.0.0.1", &ip4addr.sin_addr); #endif s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); rc = TEST_ASSERT_SUCCESS_RAW_ERRNO ( connect (s, (struct sockaddr *) &ip4addr, sizeof ip4addr)); TEST_ASSERT_GREATER_THAN_INT (-1, rc); // Mock a ZMTP 3 client so we can forcibly time out a connection mock_handshake (s, mock_ping_); // By now everything should report as connected rc = get_monitor_event (server_mon); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc); if (!mock_ping_) { // We should have been disconnected rc = get_monitor_event (server_mon); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, rc); } close (s); test_context_socket_close (server); test_context_socket_close (server_mon); }
// This checks for normal operation - that is pings and pongs being // exchanged normally. There should be an accepted event on the server, // and then no event afterwards. static void test_heartbeat_notimeout (int is_curve) { int rc; // Set up our context and sockets void *ctx = zmq_ctx_new (); assert (ctx); void * server, * server_mon; prep_server_socket(ctx, 1, is_curve, &server, &server_mon); void * client = zmq_socket(ctx, ZMQ_DEALER); if(is_curve) setup_curve(client, 0); rc = zmq_connect(client, "tcp://127.0.0.1:5556"); // Give it a sec to connect and handshake msleep(100); // By now everything should report as connected rc = get_monitor_event(server_mon); assert(rc == ZMQ_EVENT_ACCEPTED); // We should still be connected because pings and pongs are happenin' rc = get_monitor_event(server_mon); assert(rc == -1); rc = zmq_close (client); assert (rc == 0); rc = zmq_close (server); assert (rc == 0); rc = zmq_close (server_mon); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); }
// This checks that peers respect the TTL value in ping messages // We set up a mock ZMTP 3 client and send a ping message with a TLL // to a server that is not doing any heartbeating. Then we sleep, // if the server disconnects the client, then we know the TTL did // its thing correctly. static void test_heartbeat_ttl (int client_type_, int server_type_) { int rc, value; char my_endpoint[MAX_SOCKET_STRING]; void *server, *server_mon, *client; prep_server_socket (0, 0, &server, &server_mon, my_endpoint, MAX_SOCKET_STRING, server_type_); client = test_context_socket (client_type_); // Set the heartbeat TTL to 0.1 seconds value = 100; TEST_ASSERT_SUCCESS_ERRNO ( zmq_setsockopt (client, ZMQ_HEARTBEAT_TTL, &value, sizeof (value))); // Set the heartbeat interval to much longer than the TTL so that // the socket times out oon the remote side. value = 250; TEST_ASSERT_SUCCESS_ERRNO ( zmq_setsockopt (client, ZMQ_HEARTBEAT_IVL, &value, sizeof (value))); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint)); // By now everything should report as connected rc = get_monitor_event (server_mon); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc); msleep (SETTLE_TIME); // We should have been disconnected rc = get_monitor_event (server_mon); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, rc); test_context_socket_close (server); test_context_socket_close (server_mon); test_context_socket_close (client); }
int main (void) { setup_test_environment (); size_t len = MAX_SOCKET_STRING; char my_endpoint[MAX_SOCKET_STRING]; void *ctx = zmq_ctx_new (); assert (ctx); // We'll monitor these two sockets void *client = zmq_socket (ctx, ZMQ_DEALER); assert (client); void *server = zmq_socket (ctx, ZMQ_DEALER); assert (server); // Socket monitoring only works over inproc:// int rc = zmq_socket_monitor (client, "tcp://127.0.0.1:*", 0); assert (rc == -1); assert (zmq_errno () == EPROTONOSUPPORT); // Monitor all events on client and server sockets rc = zmq_socket_monitor (client, "inproc://monitor-client", ZMQ_EVENT_ALL); assert (rc == 0); rc = zmq_socket_monitor (server, "inproc://monitor-server", ZMQ_EVENT_ALL); assert (rc == 0); // Create two sockets for collecting monitor events void *client_mon = zmq_socket (ctx, ZMQ_PAIR); assert (client_mon); void *server_mon = zmq_socket (ctx, ZMQ_PAIR); assert (server_mon); // Connect these to the inproc endpoints so they'll get events rc = zmq_connect (client_mon, "inproc://monitor-client"); assert (rc == 0); rc = zmq_connect (server_mon, "inproc://monitor-server"); assert (rc == 0); // Now do a basic ping test rc = zmq_bind (server, "tcp://127.0.0.1:*"); assert (rc == 0); rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len); assert (rc == 0); rc = zmq_connect (client, my_endpoint); assert (rc == 0); bounce (server, client); // Close client and server close_zero_linger (client); close_zero_linger (server); // Now collect and check events from both sockets int event = get_monitor_event (client_mon, NULL, NULL); if (event == ZMQ_EVENT_CONNECT_DELAYED) event = get_monitor_event (client_mon, NULL, NULL); assert (event == ZMQ_EVENT_CONNECTED); #ifdef ZMQ_BUILD_DRAFT_API expect_monitor_event (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED); #endif expect_monitor_event (client_mon, ZMQ_EVENT_MONITOR_STOPPED); // This is the flow of server events expect_monitor_event (server_mon, ZMQ_EVENT_LISTENING); expect_monitor_event (server_mon, ZMQ_EVENT_ACCEPTED); #ifdef ZMQ_BUILD_DRAFT_API expect_monitor_event (server_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED); #endif event = get_monitor_event (server_mon, NULL, NULL); // Sometimes the server sees the client closing before it gets closed. if (event != ZMQ_EVENT_DISCONNECTED) { assert (event == ZMQ_EVENT_CLOSED); event = get_monitor_event (server_mon, NULL, NULL); } if (event != ZMQ_EVENT_DISCONNECTED) { assert (event == ZMQ_EVENT_MONITOR_STOPPED); } // Close down the sockets close_zero_linger (client_mon); close_zero_linger (server_mon); zmq_ctx_term (ctx); return 0; }
static void test_stream_handshake_timeout_accept (void) { int rc; // Set up our context and sockets void *ctx = zmq_ctx_new (); assert (ctx); // We use this socket in raw mode, to make a connection and send nothing void *stream = zmq_socket (ctx, ZMQ_STREAM); assert (stream); int zero = 0; rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)); assert (rc == 0); rc = zmq_connect (stream, "tcp://localhost:5557"); assert (rc == 0); // We'll be using this socket to test TCP stream handshake timeout void *dealer = zmq_socket (ctx, ZMQ_DEALER); assert (dealer); rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)); assert (rc == 0); int val, tenth = 100; size_t vsize = sizeof(val); // check for the expected default handshake timeout value - 30 sec rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize); assert (rc == 0); assert (vsize == sizeof(val)); assert (val == 30000); // make handshake timeout faster - 1/10 sec rc = zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)); assert (rc == 0); vsize = sizeof(val); // make sure zmq_setsockopt changed the value rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize); assert (rc == 0); assert (vsize == sizeof(val)); assert (val == tenth); // Create and connect a socket for collecting monitor events on dealer void *dealer_mon = zmq_socket (ctx, ZMQ_PAIR); assert (dealer_mon); rc = zmq_socket_monitor (dealer, "inproc://monitor-dealer", ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED); assert (rc == 0); // Connect to the inproc endpoint so we'll get events rc = zmq_connect (dealer_mon, "inproc://monitor-dealer"); assert (rc == 0); // bind dealer socket to accept connection from non-sending stream socket rc = zmq_bind (dealer, "tcp://127.0.0.1:5557"); assert (rc == 0); // we should get ZMQ_EVENT_ACCEPTED and then ZMQ_EVENT_DISCONNECTED int event = get_monitor_event (dealer_mon, NULL, NULL); assert (event == ZMQ_EVENT_ACCEPTED); event = get_monitor_event (dealer_mon, NULL, NULL); assert (event == ZMQ_EVENT_DISCONNECTED); rc = zmq_close (dealer); assert (rc == 0); rc = zmq_close (dealer_mon); assert (rc == 0); rc = zmq_close (stream); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); }
int main (void) { setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); // We'll monitor these two sockets void *client = zmq_socket (ctx, ZMQ_DEALER); assert (client); void *server = zmq_socket (ctx, ZMQ_DEALER); assert (server); // Socket monitoring only works over inproc:// int rc = zmq_socket_monitor (client, "tcp://127.0.0.1:9999", 0); assert (rc == -1); assert (zmq_errno () == EPROTONOSUPPORT); // Monitor all events on client and server sockets rc = zmq_socket_monitor (client, "inproc://monitor-client", ZMQ_EVENT_ALL); assert (rc == 0); rc = zmq_socket_monitor (server, "inproc://monitor-server", ZMQ_EVENT_ALL); assert (rc == 0); // Create two sockets for collecting monitor events void *client_mon = zmq_socket (ctx, ZMQ_PAIR); assert (client_mon); void *server_mon = zmq_socket (ctx, ZMQ_PAIR); assert (server_mon); // Connect these to the inproc endpoints so they'll get events rc = zmq_connect (client_mon, "inproc://monitor-client"); assert (rc == 0); rc = zmq_connect (server_mon, "inproc://monitor-server"); assert (rc == 0); // Now do a basic ping test rc = zmq_bind (server, "tcp://127.0.0.1:9998"); assert (rc == 0); rc = zmq_connect (client, "tcp://127.0.0.1:9998"); assert (rc == 0); bounce (client, server); // Close client and server close_zero_linger (client); close_zero_linger (server); // Now collect and check events from both sockets int event = get_monitor_event (client_mon, NULL, NULL); if (event == ZMQ_EVENT_CONNECT_DELAYED) event = get_monitor_event (client_mon, NULL, NULL); assert (event == ZMQ_EVENT_CONNECTED); event = get_monitor_event (client_mon, NULL, NULL); assert (event == ZMQ_EVENT_MONITOR_STOPPED); // This is the flow of server events event = get_monitor_event (server_mon, NULL, NULL); assert (event == ZMQ_EVENT_LISTENING); event = get_monitor_event (server_mon, NULL, NULL); assert (event == ZMQ_EVENT_ACCEPTED); event = get_monitor_event (server_mon, NULL, NULL); assert (event == ZMQ_EVENT_CLOSED); event = get_monitor_event (server_mon, NULL, NULL); assert (event == ZMQ_EVENT_MONITOR_STOPPED); // Close down the sockets close_zero_linger (client_mon); close_zero_linger (server_mon); zmq_ctx_term (ctx); return 0 ; }