wait_spec.lua (2508B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local call = n.call 5 local clear = n.clear 6 local command = n.command 7 local eval = n.eval 8 local eq = t.eq 9 local feed = n.feed 10 local feed_command = n.feed_command 11 local next_msg = n.next_msg 12 local api = n.api 13 local source = n.source 14 local pcall_err = t.pcall_err 15 16 before_each(function() 17 clear() 18 local channel = api.nvim_get_chan_info(0).id 19 api.nvim_set_var('channel', channel) 20 end) 21 22 describe('wait()', function() 23 it('waits and returns 0 when condition is satisfied', function() 24 source([[ 25 let g:_awake = 0 26 call timer_start(100, { -> nvim_command('let g:_awake = 1') }) 27 ]]) 28 eq(0, eval('g:_awake')) 29 eq(0, eval('wait(1500, { -> g:_awake })')) 30 eq(1, eval('g:_awake')) 31 32 eq(0, eval('wait(0, 1)')) 33 end) 34 35 it('returns -1 on timeout', function() 36 eq(-1, eval('wait(0, 0)')) 37 eq(-1, eval('wait(50, 0)')) 38 end) 39 40 it('returns -2 when interrupted', function() 41 feed_command( 42 'call rpcnotify(g:channel, "ready") | ' .. 'call rpcnotify(g:channel, "wait", wait(-1, 0))' 43 ) 44 eq({ 'notification', 'ready', {} }, next_msg()) 45 feed('<c-c>') 46 eq({ 'notification', 'wait', { -2 } }, next_msg()) 47 end) 48 49 it('returns -3 on error', function() 50 command('silent! let ret = wait(-1, "error")') 51 eq(-3, eval('ret')) 52 command('let ret = 0 | silent! let ret = wait(-1, { -> error })') 53 eq(-3, eval('ret')) 54 end) 55 56 it('evaluates the condition on given interval', function() 57 source([[ 58 function Count() 59 let g:counter += 1 60 return g:counter 61 endfunction 62 ]]) 63 64 -- XXX: flaky (#11137) 65 t.retry(nil, nil, function() 66 api.nvim_set_var('counter', 0) 67 eq(-1, call('wait', 20, 'Count() >= 5', 99999)) 68 end) 69 70 api.nvim_set_var('counter', 0) 71 eq(0, call('wait', 10000, 'Count() >= 5', 5)) 72 eq(5, api.nvim_get_var('counter')) 73 end) 74 75 it('validates args', function() 76 eq('Vim:E475: Invalid value for argument 1', pcall_err(call, 'wait', '', 1)) 77 eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, -1)) 78 eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, 0)) 79 eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, '')) 80 end) 81 82 it('does not leak when Nvim exits while waiting', function() 83 n.expect_exit( 84 500, 85 source, 86 [[ 87 call timer_start(10, {-> execute('qall!')}) 88 call wait(10000, 0) 89 ]] 90 ) 91 end) 92 end)