~ubuntu-branches/ubuntu/wily/zeromq3/wily-proposed

« back to all changes in this revision

Viewing changes to tests/test_spec_rep.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2014-03-16 14:02:28 UTC
  • mfrom: (1.1.6) (6.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140316140228-ig1sgh7czk59m9ux
Tags: 4.0.4+dfsg-1
* QA upload; orphan the package
  - Upload to unstable
* New upstream release
* Update repack.stub script
* Drop 02_fix-exported-symbols.patch and 03_fix-s390-rdtsc.patch
  (merged upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
 
3
 
 
4
    This file is part of 0MQ.
 
5
 
 
6
    0MQ is free software; you can redistribute it and/or modify it under
 
7
    the terms of the GNU Lesser General Public License as published by
 
8
    the Free Software Foundation; either version 3 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    0MQ is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU Lesser General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Lesser General Public License
 
17
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include "testutil.hpp"
 
21
 
 
22
const char *bind_address = 0;
 
23
const char *connect_address = 0;
 
24
 
 
25
void test_fair_queue_in (void *ctx)
 
26
{
 
27
    void *rep = zmq_socket (ctx, ZMQ_REP);
 
28
    assert (rep);
 
29
 
 
30
    int timeout = 100;
 
31
    int rc = zmq_setsockopt (rep, ZMQ_RCVTIMEO, &timeout, sizeof (int));
 
32
    assert (rc == 0);
 
33
 
 
34
    rc = zmq_bind (rep, bind_address);
 
35
    assert (rc == 0);
 
36
 
 
37
    const size_t services = 5;
 
38
    void *reqs [services];
 
39
    for (size_t peer = 0; peer < services; ++peer) {
 
40
        reqs [peer] = zmq_socket (ctx, ZMQ_REQ);
 
41
        assert (reqs [peer]);
 
42
 
 
43
        rc = zmq_setsockopt (reqs [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
 
44
        assert (rc == 0);
 
45
 
 
46
        rc = zmq_connect (reqs [peer], connect_address);
 
47
        assert (rc == 0);
 
48
    }
 
49
 
 
50
    s_send_seq (reqs [0], "A", SEQ_END);
 
51
    s_recv_seq (rep, "A", SEQ_END);
 
52
    s_send_seq (rep, "A", SEQ_END);
 
53
    s_recv_seq (reqs [0], "A", SEQ_END);
 
54
 
 
55
    s_send_seq (reqs [0], "A", SEQ_END);
 
56
    s_recv_seq (rep, "A", SEQ_END);
 
57
    s_send_seq (rep, "A", SEQ_END);
 
58
    s_recv_seq (reqs [0], "A", SEQ_END);
 
59
 
 
60
    // TODO: following test fails randomly on some boxes
 
61
#ifdef SOMEONE_FIXES_THIS
 
62
    // send N requests
 
63
    for (size_t peer = 0; peer < services; ++peer) {
 
64
        char * str = strdup("A");
 
65
        str [0] += peer;
 
66
        s_send_seq (reqs [peer], str, SEQ_END);
 
67
        free (str);
 
68
    }
 
69
 
 
70
    // handle N requests
 
71
    for (size_t peer = 0; peer < services; ++peer) {
 
72
        char * str = strdup("A");
 
73
        str [0] += peer;
 
74
        //  Test fails here
 
75
        s_recv_seq (rep, str, SEQ_END);
 
76
        s_send_seq (rep, str, SEQ_END);
 
77
        s_recv_seq (reqs [peer], str, SEQ_END);
 
78
        free (str);
 
79
    }
 
80
#endif
 
81
    close_zero_linger (rep);
 
82
 
 
83
    for (size_t peer = 0; peer < services; ++peer)
 
84
        close_zero_linger (reqs [peer]);
 
85
 
 
86
    // Wait for disconnects.
 
87
    rc = zmq_poll (0, 0, 100);
 
88
    assert (rc == 0);
 
89
}
 
90
 
 
91
void test_envelope (void *ctx)
 
92
{
 
93
    void *rep = zmq_socket (ctx, ZMQ_REP);
 
94
    assert (rep);
 
95
 
 
96
    int rc = zmq_bind (rep, bind_address);
 
97
    assert (rc == 0);
 
98
 
 
99
    void *dealer = zmq_socket (ctx, ZMQ_DEALER);
 
100
    assert (dealer);
 
101
 
 
102
    rc = zmq_connect (dealer, connect_address);
 
103
    assert (rc == 0);
 
104
 
 
105
    // minimal envelope
 
106
    s_send_seq (dealer, 0, "A", SEQ_END);
 
107
    s_recv_seq (rep, "A", SEQ_END);
 
108
    s_send_seq (rep, "A", SEQ_END);
 
109
    s_recv_seq (dealer, 0, "A", SEQ_END);
 
110
 
 
111
    // big envelope
 
112
    s_send_seq (dealer, "X", "Y", 0, "A", SEQ_END);
 
113
    s_recv_seq (rep, "A", SEQ_END);
 
114
    s_send_seq (rep, "A", SEQ_END);
 
115
    s_recv_seq (dealer, "X", "Y", 0, "A", SEQ_END);
 
116
 
 
117
    close_zero_linger (rep);
 
118
    close_zero_linger (dealer);
 
119
 
 
120
    // Wait for disconnects.
 
121
    rc = zmq_poll (0, 0, 100);
 
122
    assert (rc == 0);
 
123
}
 
124
 
 
125
int main (void)
 
126
{
 
127
    setup_test_environment();
 
128
    void *ctx = zmq_ctx_new ();
 
129
    assert (ctx);
 
130
 
 
131
    const char *binds [] = { "inproc://a", "tcp://127.0.0.1:5555" };
 
132
    const char *connects [] = { "inproc://a", "tcp://localhost:5555" };
 
133
 
 
134
    for (int transport = 0; transport < 2; ++transport) {
 
135
        bind_address = binds [transport];
 
136
        connect_address = connects [transport];
 
137
 
 
138
        // SHALL receive incoming messages from its peers using a fair-queuing
 
139
        // strategy.
 
140
        test_fair_queue_in (ctx);
 
141
 
 
142
        // For an incoming message:
 
143
        // SHALL remove and store the address envelope, including the delimiter.
 
144
        // SHALL pass the remaining data frames to its calling application.
 
145
        // SHALL wait for a single reply message from its calling application.
 
146
        // SHALL prepend the address envelope and delimiter.
 
147
        // SHALL deliver this message back to the originating peer.
 
148
        test_envelope (ctx);
 
149
    }
 
150
 
 
151
    int rc = zmq_ctx_term (ctx);
 
152
    assert (rc == 0);
 
153
 
 
154
    return 0 ;
 
155
}