-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / transport / test_transport_api_monitoring.c
index dc2a8b0974e84beab75749afd5e31a4030dbc866..221d588adf05e8723f6e49fc472848c2fd6d8d2c 100644 (file)
@@ -77,8 +77,16 @@ static char *cfg_file_p1;
 static char *cfg_file_p2;
 
 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
+
 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
 
+static int p1_c = GNUNET_NO;
+
+static int p2_c = GNUNET_NO;
+
+static int p1_c_notify = GNUNET_NO;
+
+static int p2_c_notify = GNUNET_NO;
 
 static void
 end ()
@@ -90,12 +98,22 @@ end ()
     GNUNET_SCHEDULER_cancel (send_task);
 
   if (die_task != GNUNET_SCHEDULER_NO_TASK)
+  {
     GNUNET_SCHEDULER_cancel (die_task);
+    die_task = GNUNET_SCHEDULER_NO_TASK;
+  }
 
   if (th != NULL)
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
   th = NULL;
 
+  if (NULL != p1)
+    GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
+  p1 = NULL;
+  if (NULL != p2)
+    GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
+  p2 = NULL;
+
   if (NULL != pmc_p1)
   {
     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
@@ -107,10 +125,9 @@ end ()
     pmc_p2 = NULL;
   }
 
-  GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
-  p1 = NULL;
-  GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
-  p2 = NULL;
+
+
+  ok = 0;
 }
 
 static void
@@ -153,6 +170,17 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   th = NULL;
 
+  if (NULL != pmc_p1)
+  {
+    GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
+    pmc_p1 = NULL;
+  }
+  if (NULL != pmc_p2)
+  {
+    GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
+    pmc_p2 = NULL;
+  }
+
   if (p1 != NULL)
     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
   else
@@ -242,12 +270,24 @@ sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
   GNUNET_free (receiver_s);
   s_sending = GNUNET_YES;
-  th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
+  th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
                                                TIMEOUT_TRANSMIT, &notify_ready,
                                                p1);
 }
 
 
+static void
+done ()
+{
+  if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
+    ok = 0;
+    end();
+  }
+}
+
+
 static void
 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
@@ -258,9 +298,15 @@ notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
   struct PeerContext *t = NULL;
 
   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
+  {
+    p1_c_notify = GNUNET_YES;
     t = p1;
+  }
   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
+  {
+    p2_c_notify = GNUNET_YES;
     t = p2;
+  }
   GNUNET_assert (t != NULL);
 
   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
@@ -268,6 +314,8 @@ notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
               t->no, GNUNET_i2s (peer));
+  if (p1_c_notify && p2_c_notify)
+    GNUNET_SCHEDULER_add_now(&done, NULL);
   GNUNET_free (ps);
 }
 
@@ -330,14 +378,7 @@ start_cb (struct PeerContext *p, void *cls)
                                                NULL);
 
 }
-static int p1_c = GNUNET_NO;
-static int p2_c = GNUNET_NO;
 
-static void done ()
-{
-  if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c))
-   end();
-}
 
 static void monitor1_cb (void *cls,
                         const struct GNUNET_PeerIdentity *peer,
@@ -348,8 +389,8 @@ static void monitor1_cb (void *cls,
   if ((NULL == peer) || (NULL == p1))
     return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitor 1: %s %s %s\n",
-      GNUNET_i2s (peer), GNUNET_TRANSPORT_p2s(state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Monitor 1: %s %s %s\n",
+      GNUNET_i2s (peer), GNUNET_TRANSPORT_ps2s(state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
   if ((0 == memcmp (peer, &p2->id, sizeof (p2->id)) &&
       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
       GNUNET_NO == p1_c) )
@@ -370,8 +411,8 @@ static void monitor2_cb (void *cls,
   if ((NULL == peer) || (NULL == p2))
     return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitor 2: %s %s %s\n",
-      GNUNET_i2s (peer), GNUNET_TRANSPORT_p2s (state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Monitor 2: %s %s %s\n",
+      GNUNET_i2s (peer), GNUNET_TRANSPORT_ps2s (state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
   if ((0 == memcmp (peer, &p1->id, sizeof (p1->id)) &&
       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
       GNUNET_NO == p2_c) )
@@ -398,13 +439,14 @@ run (void *cls, char *const *args, const char *cfgfile,
                                             &notify_disconnect, &start_cb,
                                             NULL);
   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
 
   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
                                             &notify_receive, &notify_connect,
                                             &notify_disconnect, &start_cb,
                                             NULL);
   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
   if ((p1 == NULL) || (p2 == NULL))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
@@ -442,6 +484,8 @@ main (int argc, char *argv[])
 {
   int ret;
 
+  ok = 1;
+
   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
@@ -466,7 +510,10 @@ main (int argc, char *argv[])
 
   GNUNET_TRANSPORT_TESTING_done (tth);
 
-  return ret;
+  if (0 != ret)
+    return ret;
+  else
+    return ok;
 }
 
 /* end of test_transport_api.c */