Make exponential backoff test compile again
[oweals/gnunet.git] / src / arm / test_exponential_backoff.c
index 3b68039b1c25bf5370efd8c4bb76a7320bac2c1a..e218e1aaeb285a1233f69c0aaf10ac1e36fec4d5 100644 (file)
  */
 #include "platform.h"
 #include "gnunet_arm_service.h"
-#include "gnunet_client_lib.h"
-#include "gnunet_configuration_lib.h"
-#include "gnunet_program_lib.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
 
-#define VERBOSE GNUNET_NO
+#define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
+
 #define START_ARM GNUNET_YES
+
 #define LOG_BACKOFF GNUNET_NO
+
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
+
 #define SERVICE_TEST_TIMEOUT GNUNET_TIME_UNIT_FOREVER_REL
+
 #define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
 
+
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
 static struct GNUNET_ARM_Handle *arm;
+
+static struct GNUNET_ARM_MonitorHandle *mon;
+
 static int ok = 1;
 
+static int phase = 0;
+
 static int trialCount;
+
 static struct GNUNET_TIME_Absolute startedWaitingAt;
+
 struct GNUNET_TIME_Relative waitedFor;
 
+struct GNUNET_TIME_Relative waitedFor_prev;
+
 #if LOG_BACKOFF
 static FILE *killLogFilePtr;
+
 static char *killLogFileName;
 #endif
 
 
+typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
+
 /**
  * Context for handling the shutdown of a service.
  */
@@ -88,11 +105,8 @@ struct ShutdownContext
 
 /**
  * Handler receiving response to service shutdown requests.
- * First call with NULL: service misbehaving, or something.
- * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
- *   - service will shutdown
- * Second call with NULL:
- *   - service has now really shut down.
+ * We expect it to be called with NULL, since the service that
+ * we are shutting down will just die without replying.
  *
  * @param cls closure
  * @param msg NULL, indicating socket closure.
@@ -102,56 +116,21 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
 {
   struct ShutdownContext *shutdown_ctx = cls;
 
-  if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
+  if (NULL == msg) 
   {
-    /* Means the other side closed the connection and never confirmed a shutdown */
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Service handle shutdown before ACK!\n");
-    if (shutdown_ctx->cont != NULL)
-      shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
-    GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
-    GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
-    GNUNET_free (shutdown_ctx);
-  }
-  else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
-  {
-#if VERBOSE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
-#endif
     if (shutdown_ctx->cont != NULL)
       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
-
+    
     GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
-    GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
     GNUNET_free (shutdown_ctx);
+    return;
   }
-  else
-  {
-    GNUNET_assert (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
-    switch (ntohs (msg->type))
-    {
-    case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
-#if VERBOSE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Received confirmation for service shutdown.\n");
-#endif
-      shutdown_ctx->confirmed = GNUNET_YES;
-      GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
-                             shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
-      break;
-    default:                   /* Fall through */
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Service shutdown refused!\n");
-      if (shutdown_ctx->cont != NULL)
-        shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES);
-
-      GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
-      GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
-      GNUNET_free (shutdown_ctx);
-      break;
-    }
-  }
+  GNUNET_assert (0);
 }
 
+
 /**
  * Shutting down took too long, cancel receive and return error.
  *
@@ -160,13 +139,13 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
  */
 void
 service_shutdown_cancel (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+                        const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ShutdownContext *shutdown_ctx = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
   shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
-  GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+  GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
   GNUNET_free (shutdown_ctx);
 }
 
@@ -188,24 +167,24 @@ write_shutdown (void *cls, size_t size, void *buf)
 
   if (size < sizeof (struct GNUNET_MessageHeader))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Failed to transmit shutdown request to client.\n"));
+    LOG ("Failed to send a shutdown request\n");
     shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
-    GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
     GNUNET_free (shutdown_ctx);
-    return 0;                   /* client disconnected */
+    return 0;                  /* client disconnected */
   }
 
   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
-                         shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
-  shutdown_ctx->cancel_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (shutdown_ctx->timeout),
-                                    &service_shutdown_cancel, shutdown_ctx);
+                        shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
+  shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
+      GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
+      &service_shutdown_cancel, shutdown_ctx);
   msg = (struct GNUNET_MessageHeader *) buf;
