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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
//
// Copyright 2018 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 "convey.h"
#include "nng.h"
#include "protocol/pipeline0/pull.h"
#include "protocol/pipeline0/push.h"
#include "stubs.h"
#include "supplemental/util/platform.h"
#include <string.h>
#define APPENDSTR(m, s) nng_msg_append(m, s, strlen(s))
#define CHECKSTR(m, s) \
So(nng_msg_len(m) == strlen(s)); \
So(memcmp(nng_msg_body(m), s, strlen(s)) == 0)
#define MILLISECOND(x) (x)
TestMain("PIPELINE (PUSH/PULL) pattern", {
atexit(nng_fini);
const char *addr = "inproc://test";
Convey("We can create a PUSH socket", {
nng_socket push;
So(nng_push_open(&push) == 0);
Reset({ nng_close(push); });
Convey("Recv fails", {
nng_msg *msg;
So(nng_recvmsg(push, &msg, 0) == NNG_ENOTSUP);
});
});
Convey("We can create a PULL socket", {
nng_socket pull;
So(nng_pull_open(&pull) == 0);
Reset({ nng_close(pull); });
Convey("Send fails", {
nng_msg *msg;
So(nng_msg_alloc(&msg, 0) == 0);
So(nng_sendmsg(pull, msg, 0) == NNG_ENOTSUP);
nng_msg_free(msg);
});
});
Convey("We can create a linked PUSH/PULL pair", {
nng_socket push;
nng_socket pull;
nng_socket what;
So(nng_push_open(&push) == 0);
So(nng_pull_open(&pull) == 0);
So(nng_push_open(&what) == 0);
Reset({
nng_close(push);
nng_close(pull);
nng_close(what);
});
// Its important to avoid a startup race that the
// sender be the dialer. Otherwise you need a delay
// since the server accept is really asynchronous.
So(nng_listen(pull, addr, NULL, 0) == 0);
So(nng_dial(push, addr, NULL, 0) == 0);
So(nng_dial(what, addr, NULL, 0) == 0);
So(nng_close(what) == 0);
nng_msleep(20);
Convey("Push can send messages, and pull can recv", {
nng_msg *msg;
So(nng_msg_alloc(&msg, 0) == 0);
APPENDSTR(msg, "hello");
So(nng_sendmsg(push, msg, 0) == 0);
msg = NULL;
So(nng_recvmsg(pull, &msg, 0) == 0);
So(msg != NULL);
CHECKSTR(msg, "hello");
nng_msg_free(msg);
});
});
Convey("Load balancing", {
nng_msg * abc;
nng_msg * def;
nng_duration msecs;
nng_socket push;
nng_socket pull1;
nng_socket pull2;
nng_socket pull3;
So(nng_push_open(&push) == 0);
So(nng_pull_open(&pull1) == 0);
So(nng_pull_open(&pull2) == 0);
So(nng_pull_open(&pull3) == 0);
Reset({
nng_close(push);
nng_close(pull1);
nng_close(pull2);
nng_close(pull3);
});
// We need to increase the buffer from zero, because
// there is no guarantee that the various listeners
// will be present, which means that they will push
// back during load balancing. Adding a small buffer
// ensures that we can write to each stream, even if
// the listeners are not running yet.
So(nng_setopt_int(push, NNG_OPT_RECVBUF, 4) == 0);
So(nng_setopt_int(push, NNG_OPT_SENDBUF, 4) == 0);
So(nng_setopt_int(pull1, NNG_OPT_RECVBUF, 4) == 0);
So(nng_setopt_int(pull1, NNG_OPT_SENDBUF, 4) == 0);
So(nng_setopt_int(pull2, NNG_OPT_RECVBUF, 4) == 0);
So(nng_setopt_int(pull2, NNG_OPT_SENDBUF, 4) == 0);
So(nng_setopt_int(pull3, NNG_OPT_RECVBUF, 4) == 0);
So(nng_setopt_int(pull3, NNG_OPT_SENDBUF, 4) == 0);
So(nng_msg_alloc(&abc, 0) == 0);
APPENDSTR(abc, "abc");
So(nng_msg_alloc(&def, 0) == 0);
APPENDSTR(def, "def");
msecs = MILLISECOND(100);
So(nng_setopt_ms(pull1, NNG_OPT_RECVTIMEO, msecs) == 0);
So(nng_setopt_ms(pull2, NNG_OPT_RECVTIMEO, msecs) == 0);
So(nng_setopt_ms(pull3, NNG_OPT_RECVTIMEO, msecs) == 0);
So(nng_listen(push, addr, NULL, 0) == 0);
So(nng_dial(pull1, addr, NULL, 0) == 0);
So(nng_dial(pull2, addr, NULL, 0) == 0);
So(nng_dial(pull3, addr, NULL, 0) == 0);
So(nng_close(pull3) == 0);
// So pull3 might not be done accepting yet, but pull1
// and pull2 definitely are, because otherwise the
// server couldn't have gotten to the accept. (The
// accept logic is single threaded.) Let's wait a bit
// though, to ensure that stuff has settled.
nng_msleep(100);
So(nng_sendmsg(push, abc, 0) == 0);
So(nng_sendmsg(push, def, 0) == 0);
abc = NULL;
def = NULL;
So(nng_recvmsg(pull1, &abc, 0) == 0);
CHECKSTR(abc, "abc");
So(nng_recvmsg(pull2, &def, 0) == 0);
CHECKSTR(def, "def");
nng_msg_free(abc);
nng_msg_free(def);
So(nng_recvmsg(pull1, &abc, 0) == NNG_ETIMEDOUT);
So(nng_recvmsg(pull2, &abc, 0) == NNG_ETIMEDOUT);
});
})
|