aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_batch.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testing/testing_api_cmd_batch.c')
-rw-r--r--src/testing/testing_api_cmd_batch.c256
1 files changed, 0 insertions, 256 deletions
diff --git a/src/testing/testing_api_cmd_batch.c b/src/testing/testing_api_cmd_batch.c
deleted file mode 100644
index af260f80d..000000000
--- a/src/testing/testing_api_cmd_batch.c
+++ /dev/null
@@ -1,256 +0,0 @@
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
21/**
22 * @file testing/testing_api_cmd_batch.c
23 * @brief Implement batch-execution of CMDs.
24 * @author Marcello Stanisci (GNU Taler testing)
25 * @author t3sserakt
26 */
27#include "platform.h"
28#include "gnunet_testing_ng_lib.h"
29#include "testing.h"
30
31/**
32 * State for a "batch" CMD.
33 */
34struct BatchState
35{
36 /**
37 * CMDs batch.
38 */
39 struct GNUNET_TESTING_Command *batch;
40
41 /**
42 * Internal command pointer.
43 */
44 unsigned int batch_ip;
45};
46
47
48/**
49 * Run the command.
50 *
51 * @param cls closure.
52 * @param cmd the command being executed.
53 * @param is the interpreter state.
54 */
55static void
56batch_run (void *cls,
57 const struct GNUNET_TESTING_Command *cmd,
58 struct GNUNET_TESTING_Interpreter *is)
59{
60 struct BatchState *bs = cls;
61
62 if (NULL != bs->batch[bs->batch_ip].label)
63 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
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 {
70 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
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
77 = GNUNET_TIME_absolute_get ();
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
85/**
86 * Cleanup the state from a "reserve status" CMD, and possibly
87 * cancel a pending operation thereof.
88 *
89 * @param cls closure.
90 * @param cmd the command which is being cleaned up.
91 */
92static void
93batch_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
109/**
110 * Offer internal data from a "batch" CMD, to other commands.
111 *
112 * @param cls closure.
113 * @param[out] ret result.
114 * @param trait name of the trait.
115 * @param index index number of the object to offer.
116 * @return #GNUNET_OK on success.
117 */
118static int
119batch_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[] = {
130 GNUNET_TESTING_make_trait_cmd
131 (CURRENT_CMD_INDEX, &bs->batch[bs->batch_ip]),
132 GNUNET_TESTING_make_trait_cmd
133 (BATCH_INDEX, bs->batch),
134 GNUNET_TESTING_trait_end ()
135 };
136
137 /* Always return current command. */
138 return GNUNET_TESTING_get_trait (traits,
139 ret,
140 trait,
141 index);
142}
143
144
145/**
146 * Create a "batch" command. Such command takes a
147 * end_CMD-terminated array of CMDs and executed them.
148 * Once it hits the end CMD, it passes the control
149 * to the next top-level CMD, regardless of it being
150 * another batch or ordinary CMD.
151 *
152 * @param label the command label.
153 * @param batch array of CMDs to execute.
154 *
155 * @return the command.
156 */
157struct GNUNET_TESTING_Command
158GNUNET_TESTING_cmd_batch (const char *label,
159 struct GNUNET_TESTING_Command *batch)
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
190/**
191 * Advance internal pointer to next command.
192 *
193 * @param is interpreter state.
194 */
195void
196GNUNET_TESTING_cmd_batch_next (struct GNUNET_TESTING_Interpreter *is)
197{
198 struct BatchState *bs = is->commands[is->ip].cls;
199
200 if (NULL == bs->batch[bs->batch_ip].label)
201 {
202 is->commands[is->ip].finish_time = GNUNET_TIME_absolute_get ();
203 is->ip++;
204 return;
205 }
206 bs->batch[bs->batch_ip].finish_time = GNUNET_TIME_absolute_get ();
207 bs->batch_ip++;
208}
209
210
211/**
212 * Test if this command is a batch command.
213 *
214 * @return false if not, true if it is a batch command
215 */
216int
217GNUNET_TESTING_cmd_is_batch (const struct GNUNET_TESTING_Command *cmd)
218{
219 return cmd->run == &batch_run;
220}
221
222
223/**
224 * Obtain what command the batch is at.
225 *
226 * @return cmd current batch command
227 */
228struct GNUNET_TESTING_Command *
229GNUNET_TESTING_cmd_batch_get_current (const struct GNUNET_TESTING_Command *cmd)
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
238/**
239 * Set what command the batch should be at.
240 *
241 * @param cmd current batch command
242 * @param new_ip where to move the IP
243 */
244void
245GNUNET_TESTING_cmd_batch_set_current (const struct GNUNET_TESTING_Command *cmd,
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}