aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-05-08 12:42:15 +0200
committerChristian Grothoff <christian@grothoff.org>2018-05-08 12:42:15 +0200
commit3915b341a6218c6a6a1b1261ae69d8cf56b424e6 (patch)
tree324a75517920af69244f608ae763febc21fb8f52 /src
parentbe4754c02fdbefc4fbf5046973c7c1ab44f8e92f (diff)
downloadgnunet-3915b341a6218c6a6a1b1261ae69d8cf56b424e6.tar.gz
gnunet-3915b341a6218c6a6a1b1261ae69d8cf56b424e6.zip
expand test_mq testcase
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_protocols.h5
-rw-r--r--src/util/test_mq.c276
2 files changed, 271 insertions, 10 deletions
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h
index 1ef00cfc8..c5e45d5c1 100644
--- a/src/include/gnunet_protocols.h
+++ b/src/include/gnunet_protocols.h
@@ -66,6 +66,11 @@ extern "C"
66 */ 66 */
67#define GNUNET_MESSAGE_TYPE_DUMMY 2 67#define GNUNET_MESSAGE_TYPE_DUMMY 2
68 68
69/**
70 * Another dummy messages for testing / benchmarking.
71 */
72#define GNUNET_MESSAGE_TYPE_DUMMY2 3
73
69/******************************************************************************* 74/*******************************************************************************
70 * RESOLVER message types 75 * RESOLVER message types
71 ******************************************************************************/ 76 ******************************************************************************/
diff --git a/src/util/test_mq.c b/src/util/test_mq.c
index 9e8fc844e..533cb8af3 100644
--- a/src/util/test_mq.c
+++ b/src/util/test_mq.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V. 3 Copyright (C) 2012, 2018 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -20,11 +20,22 @@
20 20
21/** 21/**
22 * @file util/test_mq.c 22 * @file util/test_mq.c
23 * @brief simple tests for mq 23 * @brief tests for mq
24 * @author Florian Dold
25 * @author Christian Grothoff
24 */ 26 */
25#include "platform.h" 27#include "platform.h"
26#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
27 29
30#define NUM_TRANSMISSIONS 500
31
32/**
33 * How long does the receiver take per message?
34 */
35#define RECEIVER_THROTTLE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
36
37static unsigned int received_cnt;
38
28 39
29GNUNET_NETWORK_STRUCT_BEGIN 40GNUNET_NETWORK_STRUCT_BEGIN
30 41
@@ -36,6 +47,216 @@ struct MyMessage
36 47
37GNUNET_NETWORK_STRUCT_END 48GNUNET_NETWORK_STRUCT_END
38 49
50static int global_ret;
51
52static struct GNUNET_SCHEDULER_Task *tt;
53
54static struct GNUNET_SCHEDULER_Task *dt;
55
56static struct GNUNET_MQ_Handle *cmq;
57
58
59static void
60do_shutdown (void *cls)
61{
62 (void) cls;
63 if (NULL != tt)
64 {
65 GNUNET_SCHEDULER_cancel (tt);
66 tt = NULL;
67 }
68 if (NULL != cmq)
69 {
70 GNUNET_MQ_destroy (cmq);
71 cmq = NULL;
72 }
73}
74
75
76static void
77do_timeout (void *cls)
78{
79 (void) cls;
80 tt = NULL;
81 GNUNET_SCHEDULER_shutdown ();
82 global_ret = 1;
83}
84
85
86/**
87 * Generic error handler, called with the appropriate
88 * error code and the same closure specified at the creation of
89 * the message queue.
90 * Not every message queue implementation supports an error handler.
91 *
92 * @param cls closure
93 * @param error error code
94 */
95static void
96error_cb (void *cls,
97 enum GNUNET_MQ_Error error)
98{
99 GNUNET_break (0);
100 global_ret = 3;
101 GNUNET_SCHEDULER_shutdown ();
102}
103
104
105static void
106client_continue (void *cls)
107{
108 struct GNUNET_SERVICE_Client *c = cls;
109
110 dt = NULL;
111 GNUNET_SERVICE_client_continue (c);
112}
113
114
115static void
116handle_dummy (void *cls,
117 const struct MyMessage *msg)
118{
119 struct GNUNET_SERVICE_Client *c = cls;
120
121 GNUNET_assert (NULL == dt);
122 /* artificially make receiver slower than sender */
123 dt = GNUNET_SCHEDULER_add_delayed (RECEIVER_THROTTLE,
124 &client_continue,
125 c);
126 if (received_cnt != ntohl (msg->x))
127 {
128 GNUNET_break (0);
129 global_ret = 4;
130 GNUNET_SCHEDULER_shutdown ();
131 }
132 received_cnt++;
133}
134
135
136static void
137handle_dummy2 (void *cls,
138 const struct MyMessage *msg)
139{
140 struct GNUNET_SERVICE_Client *c = cls;
141
142 GNUNET_SERVICE_client_continue (c);
143 if (NUM_TRANSMISSIONS != received_cnt)
144 {
145 GNUNET_break (0);
146 global_ret = 5;
147 }
148 GNUNET_SCHEDULER_shutdown ();
149}
150
151
152/**
153 * Function called whenever MQ has sent a message.
154 */
155static void
156notify_sent_cb (void *cls)
157{
158 static unsigned int seen;
159 unsigned int *cnt = cls;
160
161 if (seen != *cnt)
162 {
163 GNUNET_break (0);
164 global_ret = 6;
165 GNUNET_SCHEDULER_shutdown ();
166 }
167 seen++;
168 GNUNET_free (cnt);
169}
170
171
172/**
173 * Start running the actual test.
174 *
175 * @param cls closure passed to #GNUNET_SERVICE_MAIN
176 * @param cfg configuration to use for this service
177 * @param sh handle to the newly create service
178 */
179static void
180run (void *cls,
181 const struct GNUNET_CONFIGURATION_Handle *cfg,
182 struct GNUNET_SERVICE_Handle *sh)
183{
184 struct GNUNET_MQ_MessageHandler ch[] = {
185 GNUNET_MQ_handler_end ()
186 };
187 struct GNUNET_MQ_Envelope *env;
188 struct MyMessage *m;
189
190 (void) cls;
191 (void) sh;
192 cmq = GNUNET_CLIENT_connect (cfg,
193 "test_client",
194 ch,
195 &error_cb,
196 NULL);
197 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
198 NULL);
199 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
200 &do_timeout,
201 NULL);
202 for (unsigned int i=0;i<NUM_TRANSMISSIONS;i++)
203 {
204 unsigned int *cnt;
205
206 cnt = GNUNET_new (unsigned int);
207 *cnt = i;
208 env = GNUNET_MQ_msg (m,
209 GNUNET_MESSAGE_TYPE_DUMMY);
210 GNUNET_MQ_notify_sent (env,
211 &notify_sent_cb,
212 cnt);
213 m->x = htonl (i);
214 GNUNET_MQ_send (cmq,
215 env);
216 }
217 env = GNUNET_MQ_msg (m,
218 GNUNET_MESSAGE_TYPE_DUMMY2);
219 GNUNET_MQ_send (cmq,
220 env);
221}
222
223
224/**
225 * Callback to be called when a client connects to the service.
226 *
227 * @param cls closure for the service
228 * @param c the new client that connected to the service
229 * @param mq the message queue used to send messages to the client
230 * @return the client-specific (`internal') closure
231 */
232static void *
233connect_cb (void *cls,
234 struct GNUNET_SERVICE_Client *c,
235 struct GNUNET_MQ_Handle *mq)
236{
237 (void) cls;
238 (void) mq;
239 return c;
240}
241
242
243/**
244 * Callback to be called when a client disconnected from the service
245 *
246 * @param cls closure for the service
247 * @param c the client that disconnected
248 * @param internal_cls the client-specific (`internal') closure
249 */
250static void
251disconnect_cb (void *cls,
252 struct GNUNET_SERVICE_Client *c,
253 void *internal_cls)
254{
255 (void) cls;
256 (void) c;
257 (void) internal_cls;
258}
259
39 260
40static void 261static void
41test1 () 262test1 ()
@@ -46,10 +267,11 @@ test1 ()
46 mm = NULL; 267 mm = NULL;
47 mqm = NULL; 268 mqm = NULL;
48 269
49 mqm = GNUNET_MQ_msg (mm, 42); 270 mqm = GNUNET_MQ_msg (mm,
271 GNUNET_MESSAGE_TYPE_DUMMY);
50 GNUNET_assert (NULL != mqm); 272 GNUNET_assert (NULL != mqm);
51 GNUNET_assert (NULL != mm); 273 GNUNET_assert (NULL != mm);
52 GNUNET_assert (42 == ntohs (mm->header.type)); 274 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mm->header.type));
53 GNUNET_assert (sizeof (struct MyMessage) == ntohs (mm->header.size)); 275 GNUNET_assert (sizeof (struct MyMessage) == ntohs (mm->header.size));
54 GNUNET_MQ_discard (mqm); 276 GNUNET_MQ_discard (mqm);
55} 277}
@@ -61,13 +283,15 @@ test2 ()
61 struct GNUNET_MQ_Envelope *mqm; 283 struct GNUNET_MQ_Envelope *mqm;
62 struct GNUNET_MessageHeader *mh; 284 struct GNUNET_MessageHeader *mh;
63 285
64 mqm = GNUNET_MQ_msg_header (42); 286 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_DUMMY);
65 /* how could the above be checked? */ 287 /* how could the above be checked? */
66 288
67 GNUNET_MQ_discard (mqm); 289 GNUNET_MQ_discard (mqm);
68 290
69 mqm = GNUNET_MQ_msg_header_extra (mh, 20, 42); 291 mqm = GNUNET_MQ_msg_header_extra (mh,
70 GNUNET_assert (42 == ntohs (mh->type)); 292 20,
293 GNUNET_MESSAGE_TYPE_DUMMY);
294 GNUNET_assert (GNUNET_MESSAGE_TYPE_DUMMY == ntohs (mh->type));
71 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) + 20 == ntohs (mh->size)); 295 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) + 20 == ntohs (mh->size));
72 GNUNET_MQ_discard (mqm); 296 GNUNET_MQ_discard (mqm);
73} 297}
@@ -76,9 +300,41 @@ test2 ()
76int 300int
77main (int argc, char **argv) 301main (int argc, char **argv)
78{ 302{
79 GNUNET_log_setup ("test-mq", "INFO", NULL); 303 char * test_argv[] = {
304 (char *) "test_client",
305 "-c",
306 "test_client_data.conf",
307 NULL
308 };
309 struct GNUNET_MQ_MessageHandler mh[] = {
310 GNUNET_MQ_hd_fixed_size (dummy,
311 GNUNET_MESSAGE_TYPE_DUMMY,
312 struct MyMessage,
313 NULL),
314 GNUNET_MQ_hd_fixed_size (dummy2,
315 GNUNET_MESSAGE_TYPE_DUMMY2,
316 struct MyMessage,
317 NULL),
318 GNUNET_MQ_handler_end ()
319 };
320
321 (void) argc;
322 (void) argv;
323 GNUNET_log_setup ("test-mq",
324 "INFO",
325 NULL);
80 test1 (); 326 test1 ();
81 test2 (); 327 test2 ();
82 return 0; 328 if (0 !=
329 GNUNET_SERVICE_run_ (3,
330 test_argv,
331 "test_client",
332 GNUNET_SERVICE_OPTION_NONE,
333 &run,
334 &connect_cb,
335 &disconnect_cb,
336 NULL,
337 mh))
338 return 1;
339 return global_ret;
83} 340}
84