2016-03-15 23:10:59 +01:00
|
|
|
" Test for timers
|
|
|
|
|
2019-06-15 17:58:09 +02:00
|
|
|
source check.vim
|
|
|
|
CheckFeature timers
|
2016-03-15 23:10:59 +01:00
|
|
|
|
2017-09-30 14:26:58 +02:00
|
|
|
source shared.vim
|
2019-06-23 00:50:15 +02:00
|
|
|
source term_util.vim
|
2017-09-04 22:56:01 +02:00
|
|
|
|
2016-03-15 23:10:59 +01:00
|
|
|
func MyHandler(timer)
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val += 1
|
2016-03-15 23:10:59 +01:00
|
|
|
endfunc
|
|
|
|
|
2016-05-31 21:13:04 +02:00
|
|
|
func MyHandlerWithLists(lists, timer)
|
|
|
|
let x = string(a:lists)
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_oneshot()
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val = 0
|
2016-03-15 23:10:59 +01:00
|
|
|
let timer = timer_start(50, 'MyHandler')
|
2016-08-07 18:22:53 +02:00
|
|
|
let slept = WaitFor('g:val == 1')
|
|
|
|
call assert_equal(1, g:val)
|
2019-12-18 20:10:23 +01:00
|
|
|
if has('mac')
|
2019-12-25 15:47:14 +01:00
|
|
|
" Mac on Travis can be very slow.
|
|
|
|
let limit = 180
|
2019-12-18 20:10:23 +01:00
|
|
|
else
|
|
|
|
let limit = 100
|
|
|
|
endif
|
2016-08-22 21:40:29 +02:00
|
|
|
if has('reltime')
|
2019-12-18 20:10:23 +01:00
|
|
|
call assert_inrange(49, limit, slept)
|
2016-08-22 21:40:29 +02:00
|
|
|
else
|
2019-12-18 20:10:23 +01:00
|
|
|
call assert_inrange(20, limit, slept)
|
2016-08-22 21:40:29 +02:00
|
|
|
endif
|
2016-03-15 23:10:59 +01:00
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_repeat_three()
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val = 0
|
2016-03-15 23:10:59 +01:00
|
|
|
let timer = timer_start(50, 'MyHandler', {'repeat': 3})
|
2016-08-07 18:22:53 +02:00
|
|
|
let slept = WaitFor('g:val == 3')
|
|
|
|
call assert_equal(3, g:val)
|
2016-08-22 21:40:29 +02:00
|
|
|
if has('reltime')
|
2019-12-18 20:10:23 +01:00
|
|
|
if has('mac')
|
|
|
|
" Mac on Travis can be slow.
|
|
|
|
call assert_inrange(149, 400, slept)
|
|
|
|
else
|
|
|
|
call assert_inrange(149, 250, slept)
|
|
|
|
endif
|
2016-08-22 21:40:29 +02:00
|
|
|
else
|
|
|
|
call assert_inrange(80, 200, slept)
|
|
|
|
endif
|
2016-03-15 23:10:59 +01:00
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_repeat_many()
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val = 0
|
2016-03-15 23:10:59 +01:00
|
|
|
let timer = timer_start(50, 'MyHandler', {'repeat': -1})
|
|
|
|
sleep 200m
|
|
|
|
call timer_stop(timer)
|
2019-12-18 20:10:23 +01:00
|
|
|
" Mac on Travis can be slow.
|
|
|
|
if has('mac')
|
|
|
|
call assert_inrange(1, 5, g:val)
|
|
|
|
else
|
|
|
|
call assert_inrange(2, 5, g:val)
|
|
|
|
endif
|
2016-03-15 23:10:59 +01:00
|
|
|
endfunc
|
2016-03-26 18:20:41 +01:00
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_with_partial_callback()
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val = 0
|
2016-09-10 14:27:30 +02:00
|
|
|
let meow = {'one': 1}
|
|
|
|
function meow.bite(...)
|
|
|
|
let g:val += self.one
|
2016-03-26 18:20:41 +01:00
|
|
|
endfunction
|
|
|
|
|
2016-09-10 14:27:30 +02:00
|
|
|
call timer_start(50, meow.bite)
|
2016-08-07 18:22:53 +02:00
|
|
|
let slept = WaitFor('g:val == 1')
|
|
|
|
call assert_equal(1, g:val)
|
2016-08-22 21:40:29 +02:00
|
|
|
if has('reltime')
|
2016-08-28 16:06:05 +02:00
|
|
|
call assert_inrange(49, 130, slept)
|
2016-08-22 21:40:29 +02:00
|
|
|
else
|
|
|
|
call assert_inrange(20, 100, slept)
|
|
|
|
endif
|
2016-03-26 18:20:41 +01:00
|
|
|
endfunc
|
2016-05-31 21:13:04 +02:00
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_retain_partial()
|
2016-08-07 18:22:53 +02:00
|
|
|
call timer_start(50, function('MyHandlerWithLists', [['a']]))
|
2016-05-31 21:13:04 +02:00
|
|
|
call test_garbagecollect_now()
|
2016-08-07 18:22:53 +02:00
|
|
|
sleep 100m
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_info()
|
2016-08-07 18:22:53 +02:00
|
|
|
let id = timer_start(1000, 'MyHandler')
|
2019-09-08 21:51:41 +02:00
|
|
|
let info = id->timer_info()
|
2016-08-07 18:22:53 +02:00
|
|
|
call assert_equal(id, info[0]['id'])
|
|
|
|
call assert_equal(1000, info[0]['time'])
|
|
|
|
call assert_true(info[0]['remaining'] > 500)
|
|
|
|
call assert_true(info[0]['remaining'] <= 1000)
|
|
|
|
call assert_equal(1, info[0]['repeat'])
|
|
|
|
call assert_equal("function('MyHandler')", string(info[0]['callback']))
|
|
|
|
|
|
|
|
let found = 0
|
|
|
|
for info in timer_info()
|
|
|
|
if info['id'] == id
|
|
|
|
let found += 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
call assert_equal(1, found)
|
|
|
|
|
|
|
|
call timer_stop(id)
|
|
|
|
call assert_equal([], timer_info(id))
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_stopall()
|
2016-08-07 18:22:53 +02:00
|
|
|
let id1 = timer_start(1000, 'MyHandler')
|
|
|
|
let id2 = timer_start(2000, 'MyHandler')
|
|
|
|
let info = timer_info()
|
|
|
|
call assert_equal(2, len(info))
|
|
|
|
|
|
|
|
call timer_stopall()
|
|
|
|
let info = timer_info()
|
|
|
|
call assert_equal(0, len(info))
|
2016-05-31 21:13:04 +02:00
|
|
|
endfunc
|
2016-08-07 18:22:53 +02:00
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_paused()
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val = 0
|
|
|
|
|
|
|
|
let id = timer_start(50, 'MyHandler')
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(0, info[0]['paused'])
|
|
|
|
|
2019-09-08 21:51:41 +02:00
|
|
|
eval id->timer_pause(1)
|
2016-08-07 18:22:53 +02:00
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(1, info[0]['paused'])
|
|
|
|
sleep 100m
|
|
|
|
call assert_equal(0, g:val)
|
|
|
|
|
|
|
|
call timer_pause(id, 0)
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(0, info[0]['paused'])
|
|
|
|
|
|
|
|
let slept = WaitFor('g:val == 1')
|
|
|
|
call assert_equal(1, g:val)
|
2016-08-22 21:40:29 +02:00
|
|
|
if has('reltime')
|
2017-12-23 18:41:35 +01:00
|
|
|
if has('mac')
|
|
|
|
" The travis Mac machines appear to be very busy.
|
2019-12-18 20:10:23 +01:00
|
|
|
call assert_inrange(0, 90, slept)
|
2017-12-23 18:41:35 +01:00
|
|
|
else
|
|
|
|
call assert_inrange(0, 30, slept)
|
|
|
|
endif
|
2016-08-22 21:40:29 +02:00
|
|
|
else
|
|
|
|
call assert_inrange(0, 10, slept)
|
|
|
|
endif
|
2016-08-07 18:22:53 +02:00
|
|
|
endfunc
|
|
|
|
|
2016-09-01 21:26:20 +02:00
|
|
|
func StopMyself(timer)
|
|
|
|
let g:called += 1
|
|
|
|
if g:called == 2
|
|
|
|
call timer_stop(a:timer)
|
|
|
|
endif
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_delete_myself()
|
2016-09-01 21:26:20 +02:00
|
|
|
let g:called = 0
|
|
|
|
let t = timer_start(10, 'StopMyself', {'repeat': -1})
|
2018-04-30 14:28:24 +02:00
|
|
|
call WaitForAssert({-> assert_equal(2, g:called)})
|
2016-09-01 21:26:20 +02:00
|
|
|
call assert_equal(2, g:called)
|
|
|
|
call assert_equal([], timer_info(t))
|
|
|
|
endfunc
|
|
|
|
|
2016-09-05 22:45:28 +02:00
|
|
|
func StopTimer1(timer)
|
2019-09-08 21:51:41 +02:00
|
|
|
let g:timer2 = 10->timer_start('StopTimer2')
|
2016-09-05 22:45:28 +02:00
|
|
|
" avoid maxfuncdepth error
|
|
|
|
call timer_pause(g:timer1, 1)
|
2019-08-16 22:29:18 +02:00
|
|
|
sleep 20m
|
2016-09-05 22:45:28 +02:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func StopTimer2(timer)
|
|
|
|
call timer_stop(g:timer1)
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_stop_in_callback()
|
2019-08-16 21:22:41 +02:00
|
|
|
call assert_equal(0, len(timer_info()))
|
2016-09-05 22:45:28 +02:00
|
|
|
let g:timer1 = timer_start(10, 'StopTimer1')
|
2019-08-17 13:18:16 +02:00
|
|
|
let slept = 0
|
|
|
|
for i in range(10)
|
|
|
|
if len(timer_info()) == 0
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
sleep 10m
|
|
|
|
let slept += 10
|
|
|
|
endfor
|
|
|
|
" This should take only 30 msec, but on Mac it's often longer
|
|
|
|
call assert_inrange(0, 50, slept)
|
2016-09-05 22:45:28 +02:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func StopTimerAll(timer)
|
|
|
|
call timer_stopall()
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_stop_all_in_callback()
|
2019-08-16 21:22:41 +02:00
|
|
|
call assert_equal(0, len(timer_info()))
|
|
|
|
call timer_start(10, 'StopTimerAll')
|
|
|
|
call assert_equal(1, len(timer_info()))
|
|
|
|
let slept = 0
|
|
|
|
for i in range(10)
|
|
|
|
if len(timer_info()) == 0
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
sleep 10m
|
|
|
|
let slept += 10
|
|
|
|
endfor
|
|
|
|
call assert_inrange(0, 30, slept)
|
2016-09-05 22:45:28 +02:00
|
|
|
endfunc
|
|
|
|
|
2017-06-24 16:03:06 +02:00
|
|
|
func FeedkeysCb(timer)
|
|
|
|
call feedkeys("hello\<CR>", 'nt')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func InputCb(timer)
|
|
|
|
call timer_start(10, 'FeedkeysCb')
|
|
|
|
let g:val = input('?')
|
|
|
|
call Resume()
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_input_in_timer()
|
2017-06-24 16:03:06 +02:00
|
|
|
let g:val = ''
|
|
|
|
call timer_start(10, 'InputCb')
|
|
|
|
call Standby(1000)
|
|
|
|
call assert_equal('hello', g:val)
|
|
|
|
endfunc
|
2016-09-05 22:45:28 +02:00
|
|
|
|
2017-07-08 22:37:34 +02:00
|
|
|
func FuncWithError(timer)
|
|
|
|
let g:call_count += 1
|
|
|
|
if g:call_count == 4
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
doesnotexist
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_timer_errors()
|
|
|
|
let g:call_count = 0
|
|
|
|
let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
|
|
|
|
" Timer will be stopped after failing 3 out of 3 times.
|
2018-04-30 14:28:24 +02:00
|
|
|
call WaitForAssert({-> assert_equal(3, g:call_count)})
|
2017-07-08 22:37:34 +02:00
|
|
|
sleep 50m
|
|
|
|
call assert_equal(3, g:call_count)
|
|
|
|
endfunc
|
|
|
|
|
2017-09-06 23:40:10 +02:00
|
|
|
func FuncWithCaughtError(timer)
|
|
|
|
let g:call_count += 1
|
|
|
|
try
|
|
|
|
doesnotexist
|
|
|
|
catch
|
|
|
|
" nop
|
|
|
|
endtry
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_timer_catch_error()
|
|
|
|
let g:call_count = 0
|
|
|
|
let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
|
|
|
|
" Timer will not be stopped.
|
2018-04-30 14:28:24 +02:00
|
|
|
call WaitForAssert({-> assert_equal(4, g:call_count)})
|
2017-09-06 23:40:10 +02:00
|
|
|
sleep 50m
|
|
|
|
call assert_equal(4, g:call_count)
|
|
|
|
endfunc
|
|
|
|
|
2017-09-03 15:48:12 +02:00
|
|
|
func FeedAndPeek(timer)
|
|
|
|
call test_feedinput('a')
|
|
|
|
call getchar(1)
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Interrupt(timer)
|
2019-09-08 18:58:44 +02:00
|
|
|
eval "\<C-C>"->test_feedinput()
|
2017-09-03 15:48:12 +02:00
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_peek_and_get_char()
|
2019-08-07 23:07:07 +02:00
|
|
|
CheckUnix
|
|
|
|
CheckGui
|
|
|
|
|
2017-09-03 15:48:12 +02:00
|
|
|
call timer_start(0, 'FeedAndPeek')
|
|
|
|
let intr = timer_start(100, 'Interrupt')
|
|
|
|
let c = getchar()
|
|
|
|
call assert_equal(char2nr('a'), c)
|
2019-09-08 21:51:41 +02:00
|
|
|
eval intr->timer_stop()
|
2017-09-03 15:48:12 +02:00
|
|
|
endfunc
|
2017-07-08 22:37:34 +02:00
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_getchar_zero()
|
2019-01-30 22:01:40 +01:00
|
|
|
if has('win32') && !has('gui_running')
|
2019-08-07 23:07:07 +02:00
|
|
|
throw 'Skipped: cannot get low-level input'
|
2019-01-28 23:20:04 +01:00
|
|
|
endif
|
|
|
|
|
2019-01-29 20:36:56 +01:00
|
|
|
" Measure the elapsed time to avoid a hang when it fails.
|
|
|
|
let start = reltime()
|
2019-01-30 22:01:40 +01:00
|
|
|
let id = timer_start(20, {-> feedkeys('x', 'L')})
|
2019-01-28 22:32:58 +01:00
|
|
|
let c = 0
|
2019-01-29 20:36:56 +01:00
|
|
|
while c == 0 && reltimefloat(reltime(start)) < 0.2
|
2019-01-28 22:32:58 +01:00
|
|
|
let c = getchar(0)
|
|
|
|
sleep 10m
|
|
|
|
endwhile
|
|
|
|
call assert_equal('x', nr2char(c))
|
2019-01-28 23:20:04 +01:00
|
|
|
call timer_stop(id)
|
2019-01-28 22:32:58 +01:00
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_ex_mode()
|
2017-09-14 22:55:37 +02:00
|
|
|
" Function with an empty line.
|
|
|
|
func Foo(...)
|
|
|
|
|
|
|
|
endfunc
|
2019-01-28 23:20:04 +01:00
|
|
|
let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
|
2017-09-14 22:55:37 +02:00
|
|
|
" This used to throw error E749.
|
|
|
|
exe "normal Qsleep 100m\rvi\r"
|
|
|
|
call timer_stop(timer)
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_restore_count()
|
2018-05-12 15:38:26 +02:00
|
|
|
if !CanRunVimInTerminal()
|
2019-06-06 16:12:12 +02:00
|
|
|
throw 'Skipped: cannot run Vim in a terminal window'
|
2018-05-12 15:38:26 +02:00
|
|
|
endif
|
|
|
|
" Check that v:count is saved and restored, not changed by a timer.
|
|
|
|
call writefile([
|
|
|
|
\ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
|
|
|
|
\ 'func Doit(id)',
|
|
|
|
\ ' normal 3j',
|
|
|
|
\ 'endfunc',
|
|
|
|
\ 'call timer_start(100, "Doit")',
|
|
|
|
\ ], 'Xtrcscript')
|
|
|
|
call writefile([
|
|
|
|
\ '1-1234',
|
|
|
|
\ '2-1234',
|
|
|
|
\ '3-1234',
|
|
|
|
\ ], 'Xtrctext')
|
|
|
|
let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
|
|
|
|
|
|
|
|
" Wait for the timer to move the cursor to the third line.
|
|
|
|
call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
|
|
|
|
call assert_equal(1, term_getcursor(buf)[1])
|
|
|
|
" Now check that v:count has not been set to 3
|
|
|
|
call term_sendkeys(buf, 'L')
|
|
|
|
call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
|
|
|
|
|
|
|
|
call StopVimInTerminal(buf)
|
|
|
|
call delete('Xtrcscript')
|
|
|
|
call delete('Xtrctext')
|
|
|
|
endfunc
|
|
|
|
|
2019-06-22 01:40:42 +02:00
|
|
|
" Test that the garbage collector isn't triggered if a timer callback invokes
|
|
|
|
" vgetc().
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_nocatch_garbage_collect()
|
2019-06-22 01:40:42 +02:00
|
|
|
" 'uptimetime. must be bigger than the timer timeout
|
|
|
|
set ut=200
|
|
|
|
call test_garbagecollect_soon()
|
|
|
|
call test_override('no_wait_return', 0)
|
|
|
|
func CauseAnError(id)
|
|
|
|
" This will show an error and wait for Enter.
|
|
|
|
let a = {'foo', 'bar'}
|
|
|
|
endfunc
|
|
|
|
func FeedChar(id)
|
|
|
|
call feedkeys('x', 't')
|
|
|
|
endfunc
|
|
|
|
call timer_start(300, 'FeedChar')
|
|
|
|
call timer_start(100, 'CauseAnError')
|
|
|
|
let x = getchar()
|
|
|
|
|
|
|
|
set ut&
|
|
|
|
call test_override('no_wait_return', 1)
|
|
|
|
delfunc CauseAnError
|
|
|
|
delfunc FeedChar
|
|
|
|
endfunc
|
|
|
|
|
2019-08-16 11:26:06 +02:00
|
|
|
func Test_timer_error_in_timer_callback()
|
2019-07-04 14:20:41 +02:00
|
|
|
if !has('terminal') || (has('win32') && has('gui_running'))
|
2019-06-25 06:28:02 +02:00
|
|
|
throw 'Skipped: cannot run Vim in a terminal window'
|
|
|
|
endif
|
|
|
|
|
|
|
|
let lines =<< trim [CODE]
|
|
|
|
func Func(timer)
|
|
|
|
" fail to create list
|
|
|
|
let x = [
|
|
|
|
endfunc
|
|
|
|
set updatetime=50
|
|
|
|
call timer_start(1, 'Func')
|
|
|
|
[CODE]
|
|
|
|
call writefile(lines, 'Xtest.vim')
|
|
|
|
|
2019-07-04 14:20:41 +02:00
|
|
|
let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
|
2019-06-25 06:28:02 +02:00
|
|
|
let job = term_getjob(buf)
|
|
|
|
call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
|
|
|
|
|
|
|
|
" GC must not run during timer callback, which can make Vim crash.
|
|
|
|
call term_wait(buf, 100)
|
|
|
|
call term_sendkeys(buf, "\<CR>")
|
|
|
|
call term_wait(buf, 100)
|
|
|
|
call assert_equal('run', job_status(job))
|
|
|
|
|
|
|
|
call term_sendkeys(buf, ":qall!\<CR>")
|
|
|
|
call WaitFor({-> job_status(job) ==# 'dead'})
|
|
|
|
if has('unix')
|
|
|
|
call assert_equal('', job_info(job).termsig)
|
|
|
|
endif
|
|
|
|
|
|
|
|
call delete('Xtest.vim')
|
|
|
|
exe buf .. 'bwipe!'
|
|
|
|
endfunc
|
|
|
|
|
2016-08-18 23:04:48 +02:00
|
|
|
" vim: shiftwidth=2 sts=2 expandtab
|