- added check against statistics
[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 3, 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 #include "gnunet_protocols.h"
30
31 #define VERBOSE GNUNET_NO
32
33 #define START_ARM GNUNET_YES
34
35 #define LOG_BACKOFF GNUNET_NO
36
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38
39 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
40
41 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
42
43
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 static struct GNUNET_ARM_Handle *arm;
47
48 static int ok = 1;
49
50 static int trialCount;
51
52 static struct GNUNET_TIME_Absolute startedWaitingAt;
53
54 struct GNUNET_TIME_Relative waitedFor;
55
56 #if LOG_BACKOFF
57 static FILE *killLogFilePtr;
58
59 static char *killLogFileName;
60 #endif
61
62
63 /**
64  * Context for handling the shutdown of a service.
65  */
66 struct ShutdownContext
67 {
68   /**
69    * Connection to the service that is being shutdown.
70    */
71   struct GNUNET_CLIENT_Connection *sock;
72
73   /**
74    * Time allowed for shutdown to happen.
75    */
76   struct GNUNET_TIME_Absolute timeout;
77
78   /**
79    * Task set up to cancel the shutdown request on timeout.
80    */
81   GNUNET_SCHEDULER_TaskIdentifier cancel_task;
82
83   /**
84    * Task to call once shutdown complete
85    */
86   GNUNET_CLIENT_ShutdownTask cont;
87
88   /**
89    * Closure for shutdown continuation
90    */
91   void *cont_cls;
92
93   /**
94    * We received a confirmation that the service will shut down.
95    */
96   int confirmed;
97
98 };
99
100 /**
101  * Handler receiving response to service shutdown requests.
102  * First call with NULL: service misbehaving, or something.
103  * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN:
104  *   - service will shutdown
105  * Second call with NULL:
106  *   - service has now really shut down.
107  *
108  * @param cls closure
109  * @param msg NULL, indicating socket closure.
110  */
111 static void
112 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
113 {
114   struct ShutdownContext *shutdown_ctx = cls;
115
116   if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
117     {
118       /* Means the other side closed the connection and never confirmed a shutdown */
119       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
120                   "Service handle shutdown before ACK!\n");
121       if (shutdown_ctx->cont != NULL)
122         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
123       GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
124       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
125       GNUNET_free (shutdown_ctx);
126     }
127   else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
128     {
129 #if VERBOSE
130       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
131 #endif
132       if (shutdown_ctx->cont != NULL)
133         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
134
135       GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
136       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
137       GNUNET_free (shutdown_ctx);
138     }
139   else
140     {
141       GNUNET_assert (ntohs (msg->size) ==
142                      sizeof (struct GNUNET_MessageHeader));
143       switch (ntohs (msg->type))
144         {
145         case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN:
146 #if VERBOSE
147           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148                       "Received confirmation for service shutdown.\n");
149 #endif
150           shutdown_ctx->confirmed = GNUNET_YES;
151           GNUNET_CLIENT_receive (shutdown_ctx->sock,
152                                  &service_shutdown_handler, shutdown_ctx,
153                                  GNUNET_TIME_UNIT_FOREVER_REL);
154           break;
155         default:                /* Fall through */
156           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157                       "Service shutdown refused!\n");
158           if (shutdown_ctx->cont != NULL)
159             shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
160
161           GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
162           GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
163           GNUNET_free (shutdown_ctx);
164           break;
165         }
166     }
167 }
168
169 /**
170  * Shutting down took too long, cancel receive and return error.
171  *
172  * @param cls closure
173  * @param tc context information (why was this task triggered now)
174  */
175 void
176 service_shutdown_cancel (void *cls,
177                          const struct GNUNET_SCHEDULER_TaskContext *tc)
178 {
179   struct ShutdownContext *shutdown_ctx = cls;
180
181   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
182   shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
183   GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
184   GNUNET_free (shutdown_ctx);
185 }
186
187
188 /**
189  * If possible, write a shutdown message to the target
190  * buffer and destroy the client connection.
191  *
192  * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
193  * @param size number of bytes available in buf
194  * @param buf NULL on error, otherwise target buffer
195  * @return number of bytes written to buf
196  */
197 static size_t
198 write_shutdown (void *cls, size_t size, void *buf)
199 {
200   struct GNUNET_MessageHeader *msg;
201   struct ShutdownContext *shutdown_ctx = cls;
202
203   if (size < sizeof (struct GNUNET_MessageHeader))
204     {
205       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206                   _("Failed to transmit shutdown request to client.\n"));
207       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
208       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
209       GNUNET_free (shutdown_ctx);
210       return 0;                 /* client disconnected */
211     }
212
213   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
214                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
215   shutdown_ctx->cancel_task =
216     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
217                                   (shutdown_ctx->timeout),
218                                   &service_shutdown_cancel, shutdown_ctx);
219   msg = (struct GNUNET_MessageHeader *) buf;
220   msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
221   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
222   return sizeof (struct GNUNET_MessageHeader);
223 }
224
225
226 /**
227  * Request that the service should shutdown.
228  * Afterwards, the connection will automatically be
229  * disconnected.  Hence the "sock" should not
230  * be used by the caller after this call
231  * (calling this function frees "sock" after a while).
232  *
233  * @param sock the socket connected to the service
234  * @param timeout how long to wait before giving up on transmission
235  * @param cont continuation to call once the service is really down
236  * @param cont_cls closure for continuation
237  *
238  */
239 static void
240 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
241                       struct GNUNET_TIME_Relative timeout,
242                       GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
243 {
244   struct ShutdownContext *shutdown_ctx;
245
246   shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
247   shutdown_ctx->cont = cont;
248   shutdown_ctx->cont_cls = cont_cls;
249   shutdown_ctx->sock = sock;
250   shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
251   GNUNET_CLIENT_notify_transmit_ready (sock,
252                                        sizeof (struct GNUNET_MessageHeader),
253                                        timeout, GNUNET_NO, &write_shutdown,
254                                        shutdown_ctx);
255 }
256
257
258 static void
259 arm_notify_stop (void *cls, enum GNUNET_ARM_ProcessStatus status)
260 {
261   GNUNET_assert ( (status == GNUNET_ARM_PROCESS_DOWN) ||
262                   (status == GNUNET_ARM_PROCESS_ALREADY_DOWN) );
263 #if START_ARM
264   GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
265 #endif
266 }
267
268
269 static void
270 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
271
272
273 static void
274 do_nothing_notify (void *cls, enum GNUNET_ARM_ProcessStatus status)
275 {
276   GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING);
277   ok = 1;
278   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
279 }
280
281 static void
282 arm_notify (void *cls, enum GNUNET_ARM_ProcessStatus status)
283 {
284   GNUNET_assert (status == GNUNET_ARM_PROCESS_STARTING);
285   GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify,
286                             NULL);
287 }
288
289
290 static void
291 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
292
293
294 static void
295 do_nothing_restarted_notify_task (void *cls,
296                                   const struct GNUNET_SCHEDULER_TaskContext
297                                   *tc)
298 {
299   static char a;
300
301   trialCount++;
302
303 #if LOG_BACKOFF
304   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
305     {
306       FPRINTF (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
307     }
308   else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
309     {
310       FPRINTF (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
311     }
312   else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
313     {
314       FPRINTF (killLogFilePtr, "%d.Service is running!\n", trialCount);
315     }
316 #endif
317   GNUNET_SCHEDULER_add_now (&kill_task, &a);
318 }
319
320
321 static void
322 do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
323 {
324   GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT,
325                               &do_nothing_restarted_notify_task, NULL);
326 }
327
328
329 static void
330 shutdown_cont (void *cls, int reason)
331 {
332   trialCount++;
333   startedWaitingAt = GNUNET_TIME_absolute_get ();
334   GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL);
335 }
336
337
338 static void
339 kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 {
341   static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
342
343   if (NULL != cbData)
344     {
345       waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
346
347 #if LOG_BACKOFF
348       FPRINTF (killLogFilePtr, "Waited for: %llu ms\n",
349                (unsigned long long) waitedFor.rel_value);
350 #endif
351     }
352   else
353     {
354       waitedFor.rel_value = 0;
355     }
356   /* Connect to the doNothing task */
357   doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
358   GNUNET_assert (doNothingConnection != NULL);
359   if (trialCount == 12)
360     {
361       GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
362       GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop,
363                                NULL);
364       ok = 0;
365       return;
366     }
367   /* Use the created connection to kill the doNothingTask */
368   arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
369 }
370
371
372 static void
373 task (void *cls, char *const *args, const char *cfgfile,
374       const struct GNUNET_CONFIGURATION_Handle *c)
375 {
376   cfg = c;
377
378   arm = GNUNET_ARM_connect (cfg, NULL);
379 #if START_ARM
380   GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
381                             NULL);
382 #else
383   arm_do_nothing (NULL, GNUNET_YES);
384 #endif
385 }
386
387
388 static int
389 check ()
390 {
391   char *const argv[] = {
392     "test-exponential-backoff",
393     "-c", "test_arm_api_data.conf",
394 #if VERBOSE
395     "-L", "DEBUG",
396 #endif
397     NULL
398   };
399   struct GNUNET_GETOPT_CommandLineOption options[] = {
400     GNUNET_GETOPT_OPTION_END
401   };
402
403   /* Running ARM  and running the do_nothing task */
404   GNUNET_assert (GNUNET_OK ==
405                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
406                                      argv, "test-exponential-backoff",
407                                      "nohelp", options, &task, NULL));
408
409
410   return ok;
411 }
412
413 static int
414 init ()
415 {
416 #if LOG_BACKOFF
417   killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
418   if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
419     {
420       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
421                                 killLogFileName);
422       GNUNET_free (killLogFileName);
423       return GNUNET_SYSERR;
424     }
425 #endif
426   return GNUNET_OK;
427 }
428
429
430 static void
431 houseKeep ()
432 {
433 #if LOG_BACKOFF
434   GNUNET_assert (0 == fclose (killLogFilePtr));
435   GNUNET_free (killLogFileName);
436 #endif
437 }
438
439
440 int
441 main (int argc, char *argv[])
442 {
443   int ret;
444
445   GNUNET_log_setup ("test-exponential-backoff",
446 #if VERBOSE
447                     "DEBUG",
448 #else
449                     "WARNING",
450 #endif
451                     NULL);
452
453   init ();
454   ret = check ();
455   houseKeep ();
456   return ret;
457 }
458
459 /* end of test_exponential_backoff.c */