aboutsummaryrefslogtreecommitdiff
path: root/tests/aio.c
blob: fc5d4bafffaa596c8f0e59cb48f8be3de3e26a65 (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
//
// 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 <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)

void
cbdone(void *p)
{
	(*(int *) p)++;
}

Main({

	Test("AIO operations", {
		const char *addr = "inproc://aio";

		Convey("Given a connected pair of sockets", {
			nng_socket s1;
			nng_socket s2;
			nng_aio *  txaio;
			nng_aio *  rxaio;
			int        txdone = 0;
			int        rxdone = 0;
			nng_msg *  m;

			So(nng_pair1_open(&s1) == 0);
			So(nng_pair1_open(&s2) == 0);

			So(nng_listen(s1, addr, NULL, 0) == 0);
			So(nng_dial(s2, addr, NULL, 0) == 0);

			So(nng_aio_alloc(&rxaio, cbdone, &rxdone) == 0);
			So(nng_aio_alloc(&txaio, cbdone, &txdone) == 0);

			Reset({
				nng_aio_free(rxaio);
				nng_aio_free(txaio);
				nng_close(s1);
				nng_close(s2);
			});

			nng_aio_set_timeout(rxaio, 100);
			nng_aio_set_timeout(txaio, 100);

			So(nng_msg_alloc(&m, 0) == 0);
			APPENDSTR(m, "hello");

			nng_recv_aio(s2, rxaio);

			nng_aio_set_msg(txaio, m);
			nng_send_aio(s1, txaio);

			nng_aio_wait(rxaio);

			So(nng_aio_result(rxaio) == 0);
			So(nng_aio_result(txaio) == 0);

			So((m = nng_aio_get_msg(rxaio)) != NULL);
			CHECKSTR(m, "hello");

			nng_msg_free(m);

			So(rxdone == 1);
			So(txdone == 1);
		});

		Convey("Failure modes work", {
			nng_socket s;
			nng_aio *  a;
			int        done = 0;

			So(nng_pair1_open(&s) == 0);

			So(nng_aio_alloc(&a, cbdone, &done) == 0);

			Reset({
				nng_aio_free(a);
				nng_close(s);
			});

			Convey("Explicit timeout works", {
				nng_aio_set_timeout(a, 40);
				nng_recv_aio(s, a);
				nng_aio_wait(a);
				So(done == 1);
				So(nng_aio_result(a) == NNG_ETIMEDOUT);
			});
			Convey("Default timeout works", {
				So(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 40) ==
				    0);
				nng_recv_aio(s, a);
				nng_aio_wait(a);
				So(done == 1);
				So(nng_aio_result(a) == NNG_ETIMEDOUT);
			});
			Convey("Zero timeout works", {
				nng_aio_set_timeout(a, NNG_DURATION_ZERO);
				nng_recv_aio(s, a);
				nng_aio_wait(a);
				So(done == 1);
				So(nng_aio_result(a) == NNG_ETIMEDOUT);
			});
			Convey("Cancellation works", {
				nng_aio_set_timeout(a, NNG_DURATION_INFINITE);
				nng_recv_aio(s, a);
				nng_aio_cancel(a);
				nng_aio_wait(a);
				So(done == 1);
				So(nng_aio_result(a) == NNG_ECANCELED);
			})

		});
	});

	nng_fini();
})