X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fset%2Ftest_set_union_copy.c;h=6fd78d43a2acd859d0e3bc035c7f0bb5b72a0b54;hb=2f45a7c9691aa2670c8902618be5e8011428f0af;hp=fdf6bf9df3848f5bd496e673074c62f0c70c040f;hpb=29e6158507a0758192075ac6ece7ba8e75ddc49a;p=oweals%2Fgnunet.git diff --git a/src/set/test_set_union_copy.c b/src/set/test_set_union_copy.c index fdf6bf9df..6fd78d43a 100644 --- a/src/set/test_set_union_copy.c +++ b/src/set/test_set_union_copy.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2015 GNUnet e.V. + Copyright (C) 2015, 2016 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -21,6 +21,7 @@ /** * @file set/test_set_union_copy.c * @brief testcase for lazy copying of union sets + * @author Florian Dold */ #include "platform.h" #include "gnunet_util_lib.h" @@ -42,30 +43,38 @@ static struct GNUNET_SET_Handle *set2; static const struct GNUNET_CONFIGURATION_Handle *config; +static struct GNUNET_SCHEDULER_Task *tt; + static void -add_element_str (struct GNUNET_SET_Handle *set, char *str) +add_element_str (struct GNUNET_SET_Handle *set, + char *str) { struct GNUNET_SET_Element element; element.element_type = 0; element.data = str; element.size = strlen (str); - - GNUNET_SET_add_element (set, &element, NULL, NULL); + GNUNET_SET_add_element (set, + &element, + NULL, + NULL); } static void -remove_element_str (struct GNUNET_SET_Handle *set, char *str) +remove_element_str (struct GNUNET_SET_Handle *set, + char *str) { struct GNUNET_SET_Element element; element.element_type = 0; element.data = str; element.size = strlen (str); - - GNUNET_SET_remove_element (set, &element, NULL, NULL); + GNUNET_SET_remove_element (set, + &element, + NULL, + NULL); } @@ -77,23 +86,17 @@ remove_element_str (struct GNUNET_SET_Handle *set, char *str) static void timeout_fail (void *cls) { - const struct GNUNET_SCHEDULER_TaskContext *tc; - - tc = GNUNET_SCHEDULER_get_task_context (); - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; + tt = NULL; GNUNET_SCHEDULER_shutdown (); ret = 1; } -typedef void (*Continuation) (void *cls); - struct CountIterClosure { unsigned int expected_count; unsigned int ongoing_count; - Continuation cont; + GNUNET_SCHEDULER_TaskCallback cont; void *cont_cls; char *what; }; @@ -125,12 +128,11 @@ check_count_iter (void *cls, } - -void +static void check_count (struct GNUNET_SET_Handle *set, char *what, unsigned int expected_count, - Continuation cont, + GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls) { struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure); @@ -141,28 +143,34 @@ check_count (struct GNUNET_SET_Handle *set, ci_cls->cont_cls = cont_cls; ci_cls->what = what; - GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls)); + GNUNET_assert (GNUNET_YES == + GNUNET_SET_iterate (set, + &check_count_iter, + ci_cls)); } -void test_done (void *cls) +static void +test_done (void *cls) { - if (NULL != set1) - GNUNET_SET_destroy (set1); - if (NULL != set2) - GNUNET_SET_destroy (set2); - GNUNET_SCHEDULER_shutdown (); } -void check_new_set_count (void *cls) +static void +check_new_set_count (void *cls) { - check_count (set2, "new set", 4, &test_done, NULL); + check_count (set2, + "new set", + 4, + &test_done, + NULL); } -void copy_done (void *cls, struct GNUNET_SET_Handle *new_set) +static void +copy_done (void *cls, + struct GNUNET_SET_Handle *new_set) { printf ("copy done\n"); set2 = new_set; @@ -173,21 +181,50 @@ void copy_done (void *cls, struct GNUNET_SET_Handle *new_set) remove_element_str (set2, "new3"); // Check that set1 didn't change. check_count (set1, "old set", 3, - &check_new_set_count, NULL); + &check_new_set_count, + NULL); } -void test_copy (void *cls) +static void +test_copy (void *cls) { printf ("about to copy\n"); - GNUNET_SET_copy_lazy (set1, copy_done, NULL); + GNUNET_SET_copy_lazy (set1, + ©_done, + NULL); } +/** + * Function run on shutdown. + * + * @param cls closure + */ +static void +do_shutdown (void *cls) +{ + if (NULL != tt) + { + GNUNET_SCHEDULER_cancel (tt); + tt = NULL; + } + if (NULL != set1) + { + GNUNET_SET_destroy (set1); + set1 = NULL; + } + if (NULL != set2) + { + GNUNET_SET_destroy (set2); + set2 = NULL; + } +} + /** * Signature of the 'main' function for a (single-peer) testcase that - * is run using 'GNUNET_TESTING_peer_run'. + * is run using #GNUNET_TESTING_peer_run(). * * @param cls closure * @param cfg configuration of the peer that was started @@ -198,10 +235,11 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Peer *peer) { - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), - &timeout_fail, - NULL); - + tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), + &timeout_fail, + NULL); + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + NULL); config = cfg; GNUNET_TESTING_peer_get_identity (peer, &local_id); @@ -219,7 +257,11 @@ run (void *cls, remove_element_str (set1, "foo"); add_element_str (set1, "eggs"); - check_count (set1, "initial test", 3, &test_copy, NULL); + check_count (set1, + "initial test", + 3, + &test_copy, + NULL); } @@ -230,7 +272,8 @@ main (int argc, char **argv) "test_set.conf", &run, NULL)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "failed to start testing peer\n"); return 1; } return ret;