// 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); }