2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
local call = n.call
|
|
|
|
local clear = n.clear
|
|
|
|
local command = n.command
|
|
|
|
local eval = n.eval
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local feed = n.feed
|
|
|
|
local feed_command = n.feed_command
|
|
|
|
local next_msg = n.next_msg
|
|
|
|
local api = n.api
|
|
|
|
local source = n.source
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
2019-07-01 13:43:50 -07:00
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-02-12 06:50:39 -07:00
|
|
|
local channel = api.nvim_get_chan_info(0).id
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('channel', channel)
|
2019-07-01 13:43:50 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('wait()', function()
|
|
|
|
it('waits and returns 0 when condition is satisfied', function()
|
|
|
|
source([[
|
|
|
|
let g:_awake = 0
|
|
|
|
call timer_start(100, { -> nvim_command('let g:_awake = 1') })
|
|
|
|
]])
|
|
|
|
eq(0, eval('g:_awake'))
|
|
|
|
eq(0, eval('wait(1500, { -> g:_awake })'))
|
|
|
|
eq(1, eval('g:_awake'))
|
|
|
|
|
|
|
|
eq(0, eval('wait(0, 1)'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('returns -1 on timeout', function()
|
|
|
|
eq(-1, eval('wait(0, 0)'))
|
|
|
|
eq(-1, eval('wait(50, 0)'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('returns -2 when interrupted', function()
|
|
|
|
feed_command(
|
|
|
|
'call rpcnotify(g:channel, "ready") | ' .. 'call rpcnotify(g:channel, "wait", wait(-1, 0))'
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2019-07-01 13:43:50 -07:00
|
|
|
eq({ 'notification', 'ready', {} }, next_msg())
|
|
|
|
feed('<c-c>')
|
|
|
|
eq({ 'notification', 'wait', { -2 } }, next_msg())
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('returns -3 on error', function()
|
|
|
|
command('silent! let ret = wait(-1, "error")')
|
|
|
|
eq(-3, eval('ret'))
|
|
|
|
command('let ret = 0 | silent! let ret = wait(-1, { -> error })')
|
|
|
|
eq(-3, eval('ret'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('evaluates the condition on given interval', function()
|
|
|
|
source([[
|
|
|
|
function Count()
|
|
|
|
let g:counter += 1
|
|
|
|
return g:counter
|
|
|
|
endfunction
|
|
|
|
]])
|
|
|
|
|
2019-10-06 13:47:40 -07:00
|
|
|
-- XXX: flaky (#11137)
|
2024-04-08 02:03:20 -07:00
|
|
|
t.retry(nil, nil, function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('counter', 0)
|
2019-10-06 13:47:40 -07:00
|
|
|
eq(-1, call('wait', 20, 'Count() >= 5', 99999))
|
|
|
|
end)
|
2019-07-01 13:43:50 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('counter', 0)
|
2019-09-11 12:56:46 -07:00
|
|
|
eq(0, call('wait', 10000, 'Count() >= 5', 5))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(5, api.nvim_get_var('counter'))
|
2019-07-01 13:43:50 -07:00
|
|
|
end)
|
|
|
|
|
2019-09-06 17:17:37 -07:00
|
|
|
it('validates args', function()
|
|
|
|
eq('Vim:E475: Invalid value for argument 1', pcall_err(call, 'wait', '', 1))
|
|
|
|
eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, -1))
|
|
|
|
eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, 0))
|
|
|
|
eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, ''))
|
2019-07-01 13:43:50 -07:00
|
|
|
end)
|
|
|
|
end)
|