GNUnet  0.11.x
testing_api_cmd_finish.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2021 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_ng_lib.h"
28 
34 {
38  void *cls;
39 
43  const char *async_label;
44 
49 
54 
59 
60 };
61 
62 
69 static void
70 done_finish (void *cls)
71 {
72  struct FinishState *finish_state = cls;
73 
74  GNUNET_SCHEDULER_cancel (finish_state->finish_task);
75  finish_state->finish_task = NULL;
76  GNUNET_TESTING_async_finish (&finish_state->ac);
77 }
78 
79 
86 static void
88 {
89  struct FinishState *finish_state = cls;
90 
91  finish_state->finish_task = NULL;
93  "Timeout waiting for command `%s' to finish\n",
94  finish_state->async_label);
95  GNUNET_TESTING_async_fail (&finish_state->ac);
96 }
97 
98 
104 static void
107 {
108  struct FinishState *finish_state = cls;
109  const struct GNUNET_TESTING_Command *async_cmd;
110  struct GNUNET_TESTING_AsyncContext *aac;
111 
112  async_cmd
114  finish_state->async_label);
115  if (NULL == async_cmd)
116  {
118  "Did not find command `%s'\n",
119  finish_state->async_label);
121  return;
122  }
123  if ( (NULL == (aac = async_cmd->ac)) ||
124  (! async_cmd->asynchronous_finish) )
125  {
127  "Cannot finish `%s': not asynchronous\n",
128  finish_state->async_label);
130  return;
131  }
132  if (GNUNET_NO != aac->finished)
133  {
134  /* Command is already finished, so are we! */
135  GNUNET_TESTING_async_finish (&finish_state->ac);
136  return;
137  }
138  finish_state->finish_task
139  = GNUNET_SCHEDULER_add_delayed (finish_state->timeout,
141  finish_state);
142  aac->cont = &done_finish;
143  aac->cont_cls = finish_state;
144 }
145 
146 
152 static void
153 cleanup_finish (void *cls)
154 {
155  struct FinishState *finish_state = cls;
156 
157  if (NULL != finish_state->finish_task)
158  {
159  GNUNET_SCHEDULER_cancel (finish_state->finish_task);
160  finish_state->finish_task = NULL;
161  }
162  GNUNET_free (finish_state);
163 }
164 
165 
166 const struct GNUNET_TESTING_Command
167 GNUNET_TESTING_cmd_finish (const char *finish_label,
168  const char *cmd_ref,
170 {
171  struct FinishState *finish_state;
172 
173  finish_state = GNUNET_new (struct FinishState);
174  finish_state->async_label = cmd_ref;
175  finish_state->timeout = timeout;
176  {
177  struct GNUNET_TESTING_Command cmd = {
178  .cls = finish_state,
179  .label = finish_label,
180  .run = &run_finish,
181  .ac = &finish_state->ac,
182  .cleanup = &cleanup_finish
183  };
184 
185  return cmd;
186  }
187 }
188 
189 
192 {
193  /* do not permit this function to be used on
194  a finish command! */
195  GNUNET_assert (cmd.run != &run_finish);
196  cmd.asynchronous_finish = true;
197  return cmd;
198 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
#define GNUNET_log(kind,...)
@ GNUNET_NO
Definition: gnunet_common.h:94
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
void GNUNET_TESTING_interpreter_fail(struct GNUNET_TESTING_Interpreter *is)
Current command failed, clean up and fail the test case.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1269
Struct to use for command-specific context information closure of a command waiting for another comma...
struct GNUNET_TESTING_AsyncContext ac
Function to call when done.
void * cls
Closure for all commands with command-specific context information.
const char * async_label
Label of the asynchronous command the synchronous command of this closure waits for.
struct GNUNET_TIME_Relative timeout
How long to wait until finish fails hard?
struct GNUNET_SCHEDULER_Task * finish_task
Task for running the finish method of the asynchronous task the command is waiting for.
Entry in list of pending tasks.
Definition: scheduler.c:135
State each asynchronous command must have in its closure.
GNUNET_SCHEDULER_TaskCallback cont
Function to call when done.
void * cont_cls
Closure for cont.
enum GNUNET_GenericReturnValue finished
Indication if the command finished (GNUNET_OK).
A command to be run by the interpreter.
struct GNUNET_TESTING_AsyncContext * ac
Pointer to the asynchronous context in the command's closure.
void(* run)(void *cls, struct GNUNET_TESTING_Interpreter *is)
Runs the command.
bool asynchronous_finish
If "true", the interpreter should not immediately call finish, even if finish is non-NULL.
void * cls
Closure for all commands with command-specific context information.
Global state of the interpreter, used by a command to access information about other commands.
Time for relative time used by GNUnet, in microseconds.
static void timeout_finish(void *cls)
Function triggered if the command we are waiting for did not complete on time.
static void cleanup_finish(void *cls)
Cleanup state of a finish command.
const struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_finish(const char *finish_label, const char *cmd_ref, struct GNUNET_TIME_Relative timeout)
Create (synchronous) command that waits for another command to finish.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_make_unblocking(struct GNUNET_TESTING_Command cmd)
Turn asynchronous command into non blocking command by setting asynchronous_finish to true.
static void run_finish(void *cls, struct GNUNET_TESTING_Interpreter *is)
Run method of the command created by the interpreter to wait for another command to finish.
static void done_finish(void *cls)
Function called when the command we are waiting on is finished.