GNUnet  last
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 
45 
49  unsigned int batch_ip;
50 };
51 
52 
59 static void
60 batch_run (void *cls,
62 {
63  struct BatchState *bs = cls;
64 
65  if (NULL != bs->batch[bs->batch_ip].run)
67  "Running batched command: %s\n",
68  bs->batch[bs->batch_ip].label.value);
69 
70  /* hit end command, leap to next top-level command. */
71  if (NULL == bs->batch[bs->batch_ip].run)
72  {
74  "Exiting from batch: %s\n",
75  bs->label.value);
76  return;
77  }
78  bs->batch[bs->batch_ip].start_time
79  = bs->batch[bs->batch_ip].last_req_time
81  bs->batch[bs->batch_ip].num_tries = 1;
82  bs->batch[bs->batch_ip].run (bs->batch[bs->batch_ip].cls,
83  is);
84 }
85 
86 
93 static void
94 batch_cleanup (void *cls)
95 {
96  struct BatchState *bs = cls;
97 
98  for (unsigned int i = 0;
99  NULL != bs->batch[i].run;
100  i++)
101  bs->batch[i].cleanup (bs->batch[i].cls);
102  GNUNET_free (bs->batch);
103  GNUNET_free (bs);
104 }
105 
106 
116 static enum GNUNET_GenericReturnValue
117 batch_traits (void *cls,
118  const void **ret,
119  const char *trait,
120  unsigned int index)
121 {
122  struct BatchState *bs = cls;
123  // FIXME: these constants should be more global!
124 #define CURRENT_CMD_INDEX 0
125 #define BATCH_INDEX 1
126  struct GNUNET_TESTING_Trait traits[] = {
128  &bs->batch[bs->batch_ip]),
130  bs->batch),
132  };
133 
134  /* Always return current command. */
135  return GNUNET_TESTING_get_trait (traits,
136  ret,
137  trait,
138  index);
139 }
140 
141 
155 GNUNET_TESTING_cmd_batch (const char *label,
156  struct GNUNET_TESTING_Command *batch)
157 {
158  struct BatchState *bs;
159  unsigned int i;
160 
161  bs = GNUNET_new (struct BatchState);
163  label);
164  /* Get number of commands. */
165  for (i = 0; NULL != batch[i].run; i++)
166  /* noop */
167  ;
168 
169  bs->batch = GNUNET_new_array (i + 1,
170  struct GNUNET_TESTING_Command);
171  memcpy (bs->batch,
172  batch,
173  sizeof (struct GNUNET_TESTING_Command) * i);
174  return GNUNET_TESTING_command_new (bs,
175  label,
176  &batch_run,
177  &batch_cleanup,
178  &batch_traits, NULL);
179 }
180 
181 
182 bool
184 {
185  struct BatchState *bs = cls;
186 
187  if (NULL == bs->batch[bs->batch_ip].run)
188  return false;
189  bs->batch[bs->batch_ip].finish_time
191  bs->batch_ip++;
192  return true;
193 }
194 
195 
196 bool
198 {
199  return cmd->run == &batch_run;
200 }
201 
202 
203 struct GNUNET_TESTING_Command *
205 {
206  struct BatchState *bs = cmd->cls;
207 
209  return &bs->batch[bs->batch_ip];
210 }
211 
212 
213 void
215  unsigned int new_ip)
216 {
217  struct BatchState *bs = cmd->cls;
218 
219  /* sanity checks */
221  /* actual logic */
222  bs->batch_ip = new_ip;
223 }
static int ret
Final status code.
Definition: gnunet-arm.c:94
struct GNUNET_TESTING_Interpreter * is
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.
struct GNUNET_TESTING_Trait GNUNET_TESTING_make_trait_cmd(unsigned int index, const struct GNUNET_TESTING_Command *value)
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" of traits array.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Obtain value of a trait from a command.
void GNUNET_TESTING_set_label(struct GNUNET_TESTING_CommandLabel *label, const char *value)
Set label to value.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
State for a "batch" CMD.
unsigned int batch_ip
Internal command pointer.
struct GNUNET_TESTING_CommandLabel label
Our label.
struct GNUNET_TESTING_Command * batch
CMDs batch.
Structure with storage space for a label.
A command to be run by the interpreter.
GNUNET_TESTING_CommandCleanupRoutine cleanup
Clean up after the command.
struct GNUNET_TIME_Absolute finish_time
When did the execution of this command finish?
struct GNUNET_TESTING_CommandLabel label
Label for 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...
GNUNET_TESTING_CommandRunRoutine run
Runs the command.
unsigned int num_tries
How often did we try to execute this command? (In case it is a request that is repated....
struct GNUNET_TIME_Absolute start_time
When did the execution of this command start?
void * cls
Closure for all commands with command-specific context information.
Global state of the interpreter, used by a command to access information about other commands.
A struct GNUNET_TESTING_Trait can be used to exchange data between cmds.
unsigned int index
Index number associated with the trait.
static void batch_cleanup(void *cls)
Cleanup the state from a "reserve status" CMD, and possibly cancel a pending operation thereof.
#define CURRENT_CMD_INDEX
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.
static void batch_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
Run the command.
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_Command GNUNET_TESTING_cmd_batch(const char *label, struct GNUNET_TESTING_Command *batch)
Create a "batch" command.
static enum GNUNET_GenericReturnValue batch_traits(void *cls, const void **ret, const char *trait, unsigned int index)
Offer internal data from a "batch" CMD, to other commands.
bool GNUNET_TESTING_cmd_batch_next_(void *cls)
Advance internal pointer to next command.
bool GNUNET_TESTING_cmd_is_batch_(const struct GNUNET_TESTING_Command *cmd)
Test if this command is a batch command.
#define BATCH_INDEX