From ef83248c00e4d924b57c686e591be7559c1fc118 Mon Sep 17 00:00:00 2001 From: artyom dubinin Date: Fri, 11 Jun 2021 13:29:22 +0300 Subject: [PATCH] test: testing features from #50 --- test/unit/atomic_iteration_test.lua | 110 +++++++++++++++++++ test/unit/iterating_by_custom_index_test.lua | 81 ++++++++++++++ test/unit/iterator_type_test.lua | 29 +++++ test/unit/process_while_test.lua | 47 ++++++++ test/unit/start_key_test.lua | 29 +++++ 5 files changed, 296 insertions(+) create mode 100644 test/unit/atomic_iteration_test.lua create mode 100644 test/unit/iterating_by_custom_index_test.lua create mode 100644 test/unit/iterator_type_test.lua create mode 100644 test/unit/process_while_test.lua create mode 100644 test/unit/start_key_test.lua diff --git a/test/unit/atomic_iteration_test.lua b/test/unit/atomic_iteration_test.lua new file mode 100644 index 00000000..ab3bd6c2 --- /dev/null +++ b/test/unit/atomic_iteration_test.lua @@ -0,0 +1,110 @@ +local fio = require('fio') +local expirationd = require('expirationd') +local t = require('luatest') +local g = t.group("transaction") +local xlog = require('xlog').pairs + +local helpers = require('luatest.helpers') + +local function read_xlog(file) + local val = {} + for k, v in xlog(file) do + table.insert(val, setmetatable(v, { __serialize = "map"})) + end + return val +end + +g.before_all(function() + helpers.init_spaces(g) +end) + +g.after_each(function() + helpers.truncate_spaces(g) +end) + +function g.test_atomic_iteration() + helpers.iteration_result = {} + + local transactions = {} + function f(iterator) + local transaction = {} + -- old / new_tuple is not passed for vinyl + for request_number, old_tuple, new_tuple, space_id in iterator() do + require'log'.info(old_tuple:totable()) + table.insert(transaction, old_tuple:totable()) + end + table.insert(transactions, transaction) + end + + local true_box_begin = box.begin + + -- mock box.begin + box.begin = function () + true_box_begin() + box.on_commit(f) + end + + for _, space in pairs({g.tree, g.hash}) do + -- tuples expired in one atomic_iteration + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + + + local task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {atomic_iteration = true}) + -- wait for tuples expired + helpers.retrying({}, function() + if space.index[0].type == "HASH" then + t.assert_equals(helpers.iteration_result, {{3, '1'}, {2, '2'}, {1, '3'}}) + else + t.assert_equals(helpers.iteration_result, {{1, '3'}, {2, '2'}, {3, '1'}}) + end + end) + task:kill() + helpers.iteration_result = {} + + -- check out three row transaction + if space.index[0].type == "HASH" then + t.assert_equals(transactions, { + { {3, '1'}, {2, '2'}, {1, '3'} } + }) + else + t.assert_equals(transactions, { + { {1, '3'}, {2, '2'}, {3, '1'} } + }) + end + transactions = {} + + -- tuples expired in two atomic_iteration + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + + task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {atomic_iteration = true, tuples_per_iteration = 2}) + -- wait for tuples expired + -- 2 seconds because suspend will be yield in task + helpers.retrying({}, function() + if space.index[0].type == "HASH" then + t.assert_equals(helpers.iteration_result, {{3, '1'}, {2, '2'}, {1, '3'}}) + else + t.assert_equals(helpers.iteration_result, {{1, '3'}, {2, '2'}, {3, '1'}}) + end + end) + task:kill() + helpers.iteration_result = {} + + if space.index[0].type == "HASH" then + t.assert_equals(transactions, { + { {3, '1'}, {2, '2'} }, -- check two row transaction + { {1, '3'} } -- check single row transactions + }) + else + t.assert_equals(transactions, { + { {1, '3'}, {2, '2'} }, -- check two row transaction + { {3, '1'} } -- check single row transactions + }) + end + + transactions = {} + end +end diff --git a/test/unit/iterating_by_custom_index_test.lua b/test/unit/iterating_by_custom_index_test.lua new file mode 100644 index 00000000..7db7d382 --- /dev/null +++ b/test/unit/iterating_by_custom_index_test.lua @@ -0,0 +1,81 @@ +local expirationd = require('expirationd') +local t = require('luatest') +local g = t.group("iterating_by_custom_index") + +local helpers = require('luatest.helpers') + +g.before_all(function() + helpers.init_spaces(g) +end) + +g.after_each(function() + helpers.truncate_spaces(g) +end) + +function g.test_tree_index() + for _, space in pairs({g.tree, g.vinyl}) do + helpers.iteration_result = {} + + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + + local task = expirationd.start("clean_all", space.id, helpers.is_expired_debug) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{1, '3'}, {2, '2'}, {3, '1'}}) + end) + task:kill() + helpers.iteration_result = {} + + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + + task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {index = 'index_for_first_name'}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{3, '1'}, {2, '2'}, {1, '3'}}) + end) + task:kill() + helpers.iteration_result = {} + + space:insert({1, "1", 2, 1}) + space:insert({2, "2", 2, 2}) + space:insert({3, "3", 1, 3}) + + task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {index = 'multipart_index'}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{3, '3', 1, 3}, {1, '1', 2, 1}, {2, '2', 2, 2}}) + end) + task:kill() + end +end + +function g.test_hash_index() + helpers.iteration_result = {} + g.hash:insert({1, "a"}) + g.hash:insert({2, "b"}) + g.hash:insert({3, "c"}) + + local task = expirationd.start("clean_all", g.hash.id, helpers.is_expired_debug) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{3, 'c'}, {2, 'b'}, {1, 'a'}}) + end) + task:kill() + + helpers.iteration_result = {} + g.hash:insert({1, "a"}) + g.hash:insert({2, "b"}) + g.hash:insert({3, "c"}) + + task = expirationd.start("clean_all", g.hash.id, helpers.is_expired_debug, {index = 'index_for_first_name'}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{1, 'a'}, {3, 'c'}, {2, 'b'}}) + end) + helpers.iteration_result = {} + task:kill() +end diff --git a/test/unit/iterator_type_test.lua b/test/unit/iterator_type_test.lua new file mode 100644 index 00000000..9be482dc --- /dev/null +++ b/test/unit/iterator_type_test.lua @@ -0,0 +1,29 @@ +local expirationd = require('expirationd') +local t = require('luatest') +local g = t.group("iterator_type") + +local helpers = require('luatest.helpers') + +g.before_all(function() + helpers.init_spaces(g) +end) + +g.after_each(function() + helpers.truncate_spaces(g) +end) + +function g.test_tree_index() + for _, space in pairs({g.tree, g.vinyl}) do + helpers.iteration_result = {} + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + + local task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {iterator_type = "LE"}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{3, '1'}, {2, '2'}, {1, '3'}}) + end) + task:kill() + end +end diff --git a/test/unit/process_while_test.lua b/test/unit/process_while_test.lua new file mode 100644 index 00000000..b09060f7 --- /dev/null +++ b/test/unit/process_while_test.lua @@ -0,0 +1,47 @@ +local expirationd = require('expirationd') +local t = require('luatest') +local g = t.group("process_while") + +local helpers = require('luatest.helpers') + +g.before_all(function() + helpers.init_spaces(g) +end) + +g.after_each(function() + helpers.truncate_spaces(g) +end) + +function g.test_stopper() + helpers.iteration_result = {} + local function process_while(task) + if task.checked_tuples_count >= 1 then return false end + return true + end + + -- for tree index spaces + for _, space in pairs({g.tree, g.vinyl}) do + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + local task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {process_while = process_while}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{1, "3"}}) + end) + task:kill() + helpers.iteration_result = {} + end + + -- for hash index space + g.hash:insert({1, "3"}) + g.hash:insert({2, "2"}) + g.hash:insert({3, "1"}) + + local task = expirationd.start("clean_all", g.hash.id, helpers.is_expired_debug, {process_while = process_while}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{3, "1"}}) + end) + task:kill() +end diff --git a/test/unit/start_key_test.lua b/test/unit/start_key_test.lua new file mode 100644 index 00000000..70083e30 --- /dev/null +++ b/test/unit/start_key_test.lua @@ -0,0 +1,29 @@ +local expirationd = require('expirationd') +local t = require('luatest') +local g = t.group("start_key") + +local helpers = require('luatest.helpers') + +g.before_all(function() + helpers.init_spaces(g) +end) + +g.after_each(function() + helpers.truncate_spaces(g) +end) + +function g.test_tree_index() + for _, space in pairs({g.tree, g.vinyl}) do + helpers.iteration_result = {} + space:insert({1, "3"}) + space:insert({2, "2"}) + space:insert({3, "1"}) + + local task = expirationd.start("clean_all", space.id, helpers.is_expired_debug, {start_key = 2}) + -- wait for tuples expired + helpers.retrying({}, function() + t.assert_equals(helpers.iteration_result, {{2, '2'}, {3, '1'} }) + end) + task:kill() + end +end