1" Test for timers 2 3source check.vim 4CheckFeature timers 5 6source shared.vim 7source term_util.vim 8 9func MyHandler(timer) 10 let g:val += 1 11endfunc 12 13func MyHandlerWithLists(lists, timer) 14 let x = string(a:lists) 15endfunc 16 17func Test_oneshot() 18 let g:val = 0 19 let timer = timer_start(50, 'MyHandler') 20 let slept = WaitFor('g:val == 1') 21 call assert_equal(1, g:val) 22 if has('reltime') 23 call assert_inrange(49, 100, slept) 24 else 25 call assert_inrange(20, 100, slept) 26 endif 27endfunc 28 29func Test_repeat_three() 30 let g:val = 0 31 let timer = timer_start(50, 'MyHandler', {'repeat': 3}) 32 let slept = WaitFor('g:val == 3') 33 call assert_equal(3, g:val) 34 if has('reltime') 35 call assert_inrange(149, 250, slept) 36 else 37 call assert_inrange(80, 200, slept) 38 endif 39endfunc 40 41func Test_repeat_many() 42 let g:val = 0 43 let timer = timer_start(50, 'MyHandler', {'repeat': -1}) 44 sleep 200m 45 call timer_stop(timer) 46 call assert_inrange(2, 4, g:val) 47endfunc 48 49func Test_with_partial_callback() 50 let g:val = 0 51 let meow = {'one': 1} 52 function meow.bite(...) 53 let g:val += self.one 54 endfunction 55 56 call timer_start(50, meow.bite) 57 let slept = WaitFor('g:val == 1') 58 call assert_equal(1, g:val) 59 if has('reltime') 60 call assert_inrange(49, 130, slept) 61 else 62 call assert_inrange(20, 100, slept) 63 endif 64endfunc 65 66func Test_retain_partial() 67 call timer_start(50, function('MyHandlerWithLists', [['a']])) 68 call test_garbagecollect_now() 69 sleep 100m 70endfunc 71 72func Test_info() 73 let id = timer_start(1000, 'MyHandler') 74 let info = timer_info(id) 75 call assert_equal(id, info[0]['id']) 76 call assert_equal(1000, info[0]['time']) 77 call assert_true(info[0]['remaining'] > 500) 78 call assert_true(info[0]['remaining'] <= 1000) 79 call assert_equal(1, info[0]['repeat']) 80 call assert_equal("function('MyHandler')", string(info[0]['callback'])) 81 82 let found = 0 83 for info in timer_info() 84 if info['id'] == id 85 let found += 1 86 endif 87 endfor 88 call assert_equal(1, found) 89 90 call timer_stop(id) 91 call assert_equal([], timer_info(id)) 92endfunc 93 94func Test_stopall() 95 let id1 = timer_start(1000, 'MyHandler') 96 let id2 = timer_start(2000, 'MyHandler') 97 let info = timer_info() 98 call assert_equal(2, len(info)) 99 100 call timer_stopall() 101 let info = timer_info() 102 call assert_equal(0, len(info)) 103endfunc 104 105func Test_paused() 106 let g:val = 0 107 108 let id = timer_start(50, 'MyHandler') 109 let info = timer_info(id) 110 call assert_equal(0, info[0]['paused']) 111 112 call timer_pause(id, 1) 113 let info = timer_info(id) 114 call assert_equal(1, info[0]['paused']) 115 sleep 100m 116 call assert_equal(0, g:val) 117 118 call timer_pause(id, 0) 119 let info = timer_info(id) 120 call assert_equal(0, info[0]['paused']) 121 122 let slept = WaitFor('g:val == 1') 123 call assert_equal(1, g:val) 124 if has('reltime') 125 if has('mac') 126 " The travis Mac machines appear to be very busy. 127 call assert_inrange(0, 50, slept) 128 else 129 call assert_inrange(0, 30, slept) 130 endif 131 else 132 call assert_inrange(0, 10, slept) 133 endif 134endfunc 135 136func StopMyself(timer) 137 let g:called += 1 138 if g:called == 2 139 call timer_stop(a:timer) 140 endif 141endfunc 142 143func Test_delete_myself() 144 let g:called = 0 145 let t = timer_start(10, 'StopMyself', {'repeat': -1}) 146 call WaitForAssert({-> assert_equal(2, g:called)}) 147 call assert_equal(2, g:called) 148 call assert_equal([], timer_info(t)) 149endfunc 150 151func StopTimer1(timer) 152 let g:timer2 = timer_start(10, 'StopTimer2') 153 " avoid maxfuncdepth error 154 call timer_pause(g:timer1, 1) 155 sleep 40m 156endfunc 157 158func StopTimer2(timer) 159 call timer_stop(g:timer1) 160endfunc 161 162func Test_stop_in_callback() 163 let g:timer1 = timer_start(10, 'StopTimer1') 164 sleep 40m 165endfunc 166 167func StopTimerAll(timer) 168 call timer_stopall() 169endfunc 170 171func Test_stop_all_in_callback() 172 let g:timer1 = timer_start(10, 'StopTimerAll') 173 let info = timer_info() 174 call assert_equal(1, len(info)) 175 sleep 40m 176 let info = timer_info() 177 call assert_equal(0, len(info)) 178endfunc 179 180func FeedkeysCb(timer) 181 call feedkeys("hello\<CR>", 'nt') 182endfunc 183 184func InputCb(timer) 185 call timer_start(10, 'FeedkeysCb') 186 let g:val = input('?') 187 call Resume() 188endfunc 189 190func Test_input_in_timer() 191 let g:val = '' 192 call timer_start(10, 'InputCb') 193 call Standby(1000) 194 call assert_equal('hello', g:val) 195endfunc 196 197func FuncWithError(timer) 198 let g:call_count += 1 199 if g:call_count == 4 200 return 201 endif 202 doesnotexist 203endfunc 204 205func Test_timer_errors() 206 let g:call_count = 0 207 let timer = timer_start(10, 'FuncWithError', {'repeat': -1}) 208 " Timer will be stopped after failing 3 out of 3 times. 209 call WaitForAssert({-> assert_equal(3, g:call_count)}) 210 sleep 50m 211 call assert_equal(3, g:call_count) 212endfunc 213 214func FuncWithCaughtError(timer) 215 let g:call_count += 1 216 try 217 doesnotexist 218 catch 219 " nop 220 endtry 221endfunc 222 223func Test_timer_catch_error() 224 let g:call_count = 0 225 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4}) 226 " Timer will not be stopped. 227 call WaitForAssert({-> assert_equal(4, g:call_count)}) 228 sleep 50m 229 call assert_equal(4, g:call_count) 230endfunc 231 232func FeedAndPeek(timer) 233 call test_feedinput('a') 234 call getchar(1) 235endfunc 236 237func Interrupt(timer) 238 call test_feedinput("\<C-C>") 239endfunc 240 241func Test_peek_and_get_char() 242 if !has('unix') && !has('gui_running') 243 return 244 endif 245 call timer_start(0, 'FeedAndPeek') 246 let intr = timer_start(100, 'Interrupt') 247 let c = getchar() 248 call assert_equal(char2nr('a'), c) 249 call timer_stop(intr) 250endfunc 251 252func Test_getchar_zero() 253 if has('win32') && !has('gui_running') 254 " Console: no low-level input 255 return 256 endif 257 258 " Measure the elapsed time to avoid a hang when it fails. 259 let start = reltime() 260 let id = timer_start(20, {-> feedkeys('x', 'L')}) 261 let c = 0 262 while c == 0 && reltimefloat(reltime(start)) < 0.2 263 let c = getchar(0) 264 sleep 10m 265 endwhile 266 call assert_equal('x', nr2char(c)) 267 call timer_stop(id) 268endfunc 269 270func Test_ex_mode() 271 " Function with an empty line. 272 func Foo(...) 273 274 endfunc 275 let timer = timer_start(40, function('g:Foo'), {'repeat':-1}) 276 " This used to throw error E749. 277 exe "normal Qsleep 100m\rvi\r" 278 call timer_stop(timer) 279endfunc 280 281func Test_restore_count() 282 if !CanRunVimInTerminal() 283 throw 'Skipped: cannot run Vim in a terminal window' 284 endif 285 " Check that v:count is saved and restored, not changed by a timer. 286 call writefile([ 287 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"', 288 \ 'func Doit(id)', 289 \ ' normal 3j', 290 \ 'endfunc', 291 \ 'call timer_start(100, "Doit")', 292 \ ], 'Xtrcscript') 293 call writefile([ 294 \ '1-1234', 295 \ '2-1234', 296 \ '3-1234', 297 \ ], 'Xtrctext') 298 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {}) 299 300 " Wait for the timer to move the cursor to the third line. 301 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])}) 302 call assert_equal(1, term_getcursor(buf)[1]) 303 " Now check that v:count has not been set to 3 304 call term_sendkeys(buf, 'L') 305 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])}) 306 307 call StopVimInTerminal(buf) 308 call delete('Xtrcscript') 309 call delete('Xtrctext') 310endfunc 311 312" Test that the garbage collector isn't triggered if a timer callback invokes 313" vgetc(). 314func Test_nocatch_garbage_collect() 315 " 'uptimetime. must be bigger than the timer timeout 316 set ut=200 317 call test_garbagecollect_soon() 318 call test_override('no_wait_return', 0) 319 func CauseAnError(id) 320 " This will show an error and wait for Enter. 321 let a = {'foo', 'bar'} 322 endfunc 323 func FeedChar(id) 324 call feedkeys('x', 't') 325 endfunc 326 call timer_start(300, 'FeedChar') 327 call timer_start(100, 'CauseAnError') 328 let x = getchar() 329 330 set ut& 331 call test_override('no_wait_return', 1) 332 delfunc CauseAnError 333 delfunc FeedChar 334endfunc 335 336func Test_error_in_timer_callback() 337 if !has('terminal') || (has('win32') && has('gui_running')) 338 throw 'Skipped: cannot run Vim in a terminal window' 339 endif 340 341 let lines =<< trim [CODE] 342 func Func(timer) 343 " fail to create list 344 let x = [ 345 endfunc 346 set updatetime=50 347 call timer_start(1, 'Func') 348 [CODE] 349 call writefile(lines, 'Xtest.vim') 350 351 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8}) 352 let job = term_getjob(buf) 353 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))}) 354 355 " GC must not run during timer callback, which can make Vim crash. 356 call term_wait(buf, 100) 357 call term_sendkeys(buf, "\<CR>") 358 call term_wait(buf, 100) 359 call assert_equal('run', job_status(job)) 360 361 call term_sendkeys(buf, ":qall!\<CR>") 362 call WaitFor({-> job_status(job) ==# 'dead'}) 363 if has('unix') 364 call assert_equal('', job_info(job).termsig) 365 endif 366 367 call delete('Xtest.vim') 368 exe buf .. 'bwipe!' 369endfunc 370 371" vim: shiftwidth=2 sts=2 expandtab 372