blob: fe80bb9f211b91bd0833fc6f14d2baa2b74ec424 [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
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100119 call assert_fails('call timer_info("abc")', 'E1210:')
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()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100132 " count one for the TestTimeout() timer
133 call assert_equal(3, len(info))
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200134
135 call timer_stopall()
136 let info = timer_info()
137 call assert_equal(0, len(info))
138endfunc
139
Bram Moolenaarcf3d0ea2022-10-07 11:20:29 +0100140def Test_timer_stopall_with_popup()
141 # Create a popup that times out after ten seconds.
142 # Another timer will fire in half a second and close it early after stopping
143 # all timers.
144 var pop = popup_create('Popup', {time: 10000})
145 var tmr = timer_start(500, (_) => {
146 timer_stopall()
147 popup_clear()
148 })
149 sleep 1
150 assert_equal([], timer_info(tmr))
151 assert_equal([], popup_list())
152enddef
153
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200154func Test_timer_paused()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100155 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200156 let g:val = 0
157
158 let id = timer_start(50, 'MyHandler')
159 let info = timer_info(id)
160 call assert_equal(0, info[0]['paused'])
161
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200162 eval id->timer_pause(1)
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200163 let info = timer_info(id)
164 call assert_equal(1, info[0]['paused'])
165 sleep 100m
166 call assert_equal(0, g:val)
167
168 call timer_pause(id, 0)
169 let info = timer_info(id)
170 call assert_equal(0, info[0]['paused'])
171
172 let slept = WaitFor('g:val == 1')
173 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200174 if has('reltime')
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100175 if has('mac')
176 " The travis Mac machines appear to be very busy.
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +0100177 call assert_inrange(0, 90, slept)
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100178 else
179 call assert_inrange(0, 30, slept)
180 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200181 else
182 call assert_inrange(0, 10, slept)
183 endif
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100184
185 call assert_fails('call timer_pause("abc", 1)', 'E39:')
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200186endfunc
187
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200188func StopMyself(timer)
189 let g:called += 1
190 if g:called == 2
191 call timer_stop(a:timer)
192 endif
193endfunc
194
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200195func Test_timer_delete_myself()
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200196 let g:called = 0
197 let t = timer_start(10, 'StopMyself', {'repeat': -1})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200198 call WaitForAssert({-> assert_equal(2, g:called)})
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200199 call assert_equal(2, g:called)
200 call assert_equal([], timer_info(t))
201endfunc
202
Bram Moolenaar75537a92016-09-05 22:45:28 +0200203func StopTimer1(timer)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200204 let g:timer2 = 10->timer_start('StopTimer2')
Bram Moolenaar75537a92016-09-05 22:45:28 +0200205 " avoid maxfuncdepth error
206 call timer_pause(g:timer1, 1)
Bram Moolenaar413c04e2019-08-16 22:29:18 +0200207 sleep 20m
Bram Moolenaar75537a92016-09-05 22:45:28 +0200208endfunc
209
210func StopTimer2(timer)
211 call timer_stop(g:timer1)
212endfunc
213
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200214func Test_timer_stop_in_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100215 let g:test_is_flaky = 1
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100216 call assert_equal(1, len(timer_info()))
Bram Moolenaar75537a92016-09-05 22:45:28 +0200217 let g:timer1 = timer_start(10, 'StopTimer1')
Bram Moolenaar315244d2019-08-17 13:18:16 +0200218 let slept = 0
219 for i in range(10)
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100220 if len(timer_info()) == 1
Bram Moolenaar315244d2019-08-17 13:18:16 +0200221 break
222 endif
223 sleep 10m
224 let slept += 10
225 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100226 if slept == 100
227 call assert_equal(1, len(timer_info()))
228 else
229 " This should take only 30 msec, but on Mac it's often longer
230 call assert_inrange(0, 50, slept)
231 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200232endfunc
233
234func StopTimerAll(timer)
235 call timer_stopall()
236endfunc
237
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200238func Test_timer_stop_all_in_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100239 let g:test_is_flaky = 1
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100240 " One timer is for TestTimeout()
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200241 call assert_equal(1, len(timer_info()))
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100242 call timer_start(10, 'StopTimerAll')
243 call assert_equal(2, len(timer_info()))
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200244 let slept = 0
245 for i in range(10)
246 if len(timer_info()) == 0
247 break
248 endif
249 sleep 10m
250 let slept += 10
251 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100252 if slept == 100
253 call assert_equal(0, len(timer_info()))
254 else
255 call assert_inrange(0, 30, slept)
256 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200257endfunc
258
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200259func FeedkeysCb(timer)
260 call feedkeys("hello\<CR>", 'nt')
261endfunc
262
263func InputCb(timer)
264 call timer_start(10, 'FeedkeysCb')
265 let g:val = input('?')
266 call Resume()
267endfunc
268
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200269func Test_timer_input_in_timer()
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200270 let g:val = ''
271 call timer_start(10, 'InputCb')
272 call Standby(1000)
273 call assert_equal('hello', g:val)
274endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200275
Bram Moolenaarc577d812017-07-08 22:37:34 +0200276func FuncWithError(timer)
277 let g:call_count += 1
278 if g:call_count == 4
279 return
280 endif
281 doesnotexist
282endfunc
283
284func Test_timer_errors()
285 let g:call_count = 0
286 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
287 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200288 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200289 sleep 50m
290 call assert_equal(3, g:call_count)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100291
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100292 call assert_fails('call timer_start(100, "MyHandler", "abc")', 'E1206:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100293 call assert_fails('call timer_start(100, [])', 'E921:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100294 call assert_fails('call timer_stop("abc")', 'E1210:')
Bram Moolenaarc577d812017-07-08 22:37:34 +0200295endfunc
296
Bram Moolenaare723c422017-09-06 23:40:10 +0200297func FuncWithCaughtError(timer)
298 let g:call_count += 1
299 try
300 doesnotexist
301 catch
302 " nop
303 endtry
304endfunc
305
306func Test_timer_catch_error()
307 let g:call_count = 0
308 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
309 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200310 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200311 sleep 50m
312 call assert_equal(4, g:call_count)
313endfunc
314
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200315func FeedAndPeek(timer)
316 call test_feedinput('a')
317 call getchar(1)
318endfunc
319
320func Interrupt(timer)
Bram Moolenaarce90e362019-09-08 18:58:44 +0200321 eval "\<C-C>"->test_feedinput()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200322endfunc
323
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200324func Test_timer_peek_and_get_char()
zeertzjqeb273cd2022-07-01 19:11:23 +0100325 if !has('unix') && !has('gui_running')
326 throw 'Skipped: cannot feed low-level input'
327 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200328
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200329 call timer_start(0, 'FeedAndPeek')
330 let intr = timer_start(100, 'Interrupt')
331 let c = getchar()
332 call assert_equal(char2nr('a'), c)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200333 eval intr->timer_stop()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200334endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200335
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200336func Test_timer_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100337 if has('win32') && !has('gui_running')
zeertzjqeb273cd2022-07-01 19:11:23 +0100338 throw 'Skipped: cannot feed low-level input'
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100339 endif
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100340 CheckFunction reltimefloat
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100341
Bram Moolenaar50948e42019-01-29 20:36:56 +0100342 " Measure the elapsed time to avoid a hang when it fails.
343 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100344 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100345 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100346 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100347 let c = getchar(0)
348 sleep 10m
349 endwhile
350 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100351 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100352endfunc
353
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200354func Test_timer_ex_mode()
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200355 " Function with an empty line.
356 func Foo(...)
357
358 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100359 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200360 " This used to throw error E749.
361 exe "normal Qsleep 100m\rvi\r"
362 call timer_stop(timer)
363endfunc
364
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200365func Test_timer_restore_count()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200366 CheckRunVimInTerminal
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200367 " Check that v:count is saved and restored, not changed by a timer.
368 call writefile([
369 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
370 \ 'func Doit(id)',
371 \ ' normal 3j',
372 \ 'endfunc',
373 \ 'call timer_start(100, "Doit")',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100374 \ ], 'Xtrcscript', 'D')
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200375 call writefile([
376 \ '1-1234',
377 \ '2-1234',
378 \ '3-1234',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100379 \ ], 'Xtrctext', 'D')
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200380 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
381
382 " Wait for the timer to move the cursor to the third line.
383 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
384 call assert_equal(1, term_getcursor(buf)[1])
385 " Now check that v:count has not been set to 3
386 call term_sendkeys(buf, 'L')
387 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
388
389 call StopVimInTerminal(buf)
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200390endfunc
391
Bram Moolenaaradc67142019-06-22 01:40:42 +0200392" Test that the garbage collector isn't triggered if a timer callback invokes
393" vgetc().
zeertzjqbb404f52022-07-23 06:25:29 +0100394func Test_nocatch_timer_garbage_collect()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100395 " FIXME: why does this fail only on MacOS M1?
Bram Moolenaar94722c52023-01-28 19:19:03 +0000396 try
Bram Moolenaar140f6d02022-09-24 14:49:07 +0100397 CheckNotMacM1
398 catch /Skipped/
399 let g:skipped_reason = v:exception
400 return
401 endtry
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100402
Bram Moolenaaradc67142019-06-22 01:40:42 +0200403 " 'uptimetime. must be bigger than the timer timeout
404 set ut=200
405 call test_garbagecollect_soon()
406 call test_override('no_wait_return', 0)
407 func CauseAnError(id)
408 " This will show an error and wait for Enter.
409 let a = {'foo', 'bar'}
410 endfunc
411 func FeedChar(id)
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100412 call feedkeys(":\<CR>", 't')
Bram Moolenaaradc67142019-06-22 01:40:42 +0200413 endfunc
414 call timer_start(300, 'FeedChar')
415 call timer_start(100, 'CauseAnError')
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100416 let x = getchar() " wait for error in timer
417 let x = getchar(0) " read any remaining chars
418 let x = getchar(0)
Bram Moolenaaradc67142019-06-22 01:40:42 +0200419
420 set ut&
421 call test_override('no_wait_return', 1)
422 delfunc CauseAnError
423 delfunc FeedChar
424endfunc
425
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200426func Test_timer_error_in_timer_callback()
Bram Moolenaar0d702022019-07-04 14:20:41 +0200427 if !has('terminal') || (has('win32') && has('gui_running'))
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200428 throw 'Skipped: cannot run Vim in a terminal window'
429 endif
430
431 let lines =<< trim [CODE]
432 func Func(timer)
433 " fail to create list
434 let x = [
435 endfunc
436 set updatetime=50
437 call timer_start(1, 'Func')
438 [CODE]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100439 call writefile(lines, 'Xtest.vim', 'D')
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200440
Bram Moolenaar0d702022019-07-04 14:20:41 +0200441 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200442 let job = term_getjob(buf)
443 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
444
445 " GC must not run during timer callback, which can make Vim crash.
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200446 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200447 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200448 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200449 call assert_equal('run', job_status(job))
450
451 call term_sendkeys(buf, ":qall!\<CR>")
452 call WaitFor({-> job_status(job) ==# 'dead'})
453 if has('unix')
454 call assert_equal('', job_info(job).termsig)
455 endif
456
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200457 exe buf .. 'bwipe!'
458endfunc
459
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100460" Test for garbage collection when a timer is still running
461func Test_timer_garbage_collect()
462 let timer = timer_start(1000, function('MyHandler'), {'repeat' : 10})
463 call test_garbagecollect_now()
464 let l = timer_info(timer)
465 call assert_equal(function('MyHandler'), l[0].callback)
466 call timer_stop(timer)
467endfunc
468
Bram Moolenaar14e579092020-03-07 16:59:25 +0100469func Test_timer_invalid_callback()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200470 call assert_fails('call timer_start(0, "0")', 'E921:')
Bram Moolenaar14e579092020-03-07 16:59:25 +0100471endfunc
472
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200473func Test_timer_changing_function_list()
474 CheckRunVimInTerminal
475
476 " Create a large number of functions. Should get the "more" prompt.
477 " The typing "G" triggers the timer, which changes the function table.
478 let lines =<< trim END
479 for func in map(range(1,99), "'Func' .. v:val")
480 exe "func " .. func .. "()"
481 endfunc
482 endfor
483 au CmdlineLeave : call timer_start(0, {-> 0})
484 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100485 call writefile(lines, 'XTest_timerchange', 'D')
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200486 let buf = RunVimInTerminal('-S XTest_timerchange', #{rows: 10})
487 call term_sendkeys(buf, ":fu\<CR>")
488 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 10))})
489 call term_sendkeys(buf, "G")
Bram Moolenaare2e40752020-09-04 21:18:46 +0200490 call WaitForAssert({-> assert_match('E454:', term_getline(buf, 9))})
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200491 call term_sendkeys(buf, "\<Esc>")
492
493 call StopVimInTerminal(buf)
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200494endfunc
495
Bram Moolenaar9f284162021-04-22 21:39:30 +0200496func Test_timer_outputting_message()
497 CheckRunVimInTerminal
498
499 let lines =<< trim END
500 vim9script
501 setline(1, 'some text')
502 set showcmd ut=2000 cmdheight=1
503 timer_start(0, (_) => {
504 echon repeat('x', &columns - 11)
505 })
506 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100507 call writefile(lines, 'XTest_timermessage', 'D')
Bram Moolenaar9f284162021-04-22 21:39:30 +0200508 let buf = RunVimInTerminal('-S XTest_timermessage', #{rows: 6})
509 call term_sendkeys(buf, "l")
510 call term_wait(buf)
511 " should not get a hit-enter prompt
512 call WaitForAssert({-> assert_match('xxxxxxxxxxx', term_getline(buf, 6))})
513
514 call StopVimInTerminal(buf)
Bram Moolenaar9f284162021-04-22 21:39:30 +0200515endfunc
516
Bram Moolenaare615db02022-01-20 21:00:54 +0000517func Test_timer_using_win_execute_undo_sync()
Bram Moolenaar865bf2e2022-09-24 17:44:22 +0100518 " FIXME: why does this fail only on MacOS M1?
519 CheckNotMacM1
520
Bram Moolenaare615db02022-01-20 21:00:54 +0000521 let bufnr1 = bufnr()
522 new
523 let g:bufnr2 = bufnr()
524 let g:winid = win_getid()
525 exe "buffer " .. bufnr1
526 wincmd w
527 call setline(1, ['test'])
528 autocmd InsertEnter * call timer_start(100, { -> win_execute(g:winid, 'buffer ' .. g:bufnr2) })
529 call timer_start(200, { -> feedkeys("\<CR>bbbb\<Esc>") })
530 call feedkeys("Oaaaa", 'x!t')
531 " will hang here until the second timer fires
532 call assert_equal(['aaaa', 'bbbb', 'test'], getline(1, '$'))
533 undo
534 call assert_equal(['test'], getline(1, '$'))
535
536 bwipe!
537 bwipe!
538 unlet g:winid
539 unlet g:bufnr2
540 au! InsertEnter
541endfunc
542
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100543
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200544" vim: shiftwidth=2 sts=2 expandtab