From 5ebd74089016be48cdc4703dd25a501f730d0b6b Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Thu, 9 Sep 2021 22:13:19 +0300 Subject: [PATCH] test: add tests for expirationd.stats() Closes #77 --- test/unit/expirationd_stats_test.lua | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/unit/expirationd_stats_test.lua diff --git a/test/unit/expirationd_stats_test.lua b/test/unit/expirationd_stats_test.lua new file mode 100644 index 0000000..dad85ca --- /dev/null +++ b/test/unit/expirationd_stats_test.lua @@ -0,0 +1,84 @@ +local expirationd = require("expirationd") +local fiber = require("fiber") +local t = require("luatest") +local g = t.group("stats") + +local helpers = require("test.helper") + +g.before_all(function() + helpers.init_spaces(g) +end) + +g.after_each(function() + helpers.truncate_spaces(g) +end) + +function g.test_stats_basic() + local task = expirationd.start("stats_basic", g.tree.id, helpers.is_expired_true) + local stats = expirationd.stats("stats_basic") + t.assert_equals(stats, { + checked_count = 0, + expired_count = 0, + restarts = 1, + working_time = 0, + }) + task:kill() +end + +function g.test_stats_expired_count() + helpers.iteration_result = {} + g.hash:insert({1, "a"}) + g.hash:insert({2, "b"}) + g.hash:insert({3, "c"}) + + local task = expirationd.start("stats_expired_count", g.hash.id, helpers.is_expired_debug) + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, { + {3, "c"}, + {2, "b"}, + {1, "a"} + }) + end) + local stats = expirationd.stats("stats_expired_count") + t.assert_equals(stats, { + checked_count = 3, + expired_count = 3, + restarts = 1, + working_time = 0, + }) + task:kill() +end + +function g.test_stats_restarts() + local task = expirationd.start("stats_restarts", g.tree.id, helpers.is_expired_true) + task:restart() + task:restart() + local stats = expirationd.stats("stats_restarts") + t.assert_equals(stats, { + checked_count = 0, + expired_count = 0, + restarts = 3, + working_time = 0, + }) + task:kill() +end + +function g.test_stats_working_time() + local task = expirationd.start("stats_working_time", g.tree.id, helpers.is_expired_true) + local running_time = 1 + local threshold = 0.3 + + local start_time = fiber.clock() + fiber.sleep(running_time) + local duration = fiber.clock() - start_time + + local stats = expirationd.stats("stats_working_time") + t.assert_almost_equals(stats.working_time, duration, threshold) + stats.working_time = nil + t.assert_equals(stats, { + checked_count = 0, + expired_count = 0, + restarts = 1, + }) + task:kill() +end