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