From 1de30bc05683aade10634f2d357dcc2379bfcc42 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 22 Jul 2020 21:07:02 +0200 Subject: [PATCH] Port the blackbox status_cb test. Use ephemeral MeshLink instances to speed up the test. --- test/Makefile.am | 7 +++++ test/api_set_node_status_cb.c | 58 +++++++++++++++++++++++++++++++++++ test/utils.c | 20 ++++++++++++ test/utils.h | 1 + 4 files changed, 86 insertions(+) create mode 100644 test/api_set_node_status_cb.c diff --git a/test/Makefile.am b/test/Makefile.am index 4d6d9368..6dcfc610 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -24,6 +24,9 @@ TESTS = \ utcp-benchmark \ utcp-benchmark-stream +TESTS += \ + api_set_node_status_cb + if BLACKBOX_TESTS SUBDIRS = blackbox endif @@ -34,6 +37,7 @@ AM_CPPFLAGS = $(PTHREAD_CFLAGS) -I${top_srcdir}/src -iquote. -Wall AM_LDFLAGS = $(PTHREAD_LIBS) check_PROGRAMS = \ + api_set_node_status_cb \ basic \ basicpp \ blacklist \ @@ -63,6 +67,9 @@ if INSTALL_TESTS bin_PROGRAMS = $(check_PROGRAMS) endif +api_set_node_status_cb_SOURCES = api_set_node_status_cb.c utils.c utils.h +api_set_node_status_cb_LDADD = $(top_builddir)/src/libmeshlink.la + basic_SOURCES = basic.c utils.c utils.h basic_LDADD = $(top_builddir)/src/libmeshlink.la diff --git a/test/api_set_node_status_cb.c b/test/api_set_node_status_cb.c new file mode 100644 index 00000000..7e5da192 --- /dev/null +++ b/test/api_set_node_status_cb.c @@ -0,0 +1,58 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif + +#include +#include + +#include "meshlink.h" +#include "utils.h" + +static struct sync_flag a_reachable; +static struct sync_flag b_reachable; + +static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { + (void)mesh; + + if(!reachable) { + return; + } + + if(!strcmp(node->name, "a")) { + set_sync_flag(&a_reachable, true); + } else if(!strcmp(node->name, "b")) { + set_sync_flag(&b_reachable, true); + } +} + +int main(void) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + + init_sync_flag(&a_reachable); + init_sync_flag(&b_reachable); + + meshlink_handle_t *mesh1, *mesh2; + open_meshlink_pair_ephemeral(&mesh1, &mesh2, "api_set_node_status_cb"); + + // Test case #1: check that setting a valid status callback will cause it to be called + // when the node itself is started or stopped + + meshlink_set_node_status_cb(mesh1, status_cb); + assert(meshlink_start(mesh1)); + assert(wait_sync_flag(&a_reachable, 5)); + + // Test case #2: check that the status callback will be called when another peer is started + + assert(meshlink_start(mesh2)); + assert(wait_sync_flag(&b_reachable, 5)); + + // Test case #3: check that passing a NULL pointer for the mesh returns an error + + meshlink_errno = MESHLINK_OK; + meshlink_set_node_status_cb(NULL, status_cb); + assert(meshlink_errno == MESHLINK_EINVAL); + + // Done. + + close_meshlink_pair(mesh1, mesh2); +} diff --git a/test/utils.c b/test/utils.c index 21b33f5e..f1f1c770 100644 --- a/test/utils.c +++ b/test/utils.c @@ -108,6 +108,26 @@ void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const ch *pb = b; } +void open_meshlink_pair_ephemeral(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) { + // Create two new MeshLink instances + + *pa = *pb = NULL; + + meshlink_handle_t *a = meshlink_open_ephemeral("a", prefix, DEV_CLASS_BACKBONE); + meshlink_handle_t *b = meshlink_open_ephemeral("b", prefix, DEV_CLASS_BACKBONE); + + assert(a); + assert(b); + + meshlink_enable_discovery(a, false); + meshlink_enable_discovery(b, false); + + link_meshlink_pair(a, b); + + *pa = a; + *pb = b; +} + // Don't poll in the application thread, use a condition variable to signal when the peer is online. static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { (void)node; diff --git a/test/utils.h b/test/utils.h index 2a56ed83..110c50a6 100644 --- a/test/utils.h +++ b/test/utils.h @@ -21,6 +21,7 @@ extern bool wait_sync_flag(struct sync_flag *s, int seconds); /// Create a pair of meshlink instances that are already joined together. extern void open_meshlink_pair(meshlink_handle_t **a, meshlink_handle_t **b, const char *prefix); +extern void open_meshlink_pair_ephemeral(meshlink_handle_t **a, meshlink_handle_t **b, const char *prefix); /// Start a pair of meshlink instances and wait for them to connect together. extern void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b); -- 2.39.5