1 /*
2 Copyright (c) 2012 Martin Sustrik All rights reserved.
3 Copyright 2017 Garrett D'Amore
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy 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
21 DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <nng/compat/nanomsg/nn.h>
25 #include <nng/compat/nanomsg/survey.h>
26
27 #include "compat_testutil.h"
28
29 #define SOCKET_ADDRESS "inproc://test"
30
31 int
main()32 main()
33 {
34 int rc;
35 int surveyor;
36 int respondent1;
37 int respondent2;
38 int respondent3;
39 int deadline;
40 char buf[7];
41
42 /* Test a simple survey with three respondents. */
43 surveyor = test_socket(AF_SP, NN_SURVEYOR);
44 deadline = 500;
45 rc = nn_setsockopt(surveyor, NN_SURVEYOR, NN_SURVEYOR_DEADLINE,
46 &deadline, sizeof(deadline));
47 errno_assert(rc == 0);
48 test_bind(surveyor, SOCKET_ADDRESS);
49 respondent1 = test_socket(AF_SP, NN_RESPONDENT);
50 test_connect(respondent1, SOCKET_ADDRESS);
51 respondent2 = test_socket(AF_SP, NN_RESPONDENT);
52 test_connect(respondent2, SOCKET_ADDRESS);
53 respondent3 = test_socket(AF_SP, NN_RESPONDENT);
54 test_connect(respondent3, SOCKET_ADDRESS);
55
56 /* Sleep a tiny bit. */
57 nn_sleep(1000);
58
59 /* Check that attempt to recv with no survey pending is EFSM. */
60 rc = nn_recv(surveyor, buf, sizeof(buf), 0);
61 errno_assert(rc == -1 && nn_errno() == EFSM);
62
63 /* Send the survey. */
64 test_send(surveyor, "ABC");
65
66 /* First respondent answers. */
67 test_recv(respondent1, "ABC");
68 test_send(respondent1, "DEF");
69
70 /* Second respondent answers. */
71 test_recv(respondent2, "ABC");
72 test_send(respondent2, "DEF");
73
74 /* Surveyor gets the responses. */
75 test_recv(surveyor, "DEF");
76 test_recv(surveyor, "DEF");
77
78 /* There are no more responses. Surveyor hits the deadline. */
79 rc = nn_recv(surveyor, buf, sizeof(buf), 0);
80 errno_assert(rc == -1 && nn_errno() == ETIMEDOUT);
81
82 /* Third respondent answers (it have already missed the deadline). */
83 test_recv(respondent3, "ABC");
84 test_send(respondent3, "GHI");
85
86 /* Surveyor initiates new survey. */
87 test_send(surveyor, "ABC");
88
89 /* Check that stale response from third respondent is not delivered.
90 */
91 rc = nn_recv(surveyor, buf, sizeof(buf), 0);
92 errno_assert(rc == -1 && nn_errno() == ETIMEDOUT);
93
94 /* Check that subsequent attempt to recv with no survey pending is
95 * EFSM. */
96 nn_sleep(1000); // nng - sleep a bit as there may be a thread race
97 rc = nn_recv(surveyor, buf, sizeof(buf), 0);
98 errno_assert(rc == -1 && nn_errno() == EFSM);
99
100 test_close(surveyor);
101 test_close(respondent1);
102 test_close(respondent2);
103 test_close(respondent3);
104
105 return 0;
106 }
107