test printing connected topology
[oweals/gnunet.git] / src / testing / test_testing_topology.c
index eb13c8eefd0095a805a0c4ffde48da6bd9f1ff31..a2bb350f097180febd94578b0eae61ba1084ef00 100644 (file)
 #include "platform.h"
 #include "gnunet_testing_lib.h"
 #include "gnunet_core_service.h"
+#include "gnunet_os_lib.h"
 
 #define VERBOSE GNUNET_YES
 
+#define DELAY_FOR_LOGGING GNUNET_NO
+
 /**
  * How long until we fail the whole testcase?
  */
@@ -41,7 +44,7 @@
 
 #define DEFAULT_NUM_PEERS 4
 
-#define MAX_OUTSTANDING_CONNECTIONS 300
+#define MAX_OUTSTANDING_CONNECTIONS 100
 
 static float fail_percentage = 0.05;
 
@@ -167,6 +170,20 @@ void shutdown_callback (void *cls,
     }
 }
 
+#if DELAY_FOR_LOGGING
+static void gather_log_data ()
+{
+  char *peer_number;
+  char *connect_number;
+  pid_t mem_process;
+  GNUNET_asprintf(&peer_number, "%llu", num_peers);
+  GNUNET_asprintf(&connect_number, "%llu", expected_connections);
+  mem_process = GNUNET_OS_start_process (NULL, NULL, "./memsize.pl",
+                           "memsize.pl", "totals.txt", peer_number, connect_number, NULL);
+  GNUNET_OS_process_wait(mem_process);
+}
+
+#endif
 static void
 finish_testing ()
 {
@@ -177,6 +194,7 @@ finish_testing ()
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Called finish testing, stopping daemons.\n");
 #endif
+
   int count;
   count = 0;
   pos = test_messages;
@@ -247,6 +265,37 @@ disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
   total_server_connections -= 2;
 }
 
