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