test_monitor.cpp 4.26 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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.
25 26 27 28 29

    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/>.
*/

30
#include "testutil.hpp"
31
#include "testutil_security.hpp"
32

33
int main (void)
34
{
35
    setup_test_environment();
36

37 38
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
39
    void *ctx = zmq_ctx_new ();
40
    assert (ctx);
41 42 43 44 45 46 47 48
    
    //  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://
49
    int rc = zmq_socket_monitor (client, "tcp://127.0.0.1:*", 0);
50
    assert (rc == -1);
51
    assert (zmq_errno () == EPROTONOSUPPORT);
52

53 54
    //  Monitor all events on client and server sockets
    rc = zmq_socket_monitor (client, "inproc://monitor-client", ZMQ_EVENT_ALL);
55
    assert (rc == 0);
56
    rc = zmq_socket_monitor (server, "inproc://monitor-server", ZMQ_EVENT_ALL);
57
    assert (rc == 0);
58

59 60 61 62 63
    //  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);
64

65 66
    //  Connect these to the inproc endpoints so they'll get events
    rc = zmq_connect (client_mon, "inproc://monitor-client");
67
    assert (rc == 0);
68
    rc = zmq_connect (server_mon, "inproc://monitor-server");
69
    assert (rc == 0);
70
    
71
    //  Now do a basic ping test
72
    rc = zmq_bind (server, "tcp://127.0.0.1:*");
73
    assert (rc == 0);
74 75 76
    rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
    assert (rc == 0);
    rc = zmq_connect (client, my_endpoint);
77
    assert (rc == 0);
78
    bounce (server, client);
79

80 81 82 83 84 85 86 87 88
    //  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);
89
#ifdef ZMQ_BUILD_DRAFT_API
90
    expect_monitor_event (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
91
#endif
92
    expect_monitor_event (client_mon, ZMQ_EVENT_MONITOR_STOPPED);
93 94

    //  This is the flow of server events
95 96
    expect_monitor_event (server_mon, ZMQ_EVENT_LISTENING);
    expect_monitor_event (server_mon, ZMQ_EVENT_ACCEPTED);
97
#ifdef ZMQ_BUILD_DRAFT_API
98
    expect_monitor_event (server_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
99
#endif
100
    event = get_monitor_event (server_mon, NULL, NULL);
Brian Silverman's avatar
Brian Silverman committed
101 102 103 104 105 106 107 108
    //  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);
    }
109 110 111 112
    
    //  Close down the sockets
    close_zero_linger (client_mon);
    close_zero_linger (server_mon);
113
    zmq_ctx_term (ctx);
114

115
    return 0 ;
AJ Lewis's avatar
AJ Lewis committed
116
}