GNUnet  0.19.0
testing_api_cmd_finish.c File Reference

command to wait for completion of async command More...

Include dependency graph for testing_api_cmd_finish.c:

Go to the source code of this file.

Data Structures

struct  FinishState
 Struct to use for command-specific context information closure of a command waiting for another command. More...
 

Functions

static void done_finish (void *cls)
 Function called when the command we are waiting on is finished. More...
 
static void timeout_finish (void *cls)
 Function triggered if the command we are waiting for did not complete on time. More...
 
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. More...
 
static void cleanup_finish (void *cls)
 Cleanup state of a finish command. More...
 
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. More...
 
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. More...
 

Detailed Description

command to wait for completion of async command

Author
Christian Grothoff

Definition in file testing_api_cmd_finish.c.

Function Documentation

◆ done_finish()

static void done_finish ( void *  cls)
static

Function called when the command we are waiting on is finished.

Hence we are finished, too.

Parameters
clsa struct FinishState being notified

Definition at line 72 of file testing_api_cmd_finish.c.

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 }
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:944
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.
struct GNUNET_SCHEDULER_Task * finish_task
Task for running the finish method of the asynchronous task the command is waiting for.

References FinishState::ac, FinishState::cls, FinishState::finish_task, GNUNET_SCHEDULER_cancel(), and GNUNET_TESTING_async_finish().

Referenced by run_finish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timeout_finish()

static void timeout_finish ( void *  cls)
static

Function triggered if the command we are waiting for did not complete on time.

Parameters
clsour struct FinishState

Definition at line 89 of file testing_api_cmd_finish.c.

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 }
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_ERROR
const char * async_label
Label of the asynchronous command the synchronous command of this closure waits for.

References FinishState::ac, FinishState::async_label, FinishState::cls, FinishState::finish_task, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, and GNUNET_TESTING_async_fail().

Referenced by run_finish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_finish()

static void run_finish ( void *  cls,
struct GNUNET_TESTING_Interpreter is 
)
static

Run method of the command created by the interpreter to wait for another command to finish.

Definition at line 107 of file testing_api_cmd_finish.c.

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 }
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
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.
@ GNUNET_NO
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:1241
struct GNUNET_TIME_Relative timeout
How long to wait until finish fails hard?
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.
bool asynchronous_finish
If "true", the interpreter should not immediately call finish, even if finish is non-NULL.
static void timeout_finish(void *cls)
Function triggered if the command we are waiting for did not complete on time.
static void done_finish(void *cls)
Function called when the command we are waiting on is finished.

References GNUNET_TESTING_Command::ac, FinishState::ac, FinishState::async_label, GNUNET_TESTING_Command::asynchronous_finish, FinishState::cls, GNUNET_TESTING_AsyncContext::cont, GNUNET_TESTING_AsyncContext::cont_cls, done_finish(), FinishState::finish_task, GNUNET_TESTING_AsyncContext::finished, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_TESTING_async_finish(), GNUNET_TESTING_interpreter_fail(), GNUNET_TESTING_interpreter_lookup_command(), is, FinishState::timeout, and timeout_finish().

Here is the call graph for this function:

◆ cleanup_finish()

static void cleanup_finish ( void *  cls)
static

Cleanup state of a finish command.

Parameters
clsa struct FinishState to clean up

Definition at line 155 of file testing_api_cmd_finish.c.

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 }
#define GNUNET_free(ptr)
Wrapper around free.

References FinishState::cls, FinishState::finish_task, GNUNET_free, and GNUNET_SCHEDULER_cancel().

Here is the call graph for this function:

◆ GNUNET_TESTING_cmd_finish()

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.

If cmd_ref did not finish after timeout, this command will fail the test case.

Parameters
finish_labellabel for this command
cmd_refreference to a previous command which we should wait for (call finish() on)
timeouthow long to wait at most for cmd_ref to finish
Returns
a finish-command.

Definition at line 155 of file testing_api_cmd_finish.c.

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 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void * cls
Closure for all commands with command-specific context information.
static void cleanup_finish(void *cls)
Cleanup state of a finish command.
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.

◆ GNUNET_TESTING_cmd_make_unblocking()

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.

Parameters
cmdcommand to make synchronous.
Returns
a finish-command.

Definition at line 155 of file testing_api_cmd_finish.c.

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 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void(* run)(void *cls, struct GNUNET_TESTING_Interpreter *is)
Runs the command.