Ejemplo n.º 1
0
// 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);
}
Ejemplo n.º 4
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);
}
Ejemplo n.º 6
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);
}