test_term_endpoint.cpp 4.57 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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

20
#include "testutil.hpp"
21

22
int main (void)
23
{
24
    setup_test_environment();
25
    int rc;
26 27
    const size_t buf_size = 32;
    char buf[buf_size];
28
    const char *ep = "tcp://127.0.0.1:5560";
29
    const char *ep_wc_tcp = "tcp://127.0.0.1:*";
30
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
31
    const char *ep_wc_ipc = "ipc://*";
32
#endif
33 34

    //  Create infrastructure.
35
    void *ctx = zmq_ctx_new ();
36 37 38 39 40 41 42 43 44 45
    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);

46
    //  Pass one message through to ensure the connection is established
47 48 49 50 51
    rc = zmq_send (push, "ABC", 3, 0);
    assert (rc == 3);
    rc = zmq_recv (pull, buf, sizeof (buf), 0);
    assert (rc == 3);

52
    //  Unbind the listening endpoint
53 54 55
    rc = zmq_unbind (push, ep);
    assert (rc == 0);

56
    //  Allow unbind to settle
57
    msleep (SETTLE_TIME);
58

59
    //  Check that sending would block (there's no outbound connection)
60 61 62
    rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
    assert (rc == -1 && zmq_errno () == EAGAIN);

63
    //  Clean up
64 65 66 67
    rc = zmq_close (pull);
    assert (rc == 0);
    rc = zmq_close (push);
    assert (rc == 0);
68
    rc = zmq_ctx_term (ctx);
69 70
    assert (rc == 0);

71
    //  Create infrastructure
72
    ctx = zmq_ctx_new ();
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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);

89
    //  Disconnect the bound endpoint
90 91 92
    rc = zmq_disconnect (push, ep);
    assert (rc == 0);

93
    //  Allow disconnect to settle
94
    msleep (SETTLE_TIME);
95 96 97 98 99 100 101 102 103 104

    //  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);
105
    rc = zmq_ctx_term (ctx);
106 107
    assert (rc == 0);

108 109 110 111 112 113 114
    //  Create infrastructure (wild-card binding)
    ctx = zmq_ctx_new ();
    assert (ctx);
    push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);
    rc = zmq_bind (push, ep_wc_tcp);
    assert (rc == 0);
115
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
116 117 118 119
    pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);
    rc = zmq_bind (pull, ep_wc_ipc);
    assert (rc == 0);
120
#endif
121 122 123 124 125 126

    // Unbind sockets binded by wild-card address
    rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, buf, (size_t *)&buf_size);
    assert (rc == 0);
    rc = zmq_unbind (push, buf);
    assert (rc == 0);
127
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
128 129 130 131
    rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, buf, (size_t *)&buf_size);
    assert (rc == 0);
    rc = zmq_unbind (pull, buf);
    assert (rc == 0);
132
#endif
133 134 135 136 137 138 139 140

    //  Create infrastructure (wild-card binding)
    ctx = zmq_ctx_new ();
    assert (ctx);
    push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);
    rc = zmq_bind (push, ep_wc_tcp);
    assert (rc == 0);
141
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
142 143 144 145
    pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);
    rc = zmq_bind (pull, ep_wc_ipc);
    assert (rc == 0);
146
#endif
147 148 149 150

    // Sockets binded by wild-card address can't be unbinded by wild-card address
    rc = zmq_unbind (push, ep_wc_tcp);
    assert (rc == -1 && zmq_errno () == ENOENT);
151
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
152 153
    rc = zmq_unbind (pull, ep_wc_ipc);
    assert (rc == -1 && zmq_errno () == ENOENT);
154
#endif
155

156 157
    return 0;
}