aboutsummaryrefslogtreecommitdiff
path: root/src/lib/testing/testing_api_cmd_batch.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/testing/testing_api_cmd_batch.c')
-rw-r--r--src/lib/testing/testing_api_cmd_batch.c232
1 files changed, 232 insertions, 0 deletions
diff --git a/src/lib/testing/testing_api_cmd_batch.c b/src/lib/testing/testing_api_cmd_batch.c
new file mode 100644
index 000000000..f4e866b78
--- /dev/null
+++ b/src/lib/testing/testing_api_cmd_batch.c
@@ -0,0 +1,232 @@
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_lib.h"
29#include "testing_api_cmd_batch.h"
30#include "testing_api_loop.h"
31
32/**
33 * State for a "batch" CMD.
34 */
35struct BatchState
36{
37 /**
38 * CMDs batch.
39 */
40 struct GNUNET_TESTING_Command *batch;
41
42 /**
43 * Our label.
44 */
45 struct GNUNET_TESTING_CommandLabel label;
46
47 /**
48 * Internal command pointer.
49 */
50 unsigned int batch_ip;
51};
52
53
54/**
55 * Run the command.
56 *
57 * @param cls closure.
58 * @param is the interpreter state.
59 */
60static void
61batch_run (void *cls,
62 struct GNUNET_TESTING_Interpreter *is)
63{
64 struct BatchState *bs = cls;
65 struct GNUNET_TESTING_Command *cmd;
66
67 cmd = &bs->batch[bs->batch_ip];
68 if (NULL != cmd->run)
69 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
70 "Running batched command: %s\n",
71 cmd->label.value);
72
73 /* hit end command, leap to next top-level command. */
74 if (NULL == cmd->run)
75 {
76 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
77 "Exiting from batch: %s\n",
78 bs->label.value);
79 GNUNET_TESTING_interpreter_next_ (is);
80 return;
81 }
82 GNUNET_TESTING_interpreter_run_cmd_ (is,
83 cmd);
84}
85
86
87/**
88 * Cleanup the state from a "reserve status" CMD, and possibly
89 * cancel a pending operation thereof.
90 *
91 * @param cls closure.
92 */
93static void
94batch_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
107/**
108 * Offer internal data from a "batch" CMD, to other commands.
109 *
110 * @param cls closure.
111 * @param[out] ret result.
112 * @param trait name of the trait.
113 * @param index index number of the object to offer.
114 * @return #GNUNET_OK on success.
115 */
116static enum GNUNET_GenericReturnValue
117batch_traits (void *cls,
118 const void **ret,
119 const char *trait,
120 unsigned int index)
121{
122 struct BatchState *bs = cls;
123 struct GNUNET_TESTING_Trait traits[] = {
124 GNUNET_TESTING_make_trait_cmd (&bs->batch[bs->batch_ip]),
125 GNUNET_TESTING_make_trait_batch_cmds (&bs->batch),
126 GNUNET_TESTING_trait_end ()
127 };
128
129 /* Always return current command. */
130 return GNUNET_TESTING_get_trait (traits,
131 ret,
132 trait,
133 index);
134}
135
136
137/**
138 * Create a "batch" command. Such command takes a
139 * end_CMD-terminated array of CMDs and executed them.
140 * Once it hits the end CMD, it passes the control
141 * to the next top-level CMD, regardless of it being
142 * another batch or ordinary CMD.
143 *
144 * @param label the command label.
145 * @param batch array of CMDs to execute.
146 *
147 * @return the command.
148 */
149struct GNUNET_TESTING_Command
150GNUNET_TESTING_cmd_batch (const char *label,
151 struct GNUNET_TESTING_Command *batch)
152{
153 struct BatchState *bs;
154 unsigned int i;
155
156 bs = GNUNET_new (struct BatchState);
157 GNUNET_TESTING_set_label (&bs->label,
158 label);
159 /* Get number of commands. */
160 for (i = 0; NULL != batch[i].run; i++)
161 /* noop */
162 ;
163
164 bs->batch = GNUNET_new_array (i + 1,
165 struct GNUNET_TESTING_Command);
166 memcpy (bs->batch,
167 batch,
168 sizeof (struct GNUNET_TESTING_Command) * i);
169 return GNUNET_TESTING_command_new (bs,
170 label,
171 &batch_run,
172 &batch_cleanup,
173 &batch_traits);
174}
175
176
177bool
178GNUNET_TESTING_cmd_batch_next_ (void *cls)
179{
180 struct BatchState *bs = cls;
181 struct GNUNET_TESTING_Command *bcmd = &bs->batch[bs->batch_ip];
182
183 if (NULL == bcmd->run)
184 return true; /* this batch is done */
185 if (GNUNET_TESTING_cmd_is_batch_ (bcmd))
186 {
187 if (GNUNET_TESTING_cmd_batch_next_ (bcmd->cls))
188 {
189 /* sub-batch is done */
190 bcmd->finish_time = GNUNET_TIME_absolute_get ();
191 bs->batch_ip++;
192 return false;
193 }
194 }
195 /* Simple command is done */
196 bcmd->finish_time = GNUNET_TIME_absolute_get ();
197 bs->batch_ip++;
198 return false;
199}
200
201
202bool
203GNUNET_TESTING_cmd_is_batch_ (const struct GNUNET_TESTING_Command *cmd)
204{
205 return cmd->run == &batch_run;
206}
207
208
209struct GNUNET_TESTING_Command *
210GNUNET_TESTING_cmd_batch_get_current_ (const struct GNUNET_TESTING_Command *cmd)
211{
212 struct BatchState *bs = cmd->cls;
213
214 GNUNET_assert (GNUNET_TESTING_cmd_is_batch_ (cmd));
215 return &bs->batch[bs->batch_ip];
216}
217
218
219void
220GNUNET_TESTING_cmd_batch_set_current_ (
221 const struct GNUNET_TESTING_Command *cmd,
222 unsigned int new_ip)
223{
224 struct BatchState *bs = cmd->cls;
225
226 /* sanity checks */
227 GNUNET_assert (cmd->run == &batch_run);
228 for (unsigned int i = 0; i < new_ip; i++)
229 GNUNET_assert (NULL != bs->batch[i].run);
230 /* actual logic */
231 bs->batch_ip = new_ip;
232}