]> git.meshlink.io Git - meshlink/blob - test/channels-aio-fd.c
Refactor the non-blackbox test suite.
[meshlink] / test / channels-aio-fd.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 #include <limits.h>
8
9 #include "meshlink.h"
10 #include "utils.h"
11
12 static const size_t size = 1024 * 1024; // size of data to transfer
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         FILE *file;
24         struct aio_info aio_infos[2];
25 };
26
27 static void aio_fd_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, void *priv) {
28         (void)mesh;
29         (void)channel;
30         (void)fd;
31         (void)len;
32
33         struct aio_info *info = priv;
34         gettimeofday(&info->tv, NULL);
35         info->callbacks++;
36         info->size += len;
37         set_sync_flag(&info->flag, true);
38 }
39
40 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
41         (void)mesh;
42         (void)channel;
43         (void)port;
44         (void)data;
45         (void)len;
46
47         return false;
48 }
49
50 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
51         assert(port && port <= nchannels);
52         assert(!data);
53         assert(!len);
54
55         struct channel_info *infos = mesh->priv;
56         struct channel_info *info = &infos[port - 1];
57
58         assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size / 4, aio_fd_cb, &info->aio_infos[0]));
59         assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size - size / 4, aio_fd_cb, &info->aio_infos[1]));
60
61         return true;
62 }
63
64 int main(int argc, char *argv[]) {
65         (void)argc;
66         (void)argv;
67
68         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
69
70         // Prepare file
71
72         char *outdata = malloc(size);
73         assert(outdata);
74
75         for(size_t i = 0; i < size; i++) {
76                 // Human readable output
77                 outdata[i] = i % 96 ? i % 96 + 32 : '\n';
78         }
79
80         FILE *file = fopen("channels_aio_fd.in", "w");
81         assert(file);
82         assert(fwrite(outdata, size, 1, file) == 1);
83         assert(fclose(file) == 0);
84
85         struct channel_info in_infos[nchannels];
86         struct channel_info out_infos[nchannels];
87
88         memset(in_infos, 0, sizeof(in_infos));
89         memset(out_infos, 0, sizeof(out_infos));
90
91         for(size_t i = 0; i < nchannels; i++) {
92                 char filename[PATH_MAX];
93                 snprintf(filename, sizeof(filename), "channels_aio_fd.out%d", (int)i);
94                 in_infos[i].file = fopen(filename, "w");
95                 assert(in_infos[i].file);
96                 out_infos[i].file = fopen("channels_aio_fd.in", "r");
97                 assert(out_infos[i].file);
98         }
99
100         // Open two new meshlink instance.
101
102         meshlink_handle_t *mesh_a, *mesh_b;
103         open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio_fd");
104
105         mesh_b->priv = in_infos;
106
107         meshlink_enable_discovery(mesh_a, false);
108         meshlink_enable_discovery(mesh_b, false);
109
110         // Set the callbacks.
111
112         meshlink_set_channel_accept_cb(mesh_a, reject_cb);
113         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
114
115         // Start both instances
116
117         start_meshlink_pair(mesh_a, mesh_b);
118
119         // Open channels from a to b.
120
121         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
122         assert(b);
123
124         meshlink_channel_t *channels[nchannels];
125
126         for(size_t i = 0; i < nchannels; i++) {
127                 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
128                 assert(channels[i]);
129         }
130
131         // Send a large buffer of data on each channel.
132
133         for(size_t i = 0; i < nchannels; i++) {
134                 assert(meshlink_channel_aio_fd_send(mesh_a, channels[i], fileno(out_infos[i].file), size / 3, aio_fd_cb, &out_infos[i].aio_infos[0]));
135                 assert(meshlink_channel_aio_fd_send(mesh_a, channels[i], fileno(out_infos[i].file), size - size / 3, aio_fd_cb, &out_infos[i].aio_infos[1]));
136         }
137
138         // Wait for everyone to finish.
139
140         for(size_t i = 0; i < nchannels; i++) {
141                 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
142                 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
143                 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
144                 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
145         }
146
147         // Check that everything is correct.
148
149         for(size_t i = 0; i < nchannels; i++) {
150                 assert(fclose(in_infos[i].file) == 0);
151                 assert(fclose(out_infos[i].file) == 0);
152
153                 // One callback for each AIO buffer.
154                 assert(out_infos[i].aio_infos[0].callbacks == 1);
155                 assert(out_infos[i].aio_infos[1].callbacks == 1);
156                 assert(in_infos[i].aio_infos[0].callbacks == 1);
157                 assert(in_infos[i].aio_infos[1].callbacks == 1);
158
159                 // Correct size sent and received.
160                 assert(out_infos[i].aio_infos[0].size == size / 3);
161                 assert(out_infos[i].aio_infos[1].size == size - size / 3);
162                 assert(in_infos[i].aio_infos[0].size == size / 4);
163                 assert(in_infos[i].aio_infos[1].size == size - size / 4);
164
165                 // First batch of data should all be sent and received before the second batch
166                 for(size_t j = 0; j < nchannels; j++) {
167                         assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
168                         assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
169                 }
170
171                 // Files should be identical
172                 char command[PATH_MAX];
173                 snprintf(command, sizeof(command), "cmp channels_aio_fd.in channels_aio_fd.out%d", (int)i);
174                 assert(system(command) == 0);
175
176         }
177
178         // Clean up.
179
180         close_meshlink_pair(mesh_a, mesh_b);
181 }