test_spec_rep.cpp 5.39 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

    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"
31 32 33 34 35 36 37 38 39 40 41 42 43
#include "testutil_unity.hpp"

#include <unity.h>

void setUp ()
{
    setup_test_context ();
}

void tearDown ()
{
    teardown_test_context ();
}
44

45
char connect_address[MAX_SOCKET_STRING];
46

47
void test_fair_queue_in (const char *bind_address)
48
{
49
    void *rep = test_context_socket (ZMQ_REP);
50

51
    int timeout = 250;
52 53
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (rep, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
54

55
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (rep, bind_address));
56
    size_t len = MAX_SOCKET_STRING;
57 58
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len));
59

60
    const size_t services = 5;
61
    void *reqs[services];
62
    for (size_t peer = 0; peer < services; ++peer) {
63
        reqs[peer] = test_context_socket (ZMQ_REQ);
64

65 66 67
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_setsockopt (reqs[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (reqs[peer], connect_address));
68 69
    }

70 71
    msleep (SETTLE_TIME);

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

77
    s_send_seq (reqs[0], "A", SEQ_END);
78 79
    s_recv_seq (rep, "A", SEQ_END);
    s_send_seq (rep, "A", SEQ_END);
80
    s_recv_seq (reqs[0], "A", SEQ_END);
81

82 83
    // TODO: following test fails randomly on some boxes
#ifdef SOMEONE_FIXES_THIS
84
    // send N requests
85
    for (size_t peer = 0; peer < services; ++peer) {
86 87 88
        char *str = strdup ("A");
        str[0] += peer;
        s_send_seq (reqs[peer], str, SEQ_END);
89 90 91 92
        free (str);
    }

    // handle N requests
93
    for (size_t peer = 0; peer < services; ++peer) {
94 95
        char *str = strdup ("A");
        str[0] += peer;
96
        //  Test fails here
97 98
        s_recv_seq (rep, str, SEQ_END);
        s_send_seq (rep, str, SEQ_END);
99
        s_recv_seq (reqs[peer], str, SEQ_END);
100 101
        free (str);
    }
102
#endif
103
    test_context_socket_close_zero_linger (rep);
104

105
    for (size_t peer = 0; peer < services; ++peer)
106
        test_context_socket_close_zero_linger (reqs[peer]);
107 108
}

109
void test_envelope (const char *bind_address)
110
{
111
    void *rep = test_context_socket (ZMQ_REP);
112

113
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (rep, bind_address));
114
    size_t len = MAX_SOCKET_STRING;
115 116
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len));
117

118
    void *dealer = test_context_socket (ZMQ_DEALER);
119

120
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, connect_address));
121 122 123 124 125 126 127 128 129 130 131 132 133

    // 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 136
    test_context_socket_close_zero_linger (rep);
    test_context_socket_close_zero_linger (dealer);
}
137

138 139 140 141 142 143
const char bind_inproc[] = "inproc://a";
const char bind_tcp[] = "tcp://127.0.0.1:*";

void test_fair_queue_in_inproc ()
{
    test_fair_queue_in (bind_inproc);
144 145
}

146
void test_fair_queue_in_tcp ()
147
{
148 149 150 151 152 153 154
    test_fair_queue_in (bind_tcp);
}

void test_envelope_inproc ()
{
    test_envelope (bind_inproc);
}
155

156 157 158 159
void test_envelope_tcp ()
{
    test_envelope (bind_tcp);
}
160

161 162 163
int main ()
{
    setup_test_environment ();
164

165
    UNITY_BEGIN ();
166

167 168 169 170
    // SHALL receive incoming messages from its peers using a fair-queuing
    // strategy.
    RUN_TEST (test_fair_queue_in_inproc);
    RUN_TEST (test_fair_queue_in_tcp);
171

172 173 174 175 176 177 178 179
    // 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.
    RUN_TEST (test_envelope_inproc);
    RUN_TEST (test_envelope_tcp);
180

181
    return UNITY_END ();
182
}