1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include <string.h>
#include <nng/nng.h>
#include <nng/protocol/pipeline0/pull.h>
#include <nng/protocol/pipeline0/push.h>
#include "acutest.h"
#include "testutil.h"
void
test_dial_before_listen(void)
{
nng_socket push;
nng_socket pull;
char addr[64];
testutil_scratch_addr("inproc", sizeof(addr), addr);
TEST_NNG_PASS(nng_push0_open(&push));
TEST_NNG_PASS(nng_pull0_open(&pull));
TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10));
TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10));
TEST_NNG_PASS(nng_dial(pull, addr, NULL, NNG_FLAG_NONBLOCK));
testutil_sleep(100);
TEST_NNG_PASS(nng_listen(push, addr, NULL, 0));
TEST_NNG_SEND_STR(push, "hello");
TEST_NNG_RECV_STR(pull, "hello");
TEST_NNG_PASS(nng_close(push));
TEST_NNG_PASS(nng_close(pull));
}
void
test_reconnect(void)
{
nng_socket push;
nng_socket pull;
nng_listener l;
char addr[64];
testutil_scratch_addr("inproc", sizeof(addr), addr);
TEST_NNG_PASS(nng_push0_open(&push));
TEST_NNG_PASS(nng_pull0_open(&pull));
TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10));
TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10));
TEST_NNG_PASS(nng_dial(pull, addr, NULL, NNG_FLAG_NONBLOCK));
testutil_sleep(100);
TEST_NNG_PASS(nng_listen(push, addr, &l, 0));
TEST_NNG_SEND_STR(push, "hello");
TEST_NNG_RECV_STR(pull, "hello");
// Close the listener
TEST_NNG_PASS(nng_listener_close(l));
TEST_NNG_PASS(nng_listen(push, addr, &l, 0));
TEST_NNG_SEND_STR(push, "again");
TEST_NNG_RECV_STR(pull, "again");
TEST_NNG_PASS(nng_close(push));
TEST_NNG_PASS(nng_close(pull));
}
void
test_reconnect_pipe(void)
{
nng_socket push;
nng_socket pull;
nng_listener l;
nng_msg *msg;
char addr[64];
testutil_scratch_addr("inproc", sizeof(addr), addr);
TEST_NNG_PASS(nng_push0_open(&push));
TEST_NNG_PASS(nng_pull0_open(&pull));
TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10));
TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10));
TEST_NNG_PASS(nng_dial(pull, addr, NULL, NNG_FLAG_NONBLOCK));
testutil_sleep(100);
TEST_NNG_PASS(nng_listen(push, addr, &l, 0));
TEST_NNG_SEND_STR(push, "hello");
TEST_NNG_PASS(nng_recvmsg(pull, &msg, 0));
TEST_CHECK(msg != NULL);
TEST_CHECK(nng_msg_len(msg) == 6);
TEST_CHECK(strcmp(nng_msg_body(msg), "hello") == 0);
nng_pipe_close(nng_msg_get_pipe(msg));
nng_msg_free(msg);
// We have to wait a bit, because while we closed the pipe on the
// receiver, the receiver might not have got the update. If we
// send too soon, then the message gets routed to the sender pipe
// that is about to close.
testutil_sleep(100);
// Reconnect should happen more ore less immediately.
TEST_NNG_SEND_STR(push, "again");
TEST_NNG_RECV_STR(pull, "again");
TEST_NNG_PASS(nng_close(push));
TEST_NNG_PASS(nng_close(pull));
}
TEST_LIST = {
{ "dial before listen", test_dial_before_listen },
{ "reconnect", test_reconnect },
{ "reconnect pipe", test_reconnect_pipe },
{ NULL, NULL },
};
|