X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test%2Ftrio.c;h=bdb3904e37e4540faf734e4bd5c7c145f3eddd35;hb=4b6c01b1d5383b1a7417244a31ad4652aab2d5db;hp=bfeb4f6effecb6d384228981aa5d9cdc91c1f959;hpb=668664d0ea90dc81670cccd7b7d56b36b8360eaa;p=meshlink diff --git a/test/trio.c b/test/trio.c index bfeb4f6e..bdb3904e 100644 --- a/test/trio.c +++ b/test/trio.c @@ -1,6 +1,11 @@ #define _GNU_SOURCE +#ifdef NDEBUG +#undef NDEBUG +#endif + #include +#include #include #include #include @@ -11,35 +16,44 @@ #include "devtools.h" #include "utils.h" -static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { - static struct timeval tv0; - struct timeval tv; +static struct sync_flag received; +static struct sync_flag bar_learned_baz; +static struct sync_flag baz_learned_bar; + +static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { + (void)mesh; + (void)source; - if(tv0.tv_sec == 0) { - gettimeofday(&tv0, NULL); + if(len == 5 && !memcmp(data, "Hello", 5)) { + set_sync_flag(&received, true); } +} - gettimeofday(&tv, NULL); - fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000); +static void bar_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { + (void)mesh; + (void)reachable; - if(mesh) { - fprintf(stderr, "(%s) ", mesh->name); + if(!strcmp(node->name, "baz")) { + set_sync_flag(&bar_learned_baz, true); } - - fprintf(stderr, "[%d] %s\n", level, text); } -static bool received = false; +static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { + (void)mesh; + (void)reachable; -static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { - fprintf(stderr, "RECEIVED SOMETHING\n"); - - if(len == 5 && !memcmp(data, "Hello", 5)) { - received = true; + if(!strcmp(node->name, "bar")) { + set_sync_flag(&baz_learned_bar, true); } } -int main(int argc, char *argv[]) { +int main(void) { + init_sync_flag(&received); + init_sync_flag(&bar_learned_baz); + init_sync_flag(&baz_learned_bar); + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + // Create three instances. const char *name[3] = {"foo", "bar", "baz"}; @@ -47,12 +61,15 @@ int main(int argc, char *argv[]) { char *data[3]; for(int i = 0; i < 3; i++) { - char *path; - asprintf(&path, "trio_conf.%d", i); - assert(path); + char *path = NULL; + assert(asprintf(&path, "trio_conf.%d", i) != -1 && path); + assert(meshlink_destroy(path)); mesh[i] = meshlink_open(path, name[i], "trio", DEV_CLASS_BACKBONE); assert(mesh[i]); + free(path); + + assert(meshlink_set_canonical_address(mesh[i], meshlink_get_self(mesh[i]), "localhost", NULL)); data[i] = meshlink_export(mesh[i]); assert(data[i]); @@ -75,25 +92,39 @@ int main(int argc, char *argv[]) { // start the nodes + meshlink_set_node_status_cb(mesh[1], bar_status_cb); + meshlink_set_node_status_cb(mesh[2], baz_status_cb); + for(int i = 0; i < 3; i++) { - meshlink_start(mesh[i]); + free(data[i]); + assert(meshlink_start(mesh[i])); } // the nodes should now learn about each other - assert_after(meshlink_get_node(mesh[1], name[2]), 5); - assert_after(meshlink_get_node(mesh[2], name[1]), 5); + assert(wait_sync_flag(&bar_learned_baz, 5)); + assert(wait_sync_flag(&baz_learned_bar, 5)); // Send a packet, expect it is received meshlink_set_receive_cb(mesh[1], receive_cb); - assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15); + + for(int i = 0; i < 15; i++) { + assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5)); + + if(wait_sync_flag(&received, 1)) { + break; + } + } + + assert(wait_sync_flag(&received, 15)); // Check that the second and third node have autoconnected to each other devtool_edge_t *edges = NULL; size_t nedges = 0; assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15); + free(edges); // Stop the first node @@ -102,7 +133,17 @@ int main(int argc, char *argv[]) { // Communication should still be possible - assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15); + set_sync_flag(&received, false); + + for(int i = 0; i < 15; i++) { + assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5)); + + if(wait_sync_flag(&received, 1)) { + break; + } + } + + assert(wait_sync_flag(&received, 15)); // Stop the other nodes @@ -114,10 +155,8 @@ int main(int argc, char *argv[]) { // Start just the other two nodes - meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb); - for(int i = 1; i < 3; i++) { - meshlink_start(mesh[i]); + assert(meshlink_start(mesh[i])); } assert(meshlink_get_node(mesh[1], name[2])); @@ -125,8 +164,17 @@ int main(int argc, char *argv[]) { // Communication should still be possible - received = false; - assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25); + set_sync_flag(&received, false); + + for(int i = 0; i < 15; i++) { + assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5)); + + if(wait_sync_flag(&received, 1)) { + break; + } + } + + assert(wait_sync_flag(&received, 1)); // Clean up.