aboutsummaryrefslogtreecommitdiff
path: root/src/arm/test_exponential_backoff.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/arm/test_exponential_backoff.c')
-rw-r--r--src/arm/test_exponential_backoff.c389
1 files changed, 196 insertions, 193 deletions
diff --git a/src/arm/test_exponential_backoff.c b/src/arm/test_exponential_backoff.c
index b13ab1cb8..5d1863f37 100644
--- a/src/arm/test_exponential_backoff.c
+++ b/src/arm/test_exponential_backoff.c
@@ -27,15 +27,16 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_protocols.h" 28#include "gnunet_protocols.h"
29 29
30#define LOG(...) GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) 30#define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
31 31
32#define LOG_BACKOFF GNUNET_NO 32#define LOG_BACKOFF GNUNET_NO
33 33
34#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10) 34#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35 35
36#define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL 36#define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
37 37
38#define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 5) 38#define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply ( \
39 GNUNET_TIME_UNIT_MILLISECONDS, 5)
39 40
40#define SERVICE "do-nothing" 41#define SERVICE "do-nothing"
41 42
@@ -74,7 +75,8 @@ static char *killLogFileName;
74/** 75/**
75 * Context for handling the shutdown of a service. 76 * Context for handling the shutdown of a service.
76 */ 77 */
77struct ShutdownContext { 78struct ShutdownContext
79{
78 /** 80 /**
79 * Connection to the service that is being shutdown. 81 * Connection to the service that is being shutdown.
80 */ 82 */
@@ -88,7 +90,7 @@ struct ShutdownContext {
88 90
89 91
90static void 92static void
91kill_task(void *cbData); 93kill_task (void *cbData);
92 94
93 95
94/** 96/**
@@ -97,9 +99,9 @@ kill_task(void *cbData);
97 * @param cls closure 99 * @param cls closure
98 */ 100 */
99static void 101static void
100service_shutdown_timeout(void *cls) 102service_shutdown_timeout (void *cls)
101{ 103{
102 GNUNET_assert(0); 104 GNUNET_assert (0);
103} 105}
104 106
105 107
@@ -112,196 +114,197 @@ service_shutdown_timeout(void *cls)
112 * @param error error code 114 * @param error error code
113 */ 115 */
114static void 116static void
115mq_error_handler(void *cls, 117mq_error_handler (void *cls,
116 enum GNUNET_MQ_Error error) 118 enum GNUNET_MQ_Error error)
117{ 119{
118 struct ShutdownContext *shutdown_ctx = cls; 120 struct ShutdownContext *shutdown_ctx = cls;
119 121
120 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Service shutdown complete (MQ error).\n"); 123 "Service shutdown complete (MQ error).\n");
122 GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); 124 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
123 GNUNET_MQ_destroy(shutdown_ctx->mq); 125 GNUNET_MQ_destroy (shutdown_ctx->mq);
124 GNUNET_free(shutdown_ctx); 126 GNUNET_free (shutdown_ctx);
125} 127}
126 128
127 129
128static void 130static void
129kill_task(void *cbData) 131kill_task (void *cbData)
130{ 132{
131 struct ShutdownContext *shutdown_ctx 133 struct ShutdownContext *shutdown_ctx
132 = GNUNET_new(struct ShutdownContext); 134 = GNUNET_new (struct ShutdownContext);
133 struct GNUNET_MQ_Envelope *env; 135 struct GNUNET_MQ_Envelope *env;
134 struct GNUNET_MessageHeader *msg; 136 struct GNUNET_MessageHeader *msg;
135 struct GNUNET_MQ_MessageHandler handlers[] = { 137 struct GNUNET_MQ_MessageHandler handlers[] = {
136 GNUNET_MQ_handler_end() 138 GNUNET_MQ_handler_end ()
137 }; 139 };
138 140
139 kt = NULL; 141 kt = NULL;
140 if (trialCount == 13) 142 if (trialCount == 13)
141 { 143 {
142 LOG("Saw enough kills, asking ARM to stop mock service for good\n"); 144 LOG ("Saw enough kills, asking ARM to stop mock service for good\n");
143 GNUNET_ARM_request_service_stop(arm, 145 GNUNET_ARM_request_service_stop (arm,
144 SERVICE, 146 SERVICE,
145 NULL, 147 NULL,
146 NULL); 148 NULL);
147 ok = 0; 149 ok = 0;
148 trialCount++; 150 trialCount++;
149 GNUNET_free(shutdown_ctx); 151 GNUNET_free (shutdown_ctx);
150 return; 152 return;
151 } 153 }
152 shutdown_ctx->mq = GNUNET_CLIENT_connect(cfg, 154 shutdown_ctx->mq = GNUNET_CLIENT_connect (cfg,
153 SERVICE, 155 SERVICE,
154 handlers, 156 handlers,
155 &mq_error_handler, 157 &mq_error_handler,
156 shutdown_ctx); 158 shutdown_ctx);
157 GNUNET_assert(NULL != shutdown_ctx->mq); 159 GNUNET_assert (NULL != shutdown_ctx->mq);
158 trialCount++; 160 trialCount++;
159 LOG("Sending a shutdown request to the mock service\n"); 161 LOG ("Sending a shutdown request to the mock service\n");
160 env = GNUNET_MQ_msg(msg, 162 env = GNUNET_MQ_msg (msg,
161 GNUNET_MESSAGE_TYPE_ARM_STOP); /* FIXME: abuse of message type */ 163 GNUNET_MESSAGE_TYPE_ARM_STOP); /* FIXME: abuse of message type */
162 GNUNET_MQ_send(shutdown_ctx->mq, 164 GNUNET_MQ_send (shutdown_ctx->mq,
163 env); 165 env);
164 shutdown_ctx->cancel_task 166 shutdown_ctx->cancel_task
165 = GNUNET_SCHEDULER_add_delayed(TIMEOUT, 167 = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
166 &service_shutdown_timeout, 168 &service_shutdown_timeout,
167 shutdown_ctx); 169 shutdown_ctx);
168} 170}
169 171
170 172
171static void 173static void
172trigger_disconnect(void *cls) 174trigger_disconnect (void *cls)
173{ 175{
174 GNUNET_ARM_disconnect(arm); 176 GNUNET_ARM_disconnect (arm);
175 GNUNET_ARM_monitor_stop(mon); 177 GNUNET_ARM_monitor_stop (mon);
176 if (NULL != kt) 178 if (NULL != kt)
177 { 179 {
178 GNUNET_SCHEDULER_cancel(kt); 180 GNUNET_SCHEDULER_cancel (kt);
179 kt = NULL; 181 kt = NULL;
180 } 182 }
181} 183}
182 184
183 185
184static void 186static void
185arm_stop_cb(void *cls, 187arm_stop_cb (void *cls,
186 enum GNUNET_ARM_RequestStatus status, 188 enum GNUNET_ARM_RequestStatus status,
187 enum GNUNET_ARM_Result result) 189 enum GNUNET_ARM_Result result)
188{ 190{
189 GNUNET_break(status == GNUNET_ARM_REQUEST_SENT_OK); 191 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
190 GNUNET_break(result == GNUNET_ARM_RESULT_STOPPED); 192 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
191 LOG("ARM service stopped\n"); 193 LOG ("ARM service stopped\n");
192 GNUNET_SCHEDULER_shutdown(); 194 GNUNET_SCHEDULER_shutdown ();
193} 195}
194 196
195 197
196static void 198static void
197srv_status(void *cls, 199srv_status (void *cls,
198 const char *service, 200 const char *service,
199 enum GNUNET_ARM_ServiceMonitorStatus status) 201 enum GNUNET_ARM_ServiceMonitorStatus status)
200{ 202{
201 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED) 203 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
202 { 204 {
203 LOG("ARM monitor started, starting mock service\n"); 205 LOG ("ARM monitor started, starting mock service\n");
204 phase++; 206 phase++;
205 GNUNET_ARM_request_service_start(arm, 207 GNUNET_ARM_request_service_start (arm,
206 SERVICE, 208 SERVICE,
207 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 209 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
208 NULL, 210 NULL,
209 NULL); 211 NULL);
210 return; 212 return;
211 } 213 }
212 if (0 != strcasecmp(service, SERVICE)) 214 if (0 != strcasecmp (service, SERVICE))
213 return; /* not what we care about */ 215 return; /* not what we care about */
214 if (phase == 1) 216 if (phase == 1)
217 {
218 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
219 GNUNET_break (phase == 1);
220 LOG ("do-nothing is starting\n");
221 phase++;
222 ok = 1;
223 GNUNET_assert (NULL == kt);
224 startedWaitingAt = GNUNET_TIME_absolute_get ();
225 kt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
226 &kill_task,
227 NULL);
228 }
229 else if (phase == 2)
230 {
231 /* We passively monitor ARM for status updates. ARM should tell us
232 * when do-nothing dies (no need to run a service upness test ourselves).
233 */
234 if (status == GNUNET_ARM_SERVICE_STARTING)
215 { 235 {
216 GNUNET_break(status == GNUNET_ARM_SERVICE_STARTING); 236 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
217 GNUNET_break(phase == 1); 237 LOG ("Waited for: %s\n",
218 LOG("do-nothing is starting\n"); 238 GNUNET_STRINGS_relative_time_to_string (waitedFor,
219 phase++; 239 GNUNET_YES));
220 ok = 1; 240
221 GNUNET_assert(NULL == kt); 241 LOG ("do-nothing is starting, killing it...\n");
222 startedWaitingAt = GNUNET_TIME_absolute_get(); 242 GNUNET_assert (NULL == kt);
223 kt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, 243 kt = GNUNET_SCHEDULER_add_now (&kill_task, &ok);
224 &kill_task,
225 NULL);
226 } 244 }
227 else if (phase == 2) 245 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
228 { 246 {
229 /* We passively monitor ARM for status updates. ARM should tell us 247 phase++;
230 * when do-nothing dies (no need to run a service upness test ourselves). 248 LOG ("do-nothing stopped working %u times, we are done here\n",
231 */ 249 (unsigned int) trialCount);
232 if (status == GNUNET_ARM_SERVICE_STARTING) 250 GNUNET_ARM_request_service_stop (arm,
233 { 251 "arm",
234 waitedFor = GNUNET_TIME_absolute_get_duration(startedWaitingAt); 252 &arm_stop_cb,
235 LOG("Waited for: %s\n", 253 NULL);
236 GNUNET_STRINGS_relative_time_to_string(waitedFor,
237 GNUNET_YES));
238
239 LOG("do-nothing is starting, killing it...\n");
240 GNUNET_assert(NULL == kt);
241 kt = GNUNET_SCHEDULER_add_now(&kill_task, &ok);
242 }
243 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
244 {
245 phase++;
246 LOG("do-nothing stopped working %u times, we are done here\n",
247 (unsigned int)trialCount);
248 GNUNET_ARM_request_service_stop(arm,
249 "arm",
250 &arm_stop_cb,
251 NULL);
252 }
253 } 254 }
255 }
254} 256}
255 257
256 258
257static void 259static void
258arm_start_cb(void *cls, 260arm_start_cb (void *cls,
259 enum GNUNET_ARM_RequestStatus status, 261 enum GNUNET_ARM_RequestStatus status,
260 enum GNUNET_ARM_Result result) 262 enum GNUNET_ARM_Result result)
261{ 263{
262 GNUNET_break(status == GNUNET_ARM_REQUEST_SENT_OK); 264 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
263 GNUNET_break(result == GNUNET_ARM_RESULT_STARTING); 265 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
264 GNUNET_break(phase == 0); 266 GNUNET_break (phase == 0);
265 LOG("Sent 'START' request for arm to ARM %s\n", 267 LOG ("Sent 'START' request for arm to ARM %s\n",
266 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully"); 268 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" :
269 "unsuccessfully");
267} 270}
268 271
269 272
270static void 273static void
271task(void *cls, 274task (void *cls,
272 char *const *args, 275 char *const *args,
273 const char *cfgfile, 276 const char *cfgfile,
274 const struct GNUNET_CONFIGURATION_Handle *c) 277 const struct GNUNET_CONFIGURATION_Handle *c)
275{ 278{
276 cfg = c; 279 cfg = c;
277 arm = GNUNET_ARM_connect(cfg, NULL, NULL); 280 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
278 if (NULL == arm) 281 if (NULL == arm)
279 { 282 {
280 GNUNET_break(0); 283 GNUNET_break (0);
281 return; 284 return;
282 } 285 }
283 mon = GNUNET_ARM_monitor_start(cfg, 286 mon = GNUNET_ARM_monitor_start (cfg,
284 &srv_status, 287 &srv_status,
285 NULL); 288 NULL);
286 if (NULL == mon) 289 if (NULL == mon)
287 { 290 {
288 GNUNET_break(0); 291 GNUNET_break (0);
289 GNUNET_ARM_disconnect(arm); 292 GNUNET_ARM_disconnect (arm);
290 arm = NULL; 293 arm = NULL;
291 return; 294 return;
292 } 295 }
293 GNUNET_ARM_request_service_start(arm, 296 GNUNET_ARM_request_service_start (arm,
294 "arm", 297 "arm",
295 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 298 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
296 &arm_start_cb, 299 &arm_start_cb,
297 NULL); 300 NULL);
298 GNUNET_SCHEDULER_add_shutdown(&trigger_disconnect, 301 GNUNET_SCHEDULER_add_shutdown (&trigger_disconnect,
299 NULL); 302 NULL);
300} 303}
301 304
302 305
303static int 306static int
304check() 307check ()
305{ 308{
306 char *const argv[] = { 309 char *const argv[] = {
307 "test-exponential-backoff", 310 "test-exponential-backoff",
@@ -313,14 +316,14 @@ check()
313 }; 316 };
314 317
315 /* Running ARM and running the do_nothing task */ 318 /* Running ARM and running the do_nothing task */
316 GNUNET_assert(GNUNET_OK == 319 GNUNET_assert (GNUNET_OK ==
317 GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) - 1, 320 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
318 argv, 321 argv,
319 "test-exponential-backoff", 322 "test-exponential-backoff",
320 "nohelp", 323 "nohelp",
321 options, 324 options,
322 &task, 325 &task,
323 NULL)); 326 NULL));
324 return ok; 327 return ok;
325} 328}
326 329
@@ -334,78 +337,78 @@ check()
334 337
335 338
336static int 339static int
337init() 340init ()
338{ 341{
339 struct GNUNET_CONFIGURATION_Handle *cfg; 342 struct GNUNET_CONFIGURATION_Handle *cfg;
340 char pwd[PATH_MAX]; 343 char pwd[PATH_MAX];
341 char *binary; 344 char *binary;
342 345
343 cfg = GNUNET_CONFIGURATION_create(); 346 cfg = GNUNET_CONFIGURATION_create ();
344 if (GNUNET_OK != GNUNET_CONFIGURATION_parse(cfg, 347 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
345 "test_arm_api_data.conf")) 348 "test_arm_api_data.conf"))
346 { 349 {
347 GNUNET_CONFIGURATION_destroy(cfg); 350 GNUNET_CONFIGURATION_destroy (cfg);
348 return GNUNET_SYSERR;
349 }
350 if (NULL == getcwd(pwd, PATH_MAX))
351 return GNUNET_SYSERR; 351 return GNUNET_SYSERR;
352 GNUNET_assert(0 < GNUNET_asprintf(&binary, 352 }
353 "%s/%s", 353 if (NULL == getcwd (pwd, PATH_MAX))
354 pwd, 354 return GNUNET_SYSERR;
355 BINARY)); 355 GNUNET_assert (0 < GNUNET_asprintf (&binary,
356 GNUNET_CONFIGURATION_set_value_string(cfg, 356 "%s/%s",
357 SERVICE, 357 pwd,
358 "BINARY", 358 BINARY));
359 binary); 359 GNUNET_CONFIGURATION_set_value_string (cfg,
360 GNUNET_free(binary); 360 SERVICE,
361 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg, 361 "BINARY",
362 CFGFILENAME)) 362 binary);
363 { 363 GNUNET_free (binary);
364 GNUNET_CONFIGURATION_destroy(cfg); 364 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg,
365 return GNUNET_SYSERR; 365 CFGFILENAME))
366 } 366 {
367 GNUNET_CONFIGURATION_destroy(cfg); 367 GNUNET_CONFIGURATION_destroy (cfg);
368 return GNUNET_SYSERR;
369 }
370 GNUNET_CONFIGURATION_destroy (cfg);
368 371
369#if LOG_BACKOFF 372#if LOG_BACKOFF
370 killLogFileName = GNUNET_DISK_mktemp("exponential-backoff-waiting.log"); 373 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
371 if (NULL == (killLogFilePtr = fopen(killLogFileName, 374 if (NULL == (killLogFilePtr = fopen (killLogFileName,
372 "w"))) 375 "w")))
373 { 376 {
374 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, 377 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
375 "fopen", 378 "fopen",
376 killLogFileName); 379 killLogFileName);
377 GNUNET_free(killLogFileName); 380 GNUNET_free (killLogFileName);
378 return GNUNET_SYSERR; 381 return GNUNET_SYSERR;
379 } 382 }
380#endif 383#endif
381 return GNUNET_OK; 384 return GNUNET_OK;
382} 385}
383 386
384 387
385static void 388static void
386houseKeep() 389houseKeep ()
387{ 390{
388#if LOG_BACKOFF 391#if LOG_BACKOFF
389 GNUNET_assert(0 == fclose(killLogFilePtr)); 392 GNUNET_assert (0 == fclose (killLogFilePtr));
390 GNUNET_free(killLogFileName); 393 GNUNET_free (killLogFileName);
391#endif 394#endif
392 (void)unlink(CFGFILENAME); 395 (void) unlink (CFGFILENAME);
393} 396}
394 397
395 398
396int 399int
397main(int argc, char *argv[]) 400main (int argc, char *argv[])
398{ 401{
399 int ret; 402 int ret;
400 403
401 GNUNET_log_setup("test-exponential-backoff", 404 GNUNET_log_setup ("test-exponential-backoff",
402 "WARNING", 405 "WARNING",
403 NULL); 406 NULL);
404 407
405 if (GNUNET_OK != init()) 408 if (GNUNET_OK != init ())
406 return 1; 409 return 1;
407 ret = check(); 410 ret = check ();
408 houseKeep(); 411 houseKeep ();
409 return ret; 412 return ret;
410} 413}
411 414