]> git.meshlink.io Git - meshlink/blob - test/channels-failure.c
Refactor the non-blackbox test suite.
[meshlink] / test / channels-failure.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <assert.h>
7
8 #include "../src/meshlink.h"
9 #include "utils.h"
10
11 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
12         (void)mesh;
13         (void)channel;
14         (void)port;
15         (void)data;
16         (void)len;
17
18         return true;
19 }
20
21 static struct sync_flag poll_flag;
22 static size_t poll_len;
23
24 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
25         meshlink_set_channel_poll_cb(mesh, channel, NULL);
26         poll_len = len;
27         set_sync_flag(&poll_flag, true);
28 }
29
30 static struct sync_flag receive_flag;
31 static size_t receive_len;
32
33 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
34         (void)mesh;
35         (void)channel;
36         (void)data;
37
38         receive_len = len;
39         set_sync_flag(&receive_flag, true);
40 }
41
42 int main() {
43         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
44
45         // Open two meshlink instances.
46
47         meshlink_handle_t *mesh_a, *mesh_b;
48         open_meshlink_pair(&mesh_a, &mesh_b, "channels_failure");
49
50         // Set the callbacks.
51
52         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
53
54         // Open a channel from a to b
55
56         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
57         assert(b);
58
59         meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
60         assert(channel);
61
62         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
63
64         // Start both instances
65
66         start_meshlink_pair(mesh_a, mesh_b);
67
68         // Wait for the channel to be established
69
70         assert(wait_sync_flag(&poll_flag, 10));
71         assert(poll_len != 0);
72
73         sleep(1);
74
75         // Stop mesh_b. We should get a notification that the channel has closed after a while.
76
77         meshlink_stop(mesh_b);
78
79         assert(wait_sync_flag(&receive_flag, 70));
80         assert(receive_len == 0);
81
82         meshlink_channel_close(mesh_a, channel);
83
84         // Try setting up a new channel while b is still down.
85
86         poll_flag.flag = false;
87         receive_flag.flag = false;
88
89         channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0);
90         assert(channel);
91
92         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
93
94         assert(wait_sync_flag(&poll_flag, 70));
95         assert(poll_len == 0);
96
97         // Clean up.
98
99         close_meshlink_pair(mesh_a, mesh_b);
100 }