+void topology_cb (void *cls,
+             const struct GNUNET_PeerIdentity *first,
+             const struct GNUNET_PeerIdentity *second,
+             struct GNUNET_TIME_Relative latency,
+             uint32_t distance,
+             const char *emsg)
+{
+  FILE *outfile;
+  outfile = cls;
+  if (first != NULL)
+  {
+    if (outfile == NULL)
+      fprintf(stderr, "Peer %s connected to %s\n", GNUNET_i2s(first), GNUNET_h2s(&second->hashPubKey));
+    else
+    {
+      fprintf(outfile, "\t\"%s\" -- ", GNUNET_i2s(first));
+      fprintf(outfile, "\"%s\";\n", GNUNET_i2s(second));
+    }
+  }
+  else
+    {
+      fprintf(stderr, "Finished iterating over topology!\n");
+      if (outfile != NULL)
+      {
+        fprintf(outfile, "}\n");
+        fclose(outfile);
+      }
+      GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
+    }
+}
+
 static int
 process_mtype (void *cls,
                const struct GNUNET_PeerIdentity *peer,
@@ -254,6 +303,8 @@ process_mtype (void *cls,
                struct GNUNET_TIME_Relative latency,
                uint32_t distance)
 {
+  char *dotOutFileNameFinished;
+  FILE *dotOutFileFinished;
   struct TestMessageContext *pos = cls;
   struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
   if (pos->uid != ntohl(msg->uid))
@@ -291,7 +342,14 @@ process_mtype (void *cls,
       fprintf(stdout, "100%%]\n");
 #endif
       GNUNET_SCHEDULER_cancel (sched, die_task);
-      GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
+      GNUNET_asprintf(&dotOutFileNameFinished, "%s.dot", "final_topology");
+      dotOutFileFinished = fopen (dotOutFileNameFinished, "w");
+      if (dotOutFileFinished != NULL)
+      {
+        fprintf(dotOutFileFinished, "strict graph G {\n");
+      }
+      GNUNET_TESTING_get_topology (pg, &topology_cb, dotOutFileFinished);
+      //GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
     }
   else
     {
@@ -425,14 +483,14 @@ init_notify_peer1 (void *cls,
    * Connect to the receiving peer
    */
   pos->peer2handle = GNUNET_CORE_connect (sched,
-                       pos->peer2->cfg,
-                       TIMEOUT,
-                       pos,
-                       &init_notify_peer2,
-                       NULL,
-                       NULL,
-                       NULL,
-                       GNUNET_YES, NULL, GNUNET_YES, handlers);
+                                         pos->peer2->cfg,
+                                         TIMEOUT,
+                                         pos,
+                                         &init_notify_peer2,
+                                         NULL,
+                                         NULL, 
+                                         NULL, NULL,
+                                         GNUNET_YES, NULL, GNUNET_YES, handlers);
 
 }
 
@@ -467,7 +525,7 @@ send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
                                           TIMEOUT,
                                           pos,
                                           &init_notify_peer1,
-                                          NULL,
+                                          NULL, NULL,
                                           NULL,
                                           NULL,
                                           GNUNET_NO, NULL, GNUNET_NO, no_handlers);
@@ -558,12 +616,22 @@ topology_callback (void *cls,
 #endif
       modnum = expected_messages / 4;
       dotnum = (expected_messages / 50) + 1;
-#if VERBOSE
-      fprintf(stdout, "Test message progress: [");
-#endif
       GNUNET_SCHEDULER_cancel (sched, die_task);
       die_task = GNUNET_SCHEDULER_NO_TASK;
+#if DELAY_FOR_LOGGING
+      fprintf(stdout, "Sending test messages in 10 seconds.\n");
+      GNUNET_SCHEDULER_add_delayed (sched,
+                                    GNUNET_TIME_relative_multiply
+                                    (GNUNET_TIME_UNIT_SECONDS, 10),
+                                    &send_test_messages, test_messages);
+      gather_log_data();
+#else
       GNUNET_SCHEDULER_add_now (sched, &send_test_messages, test_messages);
+#endif
+#if VERBOSE
+      fprintf(stdout, "Test message progress: [");
+#endif
+
     }
   else if (total_connections + failed_connections == expected_connections)
     {
@@ -629,8 +697,8 @@ create_topology ()
 #if VERBOSE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Topology set up, now starting peers!\n");
-#endif
       fprintf(stdout, "Daemon start progress [");
+#endif
       GNUNET_TESTING_daemons_continue_startup(pg);
     }
   else
@@ -696,10 +764,18 @@ peers_started_callback (void *cls,
        * within a reasonable amount of time */
       die_task = GNUNET_SCHEDULER_add_delayed (sched,
                                                GNUNET_TIME_relative_multiply
-                                               (GNUNET_TIME_UNIT_MINUTES, 5),
+                                               (GNUNET_TIME_UNIT_MINUTES, 8),
                                                &end_badly, "from peers_started_callback");
-
+#if DELAY_FOR_LOGGING
+      fprintf(stdout, "Connecting topology in 10 seconds\n");
+      gather_log_data();
+      GNUNET_SCHEDULER_add_delayed (sched,
+                                    GNUNET_TIME_relative_multiply
+                                    (GNUNET_TIME_UNIT_SECONDS, 10),
+                                    &connect_topology, NULL);
+#else
       connect_topology ();
+#endif
       ok = 0;
     }
 }
@@ -861,7 +937,9 @@ run (void *cls,
   peers_left = num_peers;
   modnum = num_peers / 4;
   dotnum = (num_peers / 50) + 1;
+#if VERBOSE
   fprintf (stdout, "Hostkey generation progress: \[");
+#endif
   /* Set up a task to end testing if peer start fails */
   die_task = GNUNET_SCHEDULER_add_delayed (sched,
                                            GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, SECONDS_PER_PEER_START * num_peers),