tests: centralize check for slow tests
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 12 Sep 2018 07:45:17 +0000 (09:45 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 13 Sep 2018 10:07:27 +0000 (12:07 +0200)
src/journal/test-compress-benchmark.c
src/shared/tests.c
src/shared/tests.h
src/test/test-hashmap-plain.c
src/test/test-watchdog.c

index 411df3f..e3034da 100644 (file)
@@ -8,6 +8,7 @@
 #include "process-util.h"
 #include "random-util.h"
 #include "string-util.h"
+#include "tests.h"
 #include "util.h"
 
 typedef int (compress_t)(const void *src, uint64_t src_size, void *dst,
@@ -143,23 +144,19 @@ static void test_compress_decompress(const char* label, const char* type,
 int main(int argc, char *argv[]) {
 #if HAVE_XZ || HAVE_LZ4
         const char *i;
-        int r;
 
         log_set_max_level(LOG_INFO);
+        log_parse_environment();
+        log_open();
 
         if (argc >= 2) {
                 unsigned x;
 
                 assert_se(safe_atou(argv[1], &x) >= 0);
                 arg_duration = x * USEC_PER_SEC;
-        } else {
-                bool slow;
-
-                r = getenv_bool("SYSTEMD_SLOW_TESTS");
-                slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT;
-
-                arg_duration = slow ? 2 * USEC_PER_SEC : USEC_PER_SEC / 50;
-        }
+        } else
+                arg_duration = slow_tests_enabled() ?
+                        2 * USEC_PER_SEC : USEC_PER_SEC / 50;
 
         if (argc == 3)
                 (void) safe_atozu(argv[2], &arg_start);
index 94f4629..cb5b7b6 100644 (file)
@@ -7,6 +7,7 @@
 #include <util.h>
 
 #include "alloc-util.h"
+#include "env-util.h"
 #include "fileio.h"
 #include "path-util.h"
 #include "strv.h"
@@ -76,3 +77,15 @@ const char* get_catalog_dir(void) {
         }
         return env;
 }
+
+bool slow_tests_enabled(void) {
+        int r;
+
+        r = getenv_bool("SYSTEMD_SLOW_TESTS");
+        if (r >= 0)
+                return r;
+
+        if (r != -ENXIO)
+                log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
+        return SYSTEMD_SLOW_TESTS_DEFAULT;
+}
index 0d5e6a8..44b52f5 100644 (file)
@@ -4,3 +4,4 @@
 char* setup_fake_runtime_dir(void);
 const char* get_testdata_dir(void);
 const char* get_catalog_dir(void);
+bool slow_tests_enabled(void);
index f80febc..82837da 100644 (file)
@@ -1,15 +1,13 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "alloc-util.h"
-#include "env-util.h"
 #include "hashmap.h"
 #include "log.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tests.h"
 #include "util.h"
 
-static bool arg_slow = false;
-
 void test_hashmap_funcs(void);
 
 static void test_hashmap_replace(void) {
@@ -739,15 +737,16 @@ static void test_hashmap_many(void) {
         Hashmap *h;
         unsigned i, j;
         void *v, *k;
+        bool slow = slow_tests_enabled();
         const struct {
                 const struct hash_ops *ops;
                 unsigned n_entries;
         } tests[] = {
-                { .ops = NULL,                  .n_entries = arg_slow ? 1 << 20 : 240 },
-                { .ops = &crippled_hashmap_ops, .n_entries = arg_slow ? 1 << 14 : 140 },
+                { .ops = NULL,                  .n_entries = slow ? 1 << 20 : 240 },
+                { .ops = &crippled_hashmap_ops, .n_entries = slow ? 1 << 14 : 140 },
         };
 
-        log_info("%s (%s)", __func__, arg_slow ? "slow" : "fast");
+        log_info("%s (%s)", __func__, slow ? "slow" : "fast");
 
         for (j = 0; j < ELEMENTSOF(tests); j++) {
                 assert_se(h = hashmap_new(tests[j].ops));
@@ -886,14 +885,9 @@ static void test_hashmap_reserve(void) {
 }
 
 void test_hashmap_funcs(void) {
-        int r;
-
         log_parse_environment();
         log_open();
 
-        r = getenv_bool("SYSTEMD_SLOW_TESTS");
-        arg_slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT;
-
         test_hashmap_copy();
         test_hashmap_get_strv();
         test_hashmap_move_one();
index 2aba3b5..d595ae2 100644 (file)
@@ -3,8 +3,8 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "env-util.h"
 #include "log.h"
+#include "tests.h"
 #include "watchdog.h"
 
 int main(int argc, char *argv[]) {
@@ -15,9 +15,9 @@ int main(int argc, char *argv[]) {
 
         log_set_max_level(LOG_DEBUG);
         log_parse_environment();
+        log_open();
 
-        r = getenv_bool("SYSTEMD_SLOW_TESTS");
-        slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT;
+        slow = slow_tests_enabled();
 
         t = slow ? 10 * USEC_PER_SEC : 1 * USEC_PER_SEC;
         count = slow ? 5 : 3;