]> git.meshlink.io Git - meshlink/blob - test/channels-aio.c
Don't use assert() to check the results of pthread_*() calls.
[meshlink] / test / channels-aio.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11
12 #include "meshlink.h"
13 #include "utils.h"
14
15 static const size_t size = 25000000; // size of data to transfer
16 static const size_t smallsize = 100000; // size of the data to transfer without AIO
17 static const size_t nchannels = 4; // number of simultaneous channels
18
19 struct aio_info {
20         int callbacks;
21         size_t size;
22         struct timespec ts;
23         struct sync_flag flag;
24 };
25
26 struct channel_info {
27         char *data;
28         struct aio_info aio_infos[2];
29 };
30
31 static size_t b_received_len;
32 static struct timespec b_received_ts;
33 static struct sync_flag b_received_flag;
34
35 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
36         (void)mesh;
37         (void)channel;
38         (void)data;
39         (void)len;
40
41         struct aio_info *info = priv;
42         clock_gettime(CLOCK_MONOTONIC, &info->ts);
43         info->callbacks++;
44         info->size += len;
45         set_sync_flag(&info->flag, true);
46 }
47
48 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
49         (void)mesh;
50         (void)channel;
51         (void)port;
52         (void)data;
53         (void)len;
54
55         return false;
56 }
57
58 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
59         (void)mesh;
60         (void)channel;
61         (void)data;
62
63         b_received_len += len;
64
65         if(b_received_len >= smallsize) {
66                 clock_gettime(CLOCK_MONOTONIC, &b_received_ts);
67                 set_sync_flag(&b_received_flag, true);
68         }
69 }
70
71 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
72         assert(port && port <= nchannels + 1);
73         assert(!data);
74         assert(!len);
75
76         if(port <= nchannels) {
77                 struct channel_info *infos = mesh->priv;
78                 struct channel_info *info = &infos[port - 1];
79
80                 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]));
81                 assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]));
82         } else {
83                 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
84         }
85
86         return true;
87 }
88
89 int main(void) {
90         init_sync_flag(&b_received_flag);
91
92         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
93
94         // Prepare data buffers
95
96         char *outdata = malloc(size);
97         assert(outdata);
98
99         for(size_t i = 0; i < size; i++) {
100                 outdata[i] = i;
101         }
102
103         struct channel_info in_infos[nchannels];
104
105         struct channel_info out_infos[nchannels];
106
107         memset(in_infos, 0, sizeof(in_infos));
108
109         memset(out_infos, 0, sizeof(out_infos));
110
111         for(size_t i = 0; i < nchannels; i++) {
112                 init_sync_flag(&in_infos[i].aio_infos[0].flag);
113                 init_sync_flag(&in_infos[i].aio_infos[1].flag);
114                 init_sync_flag(&out_infos[i].aio_infos[0].flag);
115                 init_sync_flag(&out_infos[i].aio_infos[1].flag);
116
117                 in_infos[i].data = malloc(size);
118                 assert(in_infos[i].data);
119                 out_infos[i].data = outdata;
120         }
121
122         // Open two new meshlink instance.
123
124         meshlink_handle_t *mesh_a, *mesh_b;
125         open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio");
126
127         // Set the callbacks.
128
129         mesh_b->priv = in_infos;
130
131         meshlink_set_channel_accept_cb(mesh_a, reject_cb);
132         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
133
134         // Start both instances
135
136         start_meshlink_pair(mesh_a, mesh_b);
137
138         // Open channels from a to b.
139
140         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
141         assert(b);
142
143         meshlink_channel_t *channels[nchannels + 1];
144
145         for(size_t i = 0; i < nchannels + 1; i++) {
146                 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
147                 assert(channels[i]);
148         }
149
150         // Send a large buffer of data on each channel.
151
152         for(size_t i = 0; i < nchannels; i++) {
153                 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata, size / 3, aio_cb, &out_infos[i].aio_infos[0]));
154                 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata + size / 3, size - size / 3, aio_cb, &out_infos[i].aio_infos[1]));
155         }
156
157         // Send a little bit on the last channel using a regular send
158
159         assert(meshlink_channel_send(mesh_a, channels[nchannels], outdata, smallsize) == (ssize_t)smallsize);
160
161         // Wait for everyone to finish.
162
163         assert(wait_sync_flag(&b_received_flag, 10));
164
165         for(size_t i = 0; i < nchannels; i++) {
166                 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
167                 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
168                 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
169                 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
170         }
171
172         // Check that everything is correct.
173
174         assert(b_received_len == smallsize);
175
176         for(size_t i = 0; i < nchannels; i++) {
177                 // Data should be transferred intact.
178                 assert(!memcmp(in_infos[i].data, out_infos[i].data, size));
179
180                 // One callback for each AIO buffer.
181                 assert(out_infos[i].aio_infos[0].callbacks == 1);
182                 assert(out_infos[i].aio_infos[1].callbacks == 1);
183                 assert(in_infos[i].aio_infos[0].callbacks == 1);
184                 assert(in_infos[i].aio_infos[1].callbacks == 1);
185
186                 // Correct size sent and received.
187                 assert(out_infos[i].aio_infos[0].size == size / 3);
188                 assert(out_infos[i].aio_infos[1].size == size - size / 3);
189                 assert(in_infos[i].aio_infos[0].size == size / 4);
190                 assert(in_infos[i].aio_infos[1].size == size - size / 4);
191
192                 // First batch of data should all be sent and received before the second batch
193                 for(size_t j = 0; j < nchannels; j++) {
194                         assert(timespec_lt(&out_infos[i].aio_infos[0].ts, &out_infos[j].aio_infos[1].ts));
195                         assert(timespec_lt(&in_infos[i].aio_infos[0].ts, &in_infos[j].aio_infos[1].ts));
196                 }
197
198                 // The non-AIO transfer should have completed before everything else
199                 assert(!timespec_lt(&out_infos[i].aio_infos[0].ts, &b_received_ts));
200                 assert(!timespec_lt(&in_infos[i].aio_infos[0].ts, &b_received_ts));
201         }
202
203         // Clean up.
204
205         close_meshlink_pair(mesh_a, mesh_b);
206 }