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