2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/test_testbed_api_operations.c
23 * @brief tests cases for testbed_api_operations.c
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "testbed_api_operations.h"
32 * Generic logging shortcut
34 #define LOG(kind,...) \
35 GNUNET_log (kind, __VA_ARGS__)
38 * Queue A. Initially the max active is set to 2 and then reduced to 0 - this
39 * should block op2 even after op1 has finished. Later the max active is set to
40 * 1 and this should start op2
42 struct OperationQueue *q1;
45 * Queue B. Max active set to 1
47 struct OperationQueue *q2;
50 * This operation should go into both queues and block op2 until it is done
52 struct GNUNET_TESTBED_Operation *op1;
55 * This operation should go into q1 and q2
57 struct GNUNET_TESTBED_Operation *op2;
60 * This operation should go into both queues and should be started after op2 has
63 struct GNUNET_TESTBED_Operation *op3;
66 * The delay task identifier
68 GNUNET_SCHEDULER_TaskIdentifier step_task;
72 * Enumeration of test stages
82 * op1 has been started
87 * op1 has been released
102 * Temporary pause where no operations should start as we set max active in q1
125 * Function to call to start an operation once all
126 * queues the operation is part of declare that the
127 * operation can be activated.
130 start_cb (void *cls);
134 * Function to cancel an operation (release all associated resources). This can
135 * be because of a call to "GNUNET_TESTBED_operation_cancel" (before the
136 * operation generated an event) or AFTER the operation generated an event due
137 * to a call to "GNUNET_TESTBED_operation_done". Thus it is not guaranteed that
138 * a callback to the 'OperationStart' preceeds the call to 'OperationRelease'.
139 * Implementations of this function are expected to clean up whatever state is
140 * in 'cls' and release all resources associated with the operation.
143 release_cb (void *cls);
147 * Task to simulate artificial delay and change the test stage
150 * @param tc the task context
153 step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != step_task);
156 step_task = GNUNET_SCHEDULER_NO_TASK;
159 case TEST_OP1_STARTED:
160 GNUNET_TESTBED_operation_release_ (op1);
161 GNUNET_TESTBED_operation_queue_reset_max_active_ (q1, 0);
162 op3 = GNUNET_TESTBED_operation_create_ (&op3, &start_cb, &release_cb);
163 GNUNET_TESTBED_operation_queue_insert_ (q1, op3);
164 GNUNET_TESTBED_operation_queue_insert_ (q2, op3);
165 GNUNET_TESTBED_operation_begin_wait_ (op3);
167 case TEST_OP2_STARTED:
168 GNUNET_TESTBED_operation_release_ (op2);
170 case TEST_OP2_RELEASED:
172 GNUNET_TESTBED_operation_queue_reset_max_active_ (q1, 1);
174 case TEST_OP3_STARTED:
175 GNUNET_TESTBED_operation_release_ (op3);
184 * Function to call to start an operation once all
185 * queues the operation is part of declare that the
186 * operation can be activated.
194 GNUNET_assert (&op1 == cls);
195 result = TEST_OP1_STARTED;
196 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == step_task);
197 step_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
199 case TEST_OP1_RELEASED:
200 GNUNET_assert (&op2 == cls);
201 result = TEST_OP2_STARTED;
202 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == step_task);
203 step_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
206 GNUNET_assert (&op3 == cls);
207 result = TEST_OP3_STARTED;
208 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == step_task);
209 step_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
218 * Function to cancel an operation (release all associated resources). This can
219 * be because of a call to "GNUNET_TESTBED_operation_cancel" (before the
220 * operation generated an event) or AFTER the operation generated an event due
221 * to a call to "GNUNET_TESTBED_operation_done". Thus it is not guaranteed that
222 * a callback to the 'OperationStart' preceeds the call to 'OperationRelease'.
223 * Implementations of this function are expected to clean up whatever state is
224 * in 'cls' and release all resources associated with the operation.
227 release_cb (void *cls)
231 case TEST_OP1_STARTED:
232 GNUNET_assert (&op1 == cls);
233 result = TEST_OP1_RELEASED;
235 //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
237 case TEST_OP2_STARTED:
238 GNUNET_assert (&op2 == cls);
239 result = TEST_OP2_RELEASED;
240 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == step_task);
241 step_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
243 case TEST_OP3_STARTED:
244 GNUNET_assert (&op3 == cls);
245 result = TEST_OP3_RELEASED;
246 GNUNET_TESTBED_operation_queue_destroy_ (q1);
247 GNUNET_TESTBED_operation_queue_destroy_ (q2);
259 * @param args arguments passed to GNUNET_PROGRAM_run
260 * @param cfgfile the path to configuration file
261 * @param cfg the configuration file handle
264 run (void *cls, char *const *args, const char *cfgfile,
265 const struct GNUNET_CONFIGURATION_Handle *config)
267 q1 = GNUNET_TESTBED_operation_queue_create_ (1);
268 GNUNET_assert (NULL != q1);
269 q2 = GNUNET_TESTBED_operation_queue_create_ (2);
270 GNUNET_assert (NULL != q2);
271 op1 = GNUNET_TESTBED_operation_create_ (&op1, start_cb, release_cb);
272 GNUNET_assert (NULL != op1);
273 op2 = GNUNET_TESTBED_operation_create_ (&op2, start_cb, release_cb);
274 GNUNET_TESTBED_operation_queue_insert_ (q1, op1);
275 GNUNET_TESTBED_operation_queue_insert_ (q2, op1);
276 GNUNET_TESTBED_operation_begin_wait_ (op1);
277 GNUNET_TESTBED_operation_queue_insert_ (q1, op2);
278 GNUNET_TESTBED_operation_queue_insert_ (q2, op2);
279 GNUNET_TESTBED_operation_begin_wait_ (op2);
287 main (int argc, char **argv)
290 char *const argv2[] =
291 { "test_testbed_api_operations", "-c", "test_testbed_api.conf", NULL };
292 struct GNUNET_GETOPT_CommandLineOption options[] =
293 { GNUNET_GETOPT_OPTION_END };
296 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
297 "test_testbed_api_operations", "nohelp", options,
299 if ((GNUNET_OK != ret) || (TEST_OP3_RELEASED != result))
309 /* end of test_testbed_api_operations.c */