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.
17 * @file testbed/test_testbed_api_barriers.c
18 * @brief testcase binary for testing testbed barriers API
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
25 #include "test_testbed_api_barriers.h"
31 #define LOG(type,...) \
32 GNUNET_log (type, __VA_ARGS__);
35 * Number of peers we start in this test case
43 struct GNUNET_TESTBED_Barrier *barrier;
46 * Identifier for the shutdown task
48 static struct GNUNET_SCHEDULER_Task *timeout_task;
51 * Result of this test case
57 * Handle SIGINT and SIGTERM
60 shutdown_handler(void *cls)
62 if (NULL != timeout_task)
64 GNUNET_SCHEDULER_cancel(timeout_task);
71 * Shutdown this test case when it takes too long
76 do_timeout (void *cls)
80 GNUNET_TESTBED_barrier_cancel (barrier);
81 GNUNET_SCHEDULER_shutdown ();
86 * Functions of this type are to be given as callback argument to
87 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
88 * information is available for the barrier.
90 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
91 * @param name the name of the barrier
92 * @param barrier the barrier handle
93 * @param status status of the barrier; #GNUNET_OK if the barrier is crossed;
94 * #GNUNET_SYSERR upon error
95 * @param emsg if the status were to be #GNUNET_SYSERR, this parameter has the
99 barrier_cb (void *cls,
101 struct GNUNET_TESTBED_Barrier *_barrier,
102 enum GNUNET_TESTBED_BarrierStatus status,
105 static enum GNUNET_TESTBED_BarrierStatus old_status;
107 GNUNET_assert (NULL == cls);
108 GNUNET_assert (_barrier == barrier);
111 case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
112 LOG (GNUNET_ERROR_TYPE_INFO,
113 "Barrier initialised\n");
116 case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
117 LOG (GNUNET_ERROR_TYPE_ERROR,
118 "Barrier initialisation failed: %s",
119 (NULL == emsg) ? "unknown reason" : emsg);
121 case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
122 LOG (GNUNET_ERROR_TYPE_INFO,
123 "Barrier crossed\n");
124 if (old_status == GNUNET_TESTBED_BARRIERSTATUS_INITIALISED)
132 GNUNET_SCHEDULER_shutdown ();
137 * Signature of a main function for a testcase.
140 * @param h the run handle
141 * @param num_peers number of peers in 'peers'
142 * @param peers_ handle to peers run in the testbed
143 * @param links_succeeded the number of overlay link connection attempts that
145 * @param links_failed the number of overlay link connection attempts that
149 test_master (void *cls,
150 struct GNUNET_TESTBED_RunHandle *h,
151 unsigned int num_peers,
152 struct GNUNET_TESTBED_Peer **peers_,
153 unsigned int links_succeeded,
154 unsigned int links_failed)
156 struct GNUNET_TESTBED_Controller *c;
158 GNUNET_assert (NULL == cls);
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 "Failing test due to timeout\n");
165 GNUNET_assert (NUM_PEERS == num_peers);
166 c = GNUNET_TESTBED_run_get_controller_handle (h);
167 barrier = GNUNET_TESTBED_barrier_init (c,
173 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
174 (GNUNET_TIME_UNIT_SECONDS,
175 10 * (NUM_PEERS + 1)),
177 GNUNET_SCHEDULER_add_shutdown(&shutdown_handler, NULL);
183 * Assumed maximum path length (for the log file name).
185 #define PATH_MAX 4096
193 main (int argc, char **argv)
195 struct GNUNET_CONFIGURATION_Handle *cfg;
200 result = GNUNET_SYSERR;
202 cfg = GNUNET_CONFIGURATION_create ();
203 GNUNET_assert (GNUNET_YES ==
204 GNUNET_CONFIGURATION_parse (cfg,
205 "test_testbed_api_barriers.conf.in"));
206 if (NULL == getcwd (pwd, PATH_MAX))
208 GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd,
209 "gnunet-service-test-barriers"));
210 GNUNET_CONFIGURATION_set_value_string (cfg, "test-barriers","BINARY", binary);
211 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
212 (cfg, "test_testbed_api_barriers.conf"));
213 GNUNET_CONFIGURATION_destroy (cfg);
215 GNUNET_free (binary);
217 (void) GNUNET_TESTBED_test_run ("test_testbed_api_barriers",
218 "test_testbed_api_barriers.conf", NUM_PEERS,
219 event_mask, NULL, NULL,
221 (void) unlink ("test_testbed_api_barriers.conf");
222 if (GNUNET_OK != result)