robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8634ec3827
master
1" Test for timers 2 3source check.vim 4CheckFeature timers 5 6source screendump.vim 7source shared.vim 8source term_util.vim 9source load.vim 10 11func SetUp () 12call timer_stopall () 13endfunc 14 15func MyHandler ( timer ) 16let g: val += 1 17endfunc 18 19func MyHandlerWithLists ( lists , timer ) 20let x = string ( a: lists ) 21endfunc 22 23func Test_timer_oneshot () 24let g: test_is_flaky = 1 25let g: val = 0 26let timer = timer_start ( 50 , 'MyHandler' ) 27let slept = WaitFor ( 'g:val == 1' ) 28call assert_equal ( 1 , g: val ) 29if has ( 'reltime' ) 30call assert_inrange ( 40 , LoadAdjust ( 120 ), slept ) 31else 32call assert_inrange ( 20 , 120 , slept ) 33endif 34endfunc 35 36func Test_timer_repeat_three () 37let g: test_is_flaky = 1 38let g: val = 0 39let timer = timer_start ( 50 , 'MyHandler' , { 'repeat' : 3 }) 40let slept = WaitFor ( 'g:val == 3' ) 41call assert_equal ( 3 , g: val ) 42if has ( 'reltime' ) 43call assert_inrange ( 120 , LoadAdjust ( 250 ), slept ) 44else 45call assert_inrange ( 80 , 200 , slept ) 46endif 47endfunc 48 49func Test_timer_repeat_many () 50let g: test_is_flaky = 1 51let g: val = 0 52let timer = timer_start ( 50 , 'MyHandler' , { 'repeat' : -1 }) 53sleep 200m 54call timer_stop ( timer ) 55call assert_inrange (( has ( 'mac' ) ? 1 : 2 ), LoadAdjust ( 5 ), g: val ) 56endfunc 57 58func Test_timer_with_partial_callback () 59let g: test_is_flaky = 1 60let g: val = 0 61let meow = { 'one' : 1 } 62function meow . bite (...) 63let g: val += self . one 64endfunction 65 66call timer_start ( 50 , meow . bite ) 67let slept = WaitFor ( 'g:val == 1' ) 68call assert_equal ( 1 , g: val ) 69if has ( 'reltime' ) 70call assert_inrange ( 40 , LoadAdjust ( 130 ), slept ) 71else 72call assert_inrange ( 20 , 100 , slept ) 73endif 74endfunc 75 76func Test_timer_retain_partial () 77call timer_start ( 50 , function ( 'MyHandlerWithLists' , [[ 'a' ]])) 78call test_garbagecollect_now () 79sleep 100m 80endfunc 81 82func Test_timer_info () 83let id = timer_start ( 1000 , 'MyHandler' ) 84let info = id ->timer_info () 85call assert_equal ( id , info [ 0 ][ 'id' ]) 86call assert_equal ( 1000 , info [ 0 ][ 'time' ]) 87call assert_equal ( "function('MyHandler')" , string ( info [ 0 ][ 'callback' ])) 88 89let found = 0 90for info in timer_info () 91if info [ 'id' ] == id 92let found += 1 93endif 94endfor 95call assert_equal ( 1 , found ) 96 97call timer_stop ( id ) 98call assert_equal ([], timer_info ( id )) 99 100call assert_fails ( 'call timer_info("abc")' , 'E1210:' ) 101 102" check repeat count inside the callback 103let g: timer_repeat = [] 104let tid = timer_start ( 10 , { tid ->execute ( "call add(g:timer_repeat, timer_info(tid)[0].repeat)" )}, #{ repeat : 3 }) 105call WaitForAssert ({ ->assert_equal ([ 2 , 1 , 0 ], g: timer_repeat )}) 106unlet g: timer_repeat 107endfunc 108 109func Test_timer_stopall () 110let id1 = timer_start ( 1000 , 'MyHandler' ) 111let id2 = timer_start ( 2000 , 'MyHandler' ) 112let info = timer_info () 113call assert_equal ( 2 , len ( info )) 114 115call timer_stopall () 116let info = timer_info () 117call assert_equal ( 0 , len ( info )) 118endfunc 119 120func Test_timer_paused () 121let g: test_is_flaky = 1 122let g: val = 0 123 124let id = timer_start ( 50 , 'MyHandler' ) 125let info = timer_info ( id ) 126call assert_equal ( 0 , info [ 0 ][ 'paused' ]) 127 128eval id ->timer_pause ( 1 ) 129let info = timer_info ( id ) 130call assert_equal ( 1 , info [ 0 ][ 'paused' ]) 131sleep 200m 132call assert_equal ( 0 , g: val ) 133 134call timer_pause ( id , 0 ) 135let info = timer_info ( id ) 136call assert_equal ( 0 , info [ 0 ][ 'paused' ]) 137 138let slept = WaitFor ( 'g:val == 1' ) 139call assert_equal ( 1 , g: val ) 140if has ( 'reltime' ) 141call assert_inrange ( 0 , LoadAdjust ( 140 ), slept ) 142else 143call assert_inrange ( 0 , 10 , slept ) 144endif 145 146call assert_fails ( 'call timer_pause("abc", 1)' , 'E39:' ) 147endfunc 148 149func StopMyself ( timer ) 150let g: called += 1 151if g: called == 2 152call timer_stop ( a: timer ) 153endif 154endfunc 155 156func Test_timer_delete_myself () 157let g: called = 0 158let t = timer_start ( 10 , 'StopMyself' , { 'repeat' : -1 }) 159call WaitForAssert ({ ->assert_equal ( 2 , g: called )}) 160call assert_equal ( 2 , g: called ) 161call assert_equal ([], timer_info ( t )) 162endfunc 163 164func StopTimer1 ( timer ) 165let g: timer2 = 10 ->timer_start ( 'StopTimer2' ) 166" avoid maxfuncdepth error 167call timer_pause ( g: timer1 , 1 ) 168sleep 20m 169endfunc 170 171func StopTimer2 ( timer ) 172call timer_stop ( g: timer1 ) 173endfunc 174 175func Test_timer_stop_in_callback () 176let g: test_is_flaky = 1 177call assert_equal ( 0 , len ( timer_info ())) 178let g: timer1 = timer_start ( 10 , 'StopTimer1' ) 179let slept = 0 180for i in range ( 10 ) 181if len ( timer_info ()) == 0 182break 183endif 184sleep 10m 185let slept += 10 186endfor 187" This should take only 30 msec, but on Mac it's often longer 188call assert_inrange ( 0 , 50 , slept ) 189endfunc 190 191func StopTimerAll ( timer ) 192call timer_stopall () 193endfunc 194 195func Test_timer_stop_all_in_callback () 196let g: test_is_flaky = 1 197call assert_equal ( 0 , len ( timer_info ())) 198call timer_start ( 10 , 'StopTimerAll' ) 199call assert_equal ( 1 , len ( timer_info ())) 200let slept = 0 201for i in range ( 10 ) 202if len ( timer_info ()) == 0 203break 204endif 205sleep 10m 206let slept += 10 207endfor 208call assert_inrange ( 0 , 30 , slept ) 209endfunc 210 211func FeedkeysCb ( timer ) 212call feedkeys ( "hello\<CR>" , 'nt' ) 213endfunc 214 215func InputCb ( timer ) 216call timer_start ( 10 , 'FeedkeysCb' ) 217let g: val = input ( '?' ) 218call Resume () 219endfunc 220 221func Test_timer_input_in_timer () 222let g: val = '' 223call timer_start ( 10 , 'InputCb' ) 224call Standby ( 1000 ) 225call assert_equal ( 'hello' , g: val ) 226endfunc 227 228func FuncWithError ( timer ) 229let g: call_count += 1 230if g: call_count == 4 231return 232endif 233doesnotexist 234endfunc 235 236func Test_timer_errors () 237let g: call_count = 0 238let timer = timer_start ( 10 , 'FuncWithError' , { 'repeat' : -1 }) 239" Timer will be stopped after failing 3 out of 3 times. 240call WaitForAssert ({ ->assert_equal ( 3 , g: call_count )}) 241sleep 50m 242call assert_equal ( 3 , g: call_count ) 243 244call assert_fails ( 'call timer_start(100, "MyHandler", "abc")' , 'E1206:' ) 245call assert_fails ( 'call timer_start(100, [])' , 'E921:' ) 246call assert_fails ( 'call timer_stop("abc")' , 'E1210:' ) 247endfunc 248 249func FuncWithCaughtError ( timer ) 250let g: call_count += 1 251try 252doesnotexist 253catch 254" nop 255endtry 256endfunc 257 258func Test_timer_catch_error () 259let g: call_count = 0 260let timer = timer_start ( 10 , 'FuncWithCaughtError' , { 'repeat' : 4 }) 261" Timer will not be stopped. 262call WaitForAssert ({ ->assert_equal ( 4 , g: call_count )}) 263sleep 50m 264call assert_equal ( 4 , g: call_count ) 265endfunc 266 267func FeedAndPeek ( timer ) 268" call test_feedinput('a') 269call nvim_input ( 'a' ) 270call getchar ( 1 ) 271endfunc 272 273func Interrupt ( timer ) 274" eval "\<C-C>"->test_feedinput() 275call nvim_input ( "\<C-C>" ) 276endfunc 277 278func Test_timer_peek_and_get_char () 279if ! has ( 'unix' ) && ! has ( 'gui_running' ) 280throw 'Skipped: cannot feed low-level input' 281endif 282 283call timer_start ( 0 , 'FeedAndPeek' ) 284let intr = timer_start ( 100 , 'Interrupt' ) 285let c = getchar () 286call assert_equal ( char2nr ( 'a' ), c ) 287eval intr ->timer_stop () 288endfunc 289 290func Test_timer_getchar_zero () 291if has ( 'win32' ) && ! has ( 'gui_running' ) 292throw 'Skipped: cannot feed low-level input' 293endif 294CheckFunction reltimefloat 295 296" Measure the elapsed time to avoid a hang when it fails. 297let start = reltime () 298let id = timer_start ( 20 , { ->feedkeys ( 'x' , 'L' )}) 299let c = 0 300while c == 0 && reltimefloat ( reltime ( start )) < 0.2 301let c = getchar ( 0 ) 302sleep 10m 303endwhile 304call assert_equal ( 'x' , nr2char ( c )) 305call timer_stop ( id ) 306endfunc 307 308func Test_timer_ex_mode () 309" Function with an empty line. 310func Foo (...) 311 312endfunc 313let timer = timer_start ( 40 , function ( 'g:Foo' ), { 'repeat' : -1 }) 314" This used to throw error E749. 315exe "normal Qsleep 100m\rvi\r" 316call timer_stop ( timer ) 317endfunc 318 319func Test_timer_restore_count () 320CheckRunVimInTerminal 321" Check that v:count is saved and restored, not changed by a timer. 322call writefile ([ 323 \'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"' , 324 \'func Doit(id)' , 325 \' normal 3j' , 326 \'endfunc' , 327 \'call timer_start(100, "Doit")' , 328 \], 'Xtrcscript' ) 329call writefile ([ 330 \'1-1234' , 331 \'2-1234' , 332 \'3-1234' , 333 \], 'Xtrctext' ) 334let buf = RunVimInTerminal ( '-S Xtrcscript Xtrctext' , {}) 335 336" Wait for the timer to move the cursor to the third line. 337call WaitForAssert ({ ->assert_equal ( 3 , term_getcursor ( buf )[ 0 ])}) 338call assert_equal ( 1 , term_getcursor ( buf )[ 1 ]) 339" Now check that v:count has not been set to 3 340call term_sendkeys ( buf , 'L' ) 341call WaitForAssert ({ ->assert_equal ( 2 , term_getcursor ( buf )[ 1 ])}) 342 343call StopVimInTerminal ( buf ) 344call delete ( 'Xtrcscript' ) 345call delete ( 'Xtrctext' ) 346endfunc 347 348" Test that the garbage collector isn't triggered if a timer callback invokes 349" vgetc(). 350func Test_nocatch_timer_garbage_collect () 351" FIXME: why does this fail only on MacOS M1? 352try 353CheckNotMacM1 354throw 'Skipped: Nvim does not support test_garbagecollect_soon(), test_override()' 355catch /Skipped/ 356let g: skipped_reason = v: exception 357return 358endtry 359 360" 'uptimetime. must be bigger than the timer timeout 361set ut = 200 362call test_garbagecollect_soon () 363call test_override ( 'no_wait_return' , 0 ) 364func CauseAnError ( id ) 365" This will show an error and wait for Enter. 366let a = { 'foo' , 'bar'} 367endfunc 368func FeedChar ( id ) 369call feedkeys ( ":\<CR>" , 't' ) 370endfunc 371call timer_start ( 300 , 'FeedChar' ) 372call timer_start ( 100 , 'CauseAnError' ) 373let x = getchar () " wait for error in timer 374let x = getchar ( 0 ) " read any remaining chars 375let x = getchar ( 0 ) 376 377set ut & 378call test_override ( 'no_wait_return' , 1 ) 379delfunc CauseAnError 380delfunc FeedChar 381endfunc 382 383func Test_timer_error_in_timer_callback () 384if ! has ( 'terminal' ) || ( has ( 'win32' ) && has ( 'gui_running' )) 385throw 'Skipped: cannot run Vim in a terminal window' 386endif 387 388let lines =<< trim [CODE] 389func Func(timer) 390" fail to create list 391let x = [ 392endfunc 393set updatetime=50 394call timer_start(1, 'Func') 395[CODE] 396call writefile ( lines , 'Xtest.vim' ) 397 398let buf = term_start ( GetVimCommandCleanTerm () .. ' -S Xtest.vim' , { 'term_rows' : 8 }) 399let job = term_getjob ( buf ) 400call WaitForAssert ({ ->assert_notequal ( '' , term_getline ( buf , 8 ))}) 401 402" GC must not run during timer callback, which can make Vim crash. 403call TermWait ( buf , 50 ) 404call term_sendkeys ( buf , "\<CR>" ) 405call TermWait ( buf , 50 ) 406call assert_equal ( 'run' , job_status ( job )) 407 408call term_sendkeys ( buf , ":qall!\<CR>" ) 409call WaitFor ({ ->job_status ( job ) ==# 'dead' }) 410if has ( 'unix' ) 411call assert_equal ( '' , job_info ( job ). termsig ) 412endif 413 414call delete ( 'Xtest.vim' ) 415exe buf .. 'bwipe!' 416endfunc 417 418" Test for garbage collection when a timer is still running 419func Test_timer_garbage_collect () 420let timer = timer_start ( 1000 , function ( 'MyHandler' ), { 'repeat' : 10 }) 421call test_garbagecollect_now () 422let l = timer_info ( timer ) 423call assert_equal ( function ( 'MyHandler' ), l [ 0 ]. callback ) 424call timer_stop ( timer ) 425endfunc 426 427func Test_timer_invalid_callback () 428call assert_fails ( 'call timer_start(0, "0")' , 'E921' ) 429endfunc 430 431func Test_timer_changing_function_list () 432CheckRunVimInTerminal 433 434" Create a large number of functions. Should get the "more" prompt. 435" The typing "G" triggers the timer, which changes the function table. 436let lines =<< trim END 437for func in map(range(1,99), "'Func' .. v:val") 438exe "func " .. func .. "()" 439endfunc 440endfor 441au CmdlineLeave : call timer_start(0, {-> 0}) 442END 443call writefile ( lines , 'XTest_timerchange' ) 444let buf = RunVimInTerminal ( '-S XTest_timerchange' , #{ rows : 10 }) 445call term_sendkeys ( buf , ":fu\<CR>" ) 446call WaitForAssert ({ ->assert_match ( '-- More --' , term_getline ( buf , 10 ))}) 447call term_sendkeys ( buf , "G" ) 448call WaitForAssert ({ ->assert_match ( 'E454' , term_getline ( buf , 9 ))}) 449call term_sendkeys ( buf , "\<Esc>" ) 450 451call StopVimInTerminal ( buf ) 452call delete ( 'XTest_timerchange' ) 453endfunc 454 455func Test_timer_using_win_execute_undo_sync () 456let bufnr1 = bufnr () 457new 458let g: bufnr2 = bufnr () 459let g: winid = win_getid () 460exe "buffer " .. bufnr1 461wincmd w 462call setline ( 1 , [ 'test' ]) 463autocmd InsertEnter * call timer_start ( 100 , { ->win_execute ( g: winid , 'buffer ' .. g: bufnr2 ) }) 464call timer_start ( 200 , { ->feedkeys ( "\<CR>bbbb\<Esc>" ) }) 465call feedkeys ( "Oaaaa" , 'x!t' ) 466" will hang here until the second timer fires 467call assert_equal ([ 'aaaa' , 'bbbb' , 'test' ], getline ( 1 , '$' )) 468undo 469call assert_equal ([ 'test' ], getline ( 1 , '$' )) 470 471bwipe ! 472bwipe ! 473unlet g: winid 474unlet g: bufnr2 475au ! InsertEnter 476endfunc 477 478" vim: shiftwidth=2 sts=2 expandtab