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_client_lib.h"
27 #include "gnunet_configuration_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_protocols.h"
31 #define VERBOSE GNUNET_NO
33 #define START_ARM GNUNET_YES
35 #define LOG_BACKOFF GNUNET_NO
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
41 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46 static struct GNUNET_ARM_Handle *arm;
50 static int trialCount;
52 static struct GNUNET_TIME_Absolute startedWaitingAt;
54 struct GNUNET_TIME_Relative waitedFor;
57 static FILE *killLogFilePtr;
59 static char *killLogFileName;
64 * Context for handling the shutdown of a service.
66 struct ShutdownContext
69 * Connection to the service that is being shutdown.
71 struct GNUNET_CLIENT_Connection *sock;
74 * Time allowed for shutdown to happen.
76 struct GNUNET_TIME_Absolute timeout;
79 * Task set up to cancel the shutdown request on timeout.
81 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
84 * Task to call once shutdown complete
86 GNUNET_CLIENT_ShutdownTask cont;
89 * Closure for shutdown continuation
94 * We received a confirmation that the service will shut down.
101 * Handler receiving response to service shutdown requests.
102 * First call with NULL: service misbehaving, or something.
103 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN:
104 * - service will shutdown
105 * Second call with NULL:
106 * - service has now really shut down.
109 * @param msg NULL, indicating socket closure.
112 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
114 struct ShutdownContext *shutdown_ctx = cls;
116 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
118 /* Means the other side closed the connection and never confirmed a shutdown */
119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
120 "Service handle shutdown before ACK!\n");
121 if (shutdown_ctx->cont != NULL)
122 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
123 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
124 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
125 GNUNET_free (shutdown_ctx);
127 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
132 if (shutdown_ctx->cont != NULL)
133 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
135 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
136 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
137 GNUNET_free (shutdown_ctx);
141 GNUNET_assert (ntohs (msg->size) ==
142 sizeof (struct GNUNET_MessageHeader));
143 switch (ntohs (msg->type))
145 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN:
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Received confirmation for service shutdown.\n");
150 shutdown_ctx->confirmed = GNUNET_YES;
151 GNUNET_CLIENT_receive (shutdown_ctx->sock,
152 &service_shutdown_handler, shutdown_ctx,
153 GNUNET_TIME_UNIT_FOREVER_REL);
155 default: /* Fall through */
156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157 "Service shutdown refused!\n");
158 if (shutdown_ctx->cont != NULL)
159 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
161 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
162 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
163 GNUNET_free (shutdown_ctx);
170 * Shutting down took too long, cancel receive and return error.
173 * @param tc context information (why was this task triggered now)
176 service_shutdown_cancel (void *cls,
177 const struct GNUNET_SCHEDULER_TaskContext *tc)
179 struct ShutdownContext *shutdown_ctx = cls;
181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
182 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
183 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
184 GNUNET_free (shutdown_ctx);
189 * If possible, write a shutdown message to the target
190 * buffer and destroy the client connection.
192 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
193 * @param size number of bytes available in buf
194 * @param buf NULL on error, otherwise target buffer
195 * @return number of bytes written to buf
198 write_shutdown (void *cls, size_t size, void *buf)
200 struct GNUNET_MessageHeader *msg;
201 struct ShutdownContext *shutdown_ctx = cls;
203 if (size < sizeof (struct GNUNET_MessageHeader))
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 _("Failed to transmit shutdown request to client.\n"));
207 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
208 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
209 GNUNET_free (shutdown_ctx);
210 return 0; /* client disconnected */
213 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
214 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
215 shutdown_ctx->cancel_task =
216 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
217 (shutdown_ctx->timeout),
218 &service_shutdown_cancel, shutdown_ctx);
219 msg = (struct GNUNET_MessageHeader *) buf;
220 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
221 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
222 return sizeof (struct GNUNET_MessageHeader);
227 * Request that the service should shutdown.
228 * Afterwards, the connection will automatically be
229 * disconnected. Hence the "sock" should not
230 * be used by the caller after this call
231 * (calling this function frees "sock" after a while).
233 * @param sock the socket connected to the service
234 * @param timeout how long to wait before giving up on transmission
235 * @param cont continuation to call once the service is really down
236 * @param cont_cls closure for continuation
240 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
241 struct GNUNET_TIME_Relative timeout,
242 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
244 struct ShutdownContext *shutdown_ctx;
246 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
247 shutdown_ctx->cont = cont;
248 shutdown_ctx->cont_cls = cont_cls;
249 shutdown_ctx->sock = sock;
250 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
251 GNUNET_CLIENT_notify_transmit_ready (sock,
252 sizeof (struct GNUNET_MessageHeader),
253 timeout, GNUNET_NO, &write_shutdown,
259 arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus status)
261 GNUNET_assert ( (status == GNUNET_ARM_PROCESS_DOWN) ||
262 (status == GNUNET_ARM_PROCESS_ALREADY_DOWN) );
264 GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
270 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
274 do_nothing_notify (void *cls, enum GNUNET_ARM_ProcessStatus status)
276 GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING);
278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
282 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus status)
284 GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING);
285 GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify,
291 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
295 do_nothing_restarted_notify_task (void *cls,
296 const struct GNUNET_SCHEDULER_TaskContext
304 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
306 FPRINTF (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
308 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
310 FPRINTF (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
312 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
314 FPRINTF (killLogFilePtr, "%d.Service is running!\n", trialCount);
317 GNUNET_SCHEDULER_add_now (&kill_task, &a);
322 do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT,
325 &do_nothing_restarted_notify_task, NULL);
330 shutdown_cont (void *cls, int reason)
333 startedWaitingAt = GNUNET_TIME_absolute_get ();
334 GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL);
339 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
345 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
348 FPRINTF (killLogFilePtr, "Waited for: %llu ms\n",
349 (unsigned long long) waitedFor.rel_value);
354 waitedFor.rel_value = 0;
356 /* Connect to the doNothing task */
357 doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
358 GNUNET_assert (doNothingConnection != NULL);
359 if (trialCount == 12)
361 GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
362 GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop,
367 /* Use the created connection to kill the doNothingTask */
368 arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
373 task (void *cls, char *const *args, const char *cfgfile,
374 const struct GNUNET_CONFIGURATION_Handle *c)
378 arm = GNUNET_ARM_connect (cfg, NULL);
380 GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
383 arm_do_nothing (NULL, GNUNET_YES);
391 char *const argv[] = {
392 "test-exponential-backoff",
393 "-c", "test_arm_api_data.conf",
399 struct GNUNET_GETOPT_CommandLineOption options[] = {
400 GNUNET_GETOPT_OPTION_END
403 /* Running ARM and running the do_nothing task */
404 GNUNET_assert (GNUNET_OK ==
405 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
406 argv, "test-exponential-backoff",
407 "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",
459 /* end of test_exponential_backoff.c */