8 #include "../src/meshlink.h"
11 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
21 static struct sync_flag poll_flag;
22 static size_t poll_len;
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);
27 set_sync_flag(&poll_flag, true);
30 static struct sync_flag receive_flag;
31 static size_t receive_len;
33 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
39 set_sync_flag(&receive_flag, true);
43 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
45 // Open two meshlink instances.
47 meshlink_handle_t *mesh_a, *mesh_b;
48 open_meshlink_pair(&mesh_a, &mesh_b, "channels_failure");
52 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
54 // Open a channel from a to b
56 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
59 meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
62 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
64 // Start both instances
66 start_meshlink_pair(mesh_a, mesh_b);
68 // Wait for the channel to be established
70 assert(wait_sync_flag(&poll_flag, 10));
71 assert(poll_len != 0);
75 // Set a very small timeout for channels to b.
77 meshlink_set_node_channel_timeout(mesh_a, b, 1);
79 // Stop mesh_b. We should get a notification that the channel has closed after a while.
81 meshlink_stop(mesh_b);
83 assert(wait_sync_flag(&receive_flag, 5));
84 assert(receive_len == 0);
86 meshlink_channel_close(mesh_a, channel);
88 // Try setting up a new channel while b is still down.
90 set_sync_flag(&poll_flag, false);
91 set_sync_flag(&receive_flag, false);
93 channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0);
96 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
98 assert(wait_sync_flag(&poll_flag, 5));
99 assert(poll_len == 0);
101 meshlink_channel_close(mesh_a, channel);
103 // Restart b and create a new channel
105 set_sync_flag(&poll_flag, false);
106 set_sync_flag(&receive_flag, false);
108 meshlink_set_node_channel_timeout(mesh_a, b, 60);
110 assert(meshlink_start(mesh_b));
112 channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
113 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
115 assert(wait_sync_flag(&poll_flag, 10));
116 assert(poll_len != 0);
118 // Close and reopen b, we should get a fast notification that the channel has been closed.
120 meshlink_close(mesh_b);
121 mesh_b = meshlink_open("channels_failure_conf.2", "b", "channels_failure", DEV_CLASS_BACKBONE);
123 assert(meshlink_start(mesh_b));
125 assert(wait_sync_flag(&receive_flag, 10));
126 assert(receive_len == 0);
130 close_meshlink_pair(mesh_a, mesh_b);