blob: 7a5cc29f5ac0fa9434e946246e6b4ab7590d5636 [file] [log] [blame]
Bram Moolenaar975b5272016-03-15 23:10:59 +01001" Test for timers
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature timers
Bram Moolenaar975b5272016-03-15 23:10:59 +01005
Bram Moolenaar3fffa972020-06-05 21:06:10 +02006source screendump.vim
Bram Moolenaar660b85e2017-09-30 14:26:58 +02007source shared.vim
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02008source term_util.vim
Bram Moolenaar11aa62f2017-09-04 22:56:01 +02009
Bram Moolenaar975b5272016-03-15 23:10:59 +010010func MyHandler(timer)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020011 let g:val += 1
Bram Moolenaar975b5272016-03-15 23:10:59 +010012endfunc
13
Bram Moolenaare3188e22016-05-31 21:13:04 +020014func MyHandlerWithLists(lists, timer)
15 let x = string(a:lists)
16endfunc
17
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020018func Test_timer_oneshot()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +010019 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +020020 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010021 let timer = timer_start(50, 'MyHandler')
Bram Moolenaarb73598e2016-08-07 18:22:53 +020022 let slept = WaitFor('g:val == 1')
23 call assert_equal(1, g:val)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010024 if has('mac')
Bram Moolenaar818fed72019-12-25 15:47:14 +010025 " Mac on Travis can be very slow.
26 let limit = 180
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020027 else
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010028 let limit = 100
29 endif
30 if has('reltime')
31 call assert_inrange(49, limit, slept)
32 else
33 call assert_inrange(20, limit, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020034 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010035endfunc
36
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020037func Test_timer_repeat_three()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +010038 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +020039 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010040 let timer = timer_start(50, 'MyHandler', {'repeat': 3})
Bram Moolenaarb73598e2016-08-07 18:22:53 +020041 let slept = WaitFor('g:val == 3')
42 call assert_equal(3, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020043 if has('reltime')
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010044 if has('mac')
45 " Mac on Travis can be slow.
46 call assert_inrange(149, 400, slept)
47 else
48 call assert_inrange(149, 250, slept)
49 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020050 else
51 call assert_inrange(80, 200, slept)
52 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010053endfunc
54
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020055func Test_timer_repeat_many()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +010056 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +020057 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010058 let timer = timer_start(50, 'MyHandler', {'repeat': -1})
59 sleep 200m
60 call timer_stop(timer)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010061 " Mac on Travis can be slow.
62 if has('mac')
63 call assert_inrange(1, 5, g:val)
64 else
65 call assert_inrange(2, 5, g:val)
66 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010067endfunc
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010068
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020069func Test_timer_with_partial_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +010070 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +020071 let g:val = 0
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020072 let meow = {'one': 1}
73 function meow.bite(...)
74 let g:val += self.one
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010075 endfunction
76
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020077 call timer_start(50, meow.bite)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020078 let slept = WaitFor('g:val == 1')
79 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020080 if has('reltime')
Bram Moolenaar5c463a22019-12-27 17:14:33 +010081 " Mac on Travis can be slow.
82 if has('mac')
83 call assert_inrange(49, 180, slept)
84 else
85 call assert_inrange(49, 130, slept)
86 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020087 else
88 call assert_inrange(20, 100, slept)
89 endif
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010090endfunc
Bram Moolenaare3188e22016-05-31 21:13:04 +020091
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020092func Test_timer_retain_partial()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020093 call timer_start(50, function('MyHandlerWithLists', [['a']]))
Bram Moolenaare3188e22016-05-31 21:13:04 +020094 call test_garbagecollect_now()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020095 sleep 100m
Bram Moolenaare3188e22016-05-31 21:13:04 +020096endfunc
Bram Moolenaarb73598e2016-08-07 18:22:53 +020097
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020098func Test_timer_info()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020099 let id = timer_start(1000, 'MyHandler')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200100 let info = id->timer_info()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200101 call assert_equal(id, info[0]['id'])
102 call assert_equal(1000, info[0]['time'])
103 call assert_true(info[0]['remaining'] > 500)
104 call assert_true(info[0]['remaining'] <= 1000)
105 call assert_equal(1, info[0]['repeat'])
106 call assert_equal("function('MyHandler')", string(info[0]['callback']))
107
108 let found = 0
109 for info in timer_info()
110 if info['id'] == id
111 let found += 1
112 endif
113 endfor
114 call assert_equal(1, found)
115
116 call timer_stop(id)
117 call assert_equal([], timer_info(id))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100118
119 call assert_fails('call timer_info("abc")', 'E39:')
Bram Moolenaar95b2dd02021-12-09 18:42:57 +0000120
121 " check repeat count inside the callback
122 let g:timer_repeat = []
123 let tid = timer_start(10, {tid -> execute("call add(g:timer_repeat, timer_info(tid)[0].repeat)")}, #{repeat: 3})
Bram Moolenaarff39a652021-12-10 10:57:08 +0000124 call WaitForAssert({-> assert_equal([2, 1, 0], g:timer_repeat)})
Bram Moolenaar95b2dd02021-12-09 18:42:57 +0000125 unlet g:timer_repeat
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200126endfunc
127
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200128func Test_timer_stopall()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200129 let id1 = timer_start(1000, 'MyHandler')
130 let id2 = timer_start(2000, 'MyHandler')
131 let info = timer_info()
132 call assert_equal(2, len(info))
133
134 call timer_stopall()
135 let info = timer_info()
136 call assert_equal(0, len(info))
137endfunc
138
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200139func Test_timer_paused()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100140 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200141 let g:val = 0
142
143 let id = timer_start(50, 'MyHandler')
144 let info = timer_info(id)
145 call assert_equal(0, info[0]['paused'])
146
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200147 eval id->timer_pause(1)
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200148 let info = timer_info(id)
149 call assert_equal(1, info[0]['paused'])
150 sleep 100m
151 call assert_equal(0, g:val)
152
153 call timer_pause(id, 0)
154 let info = timer_info(id)
155 call assert_equal(0, info[0]['paused'])
156
157 let slept = WaitFor('g:val == 1')
158 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200159 if has('reltime')
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100160 if has('mac')
161 " The travis Mac machines appear to be very busy.
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +0100162 call assert_inrange(0, 90, slept)
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100163 else
164 call assert_inrange(0, 30, slept)
165 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200166 else
167 call assert_inrange(0, 10, slept)
168 endif
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100169
170 call assert_fails('call timer_pause("abc", 1)', 'E39:')
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200171endfunc
172
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200173func StopMyself(timer)
174 let g:called += 1
175 if g:called == 2
176 call timer_stop(a:timer)
177 endif
178endfunc
179
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200180func Test_timer_delete_myself()
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200181 let g:called = 0
182 let t = timer_start(10, 'StopMyself', {'repeat': -1})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200183 call WaitForAssert({-> assert_equal(2, g:called)})
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200184 call assert_equal(2, g:called)
185 call assert_equal([], timer_info(t))
186endfunc
187
Bram Moolenaar75537a92016-09-05 22:45:28 +0200188func StopTimer1(timer)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200189 let g:timer2 = 10->timer_start('StopTimer2')
Bram Moolenaar75537a92016-09-05 22:45:28 +0200190 " avoid maxfuncdepth error
191 call timer_pause(g:timer1, 1)
Bram Moolenaar413c04e2019-08-16 22:29:18 +0200192 sleep 20m
Bram Moolenaar75537a92016-09-05 22:45:28 +0200193endfunc
194
195func StopTimer2(timer)
196 call timer_stop(g:timer1)
197endfunc
198
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200199func Test_timer_stop_in_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100200 let g:test_is_flaky = 1
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200201 call assert_equal(0, len(timer_info()))
Bram Moolenaar75537a92016-09-05 22:45:28 +0200202 let g:timer1 = timer_start(10, 'StopTimer1')
Bram Moolenaar315244d2019-08-17 13:18:16 +0200203 let slept = 0
204 for i in range(10)
205 if len(timer_info()) == 0
206 break
207 endif
208 sleep 10m
209 let slept += 10
210 endfor
211 " This should take only 30 msec, but on Mac it's often longer
212 call assert_inrange(0, 50, slept)
Bram Moolenaar75537a92016-09-05 22:45:28 +0200213endfunc
214
215func StopTimerAll(timer)
216 call timer_stopall()
217endfunc
218
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200219func Test_timer_stop_all_in_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100220 let g:test_is_flaky = 1
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200221 call assert_equal(0, len(timer_info()))
222 call timer_start(10, 'StopTimerAll')
223 call assert_equal(1, len(timer_info()))
224 let slept = 0
225 for i in range(10)
226 if len(timer_info()) == 0
227 break
228 endif
229 sleep 10m
230 let slept += 10
231 endfor
232 call assert_inrange(0, 30, slept)
Bram Moolenaar75537a92016-09-05 22:45:28 +0200233endfunc
234
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200235func FeedkeysCb(timer)
236 call feedkeys("hello\<CR>", 'nt')
237endfunc
238
239func InputCb(timer)
240 call timer_start(10, 'FeedkeysCb')
241 let g:val = input('?')
242 call Resume()
243endfunc
244
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200245func Test_timer_input_in_timer()
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200246 let g:val = ''
247 call timer_start(10, 'InputCb')
248 call Standby(1000)
249 call assert_equal('hello', g:val)
250endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200251
Bram Moolenaarc577d812017-07-08 22:37:34 +0200252func FuncWithError(timer)
253 let g:call_count += 1
254 if g:call_count == 4
255 return
256 endif
257 doesnotexist
258endfunc
259
260func Test_timer_errors()
261 let g:call_count = 0
262 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
263 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200265 sleep 50m
266 call assert_equal(3, g:call_count)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100267
268 call assert_fails('call timer_start(100, "MyHandler", "abc")', 'E475:')
269 call assert_fails('call timer_start(100, [])', 'E921:')
270 call assert_fails('call timer_stop("abc")', 'E39:')
Bram Moolenaarc577d812017-07-08 22:37:34 +0200271endfunc
272
Bram Moolenaare723c422017-09-06 23:40:10 +0200273func FuncWithCaughtError(timer)
274 let g:call_count += 1
275 try
276 doesnotexist
277 catch
278 " nop
279 endtry
280endfunc
281
282func Test_timer_catch_error()
283 let g:call_count = 0
284 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
285 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200286 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200287 sleep 50m
288 call assert_equal(4, g:call_count)
289endfunc
290
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200291func FeedAndPeek(timer)
292 call test_feedinput('a')
293 call getchar(1)
294endfunc
295
296func Interrupt(timer)
Bram Moolenaarce90e362019-09-08 18:58:44 +0200297 eval "\<C-C>"->test_feedinput()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200298endfunc
299
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200300func Test_timer_peek_and_get_char()
zeertzjqeb273cd2022-07-01 19:11:23 +0100301 if !has('unix') && !has('gui_running')
302 throw 'Skipped: cannot feed low-level input'
303 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200304
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200305 call timer_start(0, 'FeedAndPeek')
306 let intr = timer_start(100, 'Interrupt')
307 let c = getchar()
308 call assert_equal(char2nr('a'), c)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200309 eval intr->timer_stop()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200310endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200311
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200312func Test_timer_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100313 if has('win32') && !has('gui_running')
zeertzjqeb273cd2022-07-01 19:11:23 +0100314 throw 'Skipped: cannot feed low-level input'
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100315 endif
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100316 CheckFunction reltimefloat
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100317
Bram Moolenaar50948e42019-01-29 20:36:56 +0100318 " Measure the elapsed time to avoid a hang when it fails.
319 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100320 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100321 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100322 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100323 let c = getchar(0)
324 sleep 10m
325 endwhile
326 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100327 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100328endfunc
329
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200330func Test_timer_ex_mode()
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200331 " Function with an empty line.
332 func Foo(...)
333
334 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100335 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200336 " This used to throw error E749.
337 exe "normal Qsleep 100m\rvi\r"
338 call timer_stop(timer)
339endfunc
340
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200341func Test_timer_restore_count()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200342 CheckRunVimInTerminal
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200343 " Check that v:count is saved and restored, not changed by a timer.
344 call writefile([
345 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
346 \ 'func Doit(id)',
347 \ ' normal 3j',
348 \ 'endfunc',
349 \ 'call timer_start(100, "Doit")',
350 \ ], 'Xtrcscript')
351 call writefile([
352 \ '1-1234',
353 \ '2-1234',
354 \ '3-1234',
355 \ ], 'Xtrctext')
356 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
357
358 " Wait for the timer to move the cursor to the third line.
359 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
360 call assert_equal(1, term_getcursor(buf)[1])
361 " Now check that v:count has not been set to 3
362 call term_sendkeys(buf, 'L')
363 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
364
365 call StopVimInTerminal(buf)
366 call delete('Xtrcscript')
367 call delete('Xtrctext')
368endfunc
369
Bram Moolenaaradc67142019-06-22 01:40:42 +0200370" Test that the garbage collector isn't triggered if a timer callback invokes
371" vgetc().
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200372func Test_timer_nocatch_garbage_collect()
Bram Moolenaaradc67142019-06-22 01:40:42 +0200373 " 'uptimetime. must be bigger than the timer timeout
374 set ut=200
375 call test_garbagecollect_soon()
376 call test_override('no_wait_return', 0)
377 func CauseAnError(id)
378 " This will show an error and wait for Enter.
379 let a = {'foo', 'bar'}
380 endfunc
381 func FeedChar(id)
382 call feedkeys('x', 't')
383 endfunc
384 call timer_start(300, 'FeedChar')
385 call timer_start(100, 'CauseAnError')
386 let x = getchar()
387
388 set ut&
389 call test_override('no_wait_return', 1)
390 delfunc CauseAnError
391 delfunc FeedChar
392endfunc
393
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200394func Test_timer_error_in_timer_callback()
Bram Moolenaar0d702022019-07-04 14:20:41 +0200395 if !has('terminal') || (has('win32') && has('gui_running'))
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200396 throw 'Skipped: cannot run Vim in a terminal window'
397 endif
398
399 let lines =<< trim [CODE]
400 func Func(timer)
401 " fail to create list
402 let x = [
403 endfunc
404 set updatetime=50
405 call timer_start(1, 'Func')
406 [CODE]
407 call writefile(lines, 'Xtest.vim')
408
Bram Moolenaar0d702022019-07-04 14:20:41 +0200409 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200410 let job = term_getjob(buf)
411 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
412
413 " GC must not run during timer callback, which can make Vim crash.
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200414 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200415 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200416 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200417 call assert_equal('run', job_status(job))
418
419 call term_sendkeys(buf, ":qall!\<CR>")
420 call WaitFor({-> job_status(job) ==# 'dead'})
421 if has('unix')
422 call assert_equal('', job_info(job).termsig)
423 endif
424
425 call delete('Xtest.vim')
426 exe buf .. 'bwipe!'
427endfunc
428
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100429" Test for garbage collection when a timer is still running
430func Test_timer_garbage_collect()
431 let timer = timer_start(1000, function('MyHandler'), {'repeat' : 10})
432 call test_garbagecollect_now()
433 let l = timer_info(timer)
434 call assert_equal(function('MyHandler'), l[0].callback)
435 call timer_stop(timer)
436endfunc
437
Bram Moolenaar14e579092020-03-07 16:59:25 +0100438func Test_timer_invalid_callback()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200439 call assert_fails('call timer_start(0, "0")', 'E921:')
Bram Moolenaar14e579092020-03-07 16:59:25 +0100440endfunc
441
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200442func Test_timer_changing_function_list()
443 CheckRunVimInTerminal
444
445 " Create a large number of functions. Should get the "more" prompt.
446 " The typing "G" triggers the timer, which changes the function table.
447 let lines =<< trim END
448 for func in map(range(1,99), "'Func' .. v:val")
449 exe "func " .. func .. "()"
450 endfunc
451 endfor
452 au CmdlineLeave : call timer_start(0, {-> 0})
453 END
454 call writefile(lines, 'XTest_timerchange')
455 let buf = RunVimInTerminal('-S XTest_timerchange', #{rows: 10})
456 call term_sendkeys(buf, ":fu\<CR>")
457 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 10))})
458 call term_sendkeys(buf, "G")
Bram Moolenaare2e40752020-09-04 21:18:46 +0200459 call WaitForAssert({-> assert_match('E454:', term_getline(buf, 9))})
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200460 call term_sendkeys(buf, "\<Esc>")
461
462 call StopVimInTerminal(buf)
463 call delete('XTest_timerchange')
464endfunc
465
Bram Moolenaar9f284162021-04-22 21:39:30 +0200466func Test_timer_outputting_message()
467 CheckRunVimInTerminal
468
469 let lines =<< trim END
470 vim9script
471 setline(1, 'some text')
472 set showcmd ut=2000 cmdheight=1
473 timer_start(0, (_) => {
474 echon repeat('x', &columns - 11)
475 })
476 END
477 call writefile(lines, 'XTest_timermessage')
478 let buf = RunVimInTerminal('-S XTest_timermessage', #{rows: 6})
479 call term_sendkeys(buf, "l")
480 call term_wait(buf)
481 " should not get a hit-enter prompt
482 call WaitForAssert({-> assert_match('xxxxxxxxxxx', term_getline(buf, 6))})
483
484 call StopVimInTerminal(buf)
485 call delete('XTest_timermessage')
486endfunc
487
Bram Moolenaare615db02022-01-20 21:00:54 +0000488func Test_timer_using_win_execute_undo_sync()
489 let bufnr1 = bufnr()
490 new
491 let g:bufnr2 = bufnr()
492 let g:winid = win_getid()
493 exe "buffer " .. bufnr1
494 wincmd w
495 call setline(1, ['test'])
496 autocmd InsertEnter * call timer_start(100, { -> win_execute(g:winid, 'buffer ' .. g:bufnr2) })
497 call timer_start(200, { -> feedkeys("\<CR>bbbb\<Esc>") })
498 call feedkeys("Oaaaa", 'x!t')
499 " will hang here until the second timer fires
500 call assert_equal(['aaaa', 'bbbb', 'test'], getline(1, '$'))
501 undo
502 call assert_equal(['test'], getline(1, '$'))
503
504 bwipe!
505 bwipe!
506 unlet g:winid
507 unlet g:bufnr2
508 au! InsertEnter
509endfunc
510
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100511
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200512" vim: shiftwidth=2 sts=2 expandtab