test_term_endpoint.cpp 2.88 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 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 26 27 28 29
    int rc;
    char buf[32];
    const char *ep = "tcp://127.0.0.1:5560";

    //  Create infrastructure.
30
    void *ctx = zmq_ctx_new ();
31 32 33 34 35 36 37 38 39 40
    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);

41
    //  Pass one message through to ensure the connection is established
42 43 44 45 46
    rc = zmq_send (push, "ABC", 3, 0);
    assert (rc == 3);
    rc = zmq_recv (pull, buf, sizeof (buf), 0);
    assert (rc == 3);

47
    //  Unbind the listening endpoint
48 49 50
    rc = zmq_unbind (push, ep);
    assert (rc == 0);

51
    //  Allow unbind to settle
52
    msleep (SETTLE_TIME);
53

54
    //  Check that sending would block (there's no outbound connection)
55 56 57
    rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
    assert (rc == -1 && zmq_errno () == EAGAIN);

58
    //  Clean up
59 60 61 62
    rc = zmq_close (pull);
    assert (rc == 0);
    rc = zmq_close (push);
    assert (rc == 0);
63
    rc = zmq_ctx_term (ctx);
64 65
    assert (rc == 0);

66
    //  Create infrastructure
67
    ctx = zmq_ctx_new ();
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    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);

84
    //  Disconnect the bound endpoint
85 86 87
    rc = zmq_disconnect (push, ep);
    assert (rc == 0);

88
    //  Allow disconnect to settle
89
    msleep (SETTLE_TIME);
90 91 92 93 94 95 96 97 98 99

    //  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);
100
    rc = zmq_ctx_term (ctx);
101 102 103 104
    assert (rc == 0);

    return 0;
}