1 /*
2     Copyright (c) 2012 Martin Sustrik  All rights reserved.
3     Copyright 2015 Garrett D'Amore
4 
5     Permission is hereby granted, free of charge, to any person obtaining a copy
6     of this software and associated documentation files (the "Software"),
7     to deal in the Software without restriction, including without limitation
8     the rights to use, copy, modify, merge, publish, distribute, sublicense,
9     and/or sell copies of the Software, and to permit persons to whom
10     the Software is furnished to do so, subject to the following conditions:
11 
12     The above copyright notice and this permission notice shall be included
13     in all copies or substantial portions of the Software.
14 
15     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21     IN THE SOFTWARE.
22 */
23 
24 #include "../src/nn.h"
25 #include "../src/survey.h"
26 
27 #include "testutil.h"
28 
29 #define SOCKET_ADDRESS "inproc://test"
30 
main()31 int main ()
32 {
33     int rc;
34     int surveyor;
35     int respondent1;
36     int respondent2;
37     int respondent3;
38     int deadline;
39     char buf [7];
40 
41     /*  Test a simple survey with three respondents. */
42     surveyor = test_socket (AF_SP, NN_SURVEYOR);
43     deadline = 500;
44     rc = nn_setsockopt (surveyor, NN_SURVEYOR, NN_SURVEYOR_DEADLINE,
45         &deadline, sizeof (deadline));
46     errno_assert (rc == 0);
47     test_bind (surveyor, SOCKET_ADDRESS);
48     respondent1 = test_socket (AF_SP, NN_RESPONDENT);
49     test_connect (respondent1, SOCKET_ADDRESS);
50     respondent2 = test_socket (AF_SP, NN_RESPONDENT);
51     test_connect (respondent2, SOCKET_ADDRESS);
52     respondent3 = test_socket (AF_SP, NN_RESPONDENT);
53     test_connect (respondent3, SOCKET_ADDRESS);
54 
55     /* Check that attempt to recv with no survey pending is EFSM. */
56     rc = nn_recv (surveyor, buf, sizeof (buf), 0);
57     errno_assert (rc == -1 && nn_errno () == EFSM);
58 
59     /*  Send the survey. */
60     test_send (surveyor, "ABC");
61 
62     /*  First respondent answers. */
63     test_recv (respondent1, "ABC");
64     test_send (respondent1, "DEF");
65 
66     /*  Second respondent answers. */
67     test_recv (respondent2, "ABC");
68     test_send (respondent2, "DEF");
69 
70     /*  Surveyor gets the responses. */
71     test_recv (surveyor, "DEF");
72     test_recv (surveyor, "DEF");
73 
74     /*  There are no more responses. Surveyor hits the deadline. */
75     rc = nn_recv (surveyor, buf, sizeof (buf), 0);
76     errno_assert (rc == -1 && nn_errno () == ETIMEDOUT);
77 
78     /*  Third respondent answers (it have already missed the deadline). */
79     test_recv (respondent3, "ABC");
80     test_send (respondent3, "GHI");
81 
82     /*  Surveyor initiates new survey. */
83     test_send (surveyor, "ABC");
84 
85     /*  Check that stale response from third respondent is not delivered. */
86     rc = nn_recv (surveyor, buf, sizeof (buf), 0);
87     errno_assert (rc == -1 && nn_errno () == ETIMEDOUT);
88 
89     /* Check that subsequent attempt to recv with no survey pending is EFSM. */
90     rc = nn_recv (surveyor, buf, sizeof (buf), 0);
91     errno_assert (rc == -1 && nn_errno () == EFSM);
92 
93     test_close (surveyor);
94     test_close (respondent1);
95     test_close (respondent2);
96     test_close (respondent3);
97 
98     return 0;
99 }
100 
101