summaryrefslogtreecommitdiff
path: root/tests/reconnect.c
blob: 6e7ab79f29a3332dc840d9f3015d3afa5d150019 (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
//
// 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 "supplemental/util/platform.h"

#include "stubs.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)

TestMain("Reconnect works", {
	atexit(nng_fini);
	const char *addr = "inproc://test";
	Convey("We can create a pipeline", {
		nng_socket push;
		nng_socket pull;

		So(nng_push_open(&push) == 0);
		So(nng_pull_open(&pull) == 0);

		Reset({
			nng_close(push);
			nng_close(pull);
		});

		So(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10) == 0);
		So(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10) == 0);

		Convey("Dialing before listening works", {
			So(nng_dial(push, addr, NULL, NNG_FLAG_NONBLOCK) == 0);
			nng_msleep(100);
			So(nng_listen(pull, addr, NULL, 0) == 0);

			Convey("We can send a frame", {
				nng_msg *msg;

				nng_msleep(100);
				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("Reconnection works", {
			nng_listener l;
			So(nng_dial(push, addr, NULL, NNG_FLAG_NONBLOCK) == 0);
			So(nng_listen(pull, addr, &l, 0) == 0);
			nng_msleep(100);
			nng_listener_close(l);
			So(nng_listen(pull, addr, NULL, 0) == 0);

			Convey("They still exchange frames", {
				nng_msg *msg;
				nng_pipe p1;

				nng_msleep(100);
				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");
				p1 = nng_msg_get_pipe(msg);
				nng_msg_free(msg);

				Convey("Even after pipe close", {
					nng_pipe p2;

					nng_pipe_close(p1);
					nng_msleep(100);
					So(nng_msg_alloc(&msg, 0) == 0);
					APPENDSTR(msg, "again");
					So(nng_sendmsg(push, msg, 0) == 0);
					msg = NULL;
					So(nng_recvmsg(pull, &msg, 0) == 0);
					So(msg != NULL);
					CHECKSTR(msg, "again");
					p2 = nng_msg_get_pipe(msg);
					nng_msg_free(msg);
					So(p2 != p1);
				});
			});
		});
	});
});