2 This file is part of GNUnet.
3 Copyright (C) 2009 Christian Grothoff (and other contributing authors)
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.
144 * @param tc context information (why was this task triggered now)
147 service_shutdown_cancel (void *cls,
148 const struct GNUNET_SCHEDULER_TaskContext *tc)
150 struct ShutdownContext *shutdown_ctx = cls;
152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
153 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
154 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
155 GNUNET_free (shutdown_ctx);
160 * If possible, write a shutdown message to the target
161 * buffer and destroy the client connection.
163 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
164 * @param size number of bytes available in buf
165 * @param buf NULL on error, otherwise target buffer
166 * @return number of bytes written to buf
169 write_shutdown (void *cls, size_t size, void *buf)
171 struct GNUNET_MessageHeader *msg;
172 struct ShutdownContext *shutdown_ctx = cls;
174 if (size < sizeof (struct GNUNET_MessageHeader))
176 LOG ("Failed to send a shutdown request\n");
177 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
178 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
179 GNUNET_free (shutdown_ctx);
180 return 0; /* client disconnected */
183 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
184 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
185 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
186 GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
187 &service_shutdown_cancel, shutdown_ctx);
188 msg = (struct GNUNET_MessageHeader *) buf;
189 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
190 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
191 strcpy ((char *) &msg[1], SERVICE);
192 LOG ("Sent a shutdown request\n");
193 return sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1;
198 * Request that the service should shutdown.
199 * Afterwards, the connection will automatically be
200 * disconnected. Hence the "sock" should not
201 * be used by the caller after this call
202 * (calling this function frees "sock" after a while).
204 * @param sock the socket connected to the service
205 * @param timeout how long to wait before giving up on transmission
206 * @param cont continuation to call once the service is really down
207 * @param cont_cls closure for continuation
211 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
212 struct GNUNET_TIME_Relative timeout,
213 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
215 struct ShutdownContext *shutdown_ctx;
217 shutdown_ctx = GNUNET_new (struct ShutdownContext);
218 shutdown_ctx->cont = cont;
219 shutdown_ctx->cont_cls = cont_cls;
220 shutdown_ctx->sock = sock;
221 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
222 GNUNET_CLIENT_notify_transmit_ready (sock,
223 sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1,
224 timeout, GNUNET_NO, &write_shutdown,
230 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
234 shutdown_cont (void *cls, int reason)
236 if (GNUNET_NO != reason)
238 /* Re-try shutdown */
239 LOG ("do-nothing didn't die, trying again\n");
240 GNUNET_SCHEDULER_add_now (kill_task, NULL);
243 startedWaitingAt = GNUNET_TIME_absolute_get ();
244 LOG ("do-nothing is dead, starting the countdown\n");
249 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
255 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
256 LOG ("Waited for: %s\n",
257 GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
261 waitedFor.rel_value_us = 0;
263 /* Connect to the doNothing task */
264 doNothingConnection = GNUNET_CLIENT_connect (SERVICE, cfg);
265 GNUNET_assert (doNothingConnection != NULL);
266 if (trialCount == 12)
267 waitedFor_prev = waitedFor;
268 else if (trialCount == 13)
270 GNUNET_CLIENT_disconnect (doNothingConnection);
271 GNUNET_ARM_request_service_stop (arm, SERVICE, TIMEOUT, NULL, NULL);
272 if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
280 /* Use the created connection to kill the doNothingTask */
281 do_nothing_service_shutdown (doNothingConnection,
282 TIMEOUT, &shutdown_cont, NULL);
287 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 GNUNET_ARM_disconnect_and_free (arm);
290 GNUNET_ARM_monitor_disconnect_and_free (mon);
295 arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
297 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
298 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
299 LOG ("ARM service stopped\n");
300 GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
305 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
307 LOG ("Service %s is %u, phase %u\n", service, status, phase);
308 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
311 GNUNET_ARM_request_service_start (arm, SERVICE,
312 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
317 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
318 GNUNET_break (0 == strcasecmp (service, SERVICE));
319 GNUNET_break (phase == 1);
320 LOG ("do-nothing is starting\n");
323 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
325 else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
327 /* We passively monitor ARM for status updates. ARM should tell us
328 * when do-nothing dies (no need to run a service upness test ourselves).
330 if (status == GNUNET_ARM_SERVICE_STARTING)
332 LOG ("do-nothing is starting\n");
333 GNUNET_SCHEDULER_add_now (kill_task, &ok);
335 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
338 GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
345 arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
347 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
348 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
349 GNUNET_break (phase == 0);
350 LOG ("Sent 'START' request for arm to ARM %s\n",
351 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
356 task (void *cls, char *const *args, const char *cfgfile,
357 const struct GNUNET_CONFIGURATION_Handle *c)
360 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
363 mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
367 GNUNET_ARM_request_service_start (arm, "arm",
368 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
370 arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
375 GNUNET_ARM_disconnect_and_free (arm);
385 char *const argv[] = {
386 "test-exponential-backoff",
390 struct GNUNET_GETOPT_CommandLineOption options[] = {
391 GNUNET_GETOPT_OPTION_END
394 /* Running ARM and running the do_nothing task */
395 GNUNET_assert (GNUNET_OK ==
396 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
397 argv, "test-exponential-backoff",
398 "nohelp", options, &task, NULL));
408 struct GNUNET_CONFIGURATION_Handle *cfg;
412 cfg = GNUNET_CONFIGURATION_create ();
413 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
414 "test_arm_api_data.conf"))
415 return GNUNET_SYSERR;
416 if (NULL == getcwd (pwd, PATH_MAX))
417 return GNUNET_SYSERR;
418 GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
419 GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
420 GNUNET_free (binary);
421 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
423 GNUNET_CONFIGURATION_destroy (cfg);
424 return GNUNET_SYSERR;
426 GNUNET_CONFIGURATION_destroy (cfg);
429 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
430 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
432 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
434 GNUNET_free (killLogFileName);
435 return GNUNET_SYSERR;
446 GNUNET_assert (0 == fclose (killLogFilePtr));
447 GNUNET_free (killLogFileName);
449 (void) unlink (CFGFILENAME);
454 main (int argc, char *argv[])
458 GNUNET_log_setup ("test-exponential-backoff",
462 if (GNUNET_OK != init ())
469 /* end of test_exponential_backoff.c */