diff options
Diffstat (limited to 'src/testing/testing_api_loop.c')
-rw-r--r-- | src/testing/testing_api_loop.c | 116 |
1 files changed, 110 insertions, 6 deletions
diff --git a/src/testing/testing_api_loop.c b/src/testing/testing_api_loop.c index 3727d2543..e82ec33ab 100644 --- a/src/testing/testing_api_loop.c +++ b/src/testing/testing_api_loop.c | |||
@@ -53,6 +53,11 @@ struct GNUNET_TESTING_Interpreter | |||
53 | struct GNUNET_TESTING_Command *commands; | 53 | struct GNUNET_TESTING_Command *commands; |
54 | 54 | ||
55 | /** | 55 | /** |
56 | * Number of GNUNET_TESTING_Command in commands. | ||
57 | */ | ||
58 | unsigned int cmds_n; | ||
59 | |||
60 | /** | ||
56 | * Interpreter task (if one is scheduled). | 61 | * Interpreter task (if one is scheduled). |
57 | */ | 62 | */ |
58 | struct GNUNET_SCHEDULER_Task *task; | 63 | struct GNUNET_SCHEDULER_Task *task; |
@@ -83,21 +88,32 @@ struct GNUNET_TESTING_Interpreter | |||
83 | 88 | ||
84 | 89 | ||
85 | const struct GNUNET_TESTING_Command * | 90 | const struct GNUNET_TESTING_Command * |
86 | GNUNET_TESTING_interpreter_lookup_command ( | 91 | get_command (struct GNUNET_TESTING_Interpreter *is, |
87 | struct GNUNET_TESTING_Interpreter *is, | 92 | const char *label, |
88 | const char *label) | 93 | unsigned int future) |
89 | { | 94 | { |
95 | int start_i = GNUNET_NO == future ? is->ip : is->cmds_n - 1; | ||
96 | int end_i = GNUNET_NO == future ? 0 : is->ip + 1; | ||
97 | |||
98 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
99 | "start_i: %u end_i: %u\n", | ||
100 | start_i, | ||
101 | end_i); | ||
90 | if (NULL == label) | 102 | if (NULL == label) |
91 | { | 103 | { |
92 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 104 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
93 | "Attempt to lookup command for empty label\n"); | 105 | "Attempt to lookup command for empty label\n"); |
94 | return NULL; | 106 | return NULL; |
95 | } | 107 | } |
96 | /* Search backwards as we most likely reference recent commands */ | 108 | |
97 | for (int i = is->ip; i >= 0; i--) | 109 | for (int i = start_i; i >= end_i; i--) |
98 | { | 110 | { |
99 | const struct GNUNET_TESTING_Command *cmd = &is->commands[i]; | 111 | const struct GNUNET_TESTING_Command *cmd = &is->commands[i]; |
100 | 112 | ||
113 | if (NULL != cmd->label) | ||
114 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
115 | "label to compare %s\n", | ||
116 | cmd->label); | ||
101 | /* Give precedence to top-level commands. */ | 117 | /* Give precedence to top-level commands. */ |
102 | if ( (NULL != cmd->label) && | 118 | if ( (NULL != cmd->label) && |
103 | (0 == strcmp (cmd->label, | 119 | (0 == strcmp (cmd->label, |
@@ -142,6 +158,40 @@ GNUNET_TESTING_interpreter_lookup_command ( | |||
142 | 158 | ||
143 | 159 | ||
144 | /** | 160 | /** |
161 | * Lookup command by label. | ||
162 | * Only future commands are looked up. | ||
163 | * | ||
164 | * @param is interpreter to lookup command in | ||
165 | * @param label label of the command to lookup. | ||
166 | * @return the command, if it is found, or NULL. | ||
167 | */ | ||
168 | const struct GNUNET_TESTING_Command * | ||
169 | GNUNET_TESTING_interpreter_lookup_future_command ( | ||
170 | struct GNUNET_TESTING_Interpreter *is, | ||
171 | const char *label) | ||
172 | { | ||
173 | return get_command (is, label, GNUNET_YES); | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Lookup command by label. | ||
179 | * Only commands from current command to commands in the past are looked up. | ||
180 | * | ||
181 | * @param is interpreter to lookup command in | ||
182 | * @param label label of the command to lookup. | ||
183 | * @return the command, if it is found, or NULL. | ||
184 | */ | ||
185 | const struct GNUNET_TESTING_Command * | ||
186 | GNUNET_TESTING_interpreter_lookup_command ( | ||
187 | struct GNUNET_TESTING_Interpreter *is, | ||
188 | const char *label) | ||
189 | { | ||
190 | return get_command (is, label, GNUNET_NO); | ||
191 | } | ||
192 | |||
193 | |||
194 | /** | ||
145 | * Finish the test run, return the final result. | 195 | * Finish the test run, return the final result. |
146 | * | 196 | * |
147 | * @param cls the `struct GNUNET_TESTING_Interpreter` | 197 | * @param cls the `struct GNUNET_TESTING_Interpreter` |
@@ -267,6 +317,20 @@ GNUNET_TESTING_interpreter_fail (struct GNUNET_TESTING_Interpreter *is) | |||
267 | } | 317 | } |
268 | 318 | ||
269 | 319 | ||
320 | /** | ||
321 | * Returns the actual running command. | ||
322 | * | ||
323 | * @param is Global state of the interpreter, used by a command | ||
324 | * to access information about other commands. | ||
325 | * @return The actual running command. | ||
326 | */ | ||
327 | struct GNUNET_TESTING_Command * | ||
328 | GNUNET_TESTING_interpreter_get_current_command ( | ||
329 | struct GNUNET_TESTING_Interpreter *is) | ||
330 | { | ||
331 | return &is->commands[is->ip]; | ||
332 | } | ||
333 | |||
270 | const char * | 334 | const char * |
271 | GNUNET_TESTING_interpreter_get_current_label ( | 335 | GNUNET_TESTING_interpreter_get_current_label ( |
272 | struct GNUNET_TESTING_Interpreter *is) | 336 | struct GNUNET_TESTING_Interpreter *is) |
@@ -300,9 +364,17 @@ interpreter_run (void *cls) | |||
300 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 364 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
301 | "Running command `%s'\n", | 365 | "Running command `%s'\n", |
302 | cmd->label); | 366 | cmd->label); |
367 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
368 | "start time of %p expected 0 is `%lu'\n", | ||
369 | cmd, | ||
370 | cmd->start_time.abs_value_us); | ||
303 | cmd->start_time | 371 | cmd->start_time |
304 | = cmd->last_req_time | 372 | = cmd->last_req_time |
305 | = GNUNET_TIME_absolute_get (); | 373 | = GNUNET_TIME_absolute_get (); |
374 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
375 | "start time of %p expected something is `%lu'\n", | ||
376 | cmd, | ||
377 | cmd->start_time.abs_value_us); | ||
306 | cmd->num_tries = 1; | 378 | cmd->num_tries = 1; |
307 | if (NULL != cmd->ac) | 379 | if (NULL != cmd->ac) |
308 | { | 380 | { |
@@ -344,6 +416,37 @@ do_timeout (void *cls) | |||
344 | } | 416 | } |
345 | 417 | ||
346 | 418 | ||
419 | /** | ||
420 | * Check if the command is running. | ||
421 | * | ||
422 | * @param cmd The command to check. | ||
423 | * @return GNUNET_NO if the command is not running, GNUNET_YES if it is running. | ||
424 | */ | ||
425 | enum GNUNET_GenericReturnValue | ||
426 | GNUNET_TESTING_running (const struct GNUNET_TESTING_Command *command) | ||
427 | { | ||
428 | return 0 != command->start_time.abs_value_us && 0 == | ||
429 | command->finish_time.abs_value_us; | ||
430 | } | ||
431 | |||
432 | |||
433 | /** | ||
434 | * Check if a command is finished. | ||
435 | * | ||
436 | * @param cmd The command to check. | ||
437 | * @return GNUNET_NO if the command is not finished, GNUNET_YES if it is finished. | ||
438 | */ | ||
439 | enum GNUNET_GenericReturnValue | ||
440 | GNUNET_TESTING_finished (struct GNUNET_TESTING_Command *command) | ||
441 | { | ||
442 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); | ||
443 | struct GNUNET_TIME_Relative diff = GNUNET_TIME_absolute_get_difference ( | ||
444 | command->finish_time, | ||
445 | now); | ||
446 | return 0 < diff.rel_value_us; | ||
447 | } | ||
448 | |||
449 | |||
347 | void | 450 | void |
348 | GNUNET_TESTING_run (struct GNUNET_TESTING_Command *commands, | 451 | GNUNET_TESTING_run (struct GNUNET_TESTING_Command *commands, |
349 | struct GNUNET_TIME_Relative timeout, | 452 | struct GNUNET_TIME_Relative timeout, |
@@ -359,7 +462,8 @@ GNUNET_TESTING_run (struct GNUNET_TESTING_Command *commands, | |||
359 | /* get the number of commands */ | 462 | /* get the number of commands */ |
360 | for (i = 0; NULL != commands[i].label; i++) | 463 | for (i = 0; NULL != commands[i].label; i++) |
361 | ; | 464 | ; |
362 | is->commands = GNUNET_new_array (i + 1, | 465 | is->cmds_n = i + 1; |
466 | is->commands = GNUNET_new_array (is->cmds_n, | ||
363 | struct GNUNET_TESTING_Command); | 467 | struct GNUNET_TESTING_Command); |
364 | memcpy (is->commands, | 468 | memcpy (is->commands, |
365 | commands, | 469 | commands, |