Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / t / core / store_test.c
index d0ef5a910cbc7e5c15c32fb93ff58d8b85ee5bce..29c5b1b67d7a8eee65639078e65fc7800f6a0bcf 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 #include "core/store.h"
+#include "core/store-private.h"
 #include "libsysdb_test.h"
 
 #include <check.h>
@@ -277,44 +278,72 @@ START_TEST(test_store_tojson)
        } golden_data[] = {
                { 0, "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": ["
-                                               "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"k1\", \"value\": \"v1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k2\", \"value\": \"v2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k3\", \"value\": \"v3\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "], "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": [], "
                                        "\"services\": ["
-                                               "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"s1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"s2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": ["
-                                               "{\"name\": \"k1\", \"value\": \"v1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k2\", \"value\": \"v2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"k3\", \"value\": \"v3\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"k1\", \"value\": \"v1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k2\", \"value\": \"v2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"k3\", \"value\": \"v3\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "]},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"attributes\": []}"
                        "]}" },
                { SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
                                "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"services\": []},"
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", "
                                        "\"services\": ["
-                                               "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                                               "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                                               "{\"name\": \"s1\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"},"
+                                               "{\"name\": \"s2\", "
+                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                       "\"update_interval\": \"0s\"}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES | SDB_SKIP_ATTRIBUTES,
                        "{\"hosts\":["
-                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\"},"
-                               "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\"}"
+                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\"},"
+                               "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\"}"
                        "]}" },
        };
 
@@ -338,6 +367,62 @@ START_TEST(test_store_tojson)
 }
 END_TEST
 
+START_TEST(test_interval)
+{
+       sdb_store_base_t *host;
+
+       /* 10 us interval */
+       sdb_store_host("host", 10);
+       sdb_store_host("host", 20);
+       sdb_store_host("host", 30);
+       sdb_store_host("host", 40);
+
+       host = sdb_store_get_host("host");
+       fail_unless(host != NULL,
+                       "INTERNAL ERROR: store doesn't have host after adding it");
+
+       fail_unless(host->interval == 10,
+                       "sdb_store_host() did not calculate interval correctly: "
+                       "got: %"PRIscTIME"; expected: %"PRIscTIME, host->interval, 10);
+
+       /* multiple updates for the same timestamp don't modify the interval */
+       sdb_store_host("host", 40);
+       sdb_store_host("host", 40);
+       sdb_store_host("host", 40);
+       sdb_store_host("host", 40);
+
+       fail_unless(host->interval == 10,
+                       "sdb_store_host() changed interval when doing multiple updates "
+                       "using the same timestamp; got: %"PRIscTIME"; "
+                       "expected: %"PRIscTIME, host->interval, 10);
+
+       /* multiple updates using an timestamp don't modify the interval */
+       sdb_store_host("host", 20);
+       sdb_store_host("host", 20);
+       sdb_store_host("host", 20);
+       sdb_store_host("host", 20);
+
+       fail_unless(host->interval == 10,
+                       "sdb_store_host() changed interval when doing multiple updates "
+                       "using an old timestamp; got: %"PRIscTIME"; expected: %"PRIscTIME,
+                       host->interval, 10);
+
+       /* new interval: 20 us */
+       sdb_store_host("host", 60);
+       fail_unless(host->interval == 11,
+                       "sdb_store_host() did not calculate interval correctly: "
+                       "got: %"PRIscTIME"; expected: %"PRIscTIME, host->interval, 11);
+
+       /* new interval: 40 us */
+       sdb_store_host("host", 100);
+       fail_unless(host->interval == 13,
+                       "sdb_store_host() did not calculate interval correctly: "
+                       "got: %"PRIscTIME"; expected: %"PRIscTIME, host->interval, 11);
+
+       sdb_object_deref(SDB_OBJ(host));
+}
+END_TEST
+
 static int
 iter_incr(sdb_store_base_t *obj, void *user_data)
 {
@@ -412,6 +497,7 @@ core_store_suite(void)
        tcase_add_test(tc, test_store_get_host);
        tcase_add_test(tc, test_store_attr);
        tcase_add_test(tc, test_store_service);
+       tcase_add_test(tc, test_interval);
        tcase_add_test(tc, test_iterate);
        tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear);
        suite_add_tcase(s, tc);