aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_mq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_mq.c')
-rw-r--r--src/util/test_mq.c281
1 files changed, 140 insertions, 141 deletions
diff --git a/src/util/test_mq.c b/src/util/test_mq.c
index 9a396bcae..4282f06b7 100644
--- a/src/util/test_mq.c
+++ b/src/util/test_mq.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file util/test_mq.c 22 * @file util/test_mq.c
@@ -32,15 +32,14 @@
32/** 32/**
33 * How long does the receiver take per message? 33 * How long does the receiver take per message?
34 */ 34 */
35#define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1) 35#define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1)
36 36
37static unsigned int received_cnt; 37static unsigned int received_cnt;
38 38
39 39
40GNUNET_NETWORK_STRUCT_BEGIN 40GNUNET_NETWORK_STRUCT_BEGIN
41 41
42struct MyMessage 42struct MyMessage {
43{
44 struct GNUNET_MessageHeader header; 43 struct GNUNET_MessageHeader header;
45 uint32_t x GNUNET_PACKED; 44 uint32_t x GNUNET_PACKED;
46}; 45};
@@ -57,28 +56,28 @@ static struct GNUNET_MQ_Handle *cmq;
57 56
58 57
59static void 58static void
60do_shutdown (void *cls) 59do_shutdown(void *cls)
61{ 60{
62 (void) cls; 61 (void)cls;
63 if (NULL != tt) 62 if (NULL != tt)
64 { 63 {
65 GNUNET_SCHEDULER_cancel (tt); 64 GNUNET_SCHEDULER_cancel(tt);
66 tt = NULL; 65 tt = NULL;
67 } 66 }
68 if (NULL != cmq) 67 if (NULL != cmq)
69 { 68 {
70 GNUNET_MQ_destroy (cmq); 69 GNUNET_MQ_destroy(cmq);
71 cmq = NULL; 70 cmq = NULL;
72 } 71 }
73} 72}
74 73
75 74
76static void 75static void
77do_timeout (void *cls) 76do_timeout(void *cls)
78{ 77{
79 (void) cls; 78 (void)cls;
80 tt = NULL; 79 tt = NULL;
81 GNUNET_SCHEDULER_shutdown (); 80 GNUNET_SCHEDULER_shutdown();
82 global_ret = 1; 81 global_ret = 1;
83} 82}
84 83
@@ -93,59 +92,59 @@ do_timeout (void *cls)
93 * @param error error code 92 * @param error error code
94 */ 93 */
95static void 94static void
96error_cb (void *cls, 95error_cb(void *cls,
97 enum GNUNET_MQ_Error error) 96 enum GNUNET_MQ_Error error)
98{ 97{
99 GNUNET_break (0); 98 GNUNET_break(0);
100 global_ret = 3; 99 global_ret = 3;
101 GNUNET_SCHEDULER_shutdown (); 100 GNUNET_SCHEDULER_shutdown();
102} 101}
103 102
104 103
105static void 104static void
106client_continue (void *cls) 105client_continue(void *cls)
107{ 106{
108 struct GNUNET_SERVICE_Client *c = cls; 107 struct GNUNET_SERVICE_Client *c = cls;
109 108
110 dt = NULL; 109 dt = NULL;
111 GNUNET_SERVICE_client_continue (c); 110 GNUNET_SERVICE_client_continue(c);
112} 111}
113 112
114 113
115static void 114static void
116handle_dummy (void *cls, 115handle_dummy(void *cls,
117 const struct MyMessage *msg) 116 const struct MyMessage *msg)
118{ 117{
119 struct GNUNET_SERVICE_Client *c = cls; 118 struct GNUNET_SERVICE_Client *c = cls;
120 119
121 GNUNET_assert (NULL == dt); 120 GNUNET_assert(NULL == dt);
122 /* artificially make receiver slower than sender */ 121 /* artificially make receiver slower than sender */
123 dt = GNUNET_SCHEDULER_add_delayed (RECEIVER_THROTTLE, 122 dt = GNUNET_SCHEDULER_add_delayed(RECEIVER_THROTTLE,
124 &client_continue, 123 &client_continue,
125 c); 124 c);
126 if (received_cnt != ntohl (msg->x)) 125 if (received_cnt != ntohl(msg->x))
127 { 126 {
128 GNUNET_break (0); 127 GNUNET_break(0);
129 global_ret = 4; 128 global_ret = 4;
130 GNUNET_SCHEDULER_shutdown (); 129 GNUNET_SCHEDULER_shutdown();
131 } 130 }
132 received_cnt++; 131 received_cnt++;
133} 132}
134 133
135 134
136static void 135static void
137handle_dummy2 (void *cls, 136handle_dummy2(void *cls,
138 const struct MyMessage *msg) 137 const struct MyMessage *msg)
139{ 138{
140 struct GNUNET_SERVICE_Client *c = cls; 139 struct GNUNET_SERVICE_Client *c = cls;
141 140
142 GNUNET_SERVICE_client_continue (c); 141 GNUNET_SERVICE_client_continue(c);
143 if (NUM_TRANSMISSIONS != received_cnt) 142 if (NUM_TRANSMISSIONS != received_cnt)
144 { 143 {
145 GNUNET_break (0); 144 GNUNET_break(0);
146 global_ret = 5; 145 global_ret = 5;
147 } 146 }
148 GNUNET_SCHEDULER_shutdown (); 147 GNUNET_SCHEDULER_shutdown();
149} 148}
150 149
151 150
@@ -153,19 +152,19 @@ handle_dummy2 (void *cls,
153 * Function called whenever MQ has sent a message. 152 * Function called whenever MQ has sent a message.
154 */ 153 */
155static void 154static void
156notify_sent_cb (void *cls) 155notify_sent_cb(void *cls)
157{ 156{
158 static unsigned int seen; 157 static unsigned int seen;
159 unsigned int *cnt = cls; 158 unsigned int *cnt = cls;
160 159
161 if (seen != *cnt) 160 if (seen != *cnt)
162 { 161 {
163 GNUNET_break (0); 162 GNUNET_break(0);
164 global_ret = 6; 163 global_ret = 6;
165 GNUNET_SCHEDULER_shutdown (); 164 GNUNET_SCHEDULER_shutdown();
166 } 165 }
167 seen++; 166 seen++;
168 GNUNET_free (cnt); 167 GNUNET_free(cnt);
169} 168}
170 169
171 170
@@ -177,47 +176,47 @@ notify_sent_cb (void *cls)
177 * @param sh handle to the newly create service 176 * @param sh handle to the newly create service
178 */ 177 */
179static void 178static void
180run (void *cls, 179run(void *cls,
181 const struct GNUNET_CONFIGURATION_Handle *cfg, 180 const struct GNUNET_CONFIGURATION_Handle *cfg,
182 struct GNUNET_SERVICE_Handle *sh) 181 struct GNUNET_SERVICE_Handle *sh)
183{ 182{
184 struct GNUNET_MQ_MessageHandler ch[] = { 183 struct GNUNET_MQ_MessageHandler ch[] = {
185 GNUNET_MQ_handler_end () 184 GNUNET_MQ_handler_end()
186 }; 185 };
187 struct GNUNET_MQ_Envelope *env; 186 struct GNUNET_MQ_Envelope *env;
188 struct MyMessage *m; 187 struct MyMessage *m;
189 188
190 (void) cls; 189 (void)cls;
191 (void) sh; 190 (void)sh;
192 cmq = GNUNET_CLIENT_connect (cfg, 191 cmq = GNUNET_CLIENT_connect(cfg,
193 "test_client", 192 "test_client",
194 ch, 193 ch,
195 &error_cb, 194 &error_cb,
196 NULL); 195 NULL);
197 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 196 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
198 NULL); 197 NULL);
199 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 198 tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES,
200 &do_timeout, 199 &do_timeout,
201 NULL); 200 NULL);
202 for (unsigned int i=0;i<NUM_TRANSMISSIONS;i++) 201 for (unsigned int i = 0; i < NUM_TRANSMISSIONS; i++)
203 { 202 {
204 unsigned int *cnt; 203 unsigned int *cnt;
205 204
206 cnt = GNUNET_new (unsigned int); 205 cnt = GNUNET_new(unsigned int);
207 *cnt = i; 206 *cnt = i;
208 env = GNUNET_MQ_msg (m, 207 env = GNUNET_MQ_msg(m,
209 GNUNET_MESSAGE_TYPE_DUMMY); 208 GNUNET_MESSAGE_TYPE_DUMMY);
210 GNUNET_MQ_notify_sent (env, 209 GNUNET_MQ_notify_sent(env,
211 &notify_sent_cb, 210 &notify_sent_cb,
212 cnt); 211 cnt);
213 m->x = htonl (i); 212 m->x = htonl(i);
214 GNUNET_MQ_send (cmq, 213 GNUNET_MQ_send(cmq,
215 env); 214 env);
216 } 215 }
217 env = GNUNET_MQ_msg (m, 216 env = GNUNET_MQ_msg(m,
218 GNUNET_MESSAGE_TYPE_DUMMY2); 217 GNUNET_MESSAGE_TYPE_DUMMY2);
219 GNUNET_MQ_send (cmq, 218 GNUNET_MQ_send(cmq,
220 env); 219 env);
221} 220}
222 221
223 222
@@ -230,12 +229,12 @@ run (void *cls,
230 * @return the client-specific (`internal') closure 229 * @return the client-specific (`internal') closure
231 */ 230 */
232static void * 231static void *
233connect_cb (void *cls, 232connect_cb(void *cls,
234 struct GNUNET_SERVICE_Client *c, 233 struct GNUNET_SERVICE_Client *c,
235 struct GNUNET_MQ_Handle *mq) 234 struct GNUNET_MQ_Handle *mq)
236{ 235{
237 (void) cls; 236 (void)cls;
238 (void) mq; 237 (void)mq;
239 return c; 238 return c;
240} 239}
241 240
@@ -248,18 +247,18 @@ connect_cb (void *cls,
248 * @param internal_cls the client-specific (`internal') closure 247 * @param internal_cls the client-specific (`internal') closure
249 */ 248 */
250static void 249static void
251disconnect_cb (void *cls, 250disconnect_cb(void *cls,
252 struct GNUNET_SERVICE_Client *c, 251 struct GNUNET_SERVICE_Client *c,
253 void *internal_cls) 252 void *internal_cls)
254{ 253{
255 (void) cls; 254 (void)cls;
256 (void) c; 255 (void)c;
257 (void) internal_cls; 256 (void)internal_cls;
258} 257}
259 258
260 259
261static void 260static void
262test1 () 261test1()
263{ 262{
264 struct GNUNET_MQ_Envelope *mqm; 263 struct GNUNET_MQ_Envelope *mqm;
265 struct MyMessage *mm; 264 struct MyMessage *mm;
@@ -267,74 +266,74 @@ test1 ()
267 mm = NULL; 266 mm = NULL;
268 mqm = NULL; 267 mqm = NULL;
269 268
270 mqm = GNUNET_MQ_msg (mm, 269 mqm = GNUNET_MQ_msg(mm,
271 GNUNET_MESSAGE_TYPE_DUMMY); 270 GNUNET_MESSAGE_TYPE_DUMMY);
272 GNUNET_assert (NULL != mqm); 271 GNUNET_assert(NULL != mqm);
273 GNUNET_assert (NULL != mm); 272 GNUNET_assert(NULL != mm);
274 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mm->header.type)); 273 GNUNET_assert(GNUNET_MESSAGE_TYPE_DUMMY == ntohs(mm->header.type));
275 GNUNET_assert (sizeof (struct MyMessage) == ntohs (mm->header.size)); 274 GNUNET_assert(sizeof(struct MyMessage) == ntohs(mm->header.size));
276 GNUNET_MQ_discard (mqm); 275 GNUNET_MQ_discard(mqm);
277} 276}
278 277
279 278
280static void 279static void
281test2 () 280test2()
282{ 281{
283 struct GNUNET_MQ_Envelope *mqm; 282 struct GNUNET_MQ_Envelope *mqm;
284 struct GNUNET_MessageHeader *mh; 283 struct GNUNET_MessageHeader *mh;
285 284
286 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_DUMMY); 285 mqm = GNUNET_MQ_msg_header(GNUNET_MESSAGE_TYPE_DUMMY);
287 /* how could the above be checked? */ 286 /* how could the above be checked? */
288 287
289 GNUNET_MQ_discard (mqm); 288 GNUNET_MQ_discard(mqm);
290 289
291 mqm = GNUNET_MQ_msg_header_extra (mh, 290 mqm = GNUNET_MQ_msg_header_extra(mh,
292 20, 291 20,
293 GNUNET_MESSAGE_TYPE_DUMMY); 292 GNUNET_MESSAGE_TYPE_DUMMY);
294 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mh->type)); 293 GNUNET_assert(GNUNET_MESSAGE_TYPE_DUMMY == ntohs(mh->type));
295 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) + 20 == ntohs (mh->size)); 294 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) + 20 == ntohs(mh->size));
296 GNUNET_MQ_discard (mqm); 295 GNUNET_MQ_discard(mqm);
297} 296}
298 297
299 298
300int 299int
301main (int argc, char **argv) 300main(int argc, char **argv)
302{ 301{
303 char * test_argv[] = { 302 char * test_argv[] = {
304 (char *) "test_client", 303 (char *)"test_client",
305 "-c", 304 "-c",
306 "test_client_data.conf", 305 "test_client_data.conf",
307 NULL 306 NULL
308 }; 307 };
309 struct GNUNET_MQ_MessageHandler mh[] = { 308 struct GNUNET_MQ_MessageHandler mh[] = {
310 GNUNET_MQ_hd_fixed_size (dummy, 309 GNUNET_MQ_hd_fixed_size(dummy,
311 GNUNET_MESSAGE_TYPE_DUMMY, 310 GNUNET_MESSAGE_TYPE_DUMMY,
312 struct MyMessage, 311 struct MyMessage,
313 NULL), 312 NULL),
314 GNUNET_MQ_hd_fixed_size (dummy2, 313 GNUNET_MQ_hd_fixed_size(dummy2,
315 GNUNET_MESSAGE_TYPE_DUMMY2, 314 GNUNET_MESSAGE_TYPE_DUMMY2,
316 struct MyMessage, 315 struct MyMessage,
317 NULL), 316 NULL),
318 GNUNET_MQ_handler_end () 317 GNUNET_MQ_handler_end()
319 }; 318 };
320 319
321 (void) argc; 320 (void)argc;
322 (void) argv; 321 (void)argv;
323 GNUNET_log_setup ("test-mq", 322 GNUNET_log_setup("test-mq",
324 "INFO", 323 "INFO",
325 NULL); 324 NULL);
326 test1 (); 325 test1();
327 test2 (); 326 test2();
328 if (0 != 327 if (0 !=
329 GNUNET_SERVICE_run_ (3, 328 GNUNET_SERVICE_run_(3,
330 test_argv, 329 test_argv,
331 "test_client", 330 "test_client",
332 GNUNET_SERVICE_OPTION_NONE, 331 GNUNET_SERVICE_OPTION_NONE,
333 &run, 332 &run,
334 &connect_cb, 333 &connect_cb,
335 &disconnect_cb, 334 &disconnect_cb,
336 NULL, 335 NULL,
337 mh)) 336 mh))
338 return 1; 337 return 1;
339 return global_ret; 338 return global_ret;
340} 339}