GNUnet  0.11.x
Data Structures | Macros | Functions
testing_api_cmd_batch.c File Reference

Implement batch-execution of CMDs. More...

#include "platform.h"
#include "gnunet_testing_ng_lib.h"
#include "testing.h"
Include dependency graph for testing_api_cmd_batch.c:

Go to the source code of this file.

Data Structures

struct  BatchState
 State for a "batch" CMD. More...
 

Macros

#define CURRENT_CMD_INDEX   0
 
#define BATCH_INDEX   1
 

Functions

static void batch_run (void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
 Run the command. More...
 
static void batch_cleanup (void *cls, const struct GNUNET_TESTING_Command *cmd)
 Cleanup the state from a "reserve status" CMD, and possibly cancel a pending operation thereof. More...
 
static int batch_traits (void *cls, const void **ret, const char *trait, unsigned int index)
 Offer internal data from a "batch" CMD, to other commands. More...
 
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_batch (const char *label, struct GNUNET_TESTING_Command *batch)
 Create a "batch" command. More...
 
void GNUNET_TESTING_cmd_batch_next (struct GNUNET_TESTING_Interpreter *is)
 Advance internal pointer to next command. More...
 
int GNUNET_TESTING_cmd_is_batch (const struct GNUNET_TESTING_Command *cmd)
 Test if this command is a batch command. More...
 
struct GNUNET_TESTING_CommandGNUNET_TESTING_cmd_batch_get_current (const struct GNUNET_TESTING_Command *cmd)
 Obtain what command the batch is at. More...
 
void GNUNET_TESTING_cmd_batch_set_current (const struct GNUNET_TESTING_Command *cmd, unsigned int new_ip)
 Set what command the batch should be at. More...
 

Detailed Description

Implement batch-execution of CMDs.

Author
Marcello Stanisci (GNU Taler testing)
t3sserakt

Definition in file testing_api_cmd_batch.c.

Macro Definition Documentation

◆ CURRENT_CMD_INDEX

#define CURRENT_CMD_INDEX   0

Referenced by batch_traits().

◆ BATCH_INDEX

#define BATCH_INDEX   1

Referenced by batch_traits().

Function Documentation

◆ batch_run()

static void batch_run ( void *  cls,
const struct GNUNET_TESTING_Command cmd,
struct GNUNET_TESTING_Interpreter is 
)
static

Run the command.

Parameters
clsclosure.
cmdthe command being executed.
isthe interpreter state.

Definition at line 56 of file testing_api_cmd_batch.c.

References BatchState::batch, BatchState::batch_ip, GNUNET_TESTING_Command::cls, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_TIME_absolute_get(), GNUNET_TESTING_Command::label, GNUNET_TESTING_Command::last_req_time, GNUNET_TESTING_Command::num_tries, GNUNET_TESTING_Command::run, and GNUNET_TESTING_Command::start_time.

Referenced by GNUNET_TESTING_cmd_batch(), GNUNET_TESTING_cmd_batch_get_current(), GNUNET_TESTING_cmd_batch_set_current(), and GNUNET_TESTING_cmd_is_batch().

59 {
60  struct BatchState *bs = cls;
61 
62  if (NULL != bs->batch[bs->batch_ip].label)
64  "Running batched command: %s\n",
65  bs->batch[bs->batch_ip].label);
66 
67  /* hit end command, leap to next top-level command. */
68  if (NULL == bs->batch[bs->batch_ip].label)
69  {
71  "Exiting from batch: %s\n",
72  cmd->label);
73  return;
74  }
75  bs->batch[bs->batch_ip].start_time
76  = bs->batch[bs->batch_ip].last_req_time
78  bs->batch[bs->batch_ip].num_tries = 1;
79  bs->batch[bs->batch_ip].run (bs->batch[bs->batch_ip].cls,
80  &bs->batch[bs->batch_ip],
81  is);
82 }
const char * label
Label for the command.
unsigned int batch_ip
Internal command pointer.
struct GNUNET_TESTING_Command * batch
CMDs batch.
State for a "batch" CMD.
struct GNUNET_TIME_Absolute start_time
When did the execution of this command start?
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:86
void(* run)(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *i)
Runs the command.
struct GNUNET_TIME_Absolute last_req_time
When did we start the last run of this command? Delta to finish_time gives the latency for the last s...
#define GNUNET_log(kind,...)
void * cls
Closure for all commands with command-specific context information.
unsigned int num_tries
How often did we try to execute this command? (In case it is a request that is repated.) Note that a command must have some built-in retry mechanism for this value to be useful.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ batch_cleanup()

static void batch_cleanup ( void *  cls,
const struct GNUNET_TESTING_Command cmd 
)
static

