GNUnet  0.20.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 74 of file testing_api_cmd_finish.c.

75 {
76  struct FinishState *finish_state = cls;
77 
78  GNUNET_SCHEDULER_cancel (finish_state->finish_task);
79  finish_state->finish_task = NULL;
80  GNUNET_TESTING_async_finish (&finish_state->ac);
81 }
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:975
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 91 of file testing_api_cmd_finish.c.

92 {
93  struct FinishState *finish_state = cls;
94 
95  finish_state->finish_task = NULL;
97  "Timeout waiting for command `%s' to finish\n",
98  finish_state->async_label);
99  GNUNET_TESTING_async_fail (&finish_state->ac);
100 }
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 109 of file testing_api_cmd_finish.c.

111 {
112  struct FinishState *finish_state = cls;
113  const struct GNUNET_TESTING_Command *async_cmd;
114  struct GNUNET_TESTING_AsyncContext *aac;
115 
116  async_cmd
118  finish_state->async_label);
119  if (NULL == async_cmd)
120  {
122  "Did not find command `%s'\n",
123  finish_state->async_label);
125  return;
126  }
127  if ( (NULL == (aac = async_cmd->ac)) ||
128  (! async_cmd->asynchronous_finish) )
129  {
131  "Cannot finish `%s': not asynchronous\n",
132  finish_state->async_label);
134  return;
135  }
136  if (GNUNET_NO != aac->finished)
137  {
138  /* Command is already finished, so are we! */
139  GNUNET_TESTING_async_finish (&finish_state->ac);
140  return;
141  }
142  finish_state->finish_task
143  = GNUNET_SCHEDULER_add_delayed (finish_state->timeout,
145  finish_state);
146  aac->cont = &done_finish;
147  aac->cont_cls = finish_state;
148 }
struct GNUNET_TESTING_Interpreter * is
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:1272
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 157 of file testing_api_cmd_finish.c.

158 {
159  struct FinishState *finish_state = cls;
160 
161  if (NULL != finish_state->finish_task)
162  {
163  GNUNET_SCHEDULER_cancel (finish_state->finish_task);
164  finish_state->finish_task = NULL;
165  }
166  GNUNET_free (finish_state);
167 }
#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 157 of file testing_api_cmd_finish.c.

174 {
175  struct FinishState *finish_state;
176 
177  finish_state = GNUNET_new (struct FinishState);
178  finish_state->async_label = cmd_ref;
179  finish_state->timeout = timeout;
180  return GNUNET_TESTING_command_new (finish_state, finish_label,
181  &run_finish,
183  NULL, &finish_state->ac);
184 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new(void *cls, const char *label, GNUNET_TESTING_CommandRunRoutine run, GNUNET_TESTING_CommandCleanupRoutine cleanup, GNUNET_TESTING_CommandGetTraits traits, struct GNUNET_TESTING_AsyncContext *ac)
Create a new command.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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.

Modifies (and then returns) cmd simply setting the bit. By default, most commands are blocking, and by wrapping the command construction in this function a blocking command can be turned into an asynchronous command where the interpreter continues after initiating the asynchronous action. Does nothing if the command is fundamentally synchronous.

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

Definition at line 157 of file testing_api_cmd_finish.c.

189 {
190  /* do not permit this function to be used on
191  a finish command! */
192  GNUNET_assert (cmd.run != &run_finish);
193  cmd.asynchronous_finish = true;
194  return cmd;
195 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
GNUNET_TESTING_CommandRunRoutine run
Runs the command.