test_spec_rep.cpp 5.33 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
#include "testutil_unity.hpp"

33
#include <stdlib.h>
34

35
SETUP_TEARDOWN_TESTCONTEXT
36

37
char connect_address[MAX_SOCKET_STRING];
38

39
void test_fair_queue_in (const char *bind_address_)
40
{
41
    void *rep = test_context_socket (ZMQ_REP);
42

43
    int timeout = 250;
44 45
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (rep, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
46

47
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (rep, bind_address_));
48
    size_t len = MAX_SOCKET_STRING;
49 50
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len));
51

52
    const size_t services = 5;
53
    void *reqs[services];
54
    for (size_t peer = 0; peer < services; ++peer) {
55
        reqs[peer] = test_context_socket (ZMQ_REQ);
56

57 58 59
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_setsockopt (reqs[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (reqs[peer], connect_address));
60 61
    }

62 63
    msleep (SETTLE_TIME);

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

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

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

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

97
    for (size_t peer = 0; peer < services; ++peer)
98
        test_context_socket_close_zero_linger (reqs[peer]);
99 100
}

101
void test_envelope (const char *bind_address_)
102
{
103
    void *rep = test_context_socket (ZMQ_REP);
104

105
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (rep, bind_address_));
106
    size_t len = MAX_SOCKET_STRING;
107 108
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len));
109

110
    void *dealer = test_context_socket (ZMQ_DEALER);
111

112
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, connect_address));
113 114 115 116 117 118 119 120 121 122 123 124 125

    // 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);

126 127 128
    test_context_socket_close_zero_linger (rep);
    test_context_socket_close_zero_linger (dealer);
}
129

130 131 132 133 134 135
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);
136 137
}

138
void test_fair_queue_in_tcp ()
139
{
140 141 142 143 144 145 146
    test_fair_queue_in (bind_tcp);
}

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

148 149 150 151
void test_envelope_tcp ()
{
    test_envelope (bind_tcp);
}
152

153 154 155
int main ()
{
    setup_test_environment ();
156

157
    UNITY_BEGIN ();
158

159 160 161 162
    // 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);
163

164 165 166 167 168 169 170 171
    // 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);
172

173
    return UNITY_END ();
174
}