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 2, 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"
30 #define START_ARM GNUNET_YES
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 static struct GNUNET_SCHEDULER_Handle *sched;
34 static const struct GNUNET_CONFIGURATION_Handle *cfg;
35 static struct GNUNET_ARM_Handle *arm;
37 static int kill_ok = GNUNET_OK;
38 static FILE *killLogFilePtr;
39 static char *killLogFileName;
42 arm_notify_stop (void *cls, int success)
44 GNUNET_assert (success == GNUNET_NO);
46 GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
52 do_nothing_notify_stop (void *cls, int success)
54 GNUNET_assert (success == GNUNET_NO);
59 do_nothing_notify (void *cls, int success)
61 GNUNET_assert (success == GNUNET_YES);
67 arm_notify (void *cls, int success)
69 GNUNET_assert (success == GNUNET_YES);
70 GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify, NULL);
75 kill_task (void *cbData,
76 const struct GNUNET_SCHEDULER_TaskContext *tc);
78 do_nothing_restarted_notify_task (void *unused,
79 const struct GNUNET_SCHEDULER_TaskContext *tc)
82 static int trialCount = 0;
85 if (trialCount >= 11) {
86 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
87 fprintf(killLogFilePtr, "Reason is shutdown!\n");
88 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
89 fprintf(killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
90 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
91 fprintf(killLogFilePtr, "%d.Service is running!\n", trialCount);
94 GNUNET_SCHEDULER_add_now (sched, &kill_task, &a); // checks if this was too fast
99 kill_task (void *cbData,
100 const struct GNUNET_SCHEDULER_TaskContext *tc)
103 struct GNUNET_CLIENT_Connection * doNothingConnection = NULL;
104 static struct GNUNET_TIME_Absolute startedWaitingAt;
105 struct GNUNET_TIME_Relative waitedFor;
106 static int trialCount = 0;
109 if (NULL != reason) {
110 waitedFor = GNUNET_TIME_absolute_get_duration(startedWaitingAt);
112 if (trialCount >= 11)
113 fprintf(killLogFilePtr,
114 "Trial no.%d, Started waiting at: %llu. Waited for: %llu\n",
116 (unsigned long long) startedWaitingAt.value,
117 (unsigned long long) waitedFor.value);
120 /* Connect to the doNothing task */
121 doNothingConnection = GNUNET_CLIENT_connect (sched, "do-nothing", cfg);
122 if (NULL == doNothingConnection)
123 fprintf(killLogFilePtr, "Unable to connect to do-nothing process!\n");
124 else if (trialCount == 20) {
125 GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop, NULL);
129 /* Use the created connection to kill the doNothingTask */
130 GNUNET_CLIENT_service_shutdown(doNothingConnection);
132 startedWaitingAt = GNUNET_TIME_absolute_get();
134 * There is something wrong here!
135 * TIMEOUT value is set to 10 seconds (a drastically large value)
136 * debugging is showing that the reason for which
137 * do_nothing_restarted_notify_task is called is "always"
138 * TIMEOUT which means that the "do-nothing" service is not running
139 * however the overall execution time doesn't exceed 2 seconds
140 * which means there is something tricky about the TIMOUT passed
141 * to function GNUNET_CLIENT_service_test()
143 GNUNET_CLIENT_service_test(sched, "do-nothing", cfg, TIMEOUT, &do_nothing_restarted_notify_task, NULL);
148 struct GNUNET_SCHEDULER_Handle *s,
151 const struct GNUNET_CONFIGURATION_Handle *c)
156 arm = GNUNET_ARM_connect (cfg, sched, NULL);
158 GNUNET_ARM_start_service (arm, "arm", TIMEOUT, &arm_notify, NULL);
160 arm_do_nothing (NULL, GNUNET_YES);
162 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
168 char *const argv[] = {
170 "-c", "test_arm_api_data.conf",
176 struct GNUNET_GETOPT_CommandLineOption options[] = {
177 GNUNET_GETOPT_OPTION_END
180 /* Running ARM and running the do_nothing task */
181 GNUNET_assert (GNUNET_OK ==
182 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
184 "test-exponential-backoff",
185 "nohelp", options, &task, NULL));
194 killLogFileName = GNUNET_DISK_mktemp("exponential-backoff-waiting.log");
195 if (NULL == (killLogFilePtr = FOPEN(killLogFileName, "w"))) {
196 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", killLogFileName);
197 GNUNET_free (killLogFileName);
198 return GNUNET_SYSERR;
208 GNUNET_assert (0 == fclose (killLogFilePtr));
209 GNUNET_free(killLogFileName);
214 main (int argc, char *argv[])
219 GNUNET_log_setup ("test-exponential-backoff",
233 /* end of test_exponential_backoff.c */