dead
[oweals/gnunet.git] / src / arm / test_exponential_backoff.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file arm/test_exponential_backoff.c
22  * @brief testcase for gnunet-service-arm.c
23  */
24 #include "platform.h"
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
30 #define START_ARM GNUNET_YES
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static struct GNUNET_SCHEDULER_Handle *sched;
34 static const struct GNUNET_CONFIGURATION_Handle *cfg;
35 static struct GNUNET_ARM_Handle *arm;
36 static int ok = 1;
37 static FILE *killLogFilePtr;
38 static char *killLogFileName;
39
40 static void
41 arm_notify_stop (void *cls, int success)
42 {
43   GNUNET_assert (success == GNUNET_NO);
44 #if START_ARM
45   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
46 #endif
47 }
48
49
50
51 static void
52 do_nothing_notify (void *cls, int success)
53 {
54         GNUNET_assert (success == GNUNET_YES);
55         ok = 1;
56 }
57
58
59 static void
60 arm_notify (void *cls, int success)
61
62   GNUNET_assert (success == GNUNET_YES);
63   GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify, NULL);
64 }
65
66
67 static void
68 kill_task (void *cbData,
69                    const struct GNUNET_SCHEDULER_TaskContext *tc);
70 static void
71 do_nothing_restarted_notify_task (void *unused,
72                    const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {
74         static char a;
75         static int trialCount = 0;
76         
77         trialCount++;
78         if (trialCount >= 11) {
79                 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 
80                         fprintf(killLogFilePtr, "Reason is shutdown!\n");
81                 else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
82                   fprintf(killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
83                 else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
84                         fprintf(killLogFilePtr, "%d.Service is running!\n", trialCount);
85         }
86                 
87         GNUNET_SCHEDULER_add_now (sched, &kill_task, &a); // checks if this was too fast
88 }
89
90
91 static void
92 kill_task (void *cbData,
93                    const struct GNUNET_SCHEDULER_TaskContext *tc)
94 {
95         int* reason;
96         struct GNUNET_CLIENT_Connection * doNothingConnection = NULL;
97         static struct GNUNET_TIME_Absolute startedWaitingAt;
98         struct GNUNET_TIME_Relative waitedFor;
99         static int trialCount = 0;
100         
101         reason = cbData;
102         if (NULL != reason) {
103                 waitedFor = GNUNET_TIME_absolute_get_duration(startedWaitingAt);
104                 trialCount++;
105                 if (trialCount >= 11) 
106                   fprintf(killLogFilePtr,
107                           "Trial no.%d, Started waiting at: %llu. Waited for: %llu\n", 
108                           trialCount, 
109                           (unsigned long long) startedWaitingAt.value, 
110                           (unsigned long long) waitedFor.value);
111         }
112         
113          /* Connect to the doNothing task */ 
114         doNothingConnection = GNUNET_CLIENT_connect (sched, "do-nothing", cfg);
115         if (NULL == doNothingConnection)
116                 fprintf(killLogFilePtr, "Unable to connect to do-nothing process!\n");
117         else if (trialCount == 20) {
118                 GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop, NULL);
119                 return;
120         }
121         
122         /* Use the created connection to kill the doNothingTask */ 
123         GNUNET_CLIENT_service_shutdown(doNothingConnection);
124         sleep(0.005);
125         startedWaitingAt = GNUNET_TIME_absolute_get();
126         /* 
127          * There is something wrong here!
128          * TIMEOUT value is set to 10 seconds (a drastically large value)
129          * debugging is showing that the reason for which
130          * do_nothing_restarted_notify_task is called is "always"
131          * TIMEOUT which means that the "do-nothing" service is not running 
132          * however the overall execution time doesn't exceed 2 seconds
133          * which means there is something tricky about the TIMOUT passed
134          * to function GNUNET_CLIENT_service_test()
135          */
136         GNUNET_CLIENT_service_test(sched, "do-nothing", cfg, TIMEOUT, &do_nothing_restarted_notify_task, NULL);
137 }
138
139 static void
140 task (void *cls,
141       struct GNUNET_SCHEDULER_Handle *s,
142       char *const *args,
143       const char *cfgfile,
144       const struct GNUNET_CONFIGURATION_Handle *c)
145 {
146   cfg = c;
147   sched = s;
148   
149   arm = GNUNET_ARM_connect (cfg, sched, NULL);
150 #if START_ARM
151   GNUNET_ARM_start_service (arm, "arm", TIMEOUT, &arm_notify, NULL);
152 #else
153   arm_do_nothing (NULL, GNUNET_YES);
154 #endif
155   GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
156 }
157
158 static int
159 check ()
160 {
161   char *const argv[] = {
162     "test-arm-api",
163     "-c", "test_arm_api_data.conf",
164 #if VERBOSE
165     "-L", "DEBUG",
166 #endif
167     NULL
168   };
169   struct GNUNET_GETOPT_CommandLineOption options[] = {
170     GNUNET_GETOPT_OPTION_END
171   };
172   
173   /* Running ARM  and running the do_nothing task */
174   GNUNET_assert (GNUNET_OK ==
175                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
176                                      argv,
177                                      "test-exponential-backoff",
178                                      "nohelp", options, &task, NULL));
179   
180   
181   return ok;
182 }
183
184 static int
185 init()
186 {
187         killLogFileName = GNUNET_DISK_mktemp("exponential-backoff-waiting.log");
188         if (NULL == (killLogFilePtr = FOPEN(killLogFileName, "w"))) {
189                  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", killLogFileName);
190                  GNUNET_free (killLogFileName);
191                  return GNUNET_SYSERR;
192         }
193         
194         return GNUNET_OK;
195 }
196
197
198 static void
199 houseKeep()
200 {
201         GNUNET_assert (0 == fclose (killLogFilePtr));
202         GNUNET_free(killLogFileName);
203 }
204
205
206 int
207 main (int argc, char *argv[])
208 {
209   int ret;
210
211
212   GNUNET_log_setup ("test-exponential-backoff",
213 #if VERBOSE
214                     "DEBUG",
215 #else
216                     "WARNING",
217 #endif
218                     NULL);
219   
220   init();
221   ret = check ();
222   houseKeep();
223   return ret;
224 }
225
226 /* end of test_exponential_backoff.c */