X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=test%2Ftrio2.c;h=c21cba223a5b546bbc6838963bcb02cfc2d39304;hp=b55fde7348fbe5a256114996ccb30b17c00d8d77;hb=9a2520c36431a8a5fd90451e97f488c22f4decc5;hpb=ea20fcfcded669ce8fbbda3a1f93f354ed254603 diff --git a/test/trio2.c b/test/trio2.c index b55fde73..c21cba22 100644 --- a/test/trio2.c +++ b/test/trio2.c @@ -12,40 +12,40 @@ #include "devtools.h" #include "utils.h" -static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { - (void)mesh; +static struct sync_flag received; +static struct sync_flag bar_learned_baz; +static struct sync_flag baz_learned_bar; - static struct timeval tv0; - struct timeval tv; +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 receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { +static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { (void)mesh; - (void)source; + (void)reachable; - 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() { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + // Create three instances. const char *name[3] = {"foo", "bar", "baz"}; @@ -56,6 +56,7 @@ int main() { char *path = NULL; assert(asprintf(&path, "trio2_conf.%d", i) != -1 && path); + assert(meshlink_destroy(path)); mesh[i] = meshlink_open(path, name[i], "trio2", DEV_CLASS_BACKBONE); assert(mesh[i]); free(path); @@ -66,8 +67,6 @@ int main() { assert(data[i]); } - meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb); - // first node knows the two other nodes for(int i = 1; i < 3; i++) { @@ -85,6 +84,9 @@ int main() { // 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++) { free(data[i]); assert(meshlink_start(mesh[i])); @@ -92,8 +94,8 @@ int main() { // 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)); // Check that the second and third node autoconnect to each other @@ -110,8 +112,6 @@ int main() { // Start just the other two nodes - meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb); - for(int i = 1; i < 3; i++) { assert(meshlink_start(mesh[i])); } @@ -121,9 +121,17 @@ int main() { // Communication should still be possible - received = false; meshlink_set_receive_cb(mesh[1], receive_cb); - assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25); + + for(int i = 0; i < 25; 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.