aboutsummaryrefslogtreecommitdiff
path: root/src/sp/protocol/pubsub0/pub_test.c
blob: 3018ced92e561a694757f345ecc9b87490e755ee (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
//
// 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 "nng/nng.h"
#include <nuts.h>

static void
test_pub_identity(void)
{
	nng_socket  s;
	uint16_t    p;
	const char *n;

	NUTS_PASS(nng_pub0_open(&s));
	NUTS_PASS(nng_socket_proto_id(s, &p));
	NUTS_TRUE(p == NUTS_PROTO(2u, 0u)); // 32
	NUTS_PASS(nng_socket_peer_id(s, &p));
	NUTS_TRUE(p == NUTS_PROTO(2u, 1u)); // 33
	NUTS_PASS(nng_socket_proto_name(s, &n));
	NUTS_MATCH(n, "pub");
	NUTS_PASS(nng_socket_peer_name(s, &n));
	NUTS_MATCH(n, "sub");
	NUTS_CLOSE(s);
}

static void
test_pub_cannot_recv(void)
{
	nng_socket pub;

	NUTS_PASS(nng_pub0_open(&pub));
	NUTS_FAIL(nng_recv(pub, "", 0, 0), NNG_ENOTSUP);
	NUTS_CLOSE(pub);
}

static void
test_pub_no_context(void)
{
	nng_socket pub;
	nng_ctx    ctx;

	NUTS_PASS(nng_pub0_open(&pub));
	NUTS_FAIL(nng_ctx_open(&ctx, pub), NNG_ENOTSUP);
	NUTS_CLOSE(pub);
}

static void
test_pub_not_readable(void)
{
	int        fd;
	nng_socket pub;

	NUTS_PASS(nng_pub0_open(&pub));
	NUTS_FAIL(nng_socket_get_recv_poll_fd(pub, &fd), NNG_ENOTSUP);
	NUTS_CLOSE(pub);
}

static void
test_pub_poll_writeable(void)
{
	int        fd;
	nng_socket pub;
	nng_socket sub;

	NUTS_PASS(nng_sub0_open(&sub));
	NUTS_PASS(nng_pub0_open(&pub));
	NUTS_PASS(nng_socket_get_send_poll_fd(pub, &fd));
	NUTS_TRUE(fd >= 0);

	// Pub is *always* writeable
	NUTS_TRUE(nuts_poll_fd(fd));

	// Even after connect (no message yet)
	NUTS_MARRY(pub, sub);
	NUTS_TRUE(nuts_poll_fd(fd));

	// Even if we send messages.
	NUTS_SEND(pub, "abc");
	NUTS_TRUE(nuts_poll_fd(fd));

	NUTS_CLOSE(pub);
	NUTS_CLOSE(sub);
}

static void
test_pub_send_no_pipes(void)
{
	nng_socket pub;

	NUTS_PASS(nng_pub0_open(&pub));
	NUTS_SEND(pub, "DROP1");
	NUTS_SEND(pub, "DROP2");

	nng_stat       *stats;
	const nng_stat *pubs;
	NUTS_PASS(nng_stats_get(&stats));

	NUTS_TRUE(stats != NULL);
	NUTS_TRUE((pubs = nng_stat_find_socket(stats, pub)) != NULL);

	nng_stats_dump(pubs);
	nng_stats_free(stats);

	NUTS_CLOSE(pub);
}

void
test_pub_validate_peer(void)
{
	nng_socket      s1, s2;
	nng_stat       *stats;
	const nng_stat *reject;
	char           *addr;

	NUTS_ADDR(addr, "inproc");

	NUTS_PASS(nng_pub0_open(&s1));
	NUTS_PASS(nng_pub0_open(&s2));

	NUTS_PASS(nng_listen(s1, addr, NULL, 0));
	NUTS_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));

	NUTS_SLEEP(100);
	NUTS_PASS(nng_stats_get(&stats));

	NUTS_TRUE(stats != NULL);
	NUTS_TRUE((reject = nng_stat_find_socket(stats, s1)) != NULL);
	NUTS_TRUE((reject = nng_stat_find(reject, "reject")) != NULL);

	NUTS_TRUE(nng_stat_type(reject) == NNG_STAT_COUNTER);
	NUTS_TRUE(nng_stat_value(reject) > 0);

	NUTS_CLOSE(s1);
	NUTS_CLOSE(s2);
	nng_stats_dump(stats);
	nng_stats_free(stats);
}

