test_spec_rep.cpp 5.16 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 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 30 31

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

#include "testutil.hpp"

32
const char *bind_address = 0;
33
char connect_address[MAX_SOCKET_STRING];
34

35
void test_fair_queue_in (void *ctx_)
36
{
37
    void *rep = zmq_socket (ctx_, ZMQ_REP);
38 39
    assert (rep);

40
    int timeout = 250;
41
    int rc = zmq_setsockopt (rep, ZMQ_RCVTIMEO, &timeout, sizeof (int));
42 43
    assert (rc == 0);

44
    rc = zmq_bind (rep, bind_address);
45
    assert (rc == 0);
46 47 48
    size_t len = MAX_SOCKET_STRING;
    rc = zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len);
    assert (rc == 0);
49

50
    const size_t services = 5;
51
    void *reqs[services];
52
    for (size_t peer = 0; peer < services; ++peer) {
53
        reqs[peer] = zmq_socket (ctx_, ZMQ_REQ);
54
        assert (reqs[peer]);
55

56
        rc = zmq_setsockopt (reqs[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
57 58
        assert (rc == 0);

59
        rc = zmq_connect (reqs[peer], connect_address);
60 61 62
        assert (rc == 0);
    }

63 64
    msleep (SETTLE_TIME);

65
    s_send_seq (reqs[0], "A", SEQ_END);
66 67
    s_recv_seq (rep, "A", SEQ_END);
    s_send_seq (rep, "A", SEQ_END);
68
    s_recv_seq (reqs[0], "A", SEQ_END);
69

70
    s_send_seq (reqs[0], "A", SEQ_END);
71 72
    s_recv_seq (rep, "A", SEQ_END);
    s_send_seq (rep, "A", SEQ_END);
73
    s_recv_seq (reqs[0], "A", SEQ_END);
74

75 76
    // TODO: following test fails randomly on some boxes
#ifdef SOMEONE_FIXES_THIS
77
    // send N requests
78
    for (size_t peer = 0; peer < services; ++peer) {
79 80 81
        char *str = strdup ("A");
        str[0] += peer;
        s_send_seq (reqs[peer], str, SEQ_END);
82 83 84 85
        free (str);
    }

    // handle N requests
86
    for (size_t peer = 0; peer < services; ++peer) {
87 88
        char *str = strdup ("A");
        str[0] += peer;
89
        //  Test fails here
90 91
        s_recv_seq (rep, str, SEQ_END);
        s_send_seq (rep, str, SEQ_END);
92
        s_recv_seq (reqs[peer], str, SEQ_END);
93 94
        free (str);
    }
95
#endif
96
    close_zero_linger (rep);
97

98
    for (size_t peer = 0; peer < services; ++peer)
99
        close_zero_linger (reqs[peer]);
100 101

    // Wait for disconnects.
102
    msleep (SETTLE_TIME);
103 104
}

105
void test_envelope (void *ctx_)
106
{
107
    void *rep = zmq_socket (ctx_, ZMQ_REP);
108 109
    assert (rep);

110
    int rc = zmq_bind (rep, bind_address);
111
    assert (rc == 0);
112 113 114
    size_t len = MAX_SOCKET_STRING;
    rc = zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len);
    assert (rc == 0);
115

116
    void *dealer = zmq_socket (ctx_, ZMQ_DEALER);
117 118
    assert (dealer);

119
    rc = zmq_connect (dealer, connect_address);
120 121 122 123 124 125 126 127 128 129 130 131 132 133
    assert (rc == 0);

    // minimal envelope
    s_send_seq (dealer, 0, "A", SEQ_END);
    s_recv_seq (rep, "A", SEQ_END);
    s_send_seq (rep, "A", SEQ_END);
    s_recv_seq (dealer, 0, "A", SEQ_END);

    // big envelope
    s_send_seq (dealer, "X", "Y", 0, "A", SEQ_END);
    s_recv_seq (rep, "A", SEQ_END);
    s_send_seq (rep, "A", SEQ_END);
    s_recv_seq (dealer, "X", "Y", 0, "A", SEQ_END);

134 135
    close_zero_linger (rep);
    close_zero_linger (dealer);
136

137
    // Wait for disconnects.
138
    msleep (SETTLE_TIME);
139 140
}

141
int main (void)
142
{
143
    setup_test_environment ();
144 145 146
    void *ctx = zmq_ctx_new ();
    assert (ctx);

147
    const char *binds[] = {"inproc://a", "tcp://127.0.0.1:*"};
148

149
    for (int transport = 0; transport < 2; ++transport) {
150
        bind_address = binds[transport];
151 152 153 154 155 156 157 158 159 160 161 162 163

        // SHALL receive incoming messages from its peers using a fair-queuing
        // strategy.
        test_fair_queue_in (ctx);

        // For an incoming message:
        // SHALL remove and store the address envelope, including the delimiter.
        // SHALL pass the remaining data frames to its calling application.
        // SHALL wait for a single reply message from its calling application.
        // SHALL prepend the address envelope and delimiter.
        // SHALL deliver this message back to the originating peer.
        test_envelope (ctx);
    }
164 165 166 167

    int rc = zmq_ctx_term (ctx);
    assert (rc == 0);

168
    return 0;
169
}