Avoid trying to disconnect a neighbour twice
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.c
index 903e6efdcd1f04bbf2f8ceb0c564d28670c64558..fac13840e8c37151a881102141caaa9abb16cedf 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 #include "gauger.h"
 #include "transport-testing.h"
 
+/**
+ * Allow making the problem "bigger".
+ */
+#define FACTOR 1
+
 /**
  * Total number of messages to send
  *
@@ -38,7 +43,7 @@
  * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
  * messages may be dropped even for a reliable transport.
  */
-#define TOTAL_MSGS (1024 * 3)
+#define TOTAL_MSGS (1024 * 3 * FACTOR)
 
 /**
  * Message type of test messages
 /**
  * Testcase timeout
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90 * FACTOR)
 
 /**
  * How long until we give up on transmitting the message?
  */
-#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
+#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60 * FACTOR)
 
 
 /**
@@ -154,7 +159,6 @@ static int test_send_timeout;
 /**
  * Bitmap storing which messages were received
  */
-
 static char bitmap[TOTAL_MSGS / 8];
 
 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
@@ -180,17 +184,24 @@ end ()
   unsigned long long delta;
   unsigned long long rate;
   char *value_name;
+  unsigned int i;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Stopping peers\n");
 
   /* Calculcate statistics   */
   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
   rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
-  FPRINTF (stderr, "\nThroughput was %llu KiBytes/s\n",
-      rate);
-
-  GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
-  GAUGER ("TRANSPORT", value_name, (int) rate,
+  FPRINTF (stderr,
+           "\nThroughput was %llu KiBytes/s\n",
+           rate);
+
+  GNUNET_asprintf (&value_name,
+                   "unreliable_%s",
+                   test_plugin);
+  GAUGER ("TRANSPORT",
+          value_name,
+          (int) rate,
           "kb/s");
   GNUNET_free (value_name);
 
@@ -198,43 +209,46 @@ end ()
     GNUNET_SCHEDULER_cancel (die_task);
 
   if (th != NULL)
+  {
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-  th = NULL;
-
+    th = NULL;
+  }
   if (cc != NULL)
+  {
     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
-  cc = NULL;
-
+    cc = NULL;
+  }
   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
-
   GNUNET_TRANSPORT_TESTING_done (tth);
-
   ok = 0;
-
-  int i;
-
   for (i = 0; i < TOTAL_MSGS; i++)
   {
     if (get_bit (bitmap, i) == 0)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not receive message %d\n", i);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Did not receive message %d\n",
+                  i);
       ok = -1;
     }
   }
 }
 
+
 static void
 end_badly ()
 {
-  int i;
-  die_task = NULL;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
+  unsigned int i;
 
+  die_task = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Fail! Stopping peers\n");
   if (test_connected == GNUNET_YES)
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "Peers got connected\n");
   else
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Peers got NOT connected\n");
 
   if (test_sending == GNUNET_NO)
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -246,19 +260,23 @@ end_badly ()
   {
     if (get_bit (bitmap, i) == 0)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not receive message %d\n", i);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Did not receive message %u\n",
+                  i);
       ok = -1;
     }
   }
 
   if (th != NULL)
+  {
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
-  th = NULL;
-
+    th = NULL;
+  }
   if (cc != NULL)
+  {
     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
-  cc = NULL;
-
+    cc = NULL;
+  }
   if (p1 != NULL)
     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
   if (p2 != NULL)
@@ -289,7 +307,7 @@ get_size (unsigned int iter)
  * Sets a bit active in the bitmap.
  *
  * @param bitIdx which bit to set
- * @return GNUNET_SYSERR on error, GNUNET_OK on success
+ * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  */
 static int
 set_bit (unsigned int bitIdx)
@@ -299,7 +317,8 @@ set_bit (unsigned int bitIdx)
 
   if (bitIdx >= sizeof (bitmap) * 8)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "tried to set bit %d of %d(!?!?)\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "tried to set bit %d of %d(!?!?)\n",
                 bitIdx, sizeof (bitmap) * 8);
     return GNUNET_SYSERR;
   }
