blob: d2f621c26d3d0957fb4325a1b672bf1807a090f3 [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')
Yee Cheng Chine70587d2025-02-13 20:55:45 +010047 " Timer has an internal 1ms allowance in calculating due time, so it's
48 " possible for each timer to undershoot by 1ms resulting in only 49*3=147
49 " ms elapsed. Additionally we started the timer before we called
50 " WaitFor(), so the reported time could be a couple more ms below 147.
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010051 if has('mac')
Yee Cheng Chine70587d2025-02-13 20:55:45 +010052 " Mac in CI can be slow.
53 call assert_inrange(145, 400, slept)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010054 else
Yee Cheng Chine70587d2025-02-13 20:55:45 +010055 call assert_inrange(145, 250, slept)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010056 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020057 else
58 call assert_inrange(80, 200, slept)
59 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010060endfunc
61
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020062func Test_timer_repeat_many()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020063 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010064 let timer = timer_start(50, 'MyHandler', {'repeat': -1})
65 sleep 200m
66 call timer_stop(timer)
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +010067 " Mac on Travis can be slow.
68 if has('mac')
69 call assert_inrange(1, 5, g:val)
70 else
71 call assert_inrange(2, 5, g:val)
72 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010073endfunc
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010074
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020075func Test_timer_with_partial_callback()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020076 let g:val = 0
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020077 let meow = {'one': 1}
78 function meow.bite(...)
79 let g:val += self.one
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010080 endfunction
81
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020082 call timer_start(50, meow.bite)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020083 let slept = WaitFor('g:val == 1')
84 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020085 if has('reltime')
Bram Moolenaar5c463a22019-12-27 17:14:33 +010086 " Mac on Travis can be slow.
87 if has('mac')
88 call assert_inrange(49, 180, slept)
89 else
90 call assert_inrange(49, 130, slept)
91 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020092 else
93 call assert_inrange(20, 100, slept)
94 endif
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010095endfunc
Bram Moolenaare3188e22016-05-31 21:13:04 +020096
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +020097func Test_timer_retain_partial()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020098 call timer_start(50, function('MyHandlerWithLists', [['a']]))
Bram Moolenaare3188e22016-05-31 21:13:04 +020099 call test_garbagecollect_now()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200100 sleep 100m
Bram Moolenaare3188e22016-05-31 21:13:04 +0200101endfunc
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200102
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200103func Test_timer_info()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200104 let id = timer_start(1000, 'MyHandler')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200105 let info = id->timer_info()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200106 call assert_equal(id, info[0]['id'])
107 call assert_equal(1000, info[0]['time'])
108 call assert_true(info[0]['remaining'] > 500)
109 call assert_true(info[0]['remaining'] <= 1000)
110 call assert_equal(1, info[0]['repeat'])
111 call assert_equal("function('MyHandler')", string(info[0]['callback']))
112
113 let found = 0
114 for info in timer_info()
115 if info['id'] == id
116 let found += 1
117 endif
118 endfor
119 call assert_equal(1, found)
120
121 call timer_stop(id)
122 call assert_equal([], timer_info(id))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100123
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100124 call assert_fails('call timer_info("abc")', 'E1210:')
Bram Moolenaar95b2dd02021-12-09 18:42:57 +0000125
126 " check repeat count inside the callback
127 let g:timer_repeat = []
128 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 +0000129 call WaitForAssert({-> assert_equal([2, 1, 0], g:timer_repeat)})
Bram Moolenaar95b2dd02021-12-09 18:42:57 +0000130 unlet g:timer_repeat
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200131endfunc
132
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200133func Test_timer_stopall()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200134 let id1 = timer_start(1000, 'MyHandler')
135 let id2 = timer_start(2000, 'MyHandler')
136 let info = timer_info()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100137 " count one for the TestTimeout() timer
138 call assert_equal(3, len(info))
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200139
140 call timer_stopall()
141 let info = timer_info()
142 call assert_equal(0, len(info))
143endfunc
144
Bram Moolenaarcf3d0ea2022-10-07 11:20:29 +0100145def Test_timer_stopall_with_popup()
146 # Create a popup that times out after ten seconds.
147 # Another timer will fire in half a second and close it early after stopping
148 # all timers.
149 var pop = popup_create('Popup', {time: 10000})
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100150 var tmr = timer_start(100, (_) => {
Bram Moolenaarcf3d0ea2022-10-07 11:20:29 +0100151 timer_stopall()
152 popup_clear()
153 })
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100154 sleep 100m
155 g:WaitForAssert(() => assert_equal([], popup_list()), 1000)
Bram Moolenaarcf3d0ea2022-10-07 11:20:29 +0100156 assert_equal([], timer_info(tmr))
Bram Moolenaarcf3d0ea2022-10-07 11:20:29 +0100157enddef
158
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200159func Test_timer_paused()
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200160 let g:val = 0
161
162 let id = timer_start(50, 'MyHandler')
163 let info = timer_info(id)
164 call assert_equal(0, info[0]['paused'])
165
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200166 eval id->timer_pause(1)
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200167 let info = timer_info(id)
168 call assert_equal(1, info[0]['paused'])
169 sleep 100m
170 call assert_equal(0, g:val)
171
172 call timer_pause(id, 0)
173 let info = timer_info(id)
174 call assert_equal(0, info[0]['paused'])
175
176 let slept = WaitFor('g:val == 1')
177 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200178 if has('reltime')
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100179 if has('mac')
180 " The travis Mac machines appear to be very busy.
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +0100181 call assert_inrange(0, 90, slept)
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100182 else
183 call assert_inrange(0, 30, slept)
184 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200185 else
186 call assert_inrange(0, 10, slept)
187 endif
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100188
189 call assert_fails('call timer_pause("abc", 1)', 'E39:')
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200190endfunc
191
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200192func StopMyself(timer)
193 let g:called += 1
194 if g:called == 2
195 call timer_stop(a:timer)
196 endif
197endfunc
198
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200199func Test_timer_delete_myself()
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200200 let g:called = 0
201 let t = timer_start(10, 'StopMyself', {'repeat': -1})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200202 call WaitForAssert({-> assert_equal(2, g:called)})
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200203 call assert_equal(2, g:called)
204 call assert_equal([], timer_info(t))
205endfunc
206
Bram Moolenaar75537a92016-09-05 22:45:28 +0200207func StopTimer1(timer)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200208 let g:timer2 = 10->timer_start('StopTimer2')
Bram Moolenaar75537a92016-09-05 22:45:28 +0200209 " avoid maxfuncdepth error
210 call timer_pause(g:timer1, 1)
Bram Moolenaar413c04e2019-08-16 22:29:18 +0200211 sleep 20m
Bram Moolenaar75537a92016-09-05 22:45:28 +0200212endfunc
213
214func StopTimer2(timer)
215 call timer_stop(g:timer1)
216endfunc
217
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200218func Test_timer_stop_in_callback()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100219 call assert_equal(1, len(timer_info()))
Bram Moolenaar75537a92016-09-05 22:45:28 +0200220 let g:timer1 = timer_start(10, 'StopTimer1')
Bram Moolenaar315244d2019-08-17 13:18:16 +0200221 let slept = 0
222 for i in range(10)
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100223 if len(timer_info()) == 1
Bram Moolenaar315244d2019-08-17 13:18:16 +0200224 break
225 endif
226 sleep 10m
227 let slept += 10
228 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100229 if slept == 100
230 call assert_equal(1, len(timer_info()))
231 else
232 " This should take only 30 msec, but on Mac it's often longer
233 call assert_inrange(0, 50, slept)
234 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200235endfunc
236
237func StopTimerAll(timer)
238 call timer_stopall()
239endfunc
240
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200241func Test_timer_stop_all_in_callback()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100242 " One timer is for TestTimeout()
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200243 call assert_equal(1, len(timer_info()))
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100244 call timer_start(10, 'StopTimerAll')
245 call assert_equal(2, len(timer_info()))
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200246 let slept = 0
247 for i in range(10)
248 if len(timer_info()) == 0
249 break
250 endif
251 sleep 10m
252 let slept += 10
253 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100254 if slept == 100
255 call assert_equal(0, len(timer_info()))
256 else
257 call assert_inrange(0, 30, slept)
258 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200259endfunc
260
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200261func FeedkeysCb(timer)
262 call feedkeys("hello\<CR>", 'nt')
263endfunc
264
265func InputCb(timer)
266 call timer_start(10, 'FeedkeysCb')
267 let g:val = input('?')
268 call Resume()
269endfunc
270
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200271func Test_timer_input_in_timer()
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200272 let g:val = ''
273 call timer_start(10, 'InputCb')
274 call Standby(1000)
275 call assert_equal('hello', g:val)
276endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200277
Bram Moolenaarc577d812017-07-08 22:37:34 +0200278func FuncWithError(timer)
279 let g:call_count += 1
280 if g:call_count == 4
281 return
282 endif
283 doesnotexist
284endfunc
285
286func Test_timer_errors()
287 let g:call_count = 0
288 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
289 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200290 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200291 sleep 50m
292 call assert_equal(3, g:call_count)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100293
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100294 call assert_fails('call timer_start(100, "MyHandler", "abc")', 'E1206:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100295 call assert_fails('call timer_start(100, [])', 'E921:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100296 call assert_fails('call timer_stop("abc")', 'E1210:')
Bram Moolenaarc577d812017-07-08 22:37:34 +0200297endfunc
298
Bram Moolenaare723c422017-09-06 23:40:10 +0200299func FuncWithCaughtError(timer)
300 let g:call_count += 1
301 try
302 doesnotexist
303 catch
304 " nop
305 endtry
306endfunc
307
308func Test_timer_catch_error()
309 let g:call_count = 0
310 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
311 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200312 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200313 sleep 50m
314 call assert_equal(4, g:call_count)
315endfunc
316
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200317func FeedAndPeek(timer)
318 call test_feedinput('a')
319 call getchar(1)
320endfunc
321
322func Interrupt(timer)
Bram Moolenaarce90e362019-09-08 18:58:44 +0200323 eval "\<C-C>"->test_feedinput()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200324endfunc
325
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200326func Test_timer_peek_and_get_char()
zeertzjqeb273cd2022-07-01 19:11:23 +0100327 if !has('unix') && !has('gui_running')
328 throw 'Skipped: cannot feed low-level input'
329 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200330
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200331 call timer_start(0, 'FeedAndPeek')
332 let intr = timer_start(100, 'Interrupt')
333 let c = getchar()
334 call assert_equal(char2nr('a'), c)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200335 eval intr->timer_stop()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200336endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200337
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200338func Test_timer_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100339 if has('win32') && !has('gui_running')
zeertzjqeb273cd2022-07-01 19:11:23 +0100340 throw 'Skipped: cannot feed low-level input'
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100341 endif
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100342 CheckFunction reltimefloat
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100343
Bram Moolenaar50948e42019-01-29 20:36:56 +0100344 " Measure the elapsed time to avoid a hang when it fails.
345 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100346 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100347 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100348 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100349 let c = getchar(0)
350 sleep 10m
351 endwhile
352 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100353 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100354endfunc
355
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200356func Test_timer_ex_mode()
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200357 " Function with an empty line.
358 func Foo(...)
359
360 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100361 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200362 " This used to throw error E749.
363 exe "normal Qsleep 100m\rvi\r"
364 call timer_stop(timer)
365endfunc
366
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200367func Test_timer_restore_count()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200368 CheckRunVimInTerminal
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200369 " Check that v:count is saved and restored, not changed by a timer.
370 call writefile([
371 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
372 \ 'func Doit(id)',
373 \ ' normal 3j',
374 \ 'endfunc',
375 \ 'call timer_start(100, "Doit")',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100376 \ ], 'Xtrcscript', 'D')
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200377 call writefile([
378 \ '1-1234',
379 \ '2-1234',
380 \ '3-1234',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100381 \ ], 'Xtrctext', 'D')
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200382 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
383
384 " Wait for the timer to move the cursor to the third line.
385 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
386 call assert_equal(1, term_getcursor(buf)[1])
387 " Now check that v:count has not been set to 3
388 call term_sendkeys(buf, 'L')
389 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
390
391 call StopVimInTerminal(buf)
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200392endfunc
393
Bram Moolenaaradc67142019-06-22 01:40:42 +0200394" Test that the garbage collector isn't triggered if a timer callback invokes
395" vgetc().
zeertzjqbb404f52022-07-23 06:25:29 +0100396func Test_nocatch_timer_garbage_collect()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100397 " FIXME: why does this fail only on MacOS M1?
Bram Moolenaar94722c52023-01-28 19:19:03 +0000398 try
Bram Moolenaar140f6d02022-09-24 14:49:07 +0100399 CheckNotMacM1
400 catch /Skipped/
401 let g:skipped_reason = v:exception
402 return
403 endtry
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100404
Bram Moolenaaradc67142019-06-22 01:40:42 +0200405 " 'uptimetime. must be bigger than the timer timeout
406 set ut=200
407 call test_garbagecollect_soon()
408 call test_override('no_wait_return', 0)
409 func CauseAnError(id)
410 " This will show an error and wait for Enter.
411 let a = {'foo', 'bar'}
412 endfunc
413 func FeedChar(id)
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100414 call feedkeys(":\<CR>", 't')
Bram Moolenaaradc67142019-06-22 01:40:42 +0200415 endfunc
416 call timer_start(300, 'FeedChar')
417 call timer_start(100, 'CauseAnError')
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100418 let x = getchar() " wait for error in timer
419 let x = getchar(0) " read any remaining chars
420 let x = getchar(0)
Bram Moolenaaradc67142019-06-22 01:40:42 +0200421
422 set ut&
423 call test_override('no_wait_return', 1)
424 delfunc CauseAnError
425 delfunc FeedChar
426endfunc
427
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200428func Test_timer_error_in_timer_callback()
Bram Moolenaar0d702022019-07-04 14:20:41 +0200429 if !has('terminal') || (has('win32') && has('gui_running'))
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200430 throw 'Skipped: cannot run Vim in a terminal window'
431 endif
432
433 let lines =<< trim [CODE]
434 func Func(timer)
435 " fail to create list
436 let x = [
437 endfunc
438 set updatetime=50
439 call timer_start(1, 'Func')
440 [CODE]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100441 call writefile(lines, 'Xtest.vim', 'D')
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200442
Bram Moolenaar0d702022019-07-04 14:20:41 +0200443 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200444 let job = term_getjob(buf)
445 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
446
447 " GC must not run during timer callback, which can make Vim crash.
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200448 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200449 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200450 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200451 call assert_equal('run', job_status(job))
452
453 call term_sendkeys(buf, ":qall!\<CR>")
454 call WaitFor({-> job_status(job) ==# 'dead'})
455 if has('unix')
456 call assert_equal('', job_info(job).termsig)
457 endif
458
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200459 exe buf .. 'bwipe!'
460endfunc
461
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100462" Test for garbage collection when a timer is still running
463func Test_timer_garbage_collect()
464 let timer = timer_start(1000, function('MyHandler'), {'repeat' : 10})
465 call test_garbagecollect_now()
466 let l = timer_info(timer)
467 call assert_equal(function('MyHandler'), l[0].callback)
468 call timer_stop(timer)
469endfunc
470
Bram Moolenaar14e579092020-03-07 16:59:25 +0100471func Test_timer_invalid_callback()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200472 call assert_fails('call timer_start(0, "0")', 'E921:')
Bram Moolenaar14e579092020-03-07 16:59:25 +0100473endfunc
474
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200475func Test_timer_changing_function_list()
476 CheckRunVimInTerminal
477
478 " Create a large number of functions. Should get the "more" prompt.
479 " The typing "G" triggers the timer, which changes the function table.
480 let lines =<< trim END
481 for func in map(range(1,99), "'Func' .. v:val")
482 exe "func " .. func .. "()"
483 endfunc
484 endfor
485 au CmdlineLeave : call timer_start(0, {-> 0})
486 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100487 call writefile(lines, 'XTest_timerchange', 'D')
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200488 let buf = RunVimInTerminal('-S XTest_timerchange', #{rows: 10})
489 call term_sendkeys(buf, ":fu\<CR>")
490 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 10))})
491 call term_sendkeys(buf, "G")
Bram Moolenaare2e40752020-09-04 21:18:46 +0200492 call WaitForAssert({-> assert_match('E454:', term_getline(buf, 9))})
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200493 call term_sendkeys(buf, "\<Esc>")
494
495 call StopVimInTerminal(buf)
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200496endfunc
497
Bram Moolenaar9f284162021-04-22 21:39:30 +0200498func Test_timer_outputting_message()
499 CheckRunVimInTerminal
500
501 let lines =<< trim END
502 vim9script
503 setline(1, 'some text')
504 set showcmd ut=2000 cmdheight=1
505 timer_start(0, (_) => {
506 echon repeat('x', &columns - 11)
507 })
508 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100509 call writefile(lines, 'XTest_timermessage', 'D')
Bram Moolenaar9f284162021-04-22 21:39:30 +0200510 let buf = RunVimInTerminal('-S XTest_timermessage', #{rows: 6})
511 call term_sendkeys(buf, "l")
512 call term_wait(buf)
513 " should not get a hit-enter prompt
514 call WaitForAssert({-> assert_match('xxxxxxxxxxx', term_getline(buf, 6))})
515
516 call StopVimInTerminal(buf)
Bram Moolenaar9f284162021-04-22 21:39:30 +0200517endfunc
518
Bram Moolenaare615db02022-01-20 21:00:54 +0000519func Test_timer_using_win_execute_undo_sync()
Bram Moolenaar865bf2e2022-09-24 17:44:22 +0100520 " FIXME: why does this fail only on MacOS M1?
521 CheckNotMacM1
522
Bram Moolenaare615db02022-01-20 21:00:54 +0000523 let bufnr1 = bufnr()
524 new
525 let g:bufnr2 = bufnr()
526 let g:winid = win_getid()
527 exe "buffer " .. bufnr1
528 wincmd w
529 call setline(1, ['test'])
530 autocmd InsertEnter * call timer_start(100, { -> win_execute(g:winid, 'buffer ' .. g:bufnr2) })
531 call timer_start(200, { -> feedkeys("\<CR>bbbb\<Esc>") })
532 call feedkeys("Oaaaa", 'x!t')
533 " will hang here until the second timer fires
534 call assert_equal(['aaaa', 'bbbb', 'test'], getline(1, '$'))
535 undo
536 call assert_equal(['test'], getline(1, '$'))
537
538 bwipe!
539 bwipe!
540 unlet g:winid
541 unlet g:bufnr2
542 au! InsertEnter
543endfunc
544
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100545
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200546" vim: shiftwidth=2 sts=2 expandtab