blob: caeb23014987b04ca4ec43dfd361761f2dab942f [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 Moolenaar660b85e2017-09-30 14:26:58 +02006source shared.vim
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02007source term_util.vim
Bram Moolenaar11aa62f2017-09-04 22:56:01 +02008
Bram Moolenaar975b5272016-03-15 23:10:59 +01009func MyHandler(timer)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020010 let g:val += 1
Bram Moolenaar975b5272016-03-15 23:10:59 +010011endfunc
12
Bram Moolenaare3188e22016-05-31 21:13:04 +020013func MyHandlerWithLists(lists, timer)
14 let x = string(a:lists)
15endfunc
16
Bram Moolenaar975b5272016-03-15 23:10:59 +010017func Test_oneshot()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020018 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010019 let timer = timer_start(50, 'MyHandler')
Bram Moolenaarb73598e2016-08-07 18:22:53 +020020 let slept = WaitFor('g:val == 1')
21 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020022 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020023 call assert_inrange(49, 100, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020024 else
25 call assert_inrange(20, 100, slept)
26 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010027endfunc
28
29func Test_repeat_three()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020030 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010031 let timer = timer_start(50, 'MyHandler', {'repeat': 3})
Bram Moolenaarb73598e2016-08-07 18:22:53 +020032 let slept = WaitFor('g:val == 3')
33 call assert_equal(3, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020034 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020035 call assert_inrange(149, 250, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020036 else
37 call assert_inrange(80, 200, slept)
38 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010039endfunc
40
41func Test_repeat_many()
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': -1})
44 sleep 200m
45 call timer_stop(timer)
Bram Moolenaar7e6feb92019-08-15 23:42:21 +020046 call assert_inrange(2, 5, g:val)
Bram Moolenaar975b5272016-03-15 23:10:59 +010047endfunc
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010048
49func Test_with_partial_callback()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020050 let g:val = 0
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020051 let meow = {'one': 1}
52 function meow.bite(...)
53 let g:val += self.one
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010054 endfunction
55
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020056 call timer_start(50, meow.bite)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020057 let slept = WaitFor('g:val == 1')
58 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020059 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020060 call assert_inrange(49, 130, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020061 else
62 call assert_inrange(20, 100, slept)
63 endif
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010064endfunc
Bram Moolenaare3188e22016-05-31 21:13:04 +020065
66func Test_retain_partial()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020067 call timer_start(50, function('MyHandlerWithLists', [['a']]))
Bram Moolenaare3188e22016-05-31 21:13:04 +020068 call test_garbagecollect_now()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020069 sleep 100m
Bram Moolenaare3188e22016-05-31 21:13:04 +020070endfunc
Bram Moolenaarb73598e2016-08-07 18:22:53 +020071
72func Test_info()
73 let id = timer_start(1000, 'MyHandler')
74 let info = timer_info(id)
75 call assert_equal(id, info[0]['id'])
76 call assert_equal(1000, info[0]['time'])
77 call assert_true(info[0]['remaining'] > 500)
78 call assert_true(info[0]['remaining'] <= 1000)
79 call assert_equal(1, info[0]['repeat'])
80 call assert_equal("function('MyHandler')", string(info[0]['callback']))
81
82 let found = 0
83 for info in timer_info()
84 if info['id'] == id
85 let found += 1
86 endif
87 endfor
88 call assert_equal(1, found)
89
90 call timer_stop(id)
91 call assert_equal([], timer_info(id))
92endfunc
93
94func Test_stopall()
95 let id1 = timer_start(1000, 'MyHandler')
96 let id2 = timer_start(2000, 'MyHandler')
97 let info = timer_info()
98 call assert_equal(2, len(info))
99
100 call timer_stopall()
101 let info = timer_info()
102 call assert_equal(0, len(info))
103endfunc
104
105func Test_paused()
106 let g:val = 0
107
108 let id = timer_start(50, 'MyHandler')
109 let info = timer_info(id)
110 call assert_equal(0, info[0]['paused'])
111
112 call timer_pause(id, 1)
113 let info = timer_info(id)
114 call assert_equal(1, info[0]['paused'])
115 sleep 100m
116 call assert_equal(0, g:val)
117
118 call timer_pause(id, 0)
119 let info = timer_info(id)
120 call assert_equal(0, info[0]['paused'])
121
122 let slept = WaitFor('g:val == 1')
123 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200124 if has('reltime')
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100125 if has('mac')
126 " The travis Mac machines appear to be very busy.
Bram Moolenaar8dce6c52018-02-03 15:38:42 +0100127 call assert_inrange(0, 50, slept)
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100128 else
129 call assert_inrange(0, 30, slept)
130 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200131 else
132 call assert_inrange(0, 10, slept)
133 endif
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200134endfunc
135
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200136func StopMyself(timer)
137 let g:called += 1
138 if g:called == 2
139 call timer_stop(a:timer)
140 endif
141endfunc
142
143func Test_delete_myself()
144 let g:called = 0
145 let t = timer_start(10, 'StopMyself', {'repeat': -1})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200146 call WaitForAssert({-> assert_equal(2, g:called)})
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200147 call assert_equal(2, g:called)
148 call assert_equal([], timer_info(t))
149endfunc
150
Bram Moolenaar75537a92016-09-05 22:45:28 +0200151func StopTimer1(timer)
152 let g:timer2 = timer_start(10, 'StopTimer2')
153 " avoid maxfuncdepth error
154 call timer_pause(g:timer1, 1)
155 sleep 40m
156endfunc
157
158func StopTimer2(timer)
159 call timer_stop(g:timer1)
160endfunc
161
162func Test_stop_in_callback()
163 let g:timer1 = timer_start(10, 'StopTimer1')
164 sleep 40m
165endfunc
166
167func StopTimerAll(timer)
168 call timer_stopall()
169endfunc
170
171func Test_stop_all_in_callback()
172 let g:timer1 = timer_start(10, 'StopTimerAll')
173 let info = timer_info()
174 call assert_equal(1, len(info))
175 sleep 40m
176 let info = timer_info()
177 call assert_equal(0, len(info))
178endfunc
179
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200180func FeedkeysCb(timer)
181 call feedkeys("hello\<CR>", 'nt')
182endfunc
183
184func InputCb(timer)
185 call timer_start(10, 'FeedkeysCb')
186 let g:val = input('?')
187 call Resume()
188endfunc
189
190func Test_input_in_timer()
191 let g:val = ''
192 call timer_start(10, 'InputCb')
193 call Standby(1000)
194 call assert_equal('hello', g:val)
195endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200196
Bram Moolenaarc577d812017-07-08 22:37:34 +0200197func FuncWithError(timer)
198 let g:call_count += 1
199 if g:call_count == 4
200 return
201 endif
202 doesnotexist
203endfunc
204
205func Test_timer_errors()
206 let g:call_count = 0
207 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
208 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200209 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200210 sleep 50m
211 call assert_equal(3, g:call_count)
212endfunc
213
Bram Moolenaare723c422017-09-06 23:40:10 +0200214func FuncWithCaughtError(timer)
215 let g:call_count += 1
216 try
217 doesnotexist
218 catch
219 " nop
220 endtry
221endfunc
222
223func Test_timer_catch_error()
224 let g:call_count = 0
225 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
226 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200227 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200228 sleep 50m
229 call assert_equal(4, g:call_count)
230endfunc
231
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200232func FeedAndPeek(timer)
233 call test_feedinput('a')
234 call getchar(1)
235endfunc
236
237func Interrupt(timer)
238 call test_feedinput("\<C-C>")
239endfunc
240
241func Test_peek_and_get_char()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200242 CheckUnix
243 CheckGui
244
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200245 call timer_start(0, 'FeedAndPeek')
246 let intr = timer_start(100, 'Interrupt')
247 let c = getchar()
248 call assert_equal(char2nr('a'), c)
249 call timer_stop(intr)
250endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200251
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100252func Test_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100253 if has('win32') && !has('gui_running')
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200254 throw 'Skipped: cannot get low-level input'
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100255 endif
256
Bram Moolenaar50948e42019-01-29 20:36:56 +0100257 " Measure the elapsed time to avoid a hang when it fails.
258 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100259 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100260 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100261 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100262 let c = getchar(0)
263 sleep 10m
264 endwhile
265 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100266 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100267endfunc
268
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200269func Test_ex_mode()
270 " Function with an empty line.
271 func Foo(...)
272
273 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100274 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200275 " This used to throw error E749.
276 exe "normal Qsleep 100m\rvi\r"
277 call timer_stop(timer)
278endfunc
279
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200280func Test_restore_count()
281 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200282 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200283 endif
284 " Check that v:count is saved and restored, not changed by a timer.
285 call writefile([
286 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
287 \ 'func Doit(id)',
288 \ ' normal 3j',
289 \ 'endfunc',
290 \ 'call timer_start(100, "Doit")',
291 \ ], 'Xtrcscript')
292 call writefile([
293 \ '1-1234',
294 \ '2-1234',
295 \ '3-1234',
296 \ ], 'Xtrctext')
297 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
298
299 " Wait for the timer to move the cursor to the third line.
300 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
301 call assert_equal(1, term_getcursor(buf)[1])
302 " Now check that v:count has not been set to 3
303 call term_sendkeys(buf, 'L')
304 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
305
306 call StopVimInTerminal(buf)
307 call delete('Xtrcscript')
308 call delete('Xtrctext')
309endfunc
310
Bram Moolenaaradc67142019-06-22 01:40:42 +0200311" Test that the garbage collector isn't triggered if a timer callback invokes
312" vgetc().
313func Test_nocatch_garbage_collect()
314 " 'uptimetime. must be bigger than the timer timeout
315 set ut=200
316 call test_garbagecollect_soon()
317 call test_override('no_wait_return', 0)
318 func CauseAnError(id)
319 " This will show an error and wait for Enter.
320 let a = {'foo', 'bar'}
321 endfunc
322 func FeedChar(id)
323 call feedkeys('x', 't')
324 endfunc
325 call timer_start(300, 'FeedChar')
326 call timer_start(100, 'CauseAnError')
327 let x = getchar()
328
329 set ut&
330 call test_override('no_wait_return', 1)
331 delfunc CauseAnError
332 delfunc FeedChar
333endfunc
334
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200335func Test_error_in_timer_callback()
Bram Moolenaar0d702022019-07-04 14:20:41 +0200336 if !has('terminal') || (has('win32') && has('gui_running'))
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200337 throw 'Skipped: cannot run Vim in a terminal window'
338 endif
339
340 let lines =<< trim [CODE]
341 func Func(timer)
342 " fail to create list
343 let x = [
344 endfunc
345 set updatetime=50
346 call timer_start(1, 'Func')
347 [CODE]
348 call writefile(lines, 'Xtest.vim')
349
Bram Moolenaar0d702022019-07-04 14:20:41 +0200350 let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
Bram Moolenaar7d491c42019-06-25 06:28:02 +0200351 let job = term_getjob(buf)
352 call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
353
354 " GC must not run during timer callback, which can make Vim crash.
355 call term_wait(buf, 100)
356 call term_sendkeys(buf, "\<CR>")
357 call term_wait(buf, 100)
358 call assert_equal('run', job_status(job))
359
360 call term_sendkeys(buf, ":qall!\<CR>")
361 call WaitFor({-> job_status(job) ==# 'dead'})
362 if has('unix')
363 call assert_equal('', job_info(job).termsig)
364 endif
365
366 call delete('Xtest.vim')
367 exe buf .. 'bwipe!'
368endfunc
369
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200370" vim: shiftwidth=2 sts=2 expandtab