Cleanup the state from a "reserve status" CMD, and possibly cancel a pending operation thereof.

Parameters
clsclosure.
cmdthe command which is being cleaned up.

Definition at line 93 of file testing_api_cmd_batch.c.

References BatchState::batch, GNUNET_TESTING_Command::cleanup, GNUNET_TESTING_Command::cls, GNUNET_free, and GNUNET_TESTING_Command::label.

Referenced by GNUNET_TESTING_cmd_batch().

95 {
96  struct BatchState *bs = cls;
97 
98  (void) cmd;
99  for (unsigned int i = 0;
100  NULL != bs->batch[i].label;
101  i++)
102  bs->batch[i].cleanup (bs->batch[i].cls,
103  &bs->batch[i]);
104  GNUNET_free (bs->batch);
105  GNUNET_free (bs);
106 }
void(* cleanup)(void *cls, const struct GNUNET_TESTING_Command *cmd)
Clean up after the command.
const char * label
Label for the command.
struct GNUNET_TESTING_Command * batch
CMDs batch.
State for a "batch" CMD.
void * cls
Closure for all commands with command-specific context information.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ batch_traits()

static int batch_traits ( void *  cls,
const void **  ret,
const char *  trait,
unsigned int  index 
)
static

Offer internal data from a "batch" CMD, to other commands.

Parameters
clsclosure.
[out]retresult.
traitname of the trait.
indexindex number of the object to offer.
Returns
GNUNET_OK on success.

Definition at line 119 of file testing_api_cmd_batch.c.

References BatchState::batch, BATCH_INDEX, BatchState::batch_ip, CURRENT_CMD_INDEX, GNUNET_TESTING_get_trait(), GNUNET_TESTING_make_trait_cmd(), and GNUNET_TESTING_trait_end().

Referenced by GNUNET_TESTING_cmd_batch().

123 {
124 #define CURRENT_CMD_INDEX 0
125 #define BATCH_INDEX 1
126 
127  struct BatchState *bs = cls;
128 
129  struct GNUNET_TESTING_Trait traits[] = {
131  (CURRENT_CMD_INDEX, &bs->batch[bs->batch_ip]),
133  (BATCH_INDEX, bs->batch),
135  };
136 
137  /* Always return current command. */
138  return GNUNET_TESTING_get_trait (traits,
139  ret,
140  trait,
141  index);
142 }
struct GNUNET_TESTING_Trait GNUNET_TESTING_make_trait_cmd(unsigned int index, const struct GNUNET_TESTING_Command *cmd)
Offer a command in a trait.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
unsigned int batch_ip
Internal command pointer.
struct GNUNET_TESTING_Command * batch
CMDs batch.
State for a "batch" CMD.
unsigned int index
Index number associated with the trait.
#define BATCH_INDEX
#define CURRENT_CMD_INDEX
int GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Extract a trait.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" trait.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_cmd_batch()

struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_batch ( const char *  label,
struct GNUNET_TESTING_Command batch 
)

Create a "batch" command.

Such command takes a end_CMD-terminated array of CMDs and executed them. Once it hits the end CMD, it passes the control to the next top-level CMD, regardless of it being another batch or ordinary CMD.

Parameters
labelthe command label.
batcharray of CMDs to execute.
Returns
the command.

Definition at line 158 of file testing_api_cmd_batch.c.

References BatchState::batch, batch_cleanup(), batch_run(), batch_traits(), GNUNET_TESTING_Command::cls, GNUNET_new, GNUNET_new_array, and GNUNET_TESTING_Command::label.

160 {
161  struct BatchState *bs;
162  unsigned int i;
163 
164  bs = GNUNET_new (struct BatchState);
165 
166  /* Get number of commands. */
167  for (i = 0; NULL != batch[i].label; i++)
168  /* noop */
169  ;
170 
171  bs->batch = GNUNET_new_array (i + 1,
172  struct GNUNET_TESTING_Command);
173  memcpy (bs->batch,
174  batch,
175  sizeof (struct GNUNET_TESTING_Command) * i);
176  {
177  struct GNUNET_TESTING_Command cmd = {
178  .cls = bs,
179  .label = label,
180  .run = &batch_run,
181  .cleanup = &batch_cleanup,
182  .traits = &batch_traits
183  };
184 
185  return cmd;
186  }
187 }
static void batch_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
Run the command.
static int batch_traits(void *cls, const void **ret, const char *trait, unsigned int index)
Offer internal data from a "batch" CMD, to other commands.
const char * label
Label for the command.
A command to be run by the interpreter.
struct GNUNET_TESTING_Command * batch
CMDs batch.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
State for a "batch" CMD.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
void * cls
Closure for all commands with command-specific context information.
static void batch_cleanup(void *cls, const struct GNUNET_TESTING_Command *cmd)
Cleanup the state from a "reserve status" CMD, and possibly cancel a pending operation thereof...
Here is the call graph for this function:

