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_timer_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_timer_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_timer_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, 5, g:val) 47endfunc 48 49func Test_timer_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_timer_retain_partial() 67 call timer_start(50, function('MyHandlerWithLists', [['a']])) 68 call test_garbagecollect_now() 69 sleep 100m 70endfunc 71 72func Test_timer_info() 73 let id = timer_start(1000, 'MyHandler') 74 let info = id->timer_info() 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_timer_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_timer_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 eval id->timer_pause(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_timer_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 = 10->timer_start('StopTimer2') 153 " avoid maxfuncdepth error 154 call timer_pause(g:timer1, 1) 155 sleep 20m 156endfunc 157 158func StopTimer2(timer) 159 call timer_stop(g:timer1) 160endfunc 161 162func Test_timer_stop_in_callback() 163 call assert_equal(0, len(timer_info())) 164 let g:timer1 = timer_start(10, 'StopTimer1') 165 let slept = 0 166 for i in range(10) 167 if len(timer_info()) == 0 168 break 169 endif 170 sleep 10m 171 let slept += 10 172 endfor 173 " This should take only 30 msec, but on Mac it's often longer 174 call assert_inrange(0, 50, slept) 175endfunc 176 177func StopTimerAll(timer) 178 call timer_stopall() 179endfunc 180 181func Test_timer_stop_all_in_callback() 182 call assert_equal(0, len(timer_info())) 183 call timer_start(10, 'StopTimerAll') 184 call assert_equal(1, len(timer_info())) 185 let slept = 0 186 for i in range(10) 187 if len(timer_info()) == 0 188 break 189 endif 190 sleep 10m 191 let slept += 10 192 endfor 193 call assert_inrange(0, 30, slept) 194endfunc 195 196func FeedkeysCb(timer) 197 call feedkeys("hello\<CR>", 'nt') 198endfunc 199 200func InputCb(timer) 201 call timer_start(10, 'FeedkeysCb') 202 let g:val = input('?') 203 call Resume() 204endfunc 205 206func Test_timer_input_in_timer() 207 let g:val = '' 208 call timer_start(10, 'InputCb') 209 call Standby(1000) 210 call assert_equal('hello', g:val) 211endfunc 212 213func FuncWithError(timer) 214 let g:call_count += 1 215 if g:call_count == 4 216 return 217 endif 218 doesnotexist 219endfunc 220 221func Test_timer_errors() 222 let g:call_count = 0 223 let timer = timer_start(10, 'FuncWithError', {'repeat': -1}) 224 " Timer will be stopped after failing 3 out of 3 times. 225 call WaitForAssert({-> assert_equal(3, g:call_count)}) 226 sleep 50m 227 call assert_equal(3, g:call_count) 228endfunc 229 230func FuncWithCaughtError(timer) 231 let g:call_count += 1 232 try 233 doesnotexist 234 catch 235 " nop 236 endtry 237endfunc 238 239func Test_timer_catch_error() 240 let g:call_count = 0 241 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4}) 242 " Timer will not be stopped. 243 call WaitForAssert({-> assert_equal(4, g:call_count)}) 244 sleep 50m 245 call assert_equal(4, g:call_count) 246endfunc 247 248func FeedAndPeek(timer) 249 call test_feedinput('a') 250 call getchar(1) 251endfunc 252 253func Interrupt(timer) 254 eval "\<C-C>"->test_feedinput() 255endfunc 256 257func Test_timer_peek_and_get_char() 258 CheckUnix 259 CheckGui 260 261 call timer_start(0, 'FeedAndPeek') 262 let intr = timer_start(100, 'Interrupt') 263 let c = getchar() 264 call assert_equal(char2nr('a'), c) 265 eval intr->timer_stop() 266endfunc 267 268func Test_timer_getchar_zero() 269 if has('win32') && !has('gui_running') 270 throw 'Skipped: cannot get low-level input' 271 endif 272 273 " Measure the elapsed time to avoid a hang when it fails. 274 let start = reltime() 275 let id = timer_start(20, {-> feedkeys('x', 'L')}) 276 let c = 0 277 while c == 0 && reltimefloat(reltime(start)) < 0.2 278 let c = getchar(0) 279 sleep 10m 280 endwhile 281 call assert_equal('x', nr2char(c)) 282 call timer_stop(id) 283endfunc 284 285func Test_timer_ex_mode() 286 " Function with an empty line. 287 func Foo(...) 288 289 endfunc 290 let timer = timer_start(40, function('g:Foo'), {'repeat':-1}) 291 " This used to throw error E749. 292 exe "normal Qsleep 100m\rvi\r" 293 call timer_stop(timer) 294endfunc 295 296func Test_timer_restore_count() 297 if !CanRunVimInTerminal() 298 throw 'Skipped: cannot run Vim in a terminal window' 299 endif 300 " Check that v:count is saved and restored, not changed by a timer. 301 call writefile([ 302 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"', 303 \ 'func Doit(id)', 304 \ ' normal 3j', 305 \ 'endfunc', 306 \ 'call timer_start(100, "Doit")', 307 \ ], 'Xtrcscript') 308 call writefile([ 309 \ '1-1234', 310 \ '2-1234', 311 \ '3-1234', 312 \ ], 'Xtrctext') 313 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {}) 314 315 " Wait for the timer to move the cursor to the third line. 316 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])}) 317 call assert_equal(1, term_getcursor(buf)[1]) 318 " Now check that v:count has not been set to 3 319 call term_sendkeys(buf, 'L') 320 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])}) 321 322 call StopVimInTerminal(buf) 323 call delete('Xtrcscript') 324 call delete('Xtrctext') 325endfunc 326 327" Test that the garbage collector isn't triggered if a timer callback invokes 328" vgetc(). 329func Test_timer_nocatch_garbage_collect() 330 " 'uptimetime. must be bigger than the timer timeout 331 set ut=200 332 call test_garbagecollect_soon() 333 call test_override('no_wait_return', 0) 334 func CauseAnError(id) 335 " This will show an error and wait for Enter. 336 let a = {'foo', 'bar'} 337 endfunc 338 func FeedChar(id) 339 call feedkeys('x', 't') 340 endfunc 341 call timer_start(300, 'FeedChar') 342 call timer_start(100, 'CauseAnError') 343 let x = getchar() 344 345 set ut& 346 call test_override('no_wait_return', 1) 347 delfunc CauseAnError 348 delfunc FeedChar 349endfunc 350 351func Test_timer_error_in_timer_callback() 352 if !has('terminal') || (has('win32') && has('gui_running')) 353 throw 'Skipped: cannot run Vim in a terminal window' 354 endif 355 356 let lines =<< trim [CODE] 357 func Func(timer) 358 " fail to create list 359 let x = [ 360 endfunc 361 set updatetime=50 362 call timer_start(1, 'Func') 363 [CODE] 364 call writefile(lines, 'Xtest.vim') 365 366 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8}) 367 let job = term_getjob(buf) 368 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))}) 369 370 " GC must not run during timer callback, which can make Vim crash. 371 call term_wait(buf, 100) 372 call term_sendkeys(buf, "\<CR>") 373 call term_wait(buf, 100) 374 call assert_equal('run', job_status(job)) 375 376 call term_sendkeys(buf, ":qall!\<CR>") 377 call WaitFor({-> job_status(job) ==# 'dead'}) 378 if has('unix') 379 call assert_equal('', job_info(job).termsig) 380 endif 381 382 call delete('Xtest.vim') 383 exe buf .. 'bwipe!' 384endfunc 385 386" vim: shiftwidth=2 sts=2 expandtab 387