@@ -309,6 +328,7 @@ set_bit (unsigned int bitIdx)
   return GNUNET_OK;
 }
 
+
 /**
  * Obtain a bit from bitmap.
  * @param map the bitmap
@@ -381,8 +401,9 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
   {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _("Message id %u is bigger than maxmimum number of messages %u expected\n"),
-                  ntohl (hdr->num), TOTAL_MSGS);
+                  "Message id %u is bigger than maxmimum number of messages %u expected\n",
+                  ntohl (hdr->num),
+                  TOTAL_MSGS);
   }
   test_sending = GNUNET_YES;
   if (0 == (n % (TOTAL_MSGS / 100)))
@@ -443,7 +464,9 @@ notify_ready (void *cls, size_t size, void *buf)
 #if VERBOSE
     if (n % 5000 == 0)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Sending message %u of size %u\n",
+                  n,
                   s);
     }
 #endif
@@ -463,7 +486,8 @@ notify_ready (void *cls, size_t size, void *buf)
   else
   {
     FPRINTF (stderr, "%s",  "\n");
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "All messages scheduled to be sent\n");
     if (NULL != die_task)
       GNUNET_SCHEDULER_cancel (die_task);
     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
@@ -496,11 +520,13 @@ notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
   th = NULL;
 }
 
+
 static void
 sendtask ()
 {
   start_time = GNUNET_TIME_absolute_get ();
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to send %u messages\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Starting to send %u messages\n",
               TOTAL_MSGS);
   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
                                                TIMEOUT_TRANSMIT, &notify_ready,
@@ -512,7 +538,8 @@ testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
 {
   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Peers connected: %s <-> %s\n", p1_c,
               GNUNET_i2s (&p2->id));
   GNUNET_free (p1_c);
 
@@ -522,13 +549,16 @@ testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
 }
 
+
 static void
 start_cb (struct PeerContext *p, void *cls)
 {
   static int started;
   started++;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Peer %u (`%s') started\n",
+              p->no,
               GNUNET_i2s (&p->id));
 
   if (started != 2)
@@ -540,11 +570,15 @@ start_cb (struct PeerContext *p, void *cls)
 
 }
 
+
 static void
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+     char *const *args,
+     const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
+  die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                           &end_badly, NULL);
   test_send_timeout = GNUNET_NO;
 
   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
@@ -572,7 +606,8 @@ main (int argc, char *argv[])
   char *test_source;
   int ret;
 
-  static char *const argv_new[] = { "test-transport-api-reliability",
+  static char *const argv_new[] = {
+    "test-transport-api-reliability",
     "-c",
     "test_transport_api_data.conf",
     NULL
@@ -581,14 +616,15 @@ main (int argc, char *argv[])
     GNUNET_GETOPT_OPTION_END
   };
 
-  GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
-
+  GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
+                                          &test_name);
   GNUNET_log_setup (test_name,
                     "WARNING",
                     NULL);
-
-  GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
-  GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
+  GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
+                                                 &test_source);
+  GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
+                                                 test_source,
                                                  &test_plugin);
 
   tth = GNUNET_TRANSPORT_TESTING_init ();
@@ -596,21 +632,24 @@ main (int argc, char *argv[])
   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
 
-
-
 #if WRITECONFIG
   setTransportOptions ("test_transport_api_data.conf");
 #endif
   ok = GNUNET_SYSERR;
 
-  ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1, argv_new, test_name,
-                      "nohelp", options, &run, &ok);
+  ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1,
+                            argv_new, test_name,
+                            "nohelp", options,
+                            &run, &ok);
   if (GNUNET_SYSERR == ret)
+  {
+    fprintf (stderr,
+             "Test failed: %d\n",
+             ok);
     ok = -1;
-
+  }
   GNUNET_free (cfg_file_p1);
   GNUNET_free (cfg_file_p2);
-
   GNUNET_free (test_source);
   GNUNET_free (test_plugin);
   GNUNET_free (test_name);