GNUnet 0.22.0
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_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 83 of file testing_api_cmd_finish.c.

84{
85 struct FinishState *finish_state = cls;
86
88 finish_state->finish_task = NULL;
89 if (NULL != finish_state->old_notify)
90 {
91 finish_state->old_notify (finish_state->old_notify_cls);
92 finish_state->old_notify = NULL;
93 }
94 GNUNET_TESTING_async_finish (&finish_state->ac);
95}
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:979
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.
GNUNET_SCHEDULER_TaskCallback old_notify
Function to call when async operation is done.
void * old_notify_cls
Closure for notify_finished.
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(), GNUNET_TESTING_async_finish(), FinishState::old_notify, and FinishState::old_notify_cls.

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

106{
107 struct FinishState *finish_state = cls;
108
109 finish_state->finish_task = NULL;
111 "Timeout waiting for command `%s' to finish\n",
112 finish_state->async_label);
113 GNUNET_TESTING_async_fail (&finish_state->ac);
114}
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 123 of file testing_api_cmd_finish.c.

126{
127 struct FinishState *finish_state = cls;
128 const struct GNUNET_TESTING_Command *async_cmd;
129 struct GNUNET_TESTING_AsyncContext *aac;
130
131 async_cmd
133 finish_state->async_label);
134 if (NULL == async_cmd)
135 {
137 "Did not find command `%s'\n",
138 finish_state->async_label);
140 }
141 if ( (NULL == (aac = async_cmd->ac)) ||
142 (! async_cmd->asynchronous_finish) )
143 {
145 "Cannot finish `%s': not asynchronous\n",
146 finish_state->async_label);
148 }
149 if (aac->finished)
150 {
151 /* Command is already finished, so are we! */
152 GNUNET_TESTING_async_finish (&finish_state->ac);
153 return;
154 }
155 /* add timeout */
156 finish_state->finish_task
157 = GNUNET_SCHEDULER_add_delayed (finish_state->timeout,
159 finish_state);
160 /* back up old notification that we will override */
161 finish_state->old_notify = aac->notify_finished;
162 finish_state->old_notify_cls = aac->notify_finished_cls;
164 aac->notify_finished_cls = finish_state;
165}
static struct GNUNET_TESTING_Interpreter * is
#define GNUNET_TESTING_FAIL(is)
Print failing line number and trigger shutdown.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
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:1276
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 notify_finished
Function to call when async operation is done.
enum GNUNET_GenericReturnValue finished
Indication if the command finished (GNUNET_OK).
void * notify_finished_cls
Closure for notify_finished.
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 run the next command, even if this command did not ...
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, done_finish(), FinishState::finish_task, GNUNET_TESTING_AsyncContext::finished, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_SCHEDULER_add_delayed(), GNUNET_TESTING_async_finish(), GNUNET_TESTING_FAIL, GNUNET_TESTING_interpreter_lookup_command(), is, GNUNET_TESTING_AsyncContext::notify_finished, GNUNET_TESTING_AsyncContext::notify_finished_cls, FinishState::old_notify, FinishState::old_notify_cls, FinishState::timeout, and timeout_finish().

Referenced by GNUNET_TESTING_cmd_finish(), and GNUNET_TESTING_cmd_make_unblocking().

Here is the call graph for this function:
Here is the caller 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 174 of file testing_api_cmd_finish.c.

175{
176 struct FinishState *finish_state = cls;
177
178 if (NULL != finish_state->finish_task)
179 {
180 GNUNET_SCHEDULER_cancel (finish_state->finish_task);
181 finish_state->finish_task = NULL;
182 }
183 GNUNET_free (finish_state);
184}
#define GNUNET_free(ptr)
Wrapper around free.

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

Referenced by GNUNET_TESTING_cmd_finish().

Here is the call graph for this function:
Here is the caller 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 188 of file testing_api_cmd_finish.c.

192{
193 struct FinishState *finish_state;
194
195 finish_state = GNUNET_new (struct FinishState);
196 finish_state->async_label = cmd_ref;
197 finish_state->timeout = timeout;
199 finish_state,
200 finish_label,
201 &run_finish,
203 NULL,
204 &finish_state->ac);
205}
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:118
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new_ac(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 that may be asynchronous.
#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.

References FinishState::ac, FinishState::async_label, cleanup_finish(), GNUNET_new, GNUNET_TESTING_command_new_ac(), run_finish(), timeout, and FinishState::timeout.

Referenced by main().

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

◆ 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
[in,out]cmdcommand to make non-blocking
Returns
a finish-command.

Definition at line 209 of file testing_api_cmd_finish.c.

211{
212 /* do not permit this function to be used on
213 a finish command! */
214 GNUNET_assert (cmd.run != &run_finish);
215 cmd.asynchronous_finish = true;
216 return cmd;
217}
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
GNUNET_TESTING_CommandRunRoutine run
Runs the command.

References GNUNET_assert, and run_finish().

Referenced by main().

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