-  msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
+  msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
-  return sizeof (struct GNUNET_MessageHeader);
+  strcpy ((char *) &msg[1], "do-nothing");
+  LOG ("Sent a shutdown request\n");
+  return sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1;
 }
 
 
@@ -223,9 +202,9 @@ write_shutdown (void *cls, size_t size, void *buf)
  *
  */
 static void
-arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
-                      struct GNUNET_TIME_Relative timeout,
-                      GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
+do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
+                     struct GNUNET_TIME_Relative timeout,
+                     GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
 {
   struct ShutdownContext *shutdown_ctx;
 
@@ -235,19 +214,9 @@ arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
   shutdown_ctx->sock = sock;
   shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
   GNUNET_CLIENT_notify_transmit_ready (sock,
-                                       sizeof (struct GNUNET_MessageHeader),
-                                       timeout, GNUNET_NO, &write_shutdown,
-                                       shutdown_ctx);
-}
-
-
-static void
-arm_notify_stop (void *cls, int success)
-{
-  GNUNET_assert (success == GNUNET_NO);
-#if START_ARM
-  GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, NULL, NULL);
-#endif
+                                      sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1,
+                                      timeout, GNUNET_NO, &write_shutdown,
+                                      shutdown_ctx);
 }
 
 
@@ -256,101 +225,124 @@ kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
 static void
-do_nothing_notify (void *cls, int success)
-{
-  GNUNET_assert (success == GNUNET_YES);
-  ok = 1;
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
-}
-
-
-static void
-arm_notify (void *cls, int success)
+shutdown_cont (void *cls, int reason)
 {
-  GNUNET_assert (success == GNUNET_YES);
-  GNUNET_ARM_start_service (arm, "do-nothing", TIMEOUT, &do_nothing_notify,
-                            NULL);
+  if (GNUNET_NO != reason)
+  {
+    /* Re-try shutdown */
+    LOG ("do-nothing didn't die, trying again\n");
+    GNUNET_SCHEDULER_add_now (kill_task, NULL);
+    return;
+  }
+  startedWaitingAt = GNUNET_TIME_absolute_get ();
+  LOG ("do-nothing is dead, starting the countdown\n");
 }
 
 
 static void
-kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
-static void
-do_nothing_restarted_notify_task (void *cls,
-                                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  static char a;
-
-  trialCount++;
+  static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
 
-#if LOG_BACKOFF
-  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+  if (NULL != cbData)
   {
-    fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount);
+    waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
+    LOG ("Waited for: %s\n", 
+        GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
   }
-  else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
+  else
   {
-    fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount);
+    waitedFor.rel_value_us = 0;
   }
-  else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0)
+  /* Connect to the doNothing task */
+  doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
+  GNUNET_assert (doNothingConnection != NULL);
+  if (trialCount == 12)
+    waitedFor_prev = waitedFor;
+  else if (trialCount == 13)
   {
-    fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount);
+    GNUNET_CLIENT_disconnect (doNothingConnection);
+    GNUNET_ARM_request_service_stop (arm, "do-nothing", TIMEOUT, NULL, NULL);
+    if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
+      ok = 9;
+    else
+      ok = 0;
+    trialCount += 1;
+    return;
   }
-#endif
-  GNUNET_SCHEDULER_add_now (&kill_task, &a);
+  trialCount += 1;
+  /* Use the created connection to kill the doNothingTask */
+  do_nothing_service_shutdown (doNothingConnection,
+      TIMEOUT, &shutdown_cont, NULL);
 }
 
 
 static void
-do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
+trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_CLIENT_service_test ("do-nothing", cfg, TIMEOUT,
-                              &do_nothing_restarted_notify_task, NULL);
+  GNUNET_ARM_disconnect_and_free (arm);
+  GNUNET_ARM_monitor_disconnect_and_free (mon);
 }
 
 
 static void
-shutdown_cont (void *cls, int reason)
+arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
 {
-  trialCount++;
-  startedWaitingAt = GNUNET_TIME_absolute_get ();
-  GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL);
+  GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
+  GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
+  LOG ("ARM service stopped\n");
+  GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
 }
 
 
 static void
-kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
+srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
 {
-  static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
-
-  if (NULL != cbData)
+  LOG ("Service %s is %u, phase %u\n", service, status, phase);
+  if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
   {
-    waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
-
-#if LOG_BACKOFF
-    fprintf (killLogFilePtr, "Waited for: %llu ms\n",
-             (unsigned long long) waitedFor.rel_value);
-#endif
+    phase++;
+    GNUNET_ARM_request_service_start (arm, "do-nothing",
+        GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
+    return;
   }
-  else
+  if (phase == 1)
   {
-    waitedFor.rel_value = 0;
+    GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
+    GNUNET_break (0 == strcasecmp (service, "do-nothing"));
+    GNUNET_break (phase == 1);
+    LOG ("do-nothing is starting\n");
+    phase++;
+    ok = 1;
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
   }
-  /* Connect to the doNothing task */
-  doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
-  GNUNET_assert (doNothingConnection != NULL);
-  if (trialCount == 12)
+  else if ((phase == 2) && (strcasecmp ("do-nothing", service) == 0))
   {
-    GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO);
-    GNUNET_ARM_stop_service (arm, "do-nothing", TIMEOUT, &arm_notify_stop,
-                             NULL);
-    ok = 0;
-    return;
+    /* We passively monitor ARM for status updates. ARM should tell us
+     * when do-nothing dies (no need to run a service upness test ourselves).
+     */
+    if (status == GNUNET_ARM_SERVICE_STARTING)
+    {
+      LOG ("do-nothing is starting\n");
+      GNUNET_SCHEDULER_add_now (kill_task, &ok);
+    }
+    else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
+    {
+      phase++;
+      GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
+    }
   }
-  /* Use the created connection to kill the doNothingTask */
-  arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL);
+}
+
+
+static void
+arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
+{
+  GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
+  GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
+  GNUNET_break (phase == 0);
+  LOG ("Sent 'START' request for arm to ARM %s\n", 
+       (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
 }
 
 
@@ -358,26 +350,49 @@ static void
 task (void *cls, char *const *args, const char *cfgfile,
       const struct GNUNET_CONFIGURATION_Handle *c)
 {
+  char *armconfig;
   cfg = c;
+  if (NULL != cfgfile)
+  {
+    if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm",
+        "CONFIG", &armconfig))
+    {
+      GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
+                                              *) cfg, "arm", "CONFIG",
+                                             cfgfile);
+    }
+    else
+      GNUNET_free (armconfig);
+  }
 
-  arm = GNUNET_ARM_connect (cfg, NULL);
+  arm = GNUNET_ARM_connect (cfg, NULL, NULL);
+  if (NULL != arm)
+  {
+    mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
+    if (NULL != mon)
+    {
 #if START_ARM
-  GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify,
-                            NULL);
+      GNUNET_ARM_request_service_start (arm, "arm",
+          GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
 #else
-  arm_do_nothing (NULL, GNUNET_YES);
+      arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
 #endif
+    }
+    else
+    {
+      GNUNET_ARM_disconnect_and_free (arm);
+      arm = NULL;
+    }
+  }
 }
 
+
 static int
 check ()
 {
   char *const argv[] = {
-    "test-arm-api",
+    "test-exponential-backoff",
     "-c", "test_arm_api_data.conf",
-#if VERBOSE
-    "-L", "DEBUG",
-#endif
     NULL
   };
   struct GNUNET_GETOPT_CommandLineOption options[] = {
@@ -386,26 +401,27 @@ check ()
 
   /* Running ARM  and running the do_nothing task */
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
-                                     argv, "test-exponential-backoff", "nohelp",
-                                     options, &task, NULL));
+                GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
+                                    argv, "test-exponential-backoff",
+                                    "nohelp", options, &task, NULL));
 
 
   return ok;
 }
 
+
 static int
 init ()
 {
 #if LOG_BACKOFF
   killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
   if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
-  {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
-                              killLogFileName);
-    GNUNET_free (killLogFileName);
-    return GNUNET_SYSERR;
-  }
+    {
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
+                               killLogFileName);
+      GNUNET_free (killLogFileName);
+      return GNUNET_SYSERR;
+    }
 #endif
   return GNUNET_OK;
 }
@@ -427,12 +443,8 @@ main (int argc, char *argv[])
   int ret;
 
   GNUNET_log_setup ("test-exponential-backoff",
-#if VERBOSE
-                    "DEBUG",
-#else
-                    "WARNING",
-#endif
-                    NULL);
+                   "WARNING",
+                   NULL);
 
   init ();
   ret = check ();