Explorar el Código

Make env_init() and env_done() idempotent; add test wallclock.

Stefano Cossu hace 3 años
padre
commit
96b6307727
Se han modificado 5 ficheros con 42 adiciones y 1 borrados
  1. 4 0
      include/core.h
  2. 2 0
      src/core.c
  3. 5 0
      src/environment.c
  4. 11 1
      test.c
  5. 20 0
      test/test_graph.c

+ 4 - 0
include/core.h

@@ -159,6 +159,10 @@ extern char *warning_msg[], *error_msg[];
 
 extern char *LSUP_root_path;
 
+/** @brief Whether the environment is already initialized.
+ */
+extern bool LSUP_env_is_init;
+
 
 typedef enum {
     LSUP_BOOL_UNION,

+ 2 - 0
src/core.c

@@ -4,6 +4,8 @@
 #include "lmdb.h"
 
 
+bool LSUP_env_is_init = false;
+
 /** @brief Warning messages.
  *
  * The message corresponding to the rc is found by

+ 5 - 0
src/environment.c

@@ -47,6 +47,8 @@ LSUP_init (void)
 {
     LSUP_rc rc = LSUP_NOACTION;
 
+    if (LSUP_env_is_init) return rc;
+
     if (LSUP_default_env == NULL) {
 #ifdef DEBUG
         // In debug mode, always use max logging.
@@ -110,6 +112,7 @@ LSUP_init (void)
         atexit (LSUP_done);
 
         rc = LSUP_OK;
+        LSUP_env_is_init = true;
     }
 
     return rc;
@@ -142,4 +145,6 @@ LSUP_done (void)
     LSUP_env_free (LSUP_default_env);
     regfree (LSUP_uri_ptn);
     free (LSUP_uri_ptn);
+
+    LSUP_env_is_init = false;
 }

+ 11 - 1
test.c

@@ -1,3 +1,5 @@
+#include <time.h>
+
 #include "test_term.c"
 #include "test_namespace.c"
 #include "test_codec_nt.c"
@@ -14,6 +16,11 @@ int main(int argc, char **argv) {
     // Clear out database from previous test.
     rm_r (TEST_STORE_PATH);
 
+    clock_t start, end;
+    double wallclock;
+
+    start = clock();
+
     int rc = LSUP_init();
     if (rc != LSUP_OK) return rc;
 
@@ -36,8 +43,11 @@ int main(int argc, char **argv) {
         rc = 0;
     }
 
+    end = clock();
+    wallclock = (end - start) * 1000 / CLOCKS_PER_SEC;
+
     log_info ("");
-    log_info ("Tests run: %d", tests_run);
+    log_info ("Run %d tests in %lu ms.", tests_run, (size_t) wallclock);
 
     return rc;
 }

+ 20 - 0
test/test_graph.c

@@ -180,6 +180,26 @@ _graph_remove (LSUP_store_type type)
 }
 
 
+static int
+test_environment()
+{
+    // The env should already be initialized and re-initializing it is idempotent.
+    ASSERT (LSUP_init() > 0, "Environment not initialized!");
+    EXPECT_INT_EQ (LSUP_env_is_init, true);
+
+    // Tearing down is idempotent too.
+    LSUP_done();
+    EXPECT_INT_EQ (LSUP_env_is_init, false);
+    LSUP_done();
+    EXPECT_INT_EQ (LSUP_env_is_init, false);
+
+    ASSERT (LSUP_init() > 0, "Environment not initialized!");
+    EXPECT_INT_EQ (LSUP_env_is_init, true);
+    ASSERT (LSUP_init() > 0, "Environment not initialized!");
+    EXPECT_INT_EQ (LSUP_env_is_init, true);
+}
+
+
 static int test_graph_new() {
     if (_graph_new (LSUP_STORE_MEM) != 0) return -1;
     if (_graph_new (LSUP_STORE_MDB_TMP) != 0) return -1;