aboutsummaryrefslogtreecommitdiff
path: root/tests/reqstress.c
blob: 04cf73ab9c0d3208d7a12b8de3ff9473361fef66 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// 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/bus0/bus.h"
#include "protocol/reqrep0/rep.h"
#include "protocol/reqrep0/req.h"
#include "supplemental/util/platform.h"
#include "transport/inproc/inproc.h"
#include "transport/ipc/ipc.h"
#include "transport/tcp/tcp.h"

#include <stdio.h>
#include <string.h>
#include <time.h>

#include "stubs.h"

#ifdef NDEBUG
#define dprintf(...)
#else
#define dprintf printf
#endif

static int next_port = 20000; // port number kind of.

char tcp4_template[]   = "tcp://127.0.0.1:%d";
char tcp6_template[]   = "tcp://[::1]:%d";
char inproc_template[] = "inproc://nng_reqstress_%d";
char ipc_template[]    = "ipc:///tmp/nng_reqstress_%d";
char ws_template[]     = "ws://127.0.0.1:%d/nng_reqstress";

char *templates[] = {
#ifdef NNG_TRANSPORT_TCP
	tcp4_template,
#endif
// It would be nice to test TCPv6, but CI doesn't support it.
// Outside of CI, it does seem to work though.
#ifdef NNG_TEST_TCPV6
	tcp6_template,
#endif
#ifdef NNG_TRANSPORT_INPROC
	inproc_template,
#endif
#ifdef NNG_TRANSPORT_IPC
	ipc_template,
#endif
#ifdef NNG_TRANSPORT_WS
	ws_template,
#endif
};

#define NTEMPLATES (sizeof(templates) / sizeof(templates[0]))

char **addresses;
int    naddresses;
int    allocaddrs;

typedef struct test_case {
	nng_socket  socket;
	const char *name;
	nng_thread *thr;
	int         nrecv;
	int         nsend;
	int         nfail;
} test_case;

static test_case *cases;
int               ncases;
int               curcase;

void
fatal(const char *msg, int rv)
{
	fprintf(stderr, "%s: %s\n", msg, nng_strerror(rv));
	abort();
}

void
error(test_case *c, const char *msg, int rv)
{
	if (rv == NNG_ECLOSED) {
		return;
	}
	fprintf(
	    stderr, "%s: %s: %s (%d)\n", c->name, msg, nng_strerror(rv), rv);
	c->nfail++;
}

// Get a random address -- TCP, IP, whatever.
char *
getaddr(char *buf)
{
	int i;
	i = rand() % NTEMPLATES;

	snprintf(buf, NNG_MAXADDRLEN, templates[i], next_port++);
	return (buf);
}

// Request/Reply test.  For this test, we open a server socket,
// and bind it to each protocol.  Then we run a bunch of clients
// against it.  The

// Simple rep echo server.
static void
rep_server(void *arg)
{
	test_case *c = arg;
	for (;;) {
		int        rv;
		nng_msg *  msg;
		nng_socket rep = c->socket;

		if ((rv = nng_recvmsg(rep, &msg, 0)) != 0) {
			error(c, "recvmsg", rv);
			return;
		}
		c->nrecv++;
		if ((rv = nng_sendmsg(rep, msg, 0)) != 0) {
			nng_msg_free(msg);
			error(c, "sendmsg", rv);
			return;
		}
		c->nsend++;
	}
}

static void
req_client(void *arg)
{
	test_case *c = arg;
	for (;;) {
		int        rv;
		nng_socket req = c->socket;
		int        num = 0;
		nng_msg *  msg;
		char       buf[32];

		(void) snprintf(buf, sizeof(buf), "%d-%d", req, num++);

		nng_msleep(rand() % 10);

		if (((rv = nng_msg_alloc(&msg, 0)) != 0) ||
		    ((rv = nng_msg_append(msg, buf, strlen(buf) + 1)) != 0)) {
			error(c, "alloc fail", rv);
			return;
		}

		if ((rv = nng_sendmsg(req, msg, 0)) != 0) {
			error(c, "sendmsg", rv);
			return;
		}
		c->nsend++;
		if ((rv = nng_recvmsg(req, &msg, 0)) != 0) {
			error(c, "recvmsg", rv);
			return;
		}
		c->nrecv++;

		if (strcmp(nng_msg_body(msg), buf) != 0) {
			error(c, "mismatched message", NNG_EINTERNAL);
			return;
		}

		nng_msg_free(msg);
	}
}

void
reqrep_test(int ntests)
{
	test_case *srv, *cli;
	int        i;
	char       addr[NNG_MAXADDRLEN];
	int        rv;

	if (ntests < 2) {
		// Need a client *and* a server.
		return;
	}

	srv       = &cases[curcase++];
	srv->name = "rep";

	if ((rv = nng_rep0_open(&srv->socket)) != 0) {
		fatal("nng_rep0_open", rv);
	}

	if ((rv = nng_thread_create(&srv->thr, rep_server, srv)) != 0) {
		fatal("nng_thread_create", rv);
	}

	for (i = 1; i < ntests; i++) {
		cli = &cases[curcase++];
		if ((rv = nng_req0_open(&cli->socket)) != 0) {
			fatal("nng_req0_open", rv);
		}

		cli->name = "req";
		getaddr(addr);
		dprintf("DOING reqrep0 (req %d rep %d) address: %s\n",
		    cli->socket, srv->socket, addr);

		if ((rv = nng_listen(srv->socket, addr, NULL, 0)) != 0) {
			fatal("nng_listen", rv);
		}
		if ((rv = nng_dial(cli->socket, addr, NULL, 0)) != 0) {
			fatal("nng_dial", rv);
		}

		if ((rv = nng_thread_create(&cli->thr, req_client, cli)) !=
		    0) {
			fatal("nng_thread_create", rv);
		}
	}
}

Main({
	int i;

	// Each run should truly be random.
	srand((int) time(NULL));

	// Reduce the likelihood of address in use conflicts between
	// subsequent runs.
	next_port += (rand() % 100) * 100;

	// We have to keep this relatively low because some platforms
	// don't support large numbers of threads.
	ncases = 32;

	i = ncases;

	cases = calloc(ncases, sizeof(test_case));
	while (i > 1) {
		int x = rand() % NTEMPLATES;
		if (x > i) {
			x = i;
		}
		reqrep_test(x);
		i -= x;
	}

	dprintf("WAITING for 30 sec...\n");
	nng_msleep(30000); // sleep 30 sec
	nng_closeall();

	Test("Req/Rep Stress", {
		Convey("All tests worked", {
			for (i = 0; i < ncases; i++) {
				if (cases[i].thr != NULL) {
					nng_thread_destroy(cases[i].thr);
					dprintf(
					    "RESULT socket %d (%s) sent %d "
					    "recd "
					    "%d fail %d\n",
					    cases[i].socket, cases[i].name,
					    cases[i].nsend, cases[i].nrecv,
					    cases[i].nfail);
					So(cases[i].nfail == 0);
					So(cases[i].nsend > 0 ||
					    cases[i].nrecv > 0);
				}
			}
		});
	});
});