blob: f97808c1576829a353dad86a82fc6f88a9c08dc4 [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 Moolenaar9a2fddc2019-08-16 11:26:06 +0200140func Test_timer_paused()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100141 let g:test_is_flaky = 1
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200142 let g:val = 0
143
144 let id = timer_start(50, 'MyHandler')
145 let info = timer_info(id)
146 call assert_equal(0, info[0]['paused'])
147
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200148 eval id->timer_pause(1)
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200149 let info = timer_info(id)
150 call assert_equal(1, info[0]['paused'])
151 sleep 100m
152 call assert_equal(0, g:val)
153
154 call timer_pause(id, 0)
155 let info = timer_info(id)
156 call assert_equal(0, info[0]['paused'])
157
158 let slept = WaitFor('g:val == 1')
159 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200160 if has('reltime')
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100161 if has('mac')
162 " The travis Mac machines appear to be very busy.
Bram Moolenaarbc28e9f2019-12-18 20:10:23 +0100163 call assert_inrange(0, 90, slept)
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100164 else
165 call assert_inrange(0, 30, slept)
166 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200167 else
168 call assert_inrange(0, 10, slept)
169 endif
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100170
171 call assert_fails('call timer_pause("abc", 1)', 'E39:')
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200172endfunc
173
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200174func StopMyself(timer)
175 let g:called += 1
176 if g:called == 2
177 call timer_stop(a:timer)
178 endif
179endfunc
180
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200181func Test_timer_delete_myself()
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200182 let g:called = 0
183 let t = timer_start(10, 'StopMyself', {'repeat': -1})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200184 call WaitForAssert({-> assert_equal(2, g:called)})
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200185 call assert_equal(2, g:called)
186 call assert_equal([], timer_info(t))
187endfunc
188
Bram Moolenaar75537a92016-09-05 22:45:28 +0200189func StopTimer1(timer)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200190 let g:timer2 = 10->timer_start('StopTimer2')
Bram Moolenaar75537a92016-09-05 22:45:28 +0200191 " avoid maxfuncdepth error
192 call timer_pause(g:timer1, 1)
Bram Moolenaar413c04e2019-08-16 22:29:18 +0200193 sleep 20m
Bram Moolenaar75537a92016-09-05 22:45:28 +0200194endfunc
195
196func StopTimer2(timer)
197 call timer_stop(g:timer1)
198endfunc
199
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200200func Test_timer_stop_in_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100201 let g:test_is_flaky = 1
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100202 call assert_equal(1, len(timer_info()))
Bram Moolenaar75537a92016-09-05 22:45:28 +0200203 let g:timer1 = timer_start(10, 'StopTimer1')
Bram Moolenaar315244d2019-08-17 13:18:16 +0200204 let slept = 0
205 for i in range(10)
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100206 if len(timer_info()) == 1
Bram Moolenaar315244d2019-08-17 13:18:16 +0200207 break
208 endif
209 sleep 10m
210 let slept += 10
211 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100212 if slept == 100
213 call assert_equal(1, len(timer_info()))
214 else
215 " This should take only 30 msec, but on Mac it's often longer
216 call assert_inrange(0, 50, slept)
217 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200218endfunc
219
220func StopTimerAll(timer)
221 call timer_stopall()
222endfunc
223
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200224func Test_timer_stop_all_in_callback()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100225 let g:test_is_flaky = 1
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100226 " One timer is for TestTimeout()
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200227 call assert_equal(1, len(timer_info()))
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100228 call timer_start(10, 'StopTimerAll')
229 call assert_equal(2, len(timer_info()))
Bram Moolenaar427dddf2019-08-16 21:22:41 +0200230 let slept = 0
231 for i in range(10)
232 if len(timer_info()) == 0
233 break
234 endif
235 sleep 10m
236 let slept += 10
237 endfor
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100238 if slept == 100
239 call assert_equal(0, len(timer_info()))
240 else
241 call assert_inrange(0, 30, slept)
242 endif
Bram Moolenaar75537a92016-09-05 22:45:28 +0200243endfunc
244
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200245func FeedkeysCb(timer)
246 call feedkeys("hello\<CR>", 'nt')
247endfunc
248
249func InputCb(timer)
250 call timer_start(10, 'FeedkeysCb')
251 let g:val = input('?')
252 call Resume()
253endfunc
254
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200255func Test_timer_input_in_timer()
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200256 let g:val = ''
257 call timer_start(10, 'InputCb')
258 call Standby(1000)
259 call assert_equal('hello', g:val)
260endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200261
Bram Moolenaarc577d812017-07-08 22:37:34 +0200262func FuncWithError(timer)
263 let g:call_count += 1
264 if g:call_count == 4
265 return
266 endif
267 doesnotexist
268endfunc
269
270func Test_timer_errors()
271 let g:call_count = 0
272 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
273 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200274 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200275 sleep 50m
276 call assert_equal(3, g:call_count)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100277
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100278 call assert_fails('call timer_start(100, "MyHandler", "abc")', 'E1206:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100279 call assert_fails('call timer_start(100, [])', 'E921:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100280 call assert_fails('call timer_stop("abc")', 'E1210:')
Bram Moolenaarc577d812017-07-08 22:37:34 +0200281endfunc
282
Bram Moolenaare723c422017-09-06 23:40:10 +0200283func FuncWithCaughtError(timer)
284 let g:call_count += 1
285 try
286 doesnotexist
287 catch
288 " nop
289 endtry
290endfunc
291
292func Test_timer_catch_error()
293 let g:call_count = 0
294 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
295 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200296 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200297 sleep 50m
298 call assert_equal(4, g:call_count)
299endfunc
300
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200301func FeedAndPeek(timer)
302 call test_feedinput('a')
303 call getchar(1)
304endfunc
305
306func Interrupt(timer)
Bram Moolenaarce90e362019-09-08 18:58:44 +0200307 eval "\<C-C>"->test_feedinput()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200308endfunc
309
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200310func Test_timer_peek_and_get_char()
zeertzjqeb273cd2022-07-01 19:11:23 +0100311 if !has('unix') && !has('gui_running')
312 throw 'Skipped: cannot feed low-level input'
313 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200314
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200315 call timer_start(0, 'FeedAndPeek')
316 let intr = timer_start(100, 'Interrupt')
317 let c = getchar()
318 call assert_equal(char2nr('a'), c)
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200319 eval intr->timer_stop()
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200320endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200321
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200322func Test_timer_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100323 if has('win32') && !has('gui_running')
zeertzjqeb273cd2022-07-01 19:11:23 +0100324 throw 'Skipped: cannot feed low-level input'
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100325 endif
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100326 CheckFunction reltimefloat
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100327
Bram Moolenaar50948e42019-01-29 20:36:56 +0100328 " Measure the elapsed time to avoid a hang when it fails.
329 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100330 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100331 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100332 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100333 let c = getchar(0)
334 sleep 10m
335 endwhile
336 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100337 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100338endfunc
339
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200340func Test_timer_ex_mode()
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200341 " Function with an empty line.
342 func Foo(...)
343
344 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100345 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200346 " This used to throw error E749.
347 exe "normal Qsleep 100m\rvi\r"
348 call timer_stop(timer)
349endfunc
350
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200351func Test_timer_restore_count()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200352 CheckRunVimInTerminal
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200353 " Check that v:count is saved and restored, not changed by a timer.
354 call writefile([
355 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
356 \ 'func Doit(id)',
357 \ ' normal 3j',
358 \ 'endfunc',
359 \ 'call timer_start(100, "Doit")',
360 \ ], 'Xtrcscript')
361 call writefile([
362 \ '1-1234',
363 \ '2-1234',
364 \ '3-1234',
365 \ ], 'Xtrctext')
366 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
367
368 " Wait for the timer to move the cursor to the third line.
369 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
370 call assert_equal(1, term_getcursor(buf)[1])
371 " Now check that v:count has not been set to 3
372 call term_sendkeys(buf, 'L')
373 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
374
375 call StopVimInTerminal(buf)
376 call delete('Xtrcscript')
377 call delete('Xtrctext')
378endfunc
379
Bram Moolenaaradc67142019-06-22 01:40:42 +0200380" Test that the garbage collector isn't triggered if a timer callback invokes
381" vgetc().
zeertzjqbb404f52022-07-23 06:25:29 +0100382func Test_nocatch_timer_garbage_collect()
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100383 " FIXME: why does this fail only on MacOS M1?
Bram Moolenaar140f6d02022-09-24 14:49:07 +0100384 try
385 CheckNotMacM1
386 catch /Skipped/
387 let g:skipped_reason = v:exception
388 return
389 endtry
Bram Moolenaar0056ca72022-09-23 21:26:39 +0100390
Bram Moolenaaradc67142019-06-22 01:40:42 +0200391 " 'uptimetime. must be bigger than the timer timeout
392 set ut=200
393 call test_garbagecollect_soon()
394 call test_override('no_wait_return', 0)
395 func CauseAnError(id)
396 " This will show an error and wait for Enter.
397 let a = {'foo', 'bar'}
398 endfunc
399 func FeedChar(id)
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100400 call feedkeys(":\<CR>", 't')
Bram Moolenaaradc67142019-06-22 01:40:42 +0200401 endfunc
402 call timer_start(300, 'FeedChar')
403 call timer_start(100, 'CauseAnError')
Bram Moolenaar4ecf16b2022-09-23 18:22:21 +0100404 let x = getchar() " wait for error in timer
405 let x = getchar(0) " read any remaining chars
406 let x = getchar(0)
Bram Moolenaaradc67142019-06-22 01:40:42 +0200407
408 set ut&
409 call test_override('no_wait_return', 1)
410 delfunc CauseAnError
411 delfunc FeedChar
412endfunc
413
Bram Moolenaar9a2fddc2019-08-16 11:26:06 +0200414func Test_timer_error_in_timer_callback()
Bram Moolenaar0d702022019-07-04 14:20:41 +0200415 if !has('terminal') || (has('win32') && has('gui_running'))
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200416 throw 'Skipped: cannot run Vim in a terminal window'
417 endif
418
419 let lines =<< trim [CODE]
420 func Func(timer)
421 " fail to create list
422 let x = [
423 endfunc
424 set updatetime=50
425 call timer_start(1, 'Func')
426 [CODE]
427 call writefile(lines, 'Xtest.vim')
428
Bram Moolenaar0d702022019-07-04 14:20:41 +0200429 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200430 let job = term_getjob(buf)
431 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
432
433 " GC must not run during timer callback, which can make Vim crash.
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200434 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200435 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200436 call TermWait(buf, 50)
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200437 call assert_equal('run', job_status(job))
438
439 call term_sendkeys(buf, ":qall!\<CR>")
440 call WaitFor({-> job_status(job) ==# 'dead'})
441 if has('unix')
442 call assert_equal('', job_info(job).termsig)
443 endif
444
445 call delete('Xtest.vim')
446 exe buf .. 'bwipe!'
447endfunc
448
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100449" Test for garbage collection when a timer is still running
450func Test_timer_garbage_collect()
451 let timer = timer_start(1000, function('MyHandler'), {'repeat' : 10})
452 call test_garbagecollect_now()
453 let l = timer_info(timer)
454 call assert_equal(function('MyHandler'), l[0].callback)
455 call timer_stop(timer)
456endfunc
457
Bram Moolenaar14e579092020-03-07 16:59:25 +0100458func Test_timer_invalid_callback()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200459 call assert_fails('call timer_start(0, "0")', 'E921:')
Bram Moolenaar14e579092020-03-07 16:59:25 +0100460endfunc
461
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200462func Test_timer_changing_function_list()
463 CheckRunVimInTerminal
464
465 " Create a large number of functions. Should get the "more" prompt.
466 " The typing "G" triggers the timer, which changes the function table.
467 let lines =<< trim END
468 for func in map(range(1,99), "'Func' .. v:val")
469 exe "func " .. func .. "()"
470 endfunc
471 endfor
472 au CmdlineLeave : call timer_start(0, {-> 0})
473 END
474 call writefile(lines, 'XTest_timerchange')
475 let buf = RunVimInTerminal('-S XTest_timerchange', #{rows: 10})
476 call term_sendkeys(buf, ":fu\<CR>")
477 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 10))})
478 call term_sendkeys(buf, "G")
Bram Moolenaare2e40752020-09-04 21:18:46 +0200479 call WaitForAssert({-> assert_match('E454:', term_getline(buf, 9))})
Bram Moolenaar3fffa972020-06-05 21:06:10 +0200480 call term_sendkeys(buf, "\<Esc>")
481
482 call StopVimInTerminal(buf)
483 call delete('XTest_timerchange')
484endfunc
485
Bram Moolenaar9f284162021-04-22 21:39:30 +0200486func Test_timer_outputting_message()
487 CheckRunVimInTerminal
488
489 let lines =<< trim END
490 vim9script
491 setline(1, 'some text')
492 set showcmd ut=2000 cmdheight=1
493 timer_start(0, (_) => {
494 echon repeat('x', &columns - 11)
495 })
496 END
497 call writefile(lines, 'XTest_timermessage')
498 let buf = RunVimInTerminal('-S XTest_timermessage', #{rows: 6})
499 call term_sendkeys(buf, "l")
500 call term_wait(buf)
501 " should not get a hit-enter prompt
502 call WaitForAssert({-> assert_match('xxxxxxxxxxx', term_getline(buf, 6))})
503
504 call StopVimInTerminal(buf)
505 call delete('XTest_timermessage')
506endfunc
507
Bram Moolenaare615db02022-01-20 21:00:54 +0000508func Test_timer_using_win_execute_undo_sync()
509 let bufnr1 = bufnr()
510 new
511 let g:bufnr2 = bufnr()
512 let g:winid = win_getid()
513 exe "buffer " .. bufnr1
514 wincmd w
515 call setline(1, ['test'])
516 autocmd InsertEnter * call timer_start(100, { -> win_execute(g:winid, 'buffer ' .. g:bufnr2) })
517 call timer_start(200, { -> feedkeys("\<CR>bbbb\<Esc>") })
518 call feedkeys("Oaaaa", 'x!t')
519 " will hang here until the second timer fires
520 call assert_equal(['aaaa', 'bbbb', 'test'], getline(1, '$'))
521 undo
522 call assert_equal(['test'], getline(1, '$'))
523
524 bwipe!
525 bwipe!
526 unlet g:winid
527 unlet g:bufnr2
528 au! InsertEnter
529endfunc
530
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100531
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200532" vim: shiftwidth=2 sts=2 expandtab