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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file arm/test_exponential_backoff.c
17 * @brief testcase for gnunet-service-arm.c
18 * @author Christian Grothoff
21 #include "gnunet_arm_service.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_protocols.h"
25 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
27 #define LOG_BACKOFF GNUNET_NO
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
31 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
33 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
35 #define SERVICE "do-nothing"
37 #define BINARY "mockup-service"
39 #define CFGFILENAME "test_arm_api_data2.conf"
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 static struct GNUNET_ARM_Handle *arm;
46 static struct GNUNET_ARM_MonitorHandle *mon;
48 static struct GNUNET_SCHEDULER_Task *kt;
54 static int trialCount;
56 static struct GNUNET_TIME_Absolute startedWaitingAt;
58 struct GNUNET_TIME_Relative waitedFor;
60 struct GNUNET_TIME_Relative waitedFor_prev;
63 static FILE *killLogFilePtr;
65 static char *killLogFileName;
70 * Context for handling the shutdown of a service.
72 struct ShutdownContext
75 * Connection to the service that is being shutdown.
77 struct GNUNET_MQ_Handle *mq;
80 * Task set up to cancel the shutdown request on timeout.
82 struct GNUNET_SCHEDULER_Task *cancel_task;
88 kill_task (void *cbData);
92 * Shutting down took too long, cancel receive and return error.
97 service_shutdown_timeout (void *cls)
104 * Generic error handler, called with the appropriate error code and
105 * the same closure specified at the creation of the message queue.
106 * Not every message queue implementation supports an error handler.
108 * @param cls closure with the `struct ShutdownContext *`
109 * @param error error code
112 mq_error_handler (void *cls,
113 enum GNUNET_MQ_Error error)
115 struct ShutdownContext *shutdown_ctx = cls;
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "Service shutdown complete (MQ error).\n");
119 GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
120 GNUNET_MQ_destroy (shutdown_ctx->mq);
121 GNUNET_free (shutdown_ctx);
126 kill_task (void *cbData)
128 struct ShutdownContext *shutdown_ctx
129 = GNUNET_new (struct ShutdownContext);
130 struct GNUNET_MQ_Envelope *env;
131 struct GNUNET_MessageHeader *msg;
132 struct GNUNET_MQ_MessageHandler handlers[] = {
133 GNUNET_MQ_handler_end ()
137 if (trialCount == 13)
139 LOG ("Saw enough kills, asking ARM to stop mock service for good\n");
140 GNUNET_ARM_request_service_stop (arm,
146 GNUNET_free (shutdown_ctx);
149 shutdown_ctx->mq = GNUNET_CLIENT_connect (cfg,
154 GNUNET_assert (NULL != shutdown_ctx->mq);
156 LOG ("Sending a shutdown request to the mock service\n");
157 env = GNUNET_MQ_msg (msg,
158 GNUNET_MESSAGE_TYPE_ARM_STOP); /* FIXME: abuse of message type */
159 GNUNET_MQ_send (shutdown_ctx->mq,
161 shutdown_ctx->cancel_task
162 = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
163 &service_shutdown_timeout,
169 trigger_disconnect (void *cls)
171 GNUNET_ARM_disconnect (arm);
172 GNUNET_ARM_monitor_stop (mon);
175 GNUNET_SCHEDULER_cancel (kt);
182 arm_stop_cb (void *cls,
183 enum GNUNET_ARM_RequestStatus status,
184 enum GNUNET_ARM_Result result)
186 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
187 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
188 LOG ("ARM service stopped\n");
189 GNUNET_SCHEDULER_shutdown ();
194 srv_status (void *cls,
196 enum GNUNET_ARM_ServiceStatus status)
198 if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
200 LOG ("ARM monitor started, starting mock service\n");
202 GNUNET_ARM_request_service_start (arm,
204 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
209 if (0 != strcasecmp (service, SERVICE))
210 return; /* not what we care about */
213 GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
214 GNUNET_break (phase == 1);
215 LOG ("do-nothing is starting\n");
218 GNUNET_assert (NULL == kt);
219 startedWaitingAt = GNUNET_TIME_absolute_get ();
220 kt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
226 /* We passively monitor ARM for status updates. ARM should tell us
227 * when do-nothing dies (no need to run a service upness test ourselves).
229 if (status == GNUNET_ARM_SERVICE_STARTING)
231 waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
232 LOG ("Waited for: %s\n",
233 GNUNET_STRINGS_relative_time_to_string (waitedFor,
236 LOG ("do-nothing is starting, killing it...\n");
237 GNUNET_assert (NULL == kt);
238 kt = GNUNET_SCHEDULER_add_now (&kill_task, &ok);
240 else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
243 LOG ("do-nothing stopped working %u times, we are done here\n",
244 (unsigned int) trialCount);
245 GNUNET_ARM_request_service_stop (arm,
255 arm_start_cb (void *cls,
256 enum GNUNET_ARM_RequestStatus status,
257 enum GNUNET_ARM_Result result)
259 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
260 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
261 GNUNET_break (phase == 0);
262 LOG ("Sent 'START' request for arm to ARM %s\n",
263 (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
271 const struct GNUNET_CONFIGURATION_Handle *c)
274 arm = GNUNET_ARM_connect (cfg, NULL, NULL);
280 mon = GNUNET_ARM_monitor_start (cfg,
286 GNUNET_ARM_disconnect (arm);
290 GNUNET_ARM_request_service_start (arm,
292 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
295 GNUNET_SCHEDULER_add_shutdown (&trigger_disconnect,
303 char *const argv[] = {
304 "test-exponential-backoff",
308 struct GNUNET_GETOPT_CommandLineOption options[] = {
309 GNUNET_GETOPT_OPTION_END
312 /* Running ARM and running the do_nothing task */
313 GNUNET_assert (GNUNET_OK ==
314 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
316 "test-exponential-backoff",
327 * Assumed maximum path length (for the log file name).
329 #define PATH_MAX 4096
336 struct GNUNET_CONFIGURATION_Handle *cfg;
340 cfg = GNUNET_CONFIGURATION_create ();
341 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
342 "test_arm_api_data.conf"))
343 return GNUNET_SYSERR;
344 if (NULL == getcwd (pwd, PATH_MAX))
345 return GNUNET_SYSERR;
346 GNUNET_assert (0 < GNUNET_asprintf (&binary,
350 GNUNET_CONFIGURATION_set_value_string (cfg,
354 GNUNET_free (binary);
355 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg,
358 GNUNET_CONFIGURATION_destroy (cfg);
359 return GNUNET_SYSERR;
361 GNUNET_CONFIGURATION_destroy (cfg);
364 killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
365 if (NULL == (killLogFilePtr = FOPEN (killLogFileName,
368 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
371 GNUNET_free (killLogFileName);
372 return GNUNET_SYSERR;
383 GNUNET_assert (0 == fclose (killLogFilePtr));
384 GNUNET_free (killLogFileName);
386 (void) unlink (CFGFILENAME);
391 main (int argc, char *argv[])
395 GNUNET_log_setup ("test-exponential-backoff",
399 if (GNUNET_OK != init ())
406 /* end of test_exponential_backoff.c */