GNUnet  0.17.5
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"
29 
30 
36 {
40  void *cls;
41 
45  const char *async_label;
46 
51 
56 
61 
62 };
63 
64 
71 static void
72 done_finish (void *cls)
73 {
74  struct FinishState *finish_state = cls;
75 
76  GNUNET_SCHEDULER_cancel (finish_state->finish_task);
77  finish_state->finish_task = NULL;
78  GNUNET_TESTING_async_finish (&finish_state->ac);
79 }
80 
81 
88 static void
90 {
91  struct FinishState *finish_state = cls;
92 
93  finish_state->finish_task = NULL;
95  "Timeout waiting for command `%s' to finish\n",
96  finish_state->async_label);
97  GNUNET_TESTING_async_fail (&finish_state->ac);
98 }
99 
100 
106 static void
109 {
110  struct FinishState *finish_state = cls;
111  const struct GNUNET_TESTING_Command *async_cmd;
112  struct GNUNET_TESTING_AsyncContext *aac;
113 
114  async_cmd
116  finish_state->async_label);
117  if (NULL == async_cmd)
118  {
120  "Did not find command `%s'\n",
121  finish_state->async_label);
123  return;
124  }
125  if ( (NULL == (aac = async_cmd->ac)) ||
126  (! async_cmd->asynchronous_finish) )
127  {
129  "Cannot finish `%s': not asynchronous\n",
130  finish_state->async_label);
132  return;
133  }
134  if (GNUNET_NO != aac->finished)
135  {
136  /* Command is already finished, so are we! */
137  GNUNET_TESTING_async_finish (&finish_state->ac);
138  return;
139  }
140  finish_state->finish_task
141  = GNUNET_SCHEDULER_add_delayed (finish_state->timeout,
143  finish_state);
144  aac->cont = &done_finish;
145  aac->cont_cls = finish_state;
146 }
147 
148 
154 static void
155 cleanup_finish (void *cls)
156 {
157  struct FinishState *finish_state = cls;
158 
159  if (NULL != finish_state->finish_task)
160  {
161  GNUNET_SCHEDULER_cancel (finish_state->finish_task);
162  finish_state->finish_task = NULL;
163  }
164  GNUNET_free (finish_state);
165 }
166 
167 
168 const struct GNUNET_TESTING_Command
169 GNUNET_TESTING_cmd_finish (const char *finish_label,
170  const char *cmd_ref,
172 {
173  struct FinishState *finish_state;
174 
175  finish_state = GNUNET_new (struct FinishState);
176  finish_state->async_label = cmd_ref;
177  finish_state->timeout = timeout;
178  {
179  struct GNUNET_TESTING_Command cmd = {
180  .cls = finish_state,
181  .label = finish_label,
182  .run = &run_finish,
183  .ac = &finish_state->ac,
184  .cleanup = &cleanup_finish
185  };
186 
187  return cmd;
188  }
189 }
190 
191 
194 {
195  /* do not permit this function to be used on
196  a finish command! */
197  GNUNET_assert (cmd.run != &run_finish);
198  cmd.asynchronous_finish = true;
199  return cmd;
200 }
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.
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_log(kind,...)
@ GNUNET_NO
Definition: gnunet_common.h:98
#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:957
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:1254
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.