blob: 963cc23276a525f46f782dc6addc60a037bdda3f [file] [log] [blame]
Bram Moolenaar975b5272016-03-15 23:10:59 +01001" Test for timers
2
3if !has('timers')
4 finish
5endif
6
Bram Moolenaar660b85e2017-09-30 14:26:58 +02007source shared.vim
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02008source screendump.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 Moolenaar975b5272016-03-15 23:10:59 +010018func Test_oneshot()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020019 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010020 let timer = timer_start(50, 'MyHandler')
Bram Moolenaarb73598e2016-08-07 18:22:53 +020021 let slept = WaitFor('g:val == 1')
22 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020023 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020024 call assert_inrange(49, 100, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020025 else
26 call assert_inrange(20, 100, slept)
27 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010028endfunc
29
30func Test_repeat_three()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020031 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010032 let timer = timer_start(50, 'MyHandler', {'repeat': 3})
Bram Moolenaarb73598e2016-08-07 18:22:53 +020033 let slept = WaitFor('g:val == 3')
34 call assert_equal(3, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020035 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020036 call assert_inrange(149, 250, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020037 else
38 call assert_inrange(80, 200, slept)
39 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010040endfunc
41
42func Test_repeat_many()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020043 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010044 let timer = timer_start(50, 'MyHandler', {'repeat': -1})
45 sleep 200m
46 call timer_stop(timer)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020047 call assert_inrange(2, 4, g:val)
Bram Moolenaar975b5272016-03-15 23:10:59 +010048endfunc
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010049
50func Test_with_partial_callback()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020051 let g:val = 0
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020052 let meow = {'one': 1}
53 function meow.bite(...)
54 let g:val += self.one
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010055 endfunction
56
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020057 call timer_start(50, meow.bite)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020058 let slept = WaitFor('g:val == 1')
59 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020060 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020061 call assert_inrange(49, 130, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020062 else
63 call assert_inrange(20, 100, slept)
64 endif
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010065endfunc
Bram Moolenaare3188e22016-05-31 21:13:04 +020066
67func Test_retain_partial()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020068 call timer_start(50, function('MyHandlerWithLists', [['a']]))
Bram Moolenaare3188e22016-05-31 21:13:04 +020069 call test_garbagecollect_now()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020070 sleep 100m
Bram Moolenaare3188e22016-05-31 21:13:04 +020071endfunc
Bram Moolenaarb73598e2016-08-07 18:22:53 +020072
73func Test_info()
74 let id = timer_start(1000, 'MyHandler')
75 let info = timer_info(id)
76 call assert_equal(id, info[0]['id'])
77 call assert_equal(1000, info[0]['time'])
78 call assert_true(info[0]['remaining'] > 500)
79 call assert_true(info[0]['remaining'] <= 1000)
80 call assert_equal(1, info[0]['repeat'])
81 call assert_equal("function('MyHandler')", string(info[0]['callback']))
82
83 let found = 0
84 for info in timer_info()
85 if info['id'] == id
86 let found += 1
87 endif
88 endfor
89 call assert_equal(1, found)
90
91 call timer_stop(id)
92 call assert_equal([], timer_info(id))
93endfunc
94
95func Test_stopall()
96 let id1 = timer_start(1000, 'MyHandler')
97 let id2 = timer_start(2000, 'MyHandler')
98 let info = timer_info()
99 call assert_equal(2, len(info))
100
101 call timer_stopall()
102 let info = timer_info()
103 call assert_equal(0, len(info))
104endfunc
105
106func Test_paused()
107 let g:val = 0
108
109 let id = timer_start(50, 'MyHandler')
110 let info = timer_info(id)
111 call assert_equal(0, info[0]['paused'])
112
113 call timer_pause(id, 1)
114 let info = timer_info(id)
115 call assert_equal(1, info[0]['paused'])
116 sleep 100m
117 call assert_equal(0, g:val)
118
119 call timer_pause(id, 0)
120 let info = timer_info(id)
121 call assert_equal(0, info[0]['paused'])
122
123 let slept = WaitFor('g:val == 1')
124 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200125 if has('reltime')
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100126 if has('mac')
127 " The travis Mac machines appear to be very busy.
Bram Moolenaar8dce6c52018-02-03 15:38:42 +0100128 call assert_inrange(0, 50, slept)
Bram Moolenaara47ebdb2017-12-23 18:41:35 +0100129 else
130 call assert_inrange(0, 30, slept)
131 endif
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200132 else
133 call assert_inrange(0, 10, slept)
134 endif
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200135endfunc
136
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200137func StopMyself(timer)
138 let g:called += 1
139 if g:called == 2
140 call timer_stop(a:timer)
141 endif
142endfunc
143
144func Test_delete_myself()
145 let g:called = 0
146 let t = timer_start(10, 'StopMyself', {'repeat': -1})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200147 call WaitForAssert({-> assert_equal(2, g:called)})
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200148 call assert_equal(2, g:called)
149 call assert_equal([], timer_info(t))
150endfunc
151
Bram Moolenaar75537a92016-09-05 22:45:28 +0200152func StopTimer1(timer)
153 let g:timer2 = timer_start(10, 'StopTimer2')
154 " avoid maxfuncdepth error
155 call timer_pause(g:timer1, 1)
156 sleep 40m
157endfunc
158
159func StopTimer2(timer)
160 call timer_stop(g:timer1)
161endfunc
162
163func Test_stop_in_callback()
164 let g:timer1 = timer_start(10, 'StopTimer1')
165 sleep 40m
166endfunc
167
168func StopTimerAll(timer)
169 call timer_stopall()
170endfunc
171
172func Test_stop_all_in_callback()
173 let g:timer1 = timer_start(10, 'StopTimerAll')
174 let info = timer_info()
175 call assert_equal(1, len(info))
176 sleep 40m
177 let info = timer_info()
178 call assert_equal(0, len(info))
179endfunc
180
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200181func FeedkeysCb(timer)
182 call feedkeys("hello\<CR>", 'nt')
183endfunc
184
185func InputCb(timer)
186 call timer_start(10, 'FeedkeysCb')
187 let g:val = input('?')
188 call Resume()
189endfunc
190
191func Test_input_in_timer()
192 let g:val = ''
193 call timer_start(10, 'InputCb')
194 call Standby(1000)
195 call assert_equal('hello', g:val)
196endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200197
Bram Moolenaarc577d812017-07-08 22:37:34 +0200198func FuncWithError(timer)
199 let g:call_count += 1
200 if g:call_count == 4
201 return
202 endif
203 doesnotexist
204endfunc
205
206func Test_timer_errors()
207 let g:call_count = 0
208 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
209 " Timer will be stopped after failing 3 out of 3 times.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200210 call WaitForAssert({-> assert_equal(3, g:call_count)})
Bram Moolenaarc577d812017-07-08 22:37:34 +0200211 sleep 50m
212 call assert_equal(3, g:call_count)
213endfunc
214
Bram Moolenaare723c422017-09-06 23:40:10 +0200215func FuncWithCaughtError(timer)
216 let g:call_count += 1
217 try
218 doesnotexist
219 catch
220 " nop
221 endtry
222endfunc
223
224func Test_timer_catch_error()
225 let g:call_count = 0
226 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
227 " Timer will not be stopped.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200228 call WaitForAssert({-> assert_equal(4, g:call_count)})
Bram Moolenaare723c422017-09-06 23:40:10 +0200229 sleep 50m
230 call assert_equal(4, g:call_count)
231endfunc
232
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200233func FeedAndPeek(timer)
234 call test_feedinput('a')
235 call getchar(1)
236endfunc
237
238func Interrupt(timer)
239 call test_feedinput("\<C-C>")
240endfunc
241
242func Test_peek_and_get_char()
243 if !has('unix') && !has('gui_running')
244 return
245 endif
246 call timer_start(0, 'FeedAndPeek')
247 let intr = timer_start(100, 'Interrupt')
248 let c = getchar()
249 call assert_equal(char2nr('a'), c)
250 call timer_stop(intr)
251endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200252
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100253func Test_getchar_zero()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100254 if has('win32') && !has('gui_running')
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100255 " Console: no low-level input
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100256 return
257 endif
258
Bram Moolenaar50948e42019-01-29 20:36:56 +0100259 " Measure the elapsed time to avoid a hang when it fails.
260 let start = reltime()
Bram Moolenaar8d4ce562019-01-30 22:01:40 +0100261 let id = timer_start(20, {-> feedkeys('x', 'L')})
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100262 let c = 0
Bram Moolenaar50948e42019-01-29 20:36:56 +0100263 while c == 0 && reltimefloat(reltime(start)) < 0.2
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100264 let c = getchar(0)
265 sleep 10m
266 endwhile
267 call assert_equal('x', nr2char(c))
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100268 call timer_stop(id)
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100269endfunc
270
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200271func Test_ex_mode()
272 " Function with an empty line.
273 func Foo(...)
274
275 endfunc
Bram Moolenaarcb908a82019-01-28 23:20:04 +0100276 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200277 " This used to throw error E749.
278 exe "normal Qsleep 100m\rvi\r"
279 call timer_stop(timer)
280endfunc
281
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200282func Test_restore_count()
283 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200284 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +0200285 endif
286 " Check that v:count is saved and restored, not changed by a timer.
287 call writefile([
288 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"',
289 \ 'func Doit(id)',
290 \ ' normal 3j',
291 \ 'endfunc',
292 \ 'call timer_start(100, "Doit")',
293 \ ], 'Xtrcscript')
294 call writefile([
295 \ '1-1234',
296 \ '2-1234',
297 \ '3-1234',
298 \ ], 'Xtrctext')
299 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {})
300
301 " Wait for the timer to move the cursor to the third line.
302 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])})
303 call assert_equal(1, term_getcursor(buf)[1])
304 " Now check that v:count has not been set to 3
305 call term_sendkeys(buf, 'L')
306 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])})
307
308 call StopVimInTerminal(buf)
309 call delete('Xtrcscript')
310 call delete('Xtrctext')
311endfunc
312
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200313" vim: shiftwidth=2 sts=2 expandtab