blob: 4bde75faab9f0505dee3ec5b9e2b55906f034ae4 [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 Moolenaar29b4c512023-05-30 15:34:50 +010010func SetUp()
11 " The tests here use timers, thus are sensitive to timing.
12 let g:test_is_flaky = 1
13endfunc
14
Bram Moolenaar975b5272016-03-15 23:10:59 +010015func MyHandler(timer)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020016 let g:val += 1
Bram Moolenaar975b5272016-03-15 23:10:59 +010017endfunc
18
Bram Moolenaare3188e22016-05-31 21:13:04 +020019func MyHandlerWithLists(lists, timer)
20 let x = string(a:lists)
21endfunc
22
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020023func Test_timer_oneshot()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020024 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010025 let timer = timer_start(50, 'MyHandler')
Bram Moolenaarb73598e2016-08-07 18:22:53 +020026 let slept = WaitFor('g:val == 1')
27 call assert_equal(1, g:val)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010028 if has('mac')
Bram Moolenaar818fed72019-12-25 15:47:14 +010029 " Mac on Travis can be very slow.
30 let limit = 180
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020031 else
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010032 let limit = 100
33 endif
34 if has('reltime')
35 call assert_inrange(49, limit, slept)
36 else
37 call assert_inrange(20, limit, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020038 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010039endfunc
40
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020041func Test_timer_repeat_three()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020042 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010043 let timer = timer_start(50, 'MyHandler', {'repeat': 3})
Bram Moolenaarb73598e2016-08-07 18:22:53 +020044 let slept = WaitFor('g:val == 3')
45 call assert_equal(3, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020046 if has('reltime')
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010047 if has('mac')
48 " Mac on Travis can be slow.
49 call assert_inrange(149, 400, slept)
50 else
51 call assert_inrange(149, 250, slept)
52 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020053 else
54 call assert_inrange(80, 200, slept)
55 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010056endfunc
57
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020058func Test_timer_repeat_many()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020059 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010060 let timer = timer_start(50, 'MyHandler', {'repeat': -1})
61 sleep 200m
62 call timer_stop(timer)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010063 " Mac on Travis can be slow.
64 if has('mac')
65 call assert_inrange(1, 5, g:val)
66 else
67 call assert_inrange(2, 5, g:val)
68 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010069endfunc
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010070
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020071func Test_timer_with_partial_callback()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020072 let g:val = 0
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020073 let meow = {'one': 1}
74 function meow.bite(...)
75 let g:val += self.one
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010076 endfunction
77
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020078 call timer_start(50, meow.bite)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020079 let slept = WaitFor('g:val == 1')
80 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020081 if has('reltime')
Bram Moolenaar5c463a22019-12-27 17:14:33 +010082 " Mac on Travis can be slow.
83 if has('mac')
84 call assert_inrange(49, 180, slept)
85 else
86 call assert_inrange(49, 130, slept)
87 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020088 else
89 call assert_inrange(20, 100, slept)
90 endif
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010091endfunc
Bram Moolenaare3188e22016-05-31 21:13:04 +020092
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020093func Test_timer_retain_partial()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020094 call timer_start(50, function('MyHandlerWithLists', [['a']]))
Bram Moolenaare3188e22016-05-31 21:13:04 +020095 call test_garbagecollect_now()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020096 sleep 100m
Bram Moolenaare3188e22016-05-31 21:13:04 +020097endfunc
Bram Moolenaarb73598e2016-08-07 18:22:53 +020098
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020099func Test_timer_info()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200100 let id = timer_start(1000, 'MyHandler')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200101 let info = id->timer_info()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200102 call assert_equal(id, info[0]['id'])
103 call assert_equal(1000, info[0]['time'])
104 call assert_true(info[0]['remaining'] > 500)
105 call assert_true(info[0]['remaining'] <= 1000)
106 call assert_equal(1, info[0]['repeat'])
107 call assert_equal("function('MyHandler')", string(info[0]['callback']))
108
109 let found = 0
110 for info in timer_info()
111 if info['id'] == id
112 let found += 1
113 endif
114 endfor
115 call assert_equal(1, found)
116
117 call timer_stop(id)
118 call assert_equal([], timer_info(id))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100119
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100120 call assert_fails('call timer_info("abc")', 'E1210:')
Bram Moolenaar95b2dd02021-12-09 18:42:57 +0000121
122 " check repeat count inside the callback
123 let g:timer_repeat = []
124 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 +0000125 call WaitForAssert({-> assert_equal([2, 1, 0], g:timer_repeat)})
Bram Moolenaar95b2dd02021-12-09 18:42:57 +0000126 unlet g:timer_repeat
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200127endfunc
128
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200129func Test_timer_stopall()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200130 let id1 = timer_start(1000, 'MyHandler')
131 let id2 = timer_start(2000, 'MyHandler')
132 let info = timer_info()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100133 " count one for the TestTimeout() timer
134 call assert_equal(3, len(info))
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200135
136 call timer_stopall()
137 let info = timer_info()
138 call assert_equal(0, len(info))
139endfunc
140
Bram Moolenaarcf3d0ea2022-10-07 11:20:29 +0100141def Test_timer_stopall_with_popup()
142 # Create a popup that times out after ten seconds.
143 # Another timer will fire in half a second and close it early after stopping
144 # all timers.
145 var pop = popup_create('Popup', {time: 10000})
146 var tmr = timer_start(500, (_) => {
147 timer_stopall()
148 popup_clear()
149 })
150 sleep 1
151 assert_equal([], timer_info(tmr))
152 assert_equal([], popup_list())
153enddef
154
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200155func Test_timer_paused()
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 Moolenaar0056ca72022-09-23 21:26:39 +0100215 call assert_equal(1, len(timer_info()))
Bram Moolenaar75537a92016-09-05 22:45:28 +0200216 let g:timer1 = timer_start(10, 'StopTimer1')
Bram Moolenaar315244d2019-08-17 13:18:16 +0200217 let slept = 0
218 for i in range(10)
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100219 if len(timer_info()) == 1
Bram Moolenaar315244d2019-08-17 13:18:16 +0200220 break
221 endif
222 sleep 10m
223 let slept += 10
224 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100225 if slept == 100
226 call assert_equal(1, len(timer_info()))
227 else
228 " This should take only 30 msec, but on Mac it's often longer
229 call assert_inrange(0, 50, slept)
230 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200231endfunc
232
233func StopTimerAll(timer)
234 call timer_stopall()
235endfunc
236
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200237func Test_timer_stop_all_in_callback()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100238 " One timer is for TestTimeout()
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200239 call assert_equal(1, len(timer_info()))
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100240 call timer_start(10, 'StopTimerAll')
241 call assert_equal(2, len(timer_info()))
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200242 let slept = 0
243 for i in range(10)
244 if len(timer_info()) == 0
245 break
246 endif
247 sleep 10m
248 let slept += 10
249 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100250 if slept == 100
251 call assert_equal(0, len(timer_info()))
252 else
253 call assert_inrange(0, 30, slept)
254 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200255endfunc
256
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200257func FeedkeysCb(timer)
258 call feedkeys("hello\<CR>", 'nt')
259endfunc
260
261func InputCb(timer)
262 call timer_start(10, 'FeedkeysCb')
263 let g:val = input('?')
264 call Resume()
265endfunc
266
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200267func Test_timer_input_in_timer()
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200268 let g:val = ''
269 call timer_start(10, 'InputCb')
270 call Standby(1000)
271 call assert_equal('hello', g:val)
272endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200273
Bram Moolenaarc577d812017-07-08 22:37:34 +0200274func FuncWithError(timer)
275 let g:call_count += 1
276 if g:call_count == 4
277 return
278 endif
279 doesnotexist
280endfunc
281
282func Test_timer_errors()
283 let g:call_count = 0
284 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
285 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200286 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200287 sleep 50m
288 call assert_equal(3, g:call_count)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100289
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100290 call assert_fails('call timer_start(100, "MyHandler", "abc")', 'E1206:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100291 call assert_fails('call timer_start(100, [])', 'E921:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100292 call assert_fails('call timer_stop("abc")', 'E1210:')
Bram Moolenaarc577d812017-07-08 22:37:34 +0200293endfunc
294
Bram Moolenaare723c422017-09-06 23:40:10 +0200295func FuncWithCaughtError(timer)
296 let g:call_count += 1
297 try
298 doesnotexist
299 catch
300 " nop
301 endtry
302endfunc
303
304func Test_timer_catch_error()
305 let g:call_count = 0
306 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
307 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200308 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200309 sleep 50m
310 call assert_equal(4, g:call_count)
311endfunc
312
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200313func FeedAndPeek(timer)
314 call test_feedinput('a')
315 call getchar(1)
316endfunc
317
318func Interrupt(timer)
Bram Moolenaarce90e362019-09-08 18:58:44 +0200319 eval "\<C-C>"->test_feedinput()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200320endfunc
321
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200322func Test_timer_peek_and_get_char()
zeertzjqeb273cd2022-07-01 19:11:23 +0100323 if !has('unix') && !has('gui_running')
324 throw 'Skipped: cannot feed low-level input'
325 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200326
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200327 call timer_start(0, 'FeedAndPeek')
328 let intr = timer_start(100, 'Interrupt')
329 let c = getchar()
330 call assert_equal(char2nr('a'), c)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200331 eval intr->timer_stop()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200332endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200333
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200334func Test_timer_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100335 if has('win32') && !has('gui_running')
zeertzjqeb273cd2022-07-01 19:11:23 +0100336 throw 'Skipped: cannot feed low-level input'
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100337 endif
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100338 CheckFunction reltimefloat
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100339
Bram Moolenaar50948e42019-01-29 20:36:56 +0100340 " Measure the elapsed time to avoid a hang when it fails.
341 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100342 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100343 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100344 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100345 let c = getchar(0)
346 sleep 10m
347 endwhile
348 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100349 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100350endfunc
351
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200352func Test_timer_ex_mode()
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200353 " Function with an empty line.
354 func Foo(...)
355
356 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100357 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200358 " This used to throw error E749.
359 exe "normal Qsleep 100m\rvi\r"
360 call timer_stop(timer)
361endfunc
362
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200363func Test_timer_restore_count()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200364 CheckRunVimInTerminal
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200365 " Check that v:count is saved and restored, not changed by a timer.
366 call writefile([
367 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
368 \ 'func Doit(id)',
369 \ ' normal 3j',
370 \ 'endfunc',
371 \ 'call timer_start(100, "Doit")',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100372 \ ], 'Xtrcscript', 'D')
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200373 call writefile([
374 \ '1-1234',
375 \ '2-1234',
376 \ '3-1234',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100377 \ ], 'Xtrctext', 'D')
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200378 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
379
380 " Wait for the timer to move the cursor to the third line.
381 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
382 call assert_equal(1, term_getcursor(buf)[1])
383 " Now check that v:count has not been set to 3
384 call term_sendkeys(buf, 'L')
385 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
386
387 call StopVimInTerminal(buf)
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200388endfunc
389
Bram Moolenaaradc67142019-06-22 01:40:42 +0200390" Test that the garbage collector isn't triggered if a timer callback invokes
391" vgetc().
zeertzjqbb404f52022-07-23 06:25:29 +0100392func Test_nocatch_timer_garbage_collect()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100393 " FIXME: why does this fail only on MacOS M1?
Bram Moolenaar94722c52023-01-28 19:19:03 +0000394 try
Bram Moolenaar140f6d02022-09-24 14:49:07 +0100395 CheckNotMacM1
396 catch /Skipped/
397 let g:skipped_reason = v:exception
398 return
399 endtry
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100400
Bram Moolenaaradc67142019-06-22 01:40:42 +0200401 " 'uptimetime. must be bigger than the timer timeout
402 set ut=200
403 call test_garbagecollect_soon()
404 call test_override('no_wait_return', 0)
405 func CauseAnError(id)
406 " This will show an error and wait for Enter.
407 let a = {'foo', 'bar'}
408 endfunc
409 func FeedChar(id)
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100410 call feedkeys(":\<CR>", 't')
Bram Moolenaaradc67142019-06-22 01:40:42 +0200411 endfunc
412 call timer_start(300, 'FeedChar')
413 call timer_start(100, 'CauseAnError')
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100414 let x = getchar() " wait for error in timer
415 let x = getchar(0) " read any remaining chars
416 let x = getchar(0)
Bram Moolenaaradc67142019-06-22 01:40:42 +0200417
418 set ut&
419 call test_override('no_wait_return', 1)
420 delfunc CauseAnError
421 delfunc FeedChar
422endfunc
423
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200424func Test_timer_error_in_timer_callback()
Bram Moolenaar0d702022019-07-04 14:20:41 +0200425 if !has('terminal') || (has('win32') && has('gui_running'))
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200426 throw 'Skipped: cannot run Vim in a terminal window'
427 endif
428
429 let lines =<< trim [CODE]
430 func Func(timer)
431 " fail to create list
432 let x = [
433 endfunc
434 set updatetime=50
435 call timer_start(1, 'Func')
436 [CODE]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100437 call writefile(lines, 'Xtest.vim', 'D')
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200438
Bram Moolenaar0d702022019-07-04 14:20:41 +0200439 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200440 let job = term_getjob(buf)
441 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
442
443 " GC must not run during timer callback, which can make Vim crash.
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200444 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200445 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200446 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200447 call assert_equal('run', job_status(job))
448
449 call term_sendkeys(buf, ":qall!\<CR>")
450 call WaitFor({-> job_status(job) ==# 'dead'})
451 if has('unix')
452 call assert_equal('', job_info(job).termsig)
453 endif
454
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200455 exe buf .. 'bwipe!'
456endfunc
457
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100458" Test for garbage collection when a timer is still running
459func Test_timer_garbage_collect()
460 let timer = timer_start(1000, function('MyHandler'), {'repeat' : 10})
461 call test_garbagecollect_now()
462 let l = timer_info(timer)
463 call assert_equal(function('MyHandler'), l[0].callback)
464 call timer_stop(timer)
465endfunc
466
Bram Moolenaar14e579092020-03-07 16:59:25 +0100467func Test_timer_invalid_callback()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200468 call assert_fails('call timer_start(0, "0")', 'E921:')
Bram Moolenaar14e579092020-03-07 16:59:25 +0100469endfunc
470
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200471func Test_timer_changing_function_list()
472 CheckRunVimInTerminal
473
474 " Create a large number of functions. Should get the "more" prompt.
475 " The typing "G" triggers the timer, which changes the function table.
476 let lines =<< trim END
477 for func in map(range(1,99), "'Func' .. v:val")
478 exe "func " .. func .. "()"
479 endfunc
480 endfor
481 au CmdlineLeave : call timer_start(0, {-> 0})
482 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100483 call writefile(lines, 'XTest_timerchange', 'D')
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200484 let buf = RunVimInTerminal('-S XTest_timerchange', #{rows: 10})
485 call term_sendkeys(buf, ":fu\<CR>")
486 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 10))})
487 call term_sendkeys(buf, "G")
Bram Moolenaare2e40752020-09-04 21:18:46 +0200488 call WaitForAssert({-> assert_match('E454:', term_getline(buf, 9))})
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200489 call term_sendkeys(buf, "\<Esc>")
490
491 call StopVimInTerminal(buf)
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200492endfunc
493
Bram Moolenaar9f284162021-04-22 21:39:30 +0200494func Test_timer_outputting_message()
495 CheckRunVimInTerminal
496
497 let lines =<< trim END
498 vim9script
499 setline(1, 'some text')
500 set showcmd ut=2000 cmdheight=1
501 timer_start(0, (_) => {
502 echon repeat('x', &columns - 11)
503 })
504 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100505 call writefile(lines, 'XTest_timermessage', 'D')
Bram Moolenaar9f284162021-04-22 21:39:30 +0200506 let buf = RunVimInTerminal('-S XTest_timermessage', #{rows: 6})
507 call term_sendkeys(buf, "l")
508 call term_wait(buf)
509 " should not get a hit-enter prompt
510 call WaitForAssert({-> assert_match('xxxxxxxxxxx', term_getline(buf, 6))})
511
512 call StopVimInTerminal(buf)
Bram Moolenaar9f284162021-04-22 21:39:30 +0200513endfunc
514
Bram Moolenaare615db02022-01-20 21:00:54 +0000515func Test_timer_using_win_execute_undo_sync()
Bram Moolenaar865bf2e2022-09-24 17:44:22 +0100516 " FIXME: why does this fail only on MacOS M1?
517 CheckNotMacM1
518
Bram Moolenaare615db02022-01-20 21:00:54 +0000519 let bufnr1 = bufnr()
520 new
521 let g:bufnr2 = bufnr()
522 let g:winid = win_getid()
523 exe "buffer " .. bufnr1
524 wincmd w
525 call setline(1, ['test'])
526 autocmd InsertEnter * call timer_start(100, { -> win_execute(g:winid, 'buffer ' .. g:bufnr2) })
527 call timer_start(200, { -> feedkeys("\<CR>bbbb\<Esc>") })
528 call feedkeys("Oaaaa", 'x!t')
529 " will hang here until the second timer fires
530 call assert_equal(['aaaa', 'bbbb', 'test'], getline(1, '$'))
531 undo
532 call assert_equal(['test'], getline(1, '$'))
533
534 bwipe!
535 bwipe!
536 unlet g:winid
537 unlet g:bufnr2
538 au! InsertEnter
539endfunc
540
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100541
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200542" vim: shiftwidth=2 sts=2 expandtab