Skip to content

Commit

Permalink
test: update a single instance bank test
Browse files Browse the repository at this point in the history
  • Loading branch information
ligurio committed Nov 6, 2024
1 parent 3bb4c0a commit 75df367
Showing 1 changed file with 81 additions and 52 deletions.
133 changes: 81 additions & 52 deletions test/tarantool/single_instance_bank_test.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
-- https://github.com/tarantool/jepsen.tarantool/blob/master/resources/tarantool/jepsen.lua
-- TODO: fault injections,
-- https://github.com/tarantool/tarantool/blob/master/test/fuzz/lua/test_engine.lua
-- Usage:
--
-- tarantool single_instance_bank_test.lua
-- <snipped>
-- $ ls -1 tarantool_test_dir/history.*
-- tarantool_test_dir/history.json
-- tarantool_test_dir/history.txt
-- java -jar ~/sources/elle-cli/target/elle-cli-0.1.7-standalone.jar --model bank tarantool_test_dir/history.json

local fio = require('fio')
local log = require('log')
local math = require('math')
local molly = require('molly')

local runner = molly.runner
local client = molly.client
local tests = molly.tests
local log = require('log')
local fio = require('fio')
local math = require('math')

local TEST_DIR = fio.abspath('./tarantool_test_dir')

local SPACE_NAME = 'bank_space'
local ACCOUNTS = 10
local TOTAL_AMOUNT = 100
local MAX_TRANSFER = 2
local MAX_TRANSFER = 10

log.info('Tarantool version: %s', require('tarantool').version)

local function do_withdraw(table, from, to, amount)
-- true on successful transfer.
-- false on a failed transfer.
local function transfer(table, from, to, amount)
local space = box.space[table]
box.begin()
-- local b1 = space:get(from).balance - amount
-- local b2 = space:get(to).balance + amount
-- if b1 < 0 or b2 < 0 then
-- error('Negative balance')
-- end
local balance_from = space:get(from).balance
if balance_from < amount then
return false, balance_from - amount
end
space:update(from, {{'-', 'balance', amount}})
space:update(to, {{'+', 'balance', amount}})
box.commit()

return true
end

local function rmtree(path)
Expand All @@ -51,41 +60,40 @@ local function cleanup_dir(dir)
end
end

-- WARNING: Cleanup and running box.cfg() should be executed in
-- the setup method.
if fio.path.exists(TEST_DIR) then
cleanup_dir(TEST_DIR)
else
fio.mkdir(TEST_DIR)
end

local box_cfg_options = {
-- memtx_memory = 1024 * 1024,
-- memtx_use_mvcc_engine = false,
work_dir = TEST_DIR,
feedback_enabled = false,
log_level = 'verbose',
memtx_use_mvcc_engine = true,
read_only = false,
work_dir = TEST_DIR,
}

if type(box.cfg) ~= 'table' then
box.cfg(box_cfg_options)
end

local cl = client.new()
local tarantool_bank_client = client.new()

cl.open = function(self, _addr)
tarantool_bank_client.open = function(self, _addr)
rawset(self, 'conn', box)
return true
end

local function assert_ping(conn)
assert(conn)
-- if conn:ping({timeout = 2}) ~= true then
-- error(string.format('No connection to %s', addr))
-- end
end

cl.setup = function(self)
tarantool_bank_client.setup = function(self)
assert_ping(self.conn)
if self.conn.space[SPACE_NAME] then
return
end
local space = self.conn.schema.create_space(SPACE_NAME)
assert(space ~= nil)
space:format({
Expand All @@ -104,67 +112,88 @@ cl.setup = function(self)
return true
end

cl.invoke = function(self, op)
local function balances(accounts)
local bal = {}
for _, a in pairs(accounts) do
local acc_n = tostring(a[1])
local balance = a[2]
bal[acc_n] = balance
end
return bal
end

-- 8 :ok :read {0 83, 1 3, 2 7, 3 15, 4 5, 5 3, 6 10, 7 1}
-- 8 :invoke :transfer {:from 1, :to 6, :amount 5}
-- 8 :fail :transfer [:negative 1 -2]
tarantool_bank_client.invoke = function(self, op)
assert_ping(self.conn)
log.info(op)
local state = false
local v = op.v
local v = op.value
local op_type
if op.f == 'transfer' then
local from = v.from
local to = v.to
local amount = v.amount
do_withdraw(SPACE_NAME, from, to, amount)
local txn_opts = {
txn_isolation = 'read-confirmed',
timeout = 1,
}
local ok, res, neg = pcall(box.atomic, txn_opts, transfer,
SPACE_NAME, from, to, amount)
if not ok then
error(('transfer operation: %s'):format(res))
end
op_type = res and 'ok' or 'fail'
if res == false then
v = { 'negative', from, neg }
end
elseif op.f == 'read' then
local space = self.conn.space[SPACE_NAME]
v = space:select(nil, {timeout = 5, limit = ACCOUNTS})
if v ~= nil then
v = v.value
state = true
local accounts = space:select(nil, {timeout = 5, limit = ACCOUNTS})
if accounts == nil then
op_type = 'fail'
else
v = balances(accounts)
op_type = 'ok'
end
else
error(string.format('Unknown operation (%s)', op.f))
end

return {
v = v,
f = op.f,
process = op.process,
time = op.time,
state = state,
process = op.process,
value = v,
type = op_type,
}
end

cl.teardown = function(self)
tarantool_bank_client.teardown = function(self)
assert_ping(self.conn)
return true
end

cl.close = function(self)
-- if self.conn and self.conn:ping() == true then
-- self.conn:close()
-- end
tarantool_bank_client.close = function(self)
return true
end

local test_options = {
create_reports = true,
threads = 10,
threads = 1,
nodes = {
'127.0.0.1:3301',
},
}

local ok, err = runner.run_test({
client = cl,
client = tarantool_bank_client,
create_reports = true,
generator = tests.bank_gen({
accounts = ACCOUNTS,
total_amount = TOTAL_AMOUNT,
max_transfer = MAX_TRANSFER,
}):take(10^4),
}):take(100),
}, test_options)

assert(ok == true)
assert(err == nil)
if err then
log.info(err)
end

os.exit(0)
os.exit(ok and 0 or 1)

0 comments on commit 75df367

Please sign in to comment.