X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test%2Fchannels-failure.c;h=9829b4501fb4596b0b5022dcdb292e2799b171c4;hb=HEAD;hp=34018e1124e472162cea5ec103068c5aa0da8d32;hpb=9a2520c36431a8a5fd90451e97f488c22f4decc5;p=meshlink diff --git a/test/channels-failure.c b/test/channels-failure.c index 34018e11..1ac50d31 100644 --- a/test/channels-failure.c +++ b/test/channels-failure.c @@ -1,3 +1,7 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif + #include #include #include @@ -8,6 +12,13 @@ #include "../src/meshlink.h" #include "utils.h" +static bool listen_cb(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port) { + (void)mesh; + (void)node; + + return port == 7; +} + static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { (void)mesh; (void)channel; @@ -39,7 +50,10 @@ static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, con set_sync_flag(&receive_flag, true); } -int main() { +int main(void) { + init_sync_flag(&poll_flag); + init_sync_flag(&receive_flag); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Open two meshlink instances. @@ -49,6 +63,7 @@ int main() { // Set the callbacks. + meshlink_set_channel_listen_cb(mesh_b, listen_cb); meshlink_set_channel_accept_cb(mesh_b, accept_cb); // Open a channel from a to b @@ -72,28 +87,70 @@ int main() { sleep(1); + // Set a very small timeout for channels to b. + + meshlink_set_node_channel_timeout(mesh_a, b, 1); + // Stop mesh_b. We should get a notification that the channel has closed after a while. meshlink_stop(mesh_b); - assert(wait_sync_flag(&receive_flag, 70)); + assert(wait_sync_flag(&receive_flag, 5)); assert(receive_len == 0); meshlink_channel_close(mesh_a, channel); // Try setting up a new channel while b is still down. - poll_flag.flag = false; - receive_flag.flag = false; + reset_sync_flag(&poll_flag); + reset_sync_flag(&receive_flag); channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0); assert(channel); meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb); - assert(wait_sync_flag(&poll_flag, 70)); + assert(wait_sync_flag(&poll_flag, 5)); assert(poll_len == 0); + meshlink_channel_close(mesh_a, channel); + + // Restart b and create a new channel to the wrong port + + reset_sync_flag(&poll_flag); + reset_sync_flag(&receive_flag); + + meshlink_set_node_channel_timeout(mesh_a, b, 60); + + assert(meshlink_start(mesh_b)); + + channel = meshlink_channel_open(mesh_a, b, 42, receive_cb, NULL, 0); + meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb); + assert(channel); + assert(wait_sync_flag(&poll_flag, 10)); + assert(poll_len == 0); + meshlink_channel_close(mesh_a, channel); + + // Create a channel that will be accepted + + reset_sync_flag(&poll_flag); + + channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0); + meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb); + assert(channel); + assert(wait_sync_flag(&poll_flag, 10)); + assert(poll_len != 0); + + // Close and reopen b, we should get a fast notification that the channel has been closed. + + meshlink_close(mesh_b); + mesh_b = meshlink_open("channels_failure_conf.2", "b", "channels_failure", DEV_CLASS_BACKBONE); + assert(mesh_b); + assert(meshlink_start(mesh_b)); + + assert(wait_sync_flag(&receive_flag, 10)); + assert(receive_len == 0); + // Clean up. close_meshlink_pair(mesh_a, mesh_b);