1 /*
2     Copyright (c) 2010-2012 250bpm s.r.o.
3     Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
4 
5     This file is part of Crossroads I/O project.
6 
7     Crossroads I/O is free software; you can redistribute it and/or modify it
8     under the terms of the GNU Lesser General Public License as published by
9     the Free Software Foundation; either version 3 of the License, or
10     (at your option) any later version.
11 
12     Crossroads is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Lesser General Public License for more details.
16 
17     You should have received a copy of the GNU Lesser General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "testutil.hpp"
22 
XS_TEST_MAIN()23 int XS_TEST_MAIN ()
24 {
25     fprintf (stderr, "hwm test running...\n");
26 
27     void *ctx = xs_init ();
28     assert (ctx);
29 
30     //  Create pair of socket, each with high watermark of 2. Thus the total
31     //  buffer space should be 4 messages.
32     void *sb = xs_socket (ctx, XS_PULL);
33     assert (sb);
34     int hwm = 2;
35     int rc = xs_setsockopt (sb, XS_RCVHWM, &hwm, sizeof (hwm));
36     assert (rc == 0);
37     rc = xs_bind (sb, "inproc://a");
38     assert (rc != -1);
39 
40     void *sc = xs_socket (ctx, XS_PUSH);
41     assert (sc);
42     rc = xs_setsockopt (sc, XS_SNDHWM, &hwm, sizeof (hwm));
43     assert (rc == 0);
44     rc = xs_connect (sc, "inproc://a");
45     assert (rc != -1);
46 
47     //  Try to send 10 messages. Only 4 should succeed.
48     for (int i = 0; i < 10; i++)
49     {
50         rc = xs_send (sc, NULL, 0, XS_DONTWAIT);
51         if (i < 4)
52             assert (rc == 0);
53         else
54             assert (rc < 0 && errno == EAGAIN);
55     }
56 
57     // There should be now 4 messages pending, consume them.
58     for (int i = 0; i != 4; i++) {
59         rc = xs_recv (sb, NULL, 0, 0);
60         assert (rc == 0);
61     }
62 
63     // Now it should be possible to send one more.
64     rc = xs_send (sc, NULL, 0, 0);
65     assert (rc == 0);
66 
67     //  Consume the remaining message.
68     rc = xs_recv (sb, NULL, 0, 0);
69     assert (rc == 0);
70 
71     rc = xs_close (sc);
72     assert (rc == 0);
73 
74     rc = xs_close (sb);
75     assert (rc == 0);
76 
77     rc = xs_term (ctx);
78     assert (rc == 0);
79 
80     //  Following part of the tests checks whether small HWMs don't interact
81     //  with command throttling in strange ways.
82 
83     ctx = xs_init ();
84     assert (ctx);
85     void *s1 = xs_socket (ctx, XS_PULL);
86     assert (s1);
87     void *s2 = xs_socket (ctx, XS_PUSH);
88     assert (s2);
89 
90     hwm = 5;
91     rc = xs_setsockopt (s2, XS_SNDHWM, &hwm, sizeof (hwm));
92     assert (rc == 0);
93 
94     rc = xs_bind (s1, "tcp://127.0.0.1:5858");
95     assert (rc >= 0);
96     rc = xs_connect (s2, "tcp://127.0.0.1:5858");
97     assert (rc >= 0);
98 
99     for (int i = 0; i < 10; i++)
100     {
101         rc = xs_send (s2, "test", 4, XS_DONTWAIT);
102         assert (rc == 4);
103         char buf [4];
104         rc = xs_recv (s1, buf, sizeof (buf), 0);
105         assert (rc == 4);
106     }
107 
108     rc = xs_close (s2);
109     assert (rc == 0);
110     rc = xs_close (s1);
111     assert (rc == 0);
112     rc = xs_term (ctx);
113     assert (rc == 0);
114 
115 	return 0;
116 }
117