style improvments wrt Mantis 1614 patch
[oweals/gnunet.git] / src / testing / test_testing_topology.c
index f357862c5927b0b5850f117f0472617138175212..7ca58faf1af7297b99088e8850b7ff4472c7d948 100644 (file)
@@ -26,7 +26,7 @@
 #include "gnunet_core_service.h"
 #include "gnunet_os_lib.h"
 
-#define VERBOSE GNUNET_YES
+#define VERBOSE GNUNET_NO
 
 #define DELAY_FOR_LOGGING GNUNET_NO
 
@@ -40,7 +40,7 @@
  */
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 500)
 
-#define SECONDS_PER_PEER_START 45
+#define SECONDS_PER_PEER_START 120
 
 #define DEFAULT_NUM_PEERS 4
 
@@ -179,12 +179,14 @@ static void gather_log_data ()
 {
   char *peer_number;
   char *connect_number;
-  pid_t mem_process;
+  struct GNUNET_OS_Process *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);
+  GNUNET_OS_process_wait (mem_process);
+  GNUNET_OS_process_close (mem_process);
+  mem_process = NULL;
 }
 
 #endif
@@ -200,8 +202,6 @@ finish_testing ()
               "Called finish testing, stopping daemons.\n");
 #endif
 
-  int count;
-  count = 0;
   pos = test_messages;
   while (pos != NULL)
     {
@@ -270,12 +270,39 @@ 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)
+static void stats_finished (void *cls, int result)
+{
+  GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
+}
+
+/**
+ * Callback function to process statistic values.
+ *
+ * @param cls closure
+ * @param peer the peer the statistics belong to
+ * @param subsystem name of subsystem that created the statistic
+ * @param name the name of the datum
+ * @param value the current value
+ * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
+ * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
+ */
+static int stats_print  (void *cls,
+                         const struct GNUNET_PeerIdentity *peer,
+                         const char *subsystem,
+                         const char *name,
+                         uint64_t value,
+                         int is_persistent)
+{
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s:%s -- %llu\n", GNUNET_i2s(peer), subsystem, name, value);
+  return GNUNET_OK;
+}
+
+static 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;
@@ -295,7 +322,8 @@ void topology_cb (void *cls,
       {
         fprintf(outfile, "}\n");
         fclose(outfile);
-        GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
+        GNUNET_TESTING_get_statistics(pg, &stats_finished, &stats_print, NULL);
+        //GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
       }
     }
 }
@@ -348,6 +376,7 @@ process_mtype (void *cls,
       GNUNET_SCHEDULER_cancel (sched, die_task);
       GNUNET_asprintf(&dotOutFileNameFinished, "%s.dot", "final_topology");
       dotOutFileFinished = fopen (dotOutFileNameFinished, "w");
+      GNUNET_free(dotOutFileNameFinished);
       if (dotOutFileFinished != NULL)
       {
         fprintf(dotOutFileFinished, "strict graph G {\n");
@@ -512,7 +541,7 @@ send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
 {
   struct TestMessageContext *pos = cls;
 
-  if ((pos == test_messages) && (settle_time.value > 0))
+  if ((pos == test_messages) && (settle_time.rel_value > 0))
     {
       topology_connections = 0;
       GNUNET_TESTING_get_topology (pg, &topology_cb, NULL);
@@ -643,7 +672,7 @@ topology_callback (void *cls,
                                     &send_test_messages, test_messages);
       gather_log_data();
 #else
-      if (settle_time.value > 0)
+      if (settle_time.rel_value > 0)
         {
           GNUNET_TESTING_get_topology (pg, &topology_cb, NULL);
         }
@@ -679,13 +708,22 @@ topology_callback (void *cls,
     }
 }
 
+static void topology_creation_finished (void *cls,
+                                        const char *emsg)
+{
+#if VERBOSE
+  if (emsg == NULL)
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All topology connections created successfully!\n");
+#endif
+}
+
 static void
 connect_topology ()
 {
   expected_connections = -1;
   if ((pg != NULL) && (peers_left == 0))
     {
-      expected_connections = GNUNET_TESTING_connect_topology (pg, connection_topology, connect_topology_option, connect_topology_option_modifier);
+      expected_connections = GNUNET_TESTING_connect_topology (pg, connection_topology, connect_topology_option, connect_topology_option_modifier, &topology_creation_finished, NULL);
 #if VERBOSE > 1
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Have %d expected connections\n", expected_connections);
@@ -965,13 +1003,14 @@ run (void *cls,
   modnum = num_peers / 4;
   dotnum = (num_peers / 50) + 1;
 #if VERBOSE
-  fprintf (stdout, "Hostkey generation progress: \[");
+  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),
                                            &end_badly, "didn't generate all hostkeys within a reasonable amount of time!!!");
 
+  GNUNET_assert(num_peers > 0 && num_peers < (unsigned int)-1);
   pg = GNUNET_TESTING_daemons_start (sched, cfg,
                                      peers_left, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, SECONDS_PER_PEER_START * num_peers), &hostkey_callback, NULL, &peers_started_callback, NULL,
                                      &topology_callback, NULL, NULL);
@@ -1017,6 +1056,7 @@ main (int argc, char *argv[])
   char *our_binary_name;
 
   binary_start_pos = rindex(argv[0], '/');
+  GNUNET_assert(binary_start_pos != NULL);
   topology_string = strstr (binary_start_pos,
                            "_topology");
   GNUNET_assert (topology_string != NULL);