2 This file is part of GNUnet
3 Copyright (C) 2008--2013 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/>.
20 * @file testbed/test_testbed_api_barriers.c
21 * @brief testcase binary for testing testbed barriers API
22 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28 #include "test_testbed_api_barriers.h"
34 #define LOG(type,...) \
35 GNUNET_log (type, __VA_ARGS__);
38 * Number of peers we start in this test case
46 struct GNUNET_TESTBED_Barrier *barrier;
49 * Identifier for the shutdown task
51 static struct GNUNET_SCHEDULER_Task *timeout_task;
54 * Result of this test case
60 * Handle SIGINT and SIGTERM
63 shutdown_handler(void *cls)
65 if (NULL != timeout_task)
67 GNUNET_SCHEDULER_cancel(timeout_task);
74 * Shutdown this test case when it takes too long
79 do_timeout (void *cls)
83 GNUNET_TESTBED_barrier_cancel (barrier);
84 GNUNET_SCHEDULER_shutdown ();
89 * Functions of this type are to be given as callback argument to
90 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
91 * information is available for the barrier.
93 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
94 * @param name the name of the barrier
95 * @param barrier the barrier handle
96 * @param status status of the barrier; #GNUNET_OK if the barrier is crossed;
97 * #GNUNET_SYSERR upon error
98 * @param emsg if the status were to be #GNUNET_SYSERR, this parameter has the
102 barrier_cb (void *cls,
104 struct GNUNET_TESTBED_Barrier *_barrier,
105 enum GNUNET_TESTBED_BarrierStatus status,
108 static enum GNUNET_TESTBED_BarrierStatus old_status;
110 GNUNET_assert (NULL == cls);
111 GNUNET_assert (_barrier == barrier);
114 case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
115 LOG (GNUNET_ERROR_TYPE_INFO,
116 "Barrier initialised\n");
119 case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
120 LOG (GNUNET_ERROR_TYPE_ERROR,
121 "Barrier initialisation failed: %s",
122 (NULL == emsg) ? "unknown reason" : emsg);
124 case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
125 LOG (GNUNET_ERROR_TYPE_INFO,
126 "Barrier crossed\n");
127 if (old_status == GNUNET_TESTBED_BARRIERSTATUS_INITIALISED)
135 GNUNET_SCHEDULER_shutdown ();
140 * Signature of a main function for a testcase.
143 * @param h the run handle
144 * @param num_peers number of peers in 'peers'
145 * @param peers_ handle to peers run in the testbed
146 * @param links_succeeded the number of overlay link connection attempts that
148 * @param links_failed the number of overlay link connection attempts that
152 test_master (void *cls,
153 struct GNUNET_TESTBED_RunHandle *h,
154 unsigned int num_peers,
155 struct GNUNET_TESTBED_Peer **peers_,
156 unsigned int links_succeeded,
157 unsigned int links_failed)
159 struct GNUNET_TESTBED_Controller *c;
161 GNUNET_assert (NULL == cls);
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "Failing test due to timeout\n");
168 GNUNET_assert (NUM_PEERS == num_peers);
169 c = GNUNET_TESTBED_run_get_controller_handle (h);
170 barrier = GNUNET_TESTBED_barrier_init (c,
176 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
177 (GNUNET_TIME_UNIT_SECONDS,
178 10 * (NUM_PEERS + 1)),
180 GNUNET_SCHEDULER_add_shutdown(&shutdown_handler, NULL);
186 * Assumed maximum path length (for the log file name).
188 #define PATH_MAX 4096
196 main (int argc, char **argv)
198 struct GNUNET_CONFIGURATION_Handle *cfg;
203 result = GNUNET_SYSERR;
205 cfg = GNUNET_CONFIGURATION_create ();
206 GNUNET_assert (GNUNET_YES ==
207 GNUNET_CONFIGURATION_parse (cfg,
208 "test_testbed_api_barriers.conf.in"));
209 if (NULL == getcwd (pwd, PATH_MAX))
211 GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd,
212 "gnunet-service-test-barriers"));
213 GNUNET_CONFIGURATION_set_value_string (cfg, "test-barriers","BINARY", binary);
214 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
215 (cfg, "test_testbed_api_barriers.conf"));
216 GNUNET_CONFIGURATION_destroy (cfg);
218 GNUNET_free (binary);
220 (void) GNUNET_TESTBED_test_run ("test_testbed_api_barriers",
221 "test_testbed_api_barriers.conf", NUM_PEERS,
222 event_mask, NULL, NULL,
224 (void) unlink ("test_testbed_api_barriers.conf");
225 if (GNUNET_OK != result)