From 6ba5501ace0b03e1573e4ec2a3fee1491f64bfe2 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 23 Jul 2020 23:13:57 +0200 Subject: [PATCH] Fix the channels-udp test case. Close channels gracefully after the server has finished sending the streams. --- src/utcp.c | 1 - test/channels-udp.c | 51 ++++++++++++++------------------------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/utcp.c b/src/utcp.c index 1c0890c6..acd8188b 100644 --- a/src/utcp.c +++ b/src/utcp.c @@ -2043,7 +2043,6 @@ void utcp_abort_all_connections(struct utcp *utcp) { int utcp_close(struct utcp_connection *c) { if(c->rcvbuf.used) { - fprintf(stderr, "UTCP channel closed with stuff in receive buffer\n"); return reset_connection(c) ? 0 : -1; } diff --git a/test/channels-udp.c b/test/channels-udp.c index 40d5605d..6ed2abe6 100644 --- a/test/channels-udp.c +++ b/test/channels-udp.c @@ -14,47 +14,26 @@ #include "utils.h" static struct sync_flag accept_flag; -static struct sync_flag close_flag; struct client { meshlink_handle_t *mesh; meshlink_channel_t *channel; size_t received; bool got_large_packet; + struct sync_flag close_flag; }; -static void server_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { - (void)data; - - // We expect no data from clients, apart from disconnections. - assert(len == 0); - - meshlink_channel_t **c = mesh->priv; - int count = 0; - - for(int i = 0; i < 3; i++) { - if(c[i] == channel) { - c[i] = NULL; - meshlink_channel_close(mesh, channel); - } - - if(c[i]) { - count++; - } - } +static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + assert(mesh->priv); + struct client *client = mesh->priv; - if(!count) { - set_sync_flag(&close_flag, true); + if(!data && !len) { + set_sync_flag(&client->close_flag, true); + meshlink_channel_close(mesh, channel); + return; } -} - -static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { - (void)channel; - (void)data; // We expect always the same amount of data from the server. - assert(mesh->priv); - struct client *client = mesh->priv; assert(len == 512 || len == 65536); client->received += len; @@ -80,7 +59,6 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint assert(port == 1); assert(meshlink_channel_get_flags(mesh, channel) == MESHLINK_CHANNEL_UDP); - meshlink_set_channel_receive_cb(mesh, channel, server_receive_cb); assert(mesh->priv); meshlink_channel_t **c = mesh->priv; @@ -102,7 +80,6 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint int main(void) { init_sync_flag(&accept_flag); - init_sync_flag(&close_flag); meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb); @@ -125,6 +102,7 @@ int main(void) { char dir[100]; snprintf(dir, sizeof(dir), "channels_udp_conf.%d", i + 1); assert(meshlink_destroy(dir)); + init_sync_flag(&clients[i].close_flag); clients[i].mesh = meshlink_open(dir, names[i], "channels-udp", DEV_CLASS_STATIONARY); assert(clients[i].mesh); clients[i].mesh->priv = &clients[i]; @@ -170,14 +148,17 @@ int main(void) { nanosleep(&req, NULL); } - // Let the clients close the channels + // Shutdown the write side of the server's channels for(int i = 0; i < 3; i++) { - meshlink_channel_close(clients[i].mesh, clients[i].channel); - meshlink_set_node_status_cb(clients[i].mesh, NULL); + meshlink_channel_shutdown(server, channels[i], SHUT_WR); } - assert(wait_sync_flag(&close_flag, 10)); + // Wait for the clients to finish reading all the data + + for(int i = 0; i < 3; i++) { + assert(wait_sync_flag(&clients[i].close_flag, 10)); + } // Check that the clients have received (most of) the data -- 2.39.2