◆ GNUNET_TESTING_cmd_batch_next()

void GNUNET_TESTING_cmd_batch_next ( struct GNUNET_TESTING_Interpreter is)

Advance internal pointer to next command.

Parameters
isinterpreter state.

Definition at line 196 of file testing_api_cmd_batch.c.

References BatchState::batch, BatchState::batch_ip, GNUNET_TESTING_Command::cls, GNUNET_TESTING_Interpreter::commands, GNUNET_TESTING_Command::finish_time, GNUNET_TIME_absolute_get(), GNUNET_TESTING_Interpreter::ip, and GNUNET_TESTING_Command::label.

Referenced by interpreter_next().

197 {
198  struct BatchState *bs = is->commands[is->ip].cls;
199 
200  if (NULL == bs->batch[bs->batch_ip].label)
201  {
203  is->ip++;
204  return;
205  }
207  bs->batch_ip++;
208 }
const char * label
Label for the command.
unsigned int batch_ip
Internal command pointer.
struct GNUNET_TESTING_Command * batch
CMDs batch.
State for a "batch" CMD.
struct GNUNET_TIME_Absolute finish_time
When did the execution of this command finish?
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:86
struct GNUNET_TESTING_Command * commands
Commands the interpreter will run.
Definition: testing.h:40
int ip
Instruction pointer.
Definition: testing.h:67
void * cls
Closure for all commands with command-specific context information.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_cmd_is_batch()

int GNUNET_TESTING_cmd_is_batch ( const struct GNUNET_TESTING_Command cmd)

Test if this command is a batch command.

Returns
false if not, true if it is a batch command

Definition at line 217 of file testing_api_cmd_batch.c.

References batch_run(), and GNUNET_TESTING_Command::run.

Referenced by GNUNET_TESTING_interpreter_fail(), GNUNET_TESTING_interpreter_lookup_command(), and interpreter_next().

218 {
219  return cmd->run == &batch_run;
220 }
static void batch_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
Run the command.
void(* run)(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *i)
Runs the command.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_cmd_batch_get_current()

struct GNUNET_TESTING_Command* GNUNET_TESTING_cmd_batch_get_current ( const struct GNUNET_TESTING_Command cmd)

Obtain what command the batch is at.

Returns
cmd current batch command

Definition at line 229 of file testing_api_cmd_batch.c.

References BatchState::batch, BatchState::batch_ip, batch_run(), GNUNET_TESTING_Command::cls, GNUNET_assert, and GNUNET_TESTING_Command::run.

Referenced by GNUNET_TESTING_interpreter_fail(), and GNUNET_TESTING_interpreter_lookup_command().

230 {
231  struct BatchState *bs = cmd->cls;
232 
233  GNUNET_assert (cmd->run == &batch_run);
234  return &bs->batch[bs->batch_ip];
235 }
static void batch_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
Run the command.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
unsigned int batch_ip
Internal command pointer.
struct GNUNET_TESTING_Command * batch
CMDs batch.
State for a "batch" CMD.
void(* run)(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *i)
Runs the command.
void * cls
Closure for all commands with command-specific context information.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_cmd_batch_set_current()

void GNUNET_TESTING_cmd_batch_set_current ( const struct GNUNET_TESTING_Command cmd,
unsigned int  new_ip 
)

Set what command the batch should be at.

Parameters
cmdcurrent batch command
new_ipwhere to move the IP

Definition at line 245 of file testing_api_cmd_batch.c.

References BatchState::batch, BatchState::batch_ip, batch_run(), GNUNET_TESTING_Command::cls, GNUNET_assert, GNUNET_TESTING_Command::label, and GNUNET_TESTING_Command::run.

247 {
248  struct BatchState *bs = cmd->cls;
249 
250  /* sanity checks */
251  GNUNET_assert (cmd->run == &batch_run);
252  for (unsigned int i = 0; i < new_ip; i++)
253  GNUNET_assert (NULL != bs->batch[i].label);
254  /* actual logic */
255  bs->batch_ip = new_ip;
256 }
static void batch_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
Run the command.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * label
Label for the command.
unsigned int batch_ip
Internal command pointer.
struct GNUNET_TESTING_Command * batch
CMDs batch.
State for a "batch" CMD.
void(* run)(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *i)
Runs the command.
void * cls
Closure for all commands with command-specific context information.
Here is the call graph for this function: