aboutsummaryrefslogtreecommitdiff
path: root/tests/pipeline.c
blob: d6af728f5054348d579c2f134804184db708c99c (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
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// 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 <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)

Main({
	int rv;
	const char *addr = "inproc://test";

	Test("PIPELINE (PUSH/PULL) pattern", {
		Convey("We can create a PUSH socket", {
			nng_socket *push;

			So(nng_open(&push, NNG_PROTO_PUSH) == 0);
			So(push != NULL);

			Reset({
				nng_close(push);
			})

			Convey("Protocols match", {
				So(nng_protocol(push) == NNG_PROTO_PUSH);
				So(nng_peer(push) == NNG_PROTO_PULL);
			})

			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_open(&pull, NNG_PROTO_PULL) == 0);
			So(pull != NULL);

			Reset({
				nng_close(pull);
			})

			Convey("Protocols match", {
				So(nng_protocol(pull) == NNG_PROTO_PULL);
				So(nng_peer(pull) == NNG_PROTO_PUSH);
			})

			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_open(&push, NNG_PROTO_PUSH) == 0);
			So(nng_open(&pull, NNG_PROTO_PULL) == 0);
			So(nng_open(&what, NNG_PROTO_PUSH) == 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, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(push, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(what, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_shutdown(what) == 0);

			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;
			uint64_t usecs;
			nng_socket *push;
			nng_socket *pull1;
			nng_socket *pull2;
			nng_socket *pull3;

			So(nng_open(&push, NNG_PROTO_PUSH) == 0);
			So(nng_open(&pull1, NNG_PROTO_PULL) == 0);
			So(nng_open(&pull2, NNG_PROTO_PULL) == 0);
			So(nng_open(&pull3, NNG_PROTO_PULL) == 0);

			Reset({
				nng_close(push);
				nng_close(pull1);
				nng_close(pull2);
				nng_close(pull3);
			})

			So(nng_msg_alloc(&abc, 0) == 0);
			APPENDSTR(abc, "abc");
			So(nng_msg_alloc(&def, 0) == 0);
			APPENDSTR(def, "def");

			usecs = 100000;
			So(nng_setopt(pull1, NNG_OPT_RCVTIMEO, &usecs, sizeof (usecs)) == 0);
			So(nng_setopt(pull2, NNG_OPT_RCVTIMEO, &usecs, sizeof (usecs)) == 0);
			So(nng_setopt(pull3, NNG_OPT_RCVTIMEO, &usecs, sizeof (usecs)) == 0);
			So(nng_listen(push, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(pull1, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(pull2, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(pull3, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_shutdown(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.)

			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);
		})
	})
})