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
32 #define START_ARM GNUNET_YES
33 #define LOG_BACKOFF GNUNET_NO
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
36 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static struct GNUNET_ARM_Handle *arm;
42 static int trialCount;
43 static struct GNUNET_TIME_Absolute startedWaitingAt;
44 struct GNUNET_TIME_Relative waitedFor;
47 static FILE *killLogFilePtr;
48 static char *killLogFileName;
53 * Context for handling the shutdown of a service.
55 struct ShutdownContext
58 * Connection to the service that is being shutdown.
60 struct GNUNET_CLIENT_Connection *sock;
63 * Time allowed for shutdown to happen.
65 struct GNUNET_TIME_Absolute timeout;
68 * Task set up to cancel the shutdown request on timeout.
70 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
73 * Task to call once shutdown complete
75 GNUNET_CLIENT_ShutdownTask cont;
78 * Closure for shutdown continuation
83 * We received a confirmation that the service will shut down.
90 * Handler receiving response to service shutdown requests.
91 * First call with NULL: service misbehaving, or something.
92 * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
93 * - service will shutdown
94 * Second call with NULL:
95 * - service has now really shut down.
98 * @param msg NULL, indicating socket closure.
101 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
103 struct ShutdownContext *shutdown_ctx = cls;
105 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
107 /* Means the other side closed the connection and never confirmed a shutdown */
108 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
109 "Service handle shutdown before ACK!\n");
110 if (shutdown_ctx->cont != NULL)
111 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
112 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
113 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
114 GNUNET_free (shutdown_ctx);
116 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
121 if (shutdown_ctx->cont != NULL)
122 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
124 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
125 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
126 GNUNET_free (shutdown_ctx);
130 GNUNET_assert (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
131 switch (ntohs (msg->type))
133 case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "Received confirmation for service shutdown.\n");
138 shutdown_ctx->confirmed = GNUNET_YES;
139 GNUNET_CLIENT_receive (shutdown_ctx->sock,
140 &service_shutdown_handler,
141 shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
143 default: /* Fall through */
144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Service shutdown refused!\n");
145 if (shutdown_ctx->cont != NULL)
146 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
148 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
149 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
150 GNUNET_free (shutdown_ctx);
157 * Shutting down took too long, cancel receive and return error.
160 * @param tc context information (why was this task triggered now)
163 service_shutdown_cancel (void *cls,
164 const struct GNUNET_SCHEDULER_TaskContext *tc)
166 struct ShutdownContext *shutdown_ctx = cls;
168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
169 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
170 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
171 GNUNET_free (shutdown_ctx);
176 * If possible, write a shutdown message to the target
177 * buffer and destroy the client connection.
179 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
180 * @param size number of bytes available in buf
181 * @param buf NULL on error, otherwise target buffer
182 * @return number of bytes written to buf
185 write_shutdown (void *cls, size_t size, void *buf)
187 struct GNUNET_MessageHeader *msg;
188 struct ShutdownContext *shutdown_ctx = cls;
190 if (size < sizeof (struct GNUNET_MessageHeader))
192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
193 _("Failed to transmit shutdown request to client.\n"));
194 shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
195 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
196 GNUNET_free (shutdown_ctx);
197 return 0; /* client disconnected */
200 GNUNET_CLIENT_receive (shutdown_ctx->sock,
201 &service_shutdown_handler, shutdown_ctx,
202 GNUNET_TIME_UNIT_FOREVER_REL);
203 shutdown_ctx->cancel_task =
204 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
205 (shutdown_ctx->timeout),
206 &service_shutdown_cancel, shutdown_ctx);
207 msg = (struct GNUNET_MessageHeader *) buf;
208 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
209 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
210 return sizeof (struct GNUNET_MessageHeader);
215 * Request that the service should shutdown.
216 * Afterwards, the connection will automatically be
217 * disconnected. Hence the "sock" should not
218 * be used by the caller after this call
219 * (calling this function frees "sock" after a while).
221 * @param sock the socket connected to the service
222 * @param timeout how long to wait before giving up on transmission
223 * @param cont continuation to call once the service is really down
224 * @param cont_cls closure for continuation
228 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
229 struct GNUNET_TIME_Relative timeout,
230 GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
232 struct ShutdownContext *shutdown_ctx;
234 shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
235 shutdown_ctx->cont = cont;
236 shutdown_ctx->cont_cls = cont_cls;
237 shutdown_ctx->sock = sock;
238 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
239 GNUNET_CLIENT_notify_transmit_ready (sock,
241 GNUNET_MessageHeader),
244 &write_shutdown, shutdown_ctx);
249 arm_notify_stop (void *cls, int success)
251 GNUNET_assert (success == GNUNET_NO);
253 GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
259 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
263 do_nothing_notify (void *cls, int success)
265 GNUNET_assert (success == GNUNET_YES);
267 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
272 arm_notify (void *cls, int success)
274 GNUNET_assert (success == GNUNET_YES);
275 GNUNET_ARM_start_service (arm,
276 "do-nothing", TIMEOUT, &do_nothing_notify, NULL);
281 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
285 do_nothing_restarted_notify_task (void *cls,
286 const struct GNUNET_SCHEDULER_TaskContext *tc)
293 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
295 fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
297 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
299 fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
301 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
303 fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount);
306 GNUNET_SCHEDULER_add_now (&kill_task, &a);
311 do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
313 GNUNET_CLIENT_service_test ("do-nothing",
315 &do_nothing_restarted_notify_task, NULL);
320 shutdown_cont (void *cls, int reason)
323 startedWaitingAt = GNUNET_TIME_absolute_get ();
324 GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL);
329 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
335 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
338 fprintf (killLogFilePtr,
339 "Waited for: %llu ms\n", (unsigned long long) waitedFor.rel_value);
344 waitedFor.rel_value = 0;
346 /* Connect to the doNothing task */
347 doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
348 GNUNET_assert (doNothingConnection != NULL);
349 if (trialCount == 12)
351 GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
352 GNUNET_ARM_stop_service (arm,
353 "do-nothing", TIMEOUT, &arm_notify_stop, NULL);
357 /* Use the created connection to kill the doNothingTask */
358 arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
365 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
369 arm = GNUNET_ARM_connect (cfg, NULL);
371 GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
374 arm_do_nothing (NULL, GNUNET_YES);
381 char *const argv[] = {
383 "-c", "test_arm_api_data.conf",
389 struct GNUNET_GETOPT_CommandLineOption options[] = {
390 GNUNET_GETOPT_OPTION_END
393 /* Running ARM and running the do_nothing task */
394 GNUNET_assert (GNUNET_OK ==
395 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
397 "test-exponential-backoff",
398 "nohelp", options, &task, NULL));
408 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
409 if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
411 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
413 GNUNET_free (killLogFileName);
414 return GNUNET_SYSERR;
425 GNUNET_assert (0 == fclose (killLogFilePtr));
426 GNUNET_free (killLogFileName);
432 main (int argc, char *argv[])
436 GNUNET_log_setup ("test-exponential-backoff",
450 /* end of test_exponential_backoff.c */