GNUnet  0.11.x
testing_api_cmd_batch.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2021 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet_testing_ng_lib.h"
29 #include "testing.h"
30 
34 struct BatchState
35 {
40 
44  unsigned int batch_ip;
45 };
46 
47 
55 static void
56 batch_run (void *cls,
57  const struct GNUNET_TESTING_Command *cmd,
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 }
83 
84 
92 static void
93 batch_cleanup (void *cls,
94  const struct GNUNET_TESTING_Command *cmd)
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 }
107 
108 
118 static int
119 batch_traits (void *cls,
120  const void **ret,
121  const char *trait,
122  unsigned int index)
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 }
143 
144 
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 }
188 
189 
195 void
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 }
209 
210 
216 int
218 {
219  return cmd->run == &batch_run;
220 }
221 
222 
228 struct GNUNET_TESTING_Command *
230 {
231  struct BatchState *bs = cmd->cls;
232 
233  GNUNET_assert (cmd->run == &batch_run);
234  return &bs->batch[bs->batch_ip];
235 }
236 
237 
244 void
246  unsigned int new_ip)
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 }
void(* cleanup)(void *cls, const struct GNUNET_TESTING_Command *cmd)
Clean up after the command.
static void batch_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
Run the command.
struct GNUNET_TESTING_Trait GNUNET_TESTING_make_trait_cmd(unsigned int index, const struct GNUNET_TESTING_Command *cmd)
Offer a command in a trait.
int GNUNET_TESTING_cmd_is_batch(const struct GNUNET_TESTING_Command *cmd)
Test if this command is a batch 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.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * label
Label for the command.
A command to be run by the interpreter.
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.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
State for a "batch" CMD.
struct GNUNET_TIME_Absolute finish_time
When did the execution of this command finish?
Global state of the interpreter, used by a command to access information about other commands...
Definition: testing.h:34
struct GNUNET_TESTING_Command * GNUNET_TESTING_cmd_batch_get_current(const struct GNUNET_TESTING_Command *cmd)
Obtain what command the batch is at.
struct GNUNET_TESTING_Interpreter * is
struct GNUNET_TIME_Absolute start_time
When did the execution of this command start?
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.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define BATCH_INDEX
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:86
#define CURRENT_CMD_INDEX
struct GNUNET_TESTING_Command * commands
Commands the interpreter will run.
Definition: testing.h:40
int GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Extract a trait.
int ip
Instruction pointer.
Definition: testing.h:67
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.
void GNUNET_TESTING_cmd_batch_next(struct GNUNET_TESTING_Interpreter *is)
Advance internal pointer to next command.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_batch(const char *label, struct GNUNET_TESTING_Command *batch)
Create a "batch" command.
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.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" trait.
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...
#define GNUNET_free(ptr)
Wrapper around free.