vlog: Rename the currently used term 'facility' as 'destination'.
[cascardo/ovs.git] / tests / test-reconnect.c
index 2d654ff..7d39339 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 #include <config.h>
-
+#undef NDEBUG
 #include "reconnect.h"
-
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
 #include "command-line.h"
 #include "compiler.h"
+#include "ovstest.h"
 #include "svec.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 static struct reconnect *reconnect;
 static int now;
 
-static const struct command commands[];
-
 static void diff_stats(const struct reconnect_stats *old,
-                       const struct reconnect_stats *new);
+                       const struct reconnect_stats *new,
+                       int delta);
+static const struct command *get_all_commands(void);
 
-int
-main(void)
+static void
+test_reconnect_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     extern struct vlog_module VLM_reconnect;
     struct reconnect_stats prev;
@@ -46,7 +45,7 @@ main(void)
     int old_time;
     char line[128];
 
-    vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_EMER);
+    vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_OFF);
 
     now = 1000;
     reconnect = reconnect_create(now);
@@ -68,25 +67,24 @@ main(void)
         svec_parse_words(&args, line);
         svec_terminate(&args);
         if (!svec_is_empty(&args)) {
-            run_command(args.n, args.names, commands);
+            run_command(args.n, args.names, get_all_commands());
         }
         svec_destroy(&args);
 
         if (old_time != now) {
             printf("\n### t=%d ###\n", now);
-            old_time = now;
         }
 
         reconnect_get_stats(reconnect, now, &cur);
-        diff_stats(&prev, &cur);
+        diff_stats(&prev, &cur, now - old_time);
         prev = cur;
         if (reconnect_get_max_tries(reconnect) != old_max_tries) {
             old_max_tries = reconnect_get_max_tries(reconnect);
             printf("  %u tries left\n", old_max_tries);
         }
-    }
 
-    return 0;
+        old_time = now;
+    }
 }
 
 static void
@@ -146,9 +144,9 @@ do_connected(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-do_received(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+do_activity(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
-    reconnect_received(reconnect, now);
+    reconnect_activity(reconnect, now);
 }
 
 static void
@@ -164,7 +162,7 @@ do_run(int argc, char *argv[])
     switch (action) {
     default:
         if (action != 0) {
-            NOT_REACHED();
+            OVS_NOT_REACHED();
         }
         break;
 
@@ -208,7 +206,8 @@ do_set_max_tries(int argc OVS_UNUSED, char *argv[])
 
 static void
 diff_stats(const struct reconnect_stats *old,
-           const struct reconnect_stats *new)
+           const struct reconnect_stats *new,
+           int delta)
 {
     if (old->state != new->state
         || old->state_elapsed != new->state_elapsed
@@ -217,10 +216,10 @@ diff_stats(const struct reconnect_stats *old,
                new->state, new->state_elapsed, new->backoff);
     }
     if (old->creation_time != new->creation_time
-        || old->last_received != new->last_received
+        || old->last_activity != new->last_activity
         || old->last_connected != new->last_connected) {
-        printf("  created %lld, last received %lld, last connected %lld\n",
-               new->creation_time, new->last_received, new->last_connected);
+        printf("  created %lld, last activity %lld, last connected %lld\n",
+               new->creation_time, new->last_activity, new->last_connected);
     }
     if (old->n_successful_connections != new->n_successful_connections
         || old->n_attempted_connections != new->n_attempted_connections
@@ -229,20 +228,25 @@ diff_stats(const struct reconnect_stats *old,
                new->n_successful_connections, new->n_attempted_connections,
                new->seqno);
     }
-    if (old->is_connected != new->is_connected
-        || old->current_connection_duration != new->current_connection_duration
-        || old->total_connected_duration != new->total_connected_duration) {
-        printf("  %sconnected (%u ms), total %u ms connected\n",
-               new->is_connected ? "" : "not ",
-               new->current_connection_duration,
-               new->total_connected_duration);
+    if (old->is_connected != new->is_connected) {
+        printf("  %sconnected\n", new->is_connected ? "" : "dis");
     }
-    if (old->last_disconnected != new->last_disconnected) {
-        printf("  disconnected at %llu ms (%u ms ago)\n",
-               new->last_disconnected, new->current_disconnect_duration);
+    if (old->last_connected != new->last_connected
+        || (old->msec_since_connect != new->msec_since_connect - delta
+            && !(old->msec_since_connect == UINT_MAX
+                 && new->msec_since_connect == UINT_MAX))
+        || (old->total_connected_duration != new->total_connected_duration - delta
+            && !(old->total_connected_duration == 0
+                 && new->total_connected_duration == 0))) {
+        printf("  last connected %u ms ago, connected %u ms total\n",
+               new->msec_since_connect, new->total_connected_duration);
     }
-    if (old->current_disconnect_duration != new->current_disconnect_duration) {
-        printf("  disconnected for %llu ms\n", new->current_disconnect_duration);
+    if (old->last_disconnected != new->last_disconnected
+        || (old->msec_since_disconnect != new->msec_since_disconnect - delta
+            && !(old->msec_since_disconnect == UINT_MAX
+                 && new->msec_since_disconnect == UINT_MAX))) {
+        printf("  disconnected at %llu ms (%u ms ago)\n",
+               new->last_disconnected, new->msec_since_disconnect);
     }
 }
 
@@ -264,22 +268,29 @@ do_listen_error(int argc OVS_UNUSED, char *argv[])
     reconnect_listen_error(reconnect, now, atoi(argv[1]));
 }
 
-static const struct command commands[] = {
-    { "enable", 0, 0, do_enable },
-    { "disable", 0, 0, do_disable },
-    { "force-reconnect", 0, 0, do_force_reconnect },
-    { "disconnected", 0, 1, do_disconnected },
-    { "connecting", 0, 0, do_connecting },
-    { "connect-failed", 0, 1, do_connect_failed },
-    { "connected", 0, 0, do_connected },
-    { "received", 0, 0, do_received },
-    { "run", 0, 1, do_run },
-    { "advance", 1, 1, do_advance },
-    { "timeout", 0, 0, do_timeout },
-    { "set-max-tries", 1, 1, do_set_max_tries },
-    { "passive", 0, 0, do_set_passive },
-    { "listening", 0, 0, do_listening },
-    { "listen-error", 1, 1, do_listen_error },
-    { NULL, 0, 0, NULL },
+static const struct command all_commands[] = {
+    { "enable", NULL, 0, 0, do_enable },
+    { "disable", NULL, 0, 0, do_disable },
+    { "force-reconnect", NULL, 0, 0, do_force_reconnect },
+    { "disconnected", NULL, 0, 1, do_disconnected },
+    { "connecting", NULL, 0, 0, do_connecting },
+    { "connect-failed", NULL, 0, 1, do_connect_failed },
+    { "connected", NULL, 0, 0, do_connected },
+    { "activity", NULL, 0, 0, do_activity },
+    { "run", NULL, 0, 1, do_run },
+    { "advance", NULL, 1, 1, do_advance },
+    { "timeout", NULL, 0, 0, do_timeout },
+    { "set-max-tries", NULL, 1, 1, do_set_max_tries },
+    { "passive", NULL, 0, 0, do_set_passive },
+    { "listening", NULL, 0, 0, do_listening },
+    { "listen-error", NULL, 1, 1, do_listen_error },
+    { NULL, NULL, 0, 0, NULL },
 };
 
+static const struct command *
+get_all_commands(void)
+{
+    return all_commands;
+}
+
+OVSTEST_REGISTER("test-reconnect", test_reconnect_main);