2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file consensus/test_consensus_api.c
23 * @brief testcase for consensus_api.c
26 #include "gnunet_util_lib.h"
27 #include "gnunet_consensus_service.h"
28 #include "gnunet_testing_lib.h"
31 static struct GNUNET_CONSENSUS_Handle *consensus;
33 static struct GNUNET_HashCode session_id;
35 static unsigned int elements_received;
39 conclude_done(void *cls)
41 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "conclude over\n");
42 if (2 != elements_received)
44 GNUNET_SCHEDULER_shutdown();
48 on_new_element(void *cls,
49 const struct GNUNET_SET_Element *element)
55 insert_done(void *cls, int success)
57 /* make sure cb is only called once */
58 static int called = GNUNET_NO;
60 GNUNET_assert(GNUNET_NO == called);
62 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "insert done\n");
63 GNUNET_CONSENSUS_conclude(consensus, &conclude_done, NULL);
68 * Signature of the main function of a task.
73 on_shutdown(void *cls)
75 if (NULL != consensus)
77 GNUNET_CONSENSUS_destroy(consensus);
85 const struct GNUNET_CONFIGURATION_Handle *cfg,
86 struct GNUNET_TESTING_Peer *peer)
90 struct GNUNET_SET_Element el1 = { 4, 0, "foo" };
91 struct GNUNET_SET_Element el2 = { 5, 0, "quux" };
93 GNUNET_log_setup("test_consensus_api",
96 GNUNET_SCHEDULER_add_shutdown(&on_shutdown, NULL);
98 GNUNET_CRYPTO_hash(str, strlen(str), &session_id);
99 consensus = GNUNET_CONSENSUS_create(cfg, 0, NULL, &session_id,
100 GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_SECONDS),
101 GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_MINUTES),
102 on_new_element, &consensus);
103 GNUNET_assert(consensus != NULL);
105 GNUNET_CONSENSUS_insert(consensus, &el1, NULL, &consensus);
106 GNUNET_CONSENSUS_insert(consensus, &el2, &insert_done, &consensus);
111 main(int argc, char **argv)
113 return GNUNET_TESTING_peer_run("test_consensus_api",
114 "test_consensus.conf",