aboutsummaryrefslogtreecommitdiff
path: root/tests/compat_survey.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-07 19:10:09 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-07 19:12:25 -0700
commitfe2c07b6a0dced827b7e086b39b1908d1861af39 (patch)
tree7b9c92ba201dd2217636137a09353f1f503c591a /tests/compat_survey.c
parent2d4b95450141b3f3c581e510b53ee519e592f5d9 (diff)
downloadnng-fe2c07b6a0dced827b7e086b39b1908d1861af39.tar.gz
nng-fe2c07b6a0dced827b7e086b39b1908d1861af39.tar.bz2
nng-fe2c07b6a0dced827b7e086b39b1908d1861af39.zip
Add some more compatibility tests; fix surveyor compat bug.
We noticed a bug in the surveyor handling of the options; this fixes that. At the same time, we noticed a race condition in the setting of the error for future calls, a short sleep seems to cure it. This distinction (ESTATE vs ETIMEDOUT) is pretty annoying, and it would be better to have a different way to handle it. More work here is warranted.
Diffstat (limited to 'tests/compat_survey.c')
-rw-r--r--tests/compat_survey.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/compat_survey.c b/tests/compat_survey.c
new file mode 100644
index 00000000..fc5f8091
--- /dev/null
+++ b/tests/compat_survey.c
@@ -0,0 +1,105 @@
+/*
+ Copyright (c) 2012 Martin Sustrik All rights reserved.
+ Copyright 2017 Garrett D'Amore
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+#include "nng_compat.h"
+
+#include "compat_testutil.h"
+
+#define SOCKET_ADDRESS "inproc://test"
+
+int
+main()
+{
+ int rc;
+ int surveyor;
+ int respondent1;
+ int respondent2;
+ int respondent3;
+ int deadline;
+ char buf[7];
+
+ /* Test a simple survey with three respondents. */
+ surveyor = test_socket(AF_SP, NN_SURVEYOR);
+ deadline = 500;
+ rc = nn_setsockopt(surveyor, NN_SURVEYOR, NN_SURVEYOR_DEADLINE,
+ &deadline, sizeof(deadline));
+ errno_assert(rc == 0);
+ test_bind(surveyor, SOCKET_ADDRESS);
+ respondent1 = test_socket(AF_SP, NN_RESPONDENT);
+ test_connect(respondent1, SOCKET_ADDRESS);
+ respondent2 = test_socket(AF_SP, NN_RESPONDENT);
+ test_connect(respondent2, SOCKET_ADDRESS);
+ respondent3 = test_socket(AF_SP, NN_RESPONDENT);
+ test_connect(respondent3, SOCKET_ADDRESS);
+
+ /* Sleep a tiny bit. */
+ nn_sleep(1000);
+
+ /* Check that attempt to recv with no survey pending is EFSM. */
+ rc = nn_recv(surveyor, buf, sizeof(buf), 0);
+ errno_assert(rc == -1 && nn_errno() == EFSM);
+
+ /* Send the survey. */
+ test_send(surveyor, "ABC");
+
+ /* First respondent answers. */
+ test_recv(respondent1, "ABC");
+ test_send(respondent1, "DEF");
+
+ /* Second respondent answers. */
+ test_recv(respondent2, "ABC");
+ test_send(respondent2, "DEF");
+
+ /* Surveyor gets the responses. */
+ test_recv(surveyor, "DEF");
+ test_recv(surveyor, "DEF");
+
+ /* There are no more responses. Surveyor hits the deadline. */
+ rc = nn_recv(surveyor, buf, sizeof(buf), 0);
+ errno_assert(rc == -1 && nn_errno() == ETIMEDOUT);
+
+ /* Third respondent answers (it have already missed the deadline). */
+ test_recv(respondent3, "ABC");
+ test_send(respondent3, "GHI");
+
+ /* Surveyor initiates new survey. */
+ test_send(surveyor, "ABC");
+
+ /* Check that stale response from third respondent is not delivered.
+ */
+ rc = nn_recv(surveyor, buf, sizeof(buf), 0);
+ errno_assert(rc == -1 && nn_errno() == ETIMEDOUT);
+
+ /* Check that subsequent attempt to recv with no survey pending is
+ * EFSM. */
+ nn_sleep(1000); // nng - sleep a bit as there may be a thread race
+ rc = nn_recv(surveyor, buf, sizeof(buf), 0);
+ errno_assert(rc == -1 && nn_errno() == EFSM);
+
+ test_close(surveyor);
+ test_close(respondent1);
+ test_close(respondent2);
+ test_close(respondent3);
+
+ return 0;
+}