2 This file is part of GNUnet.
3 Copyright (C) 2009, 2016 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;
74 (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
78 * Context for handling the shutdown of a service.
80 struct ShutdownContext
83 * Connection to the service that is being shutdown.
85 struct GNUNET_CLIENT_Connection *sock;
88 * Time allowed for shutdown to happen.
90 struct GNUNET_TIME_Absolute timeout;
93 * Task set up to cancel the shutdown request on timeout.
95 struct GNUNET_SCHEDULER_Task *cancel_task;
98 * Task to call once shutdown complete
100 GNUNET_CLIENT_ShutdownTask cont;
103 * Closure for shutdown continuation
108 * We received a confirmation that the service will shut down.
115 * Handler receiving response to service shutdown requests.
116 * We expect it to be called with NULL, since the service that
117 * we are shutting down will just die without replying.
120 * @param msg NULL, indicating socket closure.
123 service_shutdown_handler (void *cls,
124 const struct GNUNET_MessageHeader *msg)
126 struct ShutdownContext *shutdown_ctx = cls;
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
131 if (shutdown_ctx->cont != NULL)
132 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
134 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
135 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
136 GNUNET_free (shutdown_ctx);
144 * Shutting down took too long, cancel receive and return error.
149 service_shutdown_cancel (void *cls)
151 struct ShutdownContext *shutdown_ctx = cls;
153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
154 "service_shutdown_cancel called!\n");
155 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
156 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
157 GNUNET_free (shutdown_ctx);
162 * If possible, write a shutdown message to the target
163 * buffer and destroy the client connection.
165 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
166 * @param size number of bytes available in buf
167 * @param buf NULL on error, otherwise target buffer
168 * @return number of bytes written to buf
171 write_shutdown (void *cls, size_t size, void *buf)
173 struct GNUNET_MessageHeader *msg;
174 struct ShutdownContext *shutdown_ctx = cls;
176 if (size < sizeof (struct GNUNET_MessageHeader))
178 LOG ("Failed to send a shutdown request\n");
179 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
180 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
181 GNUNET_free (shutdown_ctx);
182 return 0; /* client disconnected */
185 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
186 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
187 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
188 GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
189 &service_shutdown_cancel, shutdown_ctx);
190 msg = (struct GNUNET_MessageHeader *) buf;
191 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
192 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
193 strcpy ((char *) &msg[1], SERVICE);
194 LOG ("Sent a shutdown request\n");
195 return sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1;
200 * Request that the service should shutdown.
201 * Afterwards, the connection will automatically be
202 * disconnected. Hence the "sock" should not
203 * be used by the caller after this call
204 * (calling this function frees "sock" after a while).
206 * @param sock the socket connected to the service
207 * @param timeout how long to wait before giving up on transmission
208 * @param cont continuation to call once the service is really down
209 * @param cont_cls closure for continuation
213 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
214 struct GNUNET_TIME_Relative timeout,
215 GNUNET_CLIENT_ShutdownTask cont,
218 struct ShutdownContext *shutdown_ctx;
220 shutdown_ctx = GNUNET_new (struct ShutdownContext);
221 shutdown_ctx->cont = cont;
222 shutdown_ctx->cont_cls = cont_cls;
223 shutdown_ctx->sock = sock;
224 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
225 GNUNET_CLIENT_notify_transmit_ready (sock,
226 sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1,
227 timeout, GNUNET_NO, &write_shutdown,
233 kill_task (void *cbData);
237 shutdown_cont (void *cls, int reason)
239 if (GNUNET_NO != reason)
241 /* Re-try shutdown */
242 LOG ("do-nothing didn't die, trying again\n");
243 GNUNET_SCHEDULER_add_now (&kill_task, NULL);
246 startedWaitingAt = GNUNET_TIME_absolute_get ();
247 LOG ("do-nothing is dead, starting the countdown\n");
252 kill_task (void *cbData)
254 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
258 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
259 LOG ("Waited for: %s\n",
260 GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
264 waitedFor.rel_value_us = 0;
266 /* Connect to the doNothing task */
267 doNothingConnection = GNUNET_CLIENT_connect (SERVICE, cfg);
268 GNUNET_assert (doNothingConnection != NULL);
269 if (trialCount == 12)
270 waitedFor_prev = waitedFor;
271 else if (trialCount == 13)
273 GNUNET_CLIENT_disconnect (doNothingConnection);
274 GNUNET_ARM_request_service_stop (arm,
278 if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
286 /* Use the created connection to kill the doNothingTask */
287 do_nothing_service_shutdown (doNothingConnection,
288 TIMEOUT, &shutdown_cont, NULL);
293 trigger_disconnect (void *cls)
295 GNUNET_ARM_disconnect (arm);
296 GNUNET_ARM_monitor_stop (mon);
301 arm_stop_cb (void *cls,
302 enum GNUNET_ARM_RequestStatus status,
303 enum GNUNET_ARM_Result result)
305 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
306 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
307 LOG ("ARM service stopped\n");
308 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
313 srv_status (void *cls,
315 enum GNUNET_ARM_ServiceStatus status)
317 LOG ("Service %s is %u, phase %u\n", service, status, phase);
318 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
321 GNUNET_ARM_request_service_start (arm,
323 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
330 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
331 GNUNET_break (0 == strcasecmp (service, SERVICE));
332 GNUNET_break (phase == 1);
333 LOG ("do-nothing is starting\n");
336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
340 else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
342 /* We passively monitor ARM for status updates. ARM should tell us
343 * when do-nothing dies (no need to run a service upness test ourselves).
345 if (status == GNUNET_ARM_SERVICE_STARTING)
347 LOG ("do-nothing is starting\n");
348 GNUNET_SCHEDULER_add_now (&kill_task, &ok);
350 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
353 GNUNET_ARM_request_service_stop (arm,
363 arm_start_cb (void *cls,
364 enum GNUNET_ARM_RequestStatus status,
365 enum GNUNET_ARM_Result result)
367 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
368 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
369 GNUNET_break (phase == 0);
370 LOG ("Sent 'START' request for arm to ARM %s\n",
371 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
379 const struct GNUNET_CONFIGURATION_Handle *c)
382 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
385 mon = GNUNET_ARM_monitor_start (cfg, &srv_status, NULL);
389 GNUNET_ARM_request_service_start (arm,
391 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
397 GNUNET_ARM_REQUEST_SENT_OK,
398 GNUNET_ARM_SERVICE_STARTING);
403 GNUNET_ARM_disconnect (arm);
413 char *const argv[] = {
414 "test-exponential-backoff",
418 struct GNUNET_GETOPT_CommandLineOption options[] = {
419 GNUNET_GETOPT_OPTION_END
422 /* Running ARM and running the do_nothing task */
423 GNUNET_assert (GNUNET_OK ==
424 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
425 argv, "test-exponential-backoff",
426 "nohelp", options, &task, NULL));
435 * Assumed maximum path length (for the log file name).
437 #define PATH_MAX 4096
444 struct GNUNET_CONFIGURATION_Handle *cfg;
448 cfg = GNUNET_CONFIGURATION_create ();
449 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
450 "test_arm_api_data.conf"))
451 return GNUNET_SYSERR;
452 if (NULL == getcwd (pwd, PATH_MAX))
453 return GNUNET_SYSERR;
454 GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
455 GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
456 GNUNET_free (binary);
457 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
459 GNUNET_CONFIGURATION_destroy (cfg);
460 return GNUNET_SYSERR;
462 GNUNET_CONFIGURATION_destroy (cfg);
465 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
466 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
468 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
470 GNUNET_free (killLogFileName);
471 return GNUNET_SYSERR;
482 GNUNET_assert (0 == fclose (killLogFilePtr));
483 GNUNET_free (killLogFileName);
485 (void) unlink (CFGFILENAME);
490 main (int argc, char *argv[])
494 GNUNET_log_setup ("test-exponential-backoff",
498 if (GNUNET_OK != init ())
505 /* end of test_exponential_backoff.c */