GNUnet  0.11.x
Data Structures | Functions
testing_api_cmd_finish.c File Reference

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

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_testing_ng_lib.h"
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 70 of file testing_api_cmd_finish.c.

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 }
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:972
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 87 of file testing_api_cmd_finish.c.

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 }
#define GNUNET_log(kind,...)
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
@ 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 105 of file testing_api_cmd_finish.c.

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 }
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
@ GNUNET_NO
Definition: gnunet_common.h:94
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.
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 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 153 of file testing_api_cmd_finish.c.

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 }
#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 153 of file testing_api_cmd_finish.c.

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 }
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 153 of file testing_api_cmd_finish.c.

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 }
#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.