cleaning up arm wrt processing options and prefixes
[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 VERBOSE GNUNET_NO
31 #define START_ARM GNUNET_YES
32 #define LOG_BACKOFF GNUNET_NO
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
34 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
35 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
36
37 static struct GNUNET_SCHEDULER_Handle *sched;
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static struct GNUNET_ARM_Handle *arm;
40 static int ok = 1;
41
42 #if LOG_BACKOFF
43 static FILE *killLogFilePtr;
44 static char *killLogFileName;
45 #endif
46
47
48 static void
49 arm_notify_stop (void *cls, int success)
50 {
51   GNUNET_assert (success == GNUNET_NO);
52 #if START_ARM
53   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
54 #endif
55 }
56
57
58 static void
59 kill_task (void *cbData,
60            const struct GNUNET_SCHEDULER_TaskContext *tc);
61
62
63 static void
64 do_nothing_notify (void *cls, int success)
65 {
66   GNUNET_assert (success == GNUNET_YES);
67   ok = 1;
68   GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_UNIT_SECONDS, 
69                                 &kill_task, NULL);
70 }
71
72
73 static void
74 arm_notify (void *cls, int success)
75
76   GNUNET_assert (success == GNUNET_YES);
77   GNUNET_ARM_start_service (arm, 
78                             "do-nothing", TIMEOUT, 
79                             &do_nothing_notify, NULL);
80 }
81
82
83 static void
84 kill_task (void *cbData,
85                    const struct GNUNET_SCHEDULER_TaskContext *tc);
86
87
88 static void
89 do_nothing_restarted_notify_task (void *cls,
90                                   const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {       
92   static char a;
93   static int trialCount = 0;
94   
95   trialCount++;
96
97 #if LOG_BACKOFF  
98   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 
99     {
100       fprintf(killLogFilePtr, 
101               "%d.Reason is shutdown!\n",
102               trialCount);
103     } 
104   else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0) 
105     {
106       fprintf(killLogFilePtr, 
107               "%d.Reason is timeout!\n", 
108               trialCount);
109     }
110   else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0) 
111     {
112       fprintf(killLogFilePtr, 
113               "%d.Service is running!\n", 
114               trialCount);
115     }  
116 #endif
117   GNUNET_SCHEDULER_add_now (sched, &kill_task, &a);
118 }
119
120
121 static void
122 do_test (void *cbData,
123          const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {                                     
125   GNUNET_CLIENT_service_test(sched, "do-nothing", 
126                              cfg, TIMEOUT,
127                              &do_nothing_restarted_notify_task, NULL);
128 }
129
130
131 static void
132 kill_task (void *cbData,
133                    const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   static struct GNUNET_CLIENT_Connection * doNothingConnection = NULL;
136   static struct GNUNET_TIME_Absolute startedWaitingAt;
137   struct GNUNET_TIME_Relative waitedFor;
138   static int trialCount = 0;
139   
140   if (NULL != cbData) {
141     waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
142 #if LOG_BACKOFF
143     fprintf(killLogFilePtr, 
144             "Waited for: %llu ms\n", 
145             (unsigned long long) waitedFor.value);
146 #endif
147   }
148   else
149     waitedFor.value = 0;
150   /* Connect to the doNothing task */
151   doNothingConnection = GNUNET_CLIENT_connect (sched, "do-nothing", cfg);
152 #if LOG_BACKOFF
153   if (NULL == doNothingConnection)
154     fprintf(killLogFilePtr, 
155             "Unable to connect to do-nothing process!\n");
156 #endif  
157   if (trialCount == 12) {
158     GNUNET_ARM_stop_service (arm, 
159                              "do-nothing", 
160                              TIMEOUT,
161                              &arm_notify_stop, NULL);
162     ok = 0;
163     return;
164   }
165   
166   /* Use the created connection to kill the doNothingTask */
167   GNUNET_CLIENT_service_shutdown(doNothingConnection);
168   trialCount++;
169   startedWaitingAt = GNUNET_TIME_absolute_get();
170   GNUNET_SCHEDULER_add_delayed (sched,
171                                 waitedFor,
172                                 &do_test,
173                                 NULL);
174 }
175
176        
177 static void
178 task (void *cls,
179       struct GNUNET_SCHEDULER_Handle *s,
180       char *const *args,
181       const char *cfgfile,
182       const struct GNUNET_CONFIGURATION_Handle *c)
183 {
184   cfg = c;
185   sched = s;
186   
187   arm = GNUNET_ARM_connect (cfg, sched, NULL);
188 #if START_ARM
189   GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify, NULL);
190 #else
191   arm_do_nothing (NULL, GNUNET_YES);
192 #endif
193 }
194
195 static int
196 check ()
197 {
198   char *const argv[] = {
199     "test-arm-api",
200     "-c", "test_arm_api_data.conf",
201 #if VERBOSE
202     "-L", "DEBUG",
203 #endif
204     NULL
205   };
206   struct GNUNET_GETOPT_CommandLineOption options[] = {
207     GNUNET_GETOPT_OPTION_END
208   };
209   
210   /* Running ARM  and running the do_nothing task */
211   GNUNET_assert (GNUNET_OK ==
212                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
213                                      argv,
214                                      "test-exponential-backoff",
215                                      "nohelp", options, &task, NULL));
216   
217   
218   return ok;
219 }
220
221 static int
222 init()
223 {
224 #if LOG_BACKOFF
225   killLogFileName = GNUNET_DISK_mktemp("exponential-backoff-waiting.log");
226   if (NULL == (killLogFilePtr = FOPEN(killLogFileName, "w"))) {
227     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", killLogFileName);
228     GNUNET_free (killLogFileName);
229     return GNUNET_SYSERR;
230   }  
231 #endif
232   return GNUNET_OK;
233 }
234
235
236 static void
237 houseKeep()
238 {
239 #if LOG_BACKOFF
240   GNUNET_assert (0 == fclose (killLogFilePtr));
241   GNUNET_free(killLogFileName);
242 #endif
243 }
244
245
246 int
247 main (int argc, char *argv[])
248 {
249   int ret;
250
251   GNUNET_log_setup ("test-exponential-backoff",
252 #if VERBOSE
253                     "DEBUG",
254 #else
255                     "WARNING",
256 #endif
257                     NULL);
258   
259   init();
260   ret = check ();
261   houseKeep();
262   return ret;
263 }
264
265 /* end of test_exponential_backoff.c */