2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file arm/test_exponential_backoff.c
22 * @brief testcase for gnunet-service-arm.c
25 #include "gnunet_arm_service.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_protocols.h"
29 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
31 #define START_ARM GNUNET_YES
33 #define LOG_BACKOFF GNUNET_NO
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
39 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
41 #define SERVICE "do-nothing"
43 #define BINARY "mockup-service"
45 #define CFGFILENAME "test_arm_api_data2.conf"
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 static struct GNUNET_ARM_Handle *arm;
52 static struct GNUNET_ARM_MonitorHandle *mon;
58 static int trialCount;
60 static struct GNUNET_TIME_Absolute startedWaitingAt;
62 struct GNUNET_TIME_Relative waitedFor;
64 struct GNUNET_TIME_Relative waitedFor_prev;
67 static FILE *killLogFilePtr;
69 static char *killLogFileName;
73 typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
76 * Context for handling the shutdown of a service.
78 struct ShutdownContext
81 * Connection to the service that is being shutdown.
83 struct GNUNET_CLIENT_Connection *sock;
86 * Time allowed for shutdown to happen.
88 struct GNUNET_TIME_Absolute timeout;
91 * Task set up to cancel the shutdown request on timeout.
93 struct GNUNET_SCHEDULER_Task *cancel_task;
96 * Task to call once shutdown complete
98 GNUNET_CLIENT_ShutdownTask cont;
101 * Closure for shutdown continuation
106 * We received a confirmation that the service will shut down.
113 * Handler receiving response to service shutdown requests.
114 * We expect it to be called with NULL, since the service that
115 * we are shutting down will just die without replying.
118 * @param msg NULL, indicating socket closure.
121 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
123 struct ShutdownContext *shutdown_ctx = cls;
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
128 if (shutdown_ctx->cont != NULL)
129 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
131 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
132 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
133 GNUNET_free (shutdown_ctx);
141 * Shutting down took too long, cancel receive and return error.
146 service_shutdown_cancel (void *cls)
148 struct ShutdownContext *shutdown_ctx = cls;
150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
151 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
152 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
153 GNUNET_free (shutdown_ctx);
158 * If possible, write a shutdown message to the target
159 * buffer and destroy the client connection.
161 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
162 * @param size number of bytes available in buf
163 * @param buf NULL on error, otherwise target buffer
164 * @return number of bytes written to buf
167 write_shutdown (void *cls, size_t size, void *buf)
169 struct GNUNET_MessageHeader *msg;
170 struct ShutdownContext *shutdown_ctx = cls;
172 if (size < sizeof (struct GNUNET_MessageHeader))
174 LOG ("Failed to send a shutdown request\n");
175 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
176 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
177 GNUNET_free (shutdown_ctx);
178 return 0; /* client disconnected */
181 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
182 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
183 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
184 GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
185 &service_shutdown_cancel, shutdown_ctx);
186 msg = (struct GNUNET_MessageHeader *) buf;
187 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
188 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
189 strcpy ((char *) &msg[1], SERVICE);
190 LOG ("Sent a shutdown request\n");
191 return sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1;
196 * Request that the service should shutdown.
197 * Afterwards, the connection will automatically be
198 * disconnected. Hence the "sock" should not
199 * be used by the caller after this call
200 * (calling this function frees "sock" after a while).
202 * @param sock the socket connected to the service
203 * @param timeout how long to wait before giving up on transmission
204 * @param cont continuation to call once the service is really down
205 * @param cont_cls closure for continuation
209 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
210 struct GNUNET_TIME_Relative timeout,
211 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
213 struct ShutdownContext *shutdown_ctx;
215 shutdown_ctx = GNUNET_new (struct ShutdownContext);
216 shutdown_ctx->cont = cont;
217 shutdown_ctx->cont_cls = cont_cls;
218 shutdown_ctx->sock = sock;
219 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
220 GNUNET_CLIENT_notify_transmit_ready (sock,
221 sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1,
222 timeout, GNUNET_NO, &write_shutdown,
228 kill_task (void *cbData);
232 shutdown_cont (void *cls, int reason)
234 if (GNUNET_NO != reason)
236 /* Re-try shutdown */
237 LOG ("do-nothing didn't die, trying again\n");
238 GNUNET_SCHEDULER_add_now (&kill_task, NULL);
241 startedWaitingAt = GNUNET_TIME_absolute_get ();
242 LOG ("do-nothing is dead, starting the countdown\n");
247 kill_task (void *cbData)
249 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
253 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
254 LOG ("Waited for: %s\n",
255 GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
259 waitedFor.rel_value_us = 0;
261 /* Connect to the doNothing task */
262 doNothingConnection = GNUNET_CLIENT_connect (SERVICE, cfg);
263 GNUNET_assert (doNothingConnection != NULL);
264 if (trialCount == 12)
265 waitedFor_prev = waitedFor;
266 else if (trialCount == 13)
268 GNUNET_CLIENT_disconnect (doNothingConnection);
269 GNUNET_ARM_request_service_stop (arm, SERVICE, TIMEOUT, NULL, NULL);
270 if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
278 /* Use the created connection to kill the doNothingTask */
279 do_nothing_service_shutdown (doNothingConnection,
280 TIMEOUT, &shutdown_cont, NULL);
285 trigger_disconnect (void *cls)
287 GNUNET_ARM_disconnect_and_free (arm);
288 GNUNET_ARM_monitor_disconnect_and_free (mon);
293 arm_stop_cb (void *cls,
294 enum GNUNET_ARM_RequestStatus status,
295 const char *servicename,
296 enum GNUNET_ARM_Result result)
298 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
299 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
300 LOG ("ARM service stopped\n");
301 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
306 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
308 LOG ("Service %s is %u, phase %u\n", service, status, phase);
309 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
312 GNUNET_ARM_request_service_start (arm, SERVICE,
313 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
318 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
319 GNUNET_break (0 == strcasecmp (service, SERVICE));
320 GNUNET_break (phase == 1);
321 LOG ("do-nothing is starting\n");
324 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
327 else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
329 /* We passively monitor ARM for status updates. ARM should tell us
330 * when do-nothing dies (no need to run a service upness test ourselves).
332 if (status == GNUNET_ARM_SERVICE_STARTING)
334 LOG ("do-nothing is starting\n");
335 GNUNET_SCHEDULER_add_now (&kill_task, &ok);
337 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
340 GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
347 arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
349 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
350 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
351 GNUNET_break (phase == 0);
352 LOG ("Sent 'START' request for arm to ARM %s\n",
353 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
358 task (void *cls, char *const *args, const char *cfgfile,
359 const struct GNUNET_CONFIGURATION_Handle *c)
362 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
365 mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
369 GNUNET_ARM_request_service_start (arm, "arm",
370 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
372 arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
377 GNUNET_ARM_disconnect_and_free (arm);
387 char *const argv[] = {
388 "test-exponential-backoff",
392 struct GNUNET_GETOPT_CommandLineOption options[] = {
393 GNUNET_GETOPT_OPTION_END
396 /* Running ARM and running the do_nothing task */
397 GNUNET_assert (GNUNET_OK ==
398 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
399 argv, "test-exponential-backoff",
400 "nohelp", options, &task, NULL));
409 * Assumed maximum path length (for the log file name).
411 #define PATH_MAX 4096
418 struct GNUNET_CONFIGURATION_Handle *cfg;
422 cfg = GNUNET_CONFIGURATION_create ();
423 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
424 "test_arm_api_data.conf"))
425 return GNUNET_SYSERR;
426 if (NULL == getcwd (pwd, PATH_MAX))
427 return GNUNET_SYSERR;
428 GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
429 GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
430 GNUNET_free (binary);
431 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
433 GNUNET_CONFIGURATION_destroy (cfg);
434 return GNUNET_SYSERR;
436 GNUNET_CONFIGURATION_destroy (cfg);
439 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
440 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
442 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
444 GNUNET_free (killLogFileName);
445 return GNUNET_SYSERR;
456 GNUNET_assert (0 == fclose (killLogFilePtr));
457 GNUNET_free (killLogFileName);
459 (void) unlink (CFGFILENAME);
464 main (int argc, char *argv[])
468 GNUNET_log_setup ("test-exponential-backoff",
472 if (GNUNET_OK != init ())
479 /* end of test_exponential_backoff.c */