summaryrefslogtreecommitdiff
path: root/tests/device.c
blob: b1f9712355e21072e9f816be009907782365ac50 (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
//
// 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/pair1/pair.h"
#include "stubs.h"
#include "supplemental/util/platform.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)

struct dev_data {
	nng_socket s1;
	nng_socket s2;
};

void
dodev(void *arg)
{
	struct dev_data *d = arg;

	nng_device(d->s1, d->s2);
}

#define SECOND(x) ((x) *1000)

Main({

	Test("PAIRv1 device", {
		const char *addr1 = "inproc://dev1";
		const char *addr2 = "inproc://dev2";

		Convey("We cannot create cooked mode device", {
			nng_socket s1;
			So(nng_pair1_open(&s1) == 0);
			Reset({ nng_close(s1); });
			So(nng_device(s1, s1) == NNG_EINVAL);
		});
		Convey("We can create a PAIRv1 device", {
			nng_socket   dev1;
			nng_socket   dev2;
			nng_socket   end1;
			nng_socket   end2;
			nng_duration tmo;
			nng_msg *    msg;
			nng_thread * thr;

			So(nng_pair1_open_raw(&dev1) == 0);
			So(nng_pair1_open_raw(&dev2) == 0);

			struct dev_data ddata;
			ddata.s1 = dev1;
			ddata.s2 = dev2;

			So(nng_thread_create(&thr, dodev, &ddata) == 0);
			Reset({
				nng_close(dev1);
				nng_close(dev2);
				nng_thread_destroy(thr);
			});

			So(nng_listen(dev1, addr1, NULL, 0) == 0);
			So(nng_listen(dev2, addr2, NULL, 0) == 0);

			So(nng_pair1_open(&end1) == 0);
			So(nng_pair1_open(&end2) == 0);

			So(nng_dial(end1, addr1, NULL, 0) == 0);
			So(nng_dial(end2, addr2, NULL, 0) == 0);

			tmo = SECOND(1);
			So(nng_setopt_ms(end1, NNG_OPT_RECVTIMEO, tmo) == 0);
			So(nng_setopt_ms(end2, NNG_OPT_RECVTIMEO, tmo) == 0);

			nng_msleep(100);
			Convey("Device can send and receive", {

				So(nng_msg_alloc(&msg, 0) == 0);
				APPENDSTR(msg, "ALPHA");
				So(nng_sendmsg(end1, msg, 0) == 0);
				So(nng_recvmsg(end2, &msg, 0) == 0);
				CHECKSTR(msg, "ALPHA");
				nng_msg_free(msg);

				So(nng_msg_alloc(&msg, 0) == 0);
				APPENDSTR(msg, "OMEGA");

				So(nng_sendmsg(end2, msg, 0) == 0);
				So(nng_recvmsg(end1, &msg, 0) == 0);

				CHECKSTR(msg, "OMEGA");
				nng_msg_free(msg);
			});
		});
	});

	nng_fini();
})