-convert backoff test to new MQ API
[oweals/gnunet.git] / src / arm / test_exponential_backoff.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file arm/test_exponential_backoff.c
22  * @brief testcase for gnunet-service-arm.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29
30 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
31
32 #define LOG_BACKOFF GNUNET_NO
33
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35
36 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
37
38 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
39
40 #define SERVICE "do-nothing"
41
42 #define BINARY "mockup-service"
43
44 #define CFGFILENAME "test_arm_api_data2.conf"
45
46
47 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49 static struct GNUNET_ARM_Handle *arm;
50
51 static struct GNUNET_ARM_MonitorHandle *mon;
52
53 static struct GNUNET_SCHEDULER_Task *kt;
54
55 static int ok = 1;
56
57 static int phase = 0;
58
59 static int trialCount;
60
61 static struct GNUNET_TIME_Absolute startedWaitingAt;
62
63 struct GNUNET_TIME_Relative waitedFor;
64
65 struct GNUNET_TIME_Relative waitedFor_prev;
66
67 #if LOG_BACKOFF
68 static FILE *killLogFilePtr;
69
70 static char *killLogFileName;
71 #endif
72
73
74 /**
75  * Context for handling the shutdown of a service.
76  */
77 struct ShutdownContext
78 {
79   /**
80    * Connection to the service that is being shutdown.
81    */
82   struct GNUNET_MQ_Handle *mq;
83
84   /**
85    * Task set up to cancel the shutdown request on timeout.
86    */
87   struct GNUNET_SCHEDULER_Task *cancel_task;
88
89 };
90
91
92 static void
93 kill_task (void *cbData);
94
95
96 /**
97  * Shutting down took too long, cancel receive and return error.
98  *
99  * @param cls closure
100  */
101 static void
102 service_shutdown_timeout (void *cls)
103 {
104   GNUNET_assert (0);
105 }
106
107
108 /**
109  * Generic error handler, called with the appropriate error code and
110  * the same closure specified at the creation of the message queue.
111  * Not every message queue implementation supports an error handler.
112  *
113  * @param cls closure with the `struct ShutdownContext *`
114  * @param error error code
115  */
116 static void
117 mq_error_handler (void *cls,
118                   enum GNUNET_MQ_Error error)
119 {
120   struct ShutdownContext *shutdown_ctx = cls;
121
122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123               "Service shutdown complete (MQ error).\n");
124   GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
125   GNUNET_MQ_destroy (shutdown_ctx->mq);
126   GNUNET_free (shutdown_ctx);
127 }
128
129
130 static void
131 kill_task (void *cbData)
132 {
133   struct ShutdownContext *shutdown_ctx
134     = GNUNET_new (struct ShutdownContext);
135   struct GNUNET_MQ_Envelope *env;
136   struct GNUNET_MessageHeader *msg;
137   struct GNUNET_MQ_MessageHandler handlers[] = {
138     GNUNET_MQ_handler_end ()
139   };
140
141   kt = NULL;
142   if (trialCount == 13)
143   {
144     LOG ("Saw enough kills, asking ARM to stop mock service for good\n");
145     GNUNET_ARM_request_service_stop (arm,
146                                      SERVICE,
147                                      NULL,
148                                      NULL);
149     ok = 0;
150     trialCount++;
151     GNUNET_free (shutdown_ctx);
152     return;
153   }
154   shutdown_ctx->mq = GNUNET_CLIENT_connecT (cfg,
155                                             SERVICE,
156                                             handlers,
157                                             &mq_error_handler,
158                                             shutdown_ctx);
159   GNUNET_assert (NULL != shutdown_ctx->mq);
160   trialCount++;
161   LOG ("Sending a shutdown request to the mock service\n");
162   env = GNUNET_MQ_msg (msg,
163                        GNUNET_MESSAGE_TYPE_ARM_STOP); /* FIXME: abuse of message type */
164   GNUNET_MQ_send (shutdown_ctx->mq,
165                   env);
166   shutdown_ctx->cancel_task
167     = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
168                                     &service_shutdown_timeout,
169                                     shutdown_ctx);
170 }
171
172
173 static void
174 trigger_disconnect (void *cls)
175 {
176   GNUNET_ARM_disconnect (arm);
177   GNUNET_ARM_monitor_stop (mon);
178   if (NULL != kt)
179   {
180     GNUNET_SCHEDULER_cancel (kt);
181     kt = NULL;
182   }
183 }
184
185
186 static void
187 arm_stop_cb (void *cls,
188              enum GNUNET_ARM_RequestStatus status,
189              enum GNUNET_ARM_Result result)
190 {
191   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
192   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
193   LOG ("ARM service stopped\n");
194   GNUNET_SCHEDULER_shutdown ();
195 }
196
197
198 static void
199 srv_status (void *cls,
200             const char *service,
201             enum GNUNET_ARM_ServiceStatus status)
202 {
203   if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
204   {
205     LOG ("ARM monitor started, starting mock service\n");
206     phase++;
207     GNUNET_ARM_request_service_start (arm,
208                                       SERVICE,
209                                       GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
210                                       NULL,
211                                       NULL);
212     return;
213   }
214   if (0 != strcasecmp (service, SERVICE))
215     return; /* not what we care about */
216   if (phase == 1)
217   {
218     GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
219     GNUNET_break (phase == 1);
220     LOG ("do-nothing is starting\n");
221     phase++;
222     ok = 1;
223     GNUNET_assert (NULL == kt);
224     startedWaitingAt = GNUNET_TIME_absolute_get ();
225     kt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
226                                        &kill_task,
227                                        NULL);
228   }
229   else if (phase == 2)
230   {
231     /* We passively monitor ARM for status updates. ARM should tell us
232      * when do-nothing dies (no need to run a service upness test ourselves).
233      */
234     if (status == GNUNET_ARM_SERVICE_STARTING)
235     {
236       waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
237       LOG ("Waited for: %s\n",
238            GNUNET_STRINGS_relative_time_to_string (waitedFor,
239                                                    GNUNET_YES));
240
241       LOG ("do-nothing is starting, killing it...\n");
242       GNUNET_assert (NULL == kt);
243       kt = GNUNET_SCHEDULER_add_now (&kill_task, &ok);
244     }
245     else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
246     {
247       phase++;
248       LOG ("do-nothing stopped working %u times, we are done here\n",
249            (unsigned int) trialCount);
250       GNUNET_ARM_request_service_stop (arm,
251                                        "arm",
252                                        &arm_stop_cb,
253                                        NULL);
254     }
255   }
256 }
257
258
259 static void
260 arm_start_cb (void *cls,
261               enum GNUNET_ARM_RequestStatus status,
262               enum GNUNET_ARM_Result result)
263 {
264   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
265   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
266   GNUNET_break (phase == 0);
267   LOG ("Sent 'START' request for arm to ARM %s\n",
268        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
269 }
270
271
272 static void
273 task (void *cls,
274       char *const *args,
275       const char *cfgfile,
276       const struct GNUNET_CONFIGURATION_Handle *c)
277 {
278   cfg = c;
279   arm = GNUNET_ARM_connect (cfg, NULL, NULL);
280   if (NULL == arm)
281   {
282     GNUNET_break (0);
283     return;
284   }
285   mon = GNUNET_ARM_monitor_start (cfg,
286                                   &srv_status,
287                                   NULL);
288   if (NULL == mon)
289   {
290     GNUNET_break (0);
291     GNUNET_ARM_disconnect (arm);
292     arm = NULL;
293     return;
294   }
295   GNUNET_ARM_request_service_start (arm,
296                                     "arm",
297                                     GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
298                                     &arm_start_cb,
299                                     NULL);
300   GNUNET_SCHEDULER_add_shutdown (&trigger_disconnect,
301                                  NULL);
302 }
303
304
305 static int
306 check ()
307 {
308   char *const argv[] = {
309     "test-exponential-backoff",
310     "-c", CFGFILENAME,
311     NULL
312   };
313   struct GNUNET_GETOPT_CommandLineOption options[] = {
314     GNUNET_GETOPT_OPTION_END
315   };
316
317   /* Running ARM  and running the do_nothing task */
318   GNUNET_assert (GNUNET_OK ==
319                  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
320                                      argv,
321                                      "test-exponential-backoff",
322                                      "nohelp",
323                                      options,
324                                      &task,
325                                      NULL));
326   return ok;
327 }
328
329
330 #ifndef PATH_MAX
331 /**
332  * Assumed maximum path length (for the log file name).
333  */
334 #define PATH_MAX 4096
335 #endif
336
337
338 static int
339 init ()
340 {
341   struct GNUNET_CONFIGURATION_Handle *cfg;
342   char pwd[PATH_MAX];
343   char *binary;
344
345   cfg = GNUNET_CONFIGURATION_create ();
346   if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
347                                                "test_arm_api_data.conf"))
348     return GNUNET_SYSERR;
349   if (NULL == getcwd (pwd, PATH_MAX))
350     return GNUNET_SYSERR;
351   GNUNET_assert (0 < GNUNET_asprintf (&binary,
352                                       "%s/%s",
353                                       pwd,
354                                       BINARY));
355   GNUNET_CONFIGURATION_set_value_string (cfg,
356                                          SERVICE,
357                                          "BINARY",
358                                          binary);
359   GNUNET_free (binary);
360   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg,
361                                                CFGFILENAME))
362   {
363     GNUNET_CONFIGURATION_destroy (cfg);
364     return GNUNET_SYSERR;
365   }
366   GNUNET_CONFIGURATION_destroy (cfg);
367
368 #if LOG_BACKOFF
369   killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
370   if (NULL == (killLogFilePtr = FOPEN (killLogFileName,
371                                        "w")))
372     {
373       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
374                                 "fopen",
375                                 killLogFileName);
376       GNUNET_free (killLogFileName);
377       return GNUNET_SYSERR;
378     }
379 #endif
380   return GNUNET_OK;
381 }
382
383
384 static void
385 houseKeep ()
386 {
387 #if LOG_BACKOFF
388   GNUNET_assert (0 == fclose (killLogFilePtr));
389   GNUNET_free (killLogFileName);
390 #endif
391   (void) unlink (CFGFILENAME);
392 }
393
394
395 int
396 main (int argc, char *argv[])
397 {
398   int ret;
399
400   GNUNET_log_setup ("test-exponential-backoff",
401                     "WARNING",
402                     NULL);
403
404   if (GNUNET_OK != init ())
405     return 1;
406   ret = check ();
407   houseKeep ();
408   return ret;
409 }
410
411 /* end of test_exponential_backoff.c */