blob: 768eeade8c658261c82b7f1948bb4646fa15db7c [file] [log] [blame]
Bram Moolenaar975b5272016-03-15 23:10:59 +01001" Test for timers
2
3if !has('timers')
4 finish
5endif
6
Bram Moolenaar11aa62f2017-09-04 22:56:01 +02007if !exists('*WaitFor')
8 source shared.vim
9endif
10
Bram Moolenaar975b5272016-03-15 23:10:59 +010011func MyHandler(timer)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012 let g:val += 1
Bram Moolenaar975b5272016-03-15 23:10:59 +010013endfunc
14
Bram Moolenaare3188e22016-05-31 21:13:04 +020015func MyHandlerWithLists(lists, timer)
16 let x = string(a:lists)
17endfunc
18
Bram Moolenaar975b5272016-03-15 23:10:59 +010019func Test_oneshot()
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 Moolenaarf267f8b2016-08-22 21:40:29 +020024 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020025 call assert_inrange(49, 100, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020026 else
27 call assert_inrange(20, 100, slept)
28 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010029endfunc
30
31func Test_repeat_three()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020032 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010033 let timer = timer_start(50, 'MyHandler', {'repeat': 3})
Bram Moolenaarb73598e2016-08-07 18:22:53 +020034 let slept = WaitFor('g:val == 3')
35 call assert_equal(3, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020036 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020037 call assert_inrange(149, 250, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020038 else
39 call assert_inrange(80, 200, slept)
40 endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010041endfunc
42
43func Test_repeat_many()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020044 let g:val = 0
Bram Moolenaar975b5272016-03-15 23:10:59 +010045 let timer = timer_start(50, 'MyHandler', {'repeat': -1})
46 sleep 200m
47 call timer_stop(timer)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020048 call assert_inrange(2, 4, g:val)
Bram Moolenaar975b5272016-03-15 23:10:59 +010049endfunc
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010050
51func Test_with_partial_callback()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020052 let g:val = 0
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020053 let meow = {'one': 1}
54 function meow.bite(...)
55 let g:val += self.one
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010056 endfunction
57
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020058 call timer_start(50, meow.bite)
Bram Moolenaarb73598e2016-08-07 18:22:53 +020059 let slept = WaitFor('g:val == 1')
60 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020061 if has('reltime')
Bram Moolenaar0426bae2016-08-28 16:06:05 +020062 call assert_inrange(49, 130, slept)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +020063 else
64 call assert_inrange(20, 100, slept)
65 endif
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010066endfunc
Bram Moolenaare3188e22016-05-31 21:13:04 +020067
68func Test_retain_partial()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020069 call timer_start(50, function('MyHandlerWithLists', [['a']]))
Bram Moolenaare3188e22016-05-31 21:13:04 +020070 call test_garbagecollect_now()
Bram Moolenaarb73598e2016-08-07 18:22:53 +020071 sleep 100m
Bram Moolenaare3188e22016-05-31 21:13:04 +020072endfunc
Bram Moolenaarb73598e2016-08-07 18:22:53 +020073
74func Test_info()
75 let id = timer_start(1000, 'MyHandler')
76 let info = timer_info(id)
77 call assert_equal(id, info[0]['id'])
78 call assert_equal(1000, info[0]['time'])
79 call assert_true(info[0]['remaining'] > 500)
80 call assert_true(info[0]['remaining'] <= 1000)
81 call assert_equal(1, info[0]['repeat'])
82 call assert_equal("function('MyHandler')", string(info[0]['callback']))
83
84 let found = 0
85 for info in timer_info()
86 if info['id'] == id
87 let found += 1
88 endif
89 endfor
90 call assert_equal(1, found)
91
92 call timer_stop(id)
93 call assert_equal([], timer_info(id))
94endfunc
95
96func Test_stopall()
97 let id1 = timer_start(1000, 'MyHandler')
98 let id2 = timer_start(2000, 'MyHandler')
99 let info = timer_info()
100 call assert_equal(2, len(info))
101
102 call timer_stopall()
103 let info = timer_info()
104 call assert_equal(0, len(info))
105endfunc
106
107func Test_paused()
108 let g:val = 0
109
110 let id = timer_start(50, 'MyHandler')
111 let info = timer_info(id)
112 call assert_equal(0, info[0]['paused'])
113
114 call timer_pause(id, 1)
115 let info = timer_info(id)
116 call assert_equal(1, info[0]['paused'])
117 sleep 100m
118 call assert_equal(0, g:val)
119
120 call timer_pause(id, 0)
121 let info = timer_info(id)
122 call assert_equal(0, info[0]['paused'])
123
124 let slept = WaitFor('g:val == 1')
125 call assert_equal(1, g:val)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200126 if has('reltime')
127 call assert_inrange(0, 30, slept)
128 else
129 call assert_inrange(0, 10, slept)
130 endif
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200131endfunc
132
Bram Moolenaar417ccd72016-09-01 21:26:20 +0200133func StopMyself(timer)
134 let g:called += 1
135 if g:called == 2
136 call timer_stop(a:timer)
137 endif
138endfunc
139
140func Test_delete_myself()
141 let g:called = 0
142 let t = timer_start(10, 'StopMyself', {'repeat': -1})
143 call WaitFor('g:called == 2')
144 call assert_equal(2, g:called)
145 call assert_equal([], timer_info(t))
146endfunc
147
Bram Moolenaar75537a92016-09-05 22:45:28 +0200148func StopTimer1(timer)
149 let g:timer2 = timer_start(10, 'StopTimer2')
150 " avoid maxfuncdepth error
151 call timer_pause(g:timer1, 1)
152 sleep 40m
153endfunc
154
155func StopTimer2(timer)
156 call timer_stop(g:timer1)
157endfunc
158
159func Test_stop_in_callback()
160 let g:timer1 = timer_start(10, 'StopTimer1')
161 sleep 40m
162endfunc
163
164func StopTimerAll(timer)
165 call timer_stopall()
166endfunc
167
168func Test_stop_all_in_callback()
169 let g:timer1 = timer_start(10, 'StopTimerAll')
170 let info = timer_info()
171 call assert_equal(1, len(info))
172 sleep 40m
173 let info = timer_info()
174 call assert_equal(0, len(info))
175endfunc
176
Bram Moolenaar1e8e1452017-06-24 16:03:06 +0200177func FeedkeysCb(timer)
178 call feedkeys("hello\<CR>", 'nt')
179endfunc
180
181func InputCb(timer)
182 call timer_start(10, 'FeedkeysCb')
183 let g:val = input('?')
184 call Resume()
185endfunc
186
187func Test_input_in_timer()
188 let g:val = ''
189 call timer_start(10, 'InputCb')
190 call Standby(1000)
191 call assert_equal('hello', g:val)
192endfunc
Bram Moolenaar75537a92016-09-05 22:45:28 +0200193
Bram Moolenaarc577d812017-07-08 22:37:34 +0200194func FuncWithError(timer)
195 let g:call_count += 1
196 if g:call_count == 4
197 return
198 endif
199 doesnotexist
200endfunc
201
202func Test_timer_errors()
203 let g:call_count = 0
204 let timer = timer_start(10, 'FuncWithError', {'repeat': -1})
205 " Timer will be stopped after failing 3 out of 3 times.
206 call WaitFor('g:call_count == 3')
207 sleep 50m
208 call assert_equal(3, g:call_count)
209endfunc
210
Bram Moolenaare723c422017-09-06 23:40:10 +0200211func FuncWithCaughtError(timer)
212 let g:call_count += 1
213 try
214 doesnotexist
215 catch
216 " nop
217 endtry
218endfunc
219
220func Test_timer_catch_error()
221 let g:call_count = 0
222 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
223 " Timer will not be stopped.
224 call WaitFor('g:call_count == 4')
225 sleep 50m
226 call assert_equal(4, g:call_count)
227endfunc
228
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200229func FeedAndPeek(timer)
230 call test_feedinput('a')
231 call getchar(1)
232endfunc
233
234func Interrupt(timer)
235 call test_feedinput("\<C-C>")
236endfunc
237
238func Test_peek_and_get_char()
239 if !has('unix') && !has('gui_running')
240 return
241 endif
242 call timer_start(0, 'FeedAndPeek')
243 let intr = timer_start(100, 'Interrupt')
244 let c = getchar()
245 call assert_equal(char2nr('a'), c)
246 call timer_stop(intr)
247endfunc
Bram Moolenaarc577d812017-07-08 22:37:34 +0200248
Bram Moolenaarf5291f32017-09-14 22:55:37 +0200249func Test_ex_mode()
250 " Function with an empty line.
251 func Foo(...)
252
253 endfunc
254 let timer = timer_start(40, function('g:Foo'), {'repeat':-1})
255 " This used to throw error E749.
256 exe "normal Qsleep 100m\rvi\r"
257 call timer_stop(timer)
258endfunc
259
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200260" vim: shiftwidth=2 sts=2 expandtab