]> git.meshlink.io Git - meshlink/blob - test/channels-failure.c
Remove redundant call to add_local_addresses().
[meshlink] / test / channels-failure.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <assert.h>
11
12 #include "../src/meshlink.h"
13 #include "utils.h"
14
15 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
16         (void)mesh;
17         (void)channel;
18         (void)port;
19         (void)data;
20         (void)len;
21
22         return true;
23 }
24
25 static struct sync_flag poll_flag;
26 static size_t poll_len;
27
28 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
29         meshlink_set_channel_poll_cb(mesh, channel, NULL);
30         poll_len = len;
31         set_sync_flag(&poll_flag, true);
32 }
33
34 static struct sync_flag receive_flag;
35 static size_t receive_len;
36
37 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
38         (void)mesh;
39         (void)channel;
40         (void)data;
41
42         receive_len = len;
43         set_sync_flag(&receive_flag, true);
44 }
45
46 int main() {
47         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
48
49         // Open two meshlink instances.
50
51         meshlink_handle_t *mesh_a, *mesh_b;
52         open_meshlink_pair(&mesh_a, &mesh_b, "channels_failure");
53
54         // Set the callbacks.
55
56         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
57
58         // Open a channel from a to b
59
60         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
61         assert(b);
62
63         meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
64         assert(channel);
65
66         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
67
68         // Start both instances
69
70         start_meshlink_pair(mesh_a, mesh_b);
71
72         // Wait for the channel to be established
73
74         assert(wait_sync_flag(&poll_flag, 10));
75         assert(poll_len != 0);
76
77         sleep(1);
78
79         // Set a very small timeout for channels to b.
80
81         meshlink_set_node_channel_timeout(mesh_a, b, 1);
82
83         // Stop mesh_b. We should get a notification that the channel has closed after a while.
84
85         meshlink_stop(mesh_b);
86
87         assert(wait_sync_flag(&receive_flag, 5));
88         assert(receive_len == 0);
89
90         meshlink_channel_close(mesh_a, channel);
91
92         // Try setting up a new channel while b is still down.
93
94         set_sync_flag(&poll_flag, false);
95         set_sync_flag(&receive_flag, false);
96
97         channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0);
98         assert(channel);
99
100         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
101
102         assert(wait_sync_flag(&poll_flag, 5));
103         assert(poll_len == 0);
104
105         meshlink_channel_close(mesh_a, channel);
106
107         // Restart b and create a new channel
108
109         set_sync_flag(&poll_flag, false);
110         set_sync_flag(&receive_flag, false);
111
112         meshlink_set_node_channel_timeout(mesh_a, b, 60);
113
114         assert(meshlink_start(mesh_b));
115
116         channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
117         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
118         assert(channel);
119         assert(wait_sync_flag(&poll_flag, 10));
120         assert(poll_len != 0);
121
122         // Close and reopen b, we should get a fast notification that the channel has been closed.
123
124         meshlink_close(mesh_b);
125         mesh_b = meshlink_open("channels_failure_conf.2", "b", "channels_failure", DEV_CLASS_BACKBONE);
126         assert(mesh_b);
127         assert(meshlink_start(mesh_b));
128
129         assert(wait_sync_flag(&receive_flag, 10));
130         assert(receive_len == 0);
131
132         // Clean up.
133
134         close_meshlink_pair(mesh_a, mesh_b);
135 }