2016-03-15 23:10:59 +01:00
|
|
|
" Test for timers
|
|
|
|
|
2016-08-07 18:22:53 +02:00
|
|
|
source shared.vim
|
|
|
|
|
2016-03-15 23:10:59 +01:00
|
|
|
if !has('timers')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-03-15 23:10:59 +01:00
|
|
|
func Test_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)
|
2016-08-22 21:40:29 +02:00
|
|
|
if has('reltime')
|
2016-08-28 16:06:05 +02:00
|
|
|
call assert_inrange(49, 100, slept)
|
2016-08-22 21:40:29 +02:00
|
|
|
else
|
|
|
|
call assert_inrange(20, 100, slept)
|
|
|
|
endif
|
2016-03-15 23:10:59 +01:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_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')
|
2016-08-28 16:06:05 +02:00
|
|
|
call assert_inrange(149, 250, slept)
|
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
|
|
|
|
|
|
|
|
func Test_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)
|
2016-08-07 18:22:53 +02:00
|
|
|
call assert_inrange(2, 4, g:val)
|
2016-03-15 23:10:59 +01:00
|
|
|
endfunc
|
2016-03-26 18:20:41 +01:00
|
|
|
|
|
|
|
func Test_with_partial_callback()
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val = 0
|
2016-03-26 18:20:41 +01:00
|
|
|
let s:meow = {}
|
|
|
|
function s:meow.bite(...)
|
2016-08-07 18:22:53 +02:00
|
|
|
let g:val += 1
|
2016-03-26 18:20:41 +01:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
call timer_start(50, s: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
|
|
|
|
|
|
|
func Test_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
|
|
|
|
|
|
|
|
func Test_info()
|
|
|
|
let id = timer_start(1000, 'MyHandler')
|
|
|
|
let info = timer_info(id)
|
|
|
|
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
|
|
|
|
|
|
|
|
func Test_stopall()
|
|
|
|
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
|
|
|
|
|
|
|
func Test_paused()
|
|
|
|
let g:val = 0
|
|
|
|
|
|
|
|
let id = timer_start(50, 'MyHandler')
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(0, info[0]['paused'])
|
|
|
|
|
|
|
|
call timer_pause(id, 1)
|
|
|
|
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')
|
|
|
|
call assert_inrange(0, 30, slept)
|
|
|
|
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
|
|
|
|
|
|
|
|
func Test_delete_myself()
|
|
|
|
let g:called = 0
|
|
|
|
let t = timer_start(10, 'StopMyself', {'repeat': -1})
|
|
|
|
call WaitFor('g:called == 2')
|
|
|
|
call assert_equal(2, g:called)
|
|
|
|
call assert_equal([], timer_info(t))
|
|
|
|
endfunc
|
|
|
|
|
2016-08-18 23:04:48 +02:00
|
|
|
" vim: shiftwidth=2 sts=2 expandtab
|