test_run = require('test_run').new()
test_run:cmd("push filter 'Invalid VYLOG file: Slice [0-9]+ deleted but not registered'" .. \
             "to 'Invalid VYLOG file: Slice <NUM> deleted but not registered'")

-- restart the current server to avoid of issues from previous runs
test_run:cmd("restart server default with cleanup=True")

-- Let's test number of upserts in one transaction that exceeds
-- the limit of operations allowed in one update.
--
ups_cnt = 10000

s = box.schema.create_space('test', {engine = 'vinyl'})
pk = s:create_index('pk')

tuple = {}
for i = 1, ups_cnt do tuple[i] = i end
_ = s:insert(tuple)
box.snapshot()

box.begin()
for k = 1, ups_cnt do s:upsert({1}, {{'+', k, 1}}) end
box.commit()
-- Upserts are not able to squash, so scheduler will get stuck.
-- So let's not waste much time here, just check that no crash
-- takes place.
--
box.snapshot()
require('fiber').sleep(0.01)

s:drop()

--
-- WARNING: do not split from previous subtest.
-- gh-5141: vinyl: after test with number of upserts in one transaction
-- that exceeded the limit of operations allowed in one update, then
-- box.snapshot() call may fail with error:
--   "Invalid VYLOG file: Slice <NUM> deleted but not registered".
-- To avoid of the issue tarantool server restart is needed.
--

-- let's check the issue reproduced
s0 = box.schema.space.create('tweedledum', {engine = 'vinyl'})
i0 = s0:create_index('primary', { type = 'tree', parts = {1, 'unsigned'}})
cnt = 1
s0:replace{cnt, 'tuple'}
test_run:wait_cond(function()							\
    cnt = cnt + 1								\
    s0:replace{cnt, 'tuple ' .. cnt}						\
    local ok, err = pcall(box.snapshot)                  			\
    if ok == false then                                  			\
        require('log').info(                            			\
            "box.snapshot() returned on loop " .. cnt .. " 'err': " .. err) 	\
    end                                                  			\
    return err:match "Invalid VYLOG file: Slice %d+ deleted but not registered"	\
end, 10)
s0:drop()

-- let's check the issue is stable
s1 = box.schema.space.create('tweedledum', {engine = 'vinyl'})
i1 = s1:create_index('primary', { type = 'tree', parts = {1, 'unsigned'}})
box.snapshot()
s1:drop()

-- restart the current server to resolve the issue
test_run:cmd("restart server default with cleanup=True")

-- let's check the issue resolved
s2 = box.schema.space.create('tweedledum', {engine = 'vinyl'})
i2 = s2:create_index('primary', { type = 'tree', parts = {1, 'unsigned'}})
box.snapshot()
s2:drop()
