2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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)
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 static struct GNUNET_ARM_Handle *arm;
46 static struct GNUNET_ARM_MonitorHandle *mon;
52 static int trialCount;
54 static struct GNUNET_TIME_Absolute startedWaitingAt;
56 struct GNUNET_TIME_Relative waitedFor;
58 struct GNUNET_TIME_Relative waitedFor_prev;
61 static FILE *killLogFilePtr;
63 static char *killLogFileName;
67 typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
70 * Context for handling the shutdown of a service.
72 struct ShutdownContext
75 * Connection to the service that is being shutdown.
77 struct GNUNET_CLIENT_Connection *sock;
80 * Time allowed for shutdown to happen.
82 struct GNUNET_TIME_Absolute timeout;
85 * Task set up to cancel the shutdown request on timeout.
87 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
90 * Task to call once shutdown complete
92 GNUNET_CLIENT_ShutdownTask cont;
95 * Closure for shutdown continuation
100 * We received a confirmation that the service will shut down.
107 * Handler receiving response to service shutdown requests.
108 * We expect it to be called with NULL, since the service that
109 * we are shutting down will just die without replying.
112 * @param msg NULL, indicating socket closure.
115 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
117 struct ShutdownContext *shutdown_ctx = cls;
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
122 if (shutdown_ctx->cont != NULL)
123 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
125 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
126 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
127 GNUNET_free (shutdown_ctx);
135 * Shutting down took too long, cancel receive and return error.
138 * @param tc context information (why was this task triggered now)
141 service_shutdown_cancel (void *cls,
142 const struct GNUNET_SCHEDULER_TaskContext *tc)
144 struct ShutdownContext *shutdown_ctx = cls;
146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
147 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
148 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
149 GNUNET_free (shutdown_ctx);
154 * If possible, write a shutdown message to the target
155 * buffer and destroy the client connection.
157 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
158 * @param size number of bytes available in buf
159 * @param buf NULL on error, otherwise target buffer
160 * @return number of bytes written to buf
163 write_shutdown (void *cls, size_t size, void *buf)
165 struct GNUNET_MessageHeader *msg;
166 struct ShutdownContext *shutdown_ctx = cls;
168 if (size < sizeof (struct GNUNET_MessageHeader))
170 LOG ("Failed to send a shutdown request\n");
171 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
172 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
173 GNUNET_free (shutdown_ctx);
174 return 0; /* client disconnected */
177 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
178 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
179 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
180 GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
181 &service_shutdown_cancel, shutdown_ctx);
182 msg = (struct GNUNET_MessageHeader *) buf;
183 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
184 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
185 strcpy ((char *) &msg[1], "do-nothing");
186 LOG ("Sent a shutdown request\n");
187 return sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1;
192 * Request that the service should shutdown.
193 * Afterwards, the connection will automatically be
194 * disconnected. Hence the "sock" should not
195 * be used by the caller after this call
196 * (calling this function frees "sock" after a while).
198 * @param sock the socket connected to the service
199 * @param timeout how long to wait before giving up on transmission
200 * @param cont continuation to call once the service is really down
201 * @param cont_cls closure for continuation
205 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
206 struct GNUNET_TIME_Relative timeout,
207 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
209 struct ShutdownContext *shutdown_ctx;
211 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
212 shutdown_ctx->cont = cont;
213 shutdown_ctx->cont_cls = cont_cls;
214 shutdown_ctx->sock = sock;
215 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
216 GNUNET_CLIENT_notify_transmit_ready (sock,
217 sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1,
218 timeout, GNUNET_NO, &write_shutdown,
224 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
228 shutdown_cont (void *cls, int reason)
230 if (GNUNET_NO != reason)
232 /* Re-try shutdown */
233 LOG ("do-nothing didn't die, trying again\n");
234 GNUNET_SCHEDULER_add_now (kill_task, NULL);
237 startedWaitingAt = GNUNET_TIME_absolute_get ();
238 LOG ("do-nothing is dead, starting the countdown\n");
243 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
249 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
250 LOG ("Waited for: %s\n",
251 GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
255 waitedFor.rel_value_us = 0;
257 /* Connect to the doNothing task */
258 doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
259 GNUNET_assert (doNothingConnection != NULL);
260 if (trialCount == 12)
261 waitedFor_prev = waitedFor;
262 else if (trialCount == 13)
264 GNUNET_CLIENT_disconnect (doNothingConnection);
265 GNUNET_ARM_request_service_stop (arm, "do-nothing", TIMEOUT, NULL, NULL);
266 if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
274 /* Use the created connection to kill the doNothingTask */
275 do_nothing_service_shutdown (doNothingConnection,
276 TIMEOUT, &shutdown_cont, NULL);
281 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 GNUNET_ARM_disconnect_and_free (arm);
284 GNUNET_ARM_monitor_disconnect_and_free (mon);
289 arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
291 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
292 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
293 LOG ("ARM service stopped\n");
294 GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
299 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
301 LOG ("Service %s is %u, phase %u\n", service, status, phase);
302 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
305 GNUNET_ARM_request_service_start (arm, "do-nothing",
306 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
311 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
312 GNUNET_break (0 == strcasecmp (service, "do-nothing"));
313 GNUNET_break (phase == 1);
314 LOG ("do-nothing is starting\n");
317 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
319 else if ((phase == 2) && (strcasecmp ("do-nothing", service) == 0))
321 /* We passively monitor ARM for status updates. ARM should tell us
322 * when do-nothing dies (no need to run a service upness test ourselves).
324 if (status == GNUNET_ARM_SERVICE_STARTING)
326 LOG ("do-nothing is starting\n");
327 GNUNET_SCHEDULER_add_now (kill_task, &ok);
329 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
332 GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
339 arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
341 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
342 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
343 GNUNET_break (phase == 0);
344 LOG ("Sent 'START' request for arm to ARM %s\n",
345 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
350 task (void *cls, char *const *args, const char *cfgfile,
351 const struct GNUNET_CONFIGURATION_Handle *c)
357 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm",
358 "CONFIG", &armconfig))
360 GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
361 *) cfg, "arm", "CONFIG",
365 GNUNET_free (armconfig);
368 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
371 mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
375 GNUNET_ARM_request_service_start (arm, "arm",
376 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
378 arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
383 GNUNET_ARM_disconnect_and_free (arm);
393 char *const argv[] = {
394 "test-exponential-backoff",
395 "-c", "test_arm_api_data.conf",
398 struct GNUNET_GETOPT_CommandLineOption options[] = {
399 GNUNET_GETOPT_OPTION_END
402 /* Running ARM and running the do_nothing task */
403 GNUNET_assert (GNUNET_OK ==
404 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
405 argv, "test-exponential-backoff",
406 "nohelp", options, &task, NULL));
417 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
418 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
420 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
422 GNUNET_free (killLogFileName);
423 return GNUNET_SYSERR;
434 GNUNET_assert (0 == fclose (killLogFilePtr));
435 GNUNET_free (killLogFileName);
441 main (int argc, char *argv[])
445 GNUNET_log_setup ("test-exponential-backoff",
455 /* end of test_exponential_backoff.c */