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