1 /*
2     Copyright (c) 2012 Martin Sustrik  All rights reserved.
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"),
6     to deal in the Software without restriction, including without limitation
7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
8     and/or sell copies of the Software, and to permit persons to whom
9     the Software is furnished to do so, subject to the following conditions:
10 
11     The above copyright notice and this permission notice shall be included
12     in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20     IN THE SOFTWARE.
21 */
22 
23 #include "../src/nn.h"
24 #include "../src/bus.h"
25 #include "testutil.h"
26 
27 #define SOCKET_ADDRESS_A "inproc://a"
28 #define SOCKET_ADDRESS_B "inproc://b"
29 
main()30 int main ()
31 {
32     int rc;
33     int bus1;
34     int bus2;
35     int bus3;
36     char buf [3];
37 
38     /*  Create a simple bus topology consisting of 3 nodes. */
39     bus1 = test_socket (AF_SP, NN_BUS);
40     test_bind (bus1, SOCKET_ADDRESS_A);
41     bus2 = test_socket (AF_SP, NN_BUS);
42     test_bind (bus2, SOCKET_ADDRESS_B);
43     test_connect (bus2, SOCKET_ADDRESS_A);
44     bus3 = test_socket (AF_SP, NN_BUS);
45     test_connect (bus3, SOCKET_ADDRESS_A);
46     test_connect (bus3, SOCKET_ADDRESS_B);
47 
48     /*  Send a message from each node. */
49     test_send (bus1, "A");
50     test_send (bus2, "AB");
51     test_send (bus3, "ABC");
52 
53     /*  Check that two messages arrived at each node. */
54     rc = nn_recv (bus1, buf, 3, 0);
55     errno_assert (rc >= 0);
56     nn_assert (rc == 2 || rc == 3);
57     rc = nn_recv (bus1, buf, 3, 0);
58     errno_assert (rc >= 0);
59     nn_assert (rc == 2 || rc == 3);
60     rc = nn_recv (bus2, buf, 3, 0);
61     errno_assert (rc >= 0);
62     nn_assert (rc == 1 || rc == 3);
63     rc = nn_recv (bus2, buf, 3, 0);
64     errno_assert (rc >= 0);
65     nn_assert (rc == 1 || rc == 3);
66     rc = nn_recv (bus3, buf, 3, 0);
67     errno_assert (rc >= 0);
68     nn_assert (rc == 1 || rc == 2);
69     rc = nn_recv (bus3, buf, 3, 0);
70     errno_assert (rc >= 0);
71     nn_assert (rc == 1 || rc == 2);
72 
73     /*  Wait till both connections are established. */
74     nn_sleep (10);
75 
76     test_close (bus3);
77     test_close (bus2);
78     test_close (bus1);
79 
80     return 0;
81 }
82 
83