static void
test_pub_send_queued(void)
{
	nng_socket pub;
	nng_socket sub;

	// MB: What we really need is a mock so that we can send harder
	// than we receive -- we need a way to apply back-pressure for this
	// test to be really meaningful.
	NUTS_PASS(nng_pub0_open(&pub));
	NUTS_PASS(nng_sub0_open(&sub));
	NUTS_PASS(nng_sub0_socket_subscribe(sub, "", 0));
	NUTS_PASS(nng_socket_set_int(pub, NNG_OPT_SENDBUF, 10));
	NUTS_PASS(nng_socket_set_int(sub, NNG_OPT_RECVBUF, 10));
	NUTS_PASS(nng_socket_set_ms(pub, NNG_OPT_SENDTIMEO, 1000));
	NUTS_PASS(nng_socket_set_ms(sub, NNG_OPT_RECVTIMEO, 1000));
	NUTS_MARRY(pub, sub);
	NUTS_SEND(pub, "first");
	NUTS_SEND(pub, "second");
	NUTS_SEND(pub, "three musketeers");
	NUTS_SEND(pub, "four");
	NUTS_SLEEP(50);
	NUTS_RECV(sub, "first");
	NUTS_RECV(sub, "second");
	NUTS_RECV(sub, "three musketeers");
	NUTS_RECV(sub, "four");

	nng_stat       *stats;
	const nng_stat *pubs;
	NUTS_PASS(nng_stats_get(&stats));

	NUTS_TRUE(stats != NULL);
	NUTS_TRUE((pubs = nng_stat_find_socket(stats, pub)) != NULL);

	nng_stats_dump(pubs);
	nng_stats_free(stats);

	NUTS_CLOSE(pub);
	NUTS_CLOSE(sub);
}

static void
test_pub_send_buf_option(void)
{
	nng_socket  pub;
	int         v;
	bool        b;
	const char *opt = NNG_OPT_SENDBUF;

	NUTS_PASS(nng_pub0_open(&pub));

	NUTS_PASS(nng_socket_set_int(pub, opt, 1));
	NUTS_FAIL(nng_socket_set_int(pub, opt, 0), NNG_EINVAL);
	NUTS_FAIL(nng_socket_set_int(pub, opt, -1), NNG_EINVAL);
	NUTS_FAIL(nng_socket_set_int(pub, opt, 1000000), NNG_EINVAL);
	NUTS_PASS(nng_socket_set_int(pub, opt, 3));
	NUTS_PASS(nng_socket_get_int(pub, opt, &v));
	NUTS_TRUE(v == 3);
	NUTS_FAIL(nng_socket_set_bool(pub, opt, true), NNG_EBADTYPE);
	NUTS_FAIL(nng_socket_get_bool(pub, opt, &b), NNG_EBADTYPE);

	NUTS_CLOSE(pub);
}

static void
test_pub_cooked(void)
{
	nng_socket s;
	bool       b;

	NUTS_PASS(nng_pub0_open(&s));
	NUTS_PASS(nng_socket_raw(s, &b));
	NUTS_TRUE(!b);
	NUTS_CLOSE(s);

	// raw pub only differs in the option setting
	NUTS_PASS(nng_pub0_open_raw(&s));
	NUTS_PASS(nng_socket_raw(s, &b));
	NUTS_TRUE(b);
	NUTS_CLOSE(s);
}

NUTS_TESTS = {
	{ "pub identity", test_pub_identity },
	{ "pub cannot recv", test_pub_cannot_recv },
	{ "put no context", test_pub_no_context },
	{ "pub not readable", test_pub_not_readable },
	{ "pub poll writeable", test_pub_poll_writeable },
	{ "pub validate peer", test_pub_validate_peer },
	{ "pub send queued", test_pub_send_queued },
	{ "pub send no pipes", test_pub_send_no_pipes },
	{ "pub send buf option", test_pub_send_buf_option },
	{ "pub cooked", test_pub_cooked },
	{ NULL, NULL },
};