aboutsummaryrefslogtreecommitdiff
path: root/tests/bus.c
blob: 78740a37a5ed53256c29ca43f8444394f8c871a8 (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
//
// 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 "core/nng_impl.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({
	const char *addr = "inproc://test";

	atexit(nng_fini);

	Test("BUS pattern", {

		Convey("We can create a BUS socket", {
			nng_socket bus;

			So(nng_open(&bus, NNG_PROTO_BUS) == 0);

			Reset({
				nng_close(bus);
			})

			Convey("Protocols match", {
				So(nng_protocol(bus) == NNG_PROTO_BUS);
				So(nng_peer(bus) == NNG_PROTO_BUS);
			})
		})

		Convey("We can create a linked BUS topology", {
			nng_socket bus1;
			nng_socket bus2;
			nng_socket bus3;
			uint64_t rtimeo;

			So(nng_open(&bus1, NNG_PROTO_BUS) == 0);
			So(nng_open(&bus2, NNG_PROTO_BUS) == 0);
			So(nng_open(&bus3, NNG_PROTO_BUS) == 0);
		
			Reset({
				nng_close(bus1);
				nng_close(bus2);
				nng_close(bus3);
			})

			So(nng_listen(bus1, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(bus2, addr, NULL, NNG_FLAG_SYNCH) == 0);
			So(nng_dial(bus3, addr, NULL, NNG_FLAG_SYNCH) == 0);

			rtimeo = 50000;
			So(nng_setopt(bus1, NNG_OPT_RCVTIMEO, &rtimeo, sizeof (rtimeo)) == 0);
			rtimeo = 50000;
			So(nng_setopt(bus2, NNG_OPT_RCVTIMEO, &rtimeo, sizeof (rtimeo)) == 0);
			rtimeo = 50000;
			So(nng_setopt(bus3, NNG_OPT_RCVTIMEO, &rtimeo, sizeof (rtimeo)) == 0);

			Convey("Messages delivered", {
				nng_msg *msg;


				// This is just a poor man's sleep.
				So(nng_recvmsg(bus1, &msg, 0) == NNG_ETIMEDOUT);
				So(nng_recvmsg(bus2, &msg, 0) == NNG_ETIMEDOUT);
				So(nng_recvmsg(bus3, &msg, 0) == NNG_ETIMEDOUT);
				
				So(nng_msg_alloc(&msg, 0) == 0);
				APPENDSTR(msg, "99bits");
				So(nng_sendmsg(bus2, msg, 0) == 0);

				So(nng_recvmsg(bus1, &msg, 0) == 0);
				CHECKSTR(msg, "99bits");
				nng_msg_free(msg);
				So(nng_recvmsg(bus3, &msg, 0) == NNG_ETIMEDOUT);

				So(nng_msg_alloc(&msg, 0) == 0);
				APPENDSTR(msg, "onthe");
				So(nng_sendmsg(bus1, msg, 0) == 0);

				So(nng_recvmsg(bus2, &msg, 0) == 0);
				CHECKSTR(msg, "onthe");
				nng_msg_free(msg);

				So(nng_recvmsg(bus3, &msg, 0) == 0);
				CHECKSTR(msg, "onthe");
				nng_msg_free(msg);
			})
		})
	})
})