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;
68 * Context for handling the shutdown of a service.
70 struct ShutdownContext
73 * Connection to the service that is being shutdown.
75 struct GNUNET_CLIENT_Connection *sock;
78 * Time allowed for shutdown to happen.
80 struct GNUNET_TIME_Absolute timeout;
83 * Task set up to cancel the shutdown request on timeout.
85 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
88 * Task to call once shutdown complete
90 GNUNET_CLIENT_ShutdownTask cont;
93 * Closure for shutdown continuation
98 * We received a confirmation that the service will shut down.
105 * Handler receiving response to service shutdown requests.
106 * We expect it to be called with NULL, since the service that
107 * we are shutting down will just die without replying.
110 * @param msg NULL, indicating socket closure.
113 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
115 struct ShutdownContext *shutdown_ctx = cls;
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
120 if (shutdown_ctx->cont != NULL)
121 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
123 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
124 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
125 GNUNET_free (shutdown_ctx);
133 * Shutting down took too long, cancel receive and return error.
136 * @param tc context information (why was this task triggered now)
139 service_shutdown_cancel (void *cls,
140 const struct GNUNET_SCHEDULER_TaskContext *tc)
142 struct ShutdownContext *shutdown_ctx = cls;
144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
145 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
146 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
147 GNUNET_free (shutdown_ctx);
152 * If possible, write a shutdown message to the target
153 * buffer and destroy the client connection.
155 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
156 * @param size number of bytes available in buf
157 * @param buf NULL on error, otherwise target buffer
158 * @return number of bytes written to buf
161 write_shutdown (void *cls, size_t size, void *buf)
163 struct GNUNET_MessageHeader *msg;
164 struct ShutdownContext *shutdown_ctx = cls;
166 if (size < sizeof (struct GNUNET_MessageHeader))
168 LOG ("Failed to send a shutdown request\n");
169 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
170 GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
171 GNUNET_free (shutdown_ctx);
172 return 0; /* client disconnected */
175 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
176 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
177 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
178 GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
179 &service_shutdown_cancel, shutdown_ctx);
180 msg = (struct GNUNET_MessageHeader *) buf;
181 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
182 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
183 strcpy ((char *) &msg[1], "do-nothing");
184 LOG ("Sent a shutdown request\n");
185 return sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1;
190 * Request that the service should shutdown.
191 * Afterwards, the connection will automatically be
192 * disconnected. Hence the "sock" should not
193 * be used by the caller after this call
194 * (calling this function frees "sock" after a while).
196 * @param sock the socket connected to the service
197 * @param timeout how long to wait before giving up on transmission
198 * @param cont continuation to call once the service is really down
199 * @param cont_cls closure for continuation
203 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
204 struct GNUNET_TIME_Relative timeout,
205 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
207 struct ShutdownContext *shutdown_ctx;
209 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
210 shutdown_ctx->cont = cont;
211 shutdown_ctx->cont_cls = cont_cls;
212 shutdown_ctx->sock = sock;
213 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
214 GNUNET_CLIENT_notify_transmit_ready (sock,
215 sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1,
216 timeout, GNUNET_NO, &write_shutdown,
222 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
226 shutdown_cont (void *cls, int reason)
228 if (GNUNET_NO != reason)
230 /* Re-try shutdown */
231 LOG ("do-nothing didn't die, trying again\n");
232 GNUNET_SCHEDULER_add_now (kill_task, NULL);
235 startedWaitingAt = GNUNET_TIME_absolute_get ();
236 LOG ("do-nothing is dead, starting the countdown\n");
241 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
243 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
247 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
248 LOG ("Waited for: %s\n",
249 GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
253 waitedFor.rel_value = 0;
255 /* Connect to the doNothing task */
256 doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
257 GNUNET_assert (doNothingConnection != NULL);
258 if (trialCount == 12)
259 waitedFor_prev = waitedFor;
260 else if (trialCount == 13)
262 GNUNET_CLIENT_disconnect (doNothingConnection);
263 GNUNET_ARM_request_service_stop (arm, "do-nothing", TIMEOUT, NULL, NULL);
264 if (waitedFor_prev.rel_value >= waitedFor.rel_value)
272 /* Use the created connection to kill the doNothingTask */
273 do_nothing_service_shutdown (doNothingConnection,
274 TIMEOUT, &shutdown_cont, NULL);
279 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
281 GNUNET_ARM_disconnect_and_free (arm);
282 GNUNET_ARM_monitor_disconnect_and_free (mon);
287 arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
289 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
290 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
291 LOG ("ARM service stopped\n");
292 GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
297 srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
299 LOG ("Service %s is %u, phase %u\n", service, status, phase);
300 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
303 GNUNET_ARM_request_service_start (arm, "do-nothing",
304 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
309 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
310 GNUNET_break (0 == strcasecmp (service, "do-nothing"));
311 GNUNET_break (phase == 1);
312 LOG ("do-nothing is starting\n");
315 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
317 else if ((phase == 2) && (strcasecmp ("do-nothing", service) == 0))
319 /* We passively monitor ARM for status updates. ARM should tell us
320 * when do-nothing dies (no need to run a service upness test ourselves).
322 if (status == GNUNET_ARM_SERVICE_STARTING)
324 LOG ("do-nothing is starting\n");
325 GNUNET_SCHEDULER_add_now (kill_task, &ok);
327 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
330 GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
337 arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
339 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
340 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
341 GNUNET_break (phase == 0);
342 LOG ("Sent 'START' request for arm to ARM %s\n",
343 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
348 task (void *cls, char *const *args, const char *cfgfile,
349 const struct GNUNET_CONFIGURATION_Handle *c)
355 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm",
356 "CONFIG", &armconfig))
358 GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
359 *) cfg, "arm", "CONFIG",
363 GNUNET_free (armconfig);
366 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
369 mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
373 GNUNET_ARM_request_service_start (arm, "arm",
374 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
376 arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
381 GNUNET_ARM_disconnect_and_free (arm);
391 char *const argv[] = {
392 "test-exponential-backoff",
393 "-c", "test_arm_api_data.conf",
396 struct GNUNET_GETOPT_CommandLineOption options[] = {
397 GNUNET_GETOPT_OPTION_END
400 /* Running ARM and running the do_nothing task */
401 GNUNET_assert (GNUNET_OK ==
402 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
403 argv, "test-exponential-backoff",
404 "nohelp", options, &task, NULL));
415 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
416 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
418 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
420 GNUNET_free (killLogFileName);
421 return GNUNET_SYSERR;
432 GNUNET_assert (0 == fclose (killLogFilePtr));
433 GNUNET_free (killLogFileName);
439 main (int argc, char *argv[])
443 GNUNET_log_setup ("test-exponential-backoff",
453 /* end of test_exponential_backoff.c */