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