xref: /vim-8.2.3635/src/testdir/test_timers.vim (revision bb76f24a)
1" Test for timers
2
3source shared.vim
4
5if !has('timers')
6  finish
7endif
8
9func MyHandler(timer)
10  let g:val += 1
11endfunc
12
13func MyHandlerWithLists(lists, timer)
14  let x = string(a:lists)
15endfunc
16
17func Test_oneshot()
18  let g:val = 0
19  let timer = timer_start(50, 'MyHandler')
20  let slept = WaitFor('g:val == 1')
21  call assert_equal(1, g:val)
22  if has('reltime')
23    call assert_inrange(49, 100, slept)
24  else
25    call assert_inrange(20, 100, slept)
26  endif
27endfunc
28
29func Test_repeat_three()
30  let g:val = 0
31  let timer = timer_start(50, 'MyHandler', {'repeat': 3})
32  let slept = WaitFor('g:val == 3')
33  call assert_equal(3, g:val)
34  if has('reltime')
35    call assert_inrange(149, 250, slept)
36  else
37    call assert_inrange(80, 200, slept)
38  endif
39endfunc
40
41func Test_repeat_many()
42  let g:val = 0
43  let timer = timer_start(50, 'MyHandler', {'repeat': -1})
44  sleep 200m
45  call timer_stop(timer)
46  call assert_inrange(2, 4, g:val)
47endfunc
48
49func Test_with_partial_callback()
50  let g:val = 0
51  let meow = {'one': 1}
52  function meow.bite(...)
53    let g:val += self.one
54  endfunction
55
56  call timer_start(50, meow.bite)
57  let slept = WaitFor('g:val == 1')
58  call assert_equal(1, g:val)
59  if has('reltime')
60    call assert_inrange(49, 130, slept)
61  else
62    call assert_inrange(20, 100, slept)
63  endif
64endfunc
65
66func Test_retain_partial()
67  call timer_start(50, function('MyHandlerWithLists', [['a']]))
68  call test_garbagecollect_now()
69  sleep 100m
70endfunc
71
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)
124  if has('reltime')
125    call assert_inrange(0, 30, slept)
126  else
127    call assert_inrange(0, 10, slept)
128  endif
129endfunc
130
131func StopMyself(timer)
132  let g:called += 1
133  if g:called == 2
134    call timer_stop(a:timer)
135  endif
136endfunc
137
138func Test_delete_myself()
139  let g:called = 0
140  let t = timer_start(10, 'StopMyself', {'repeat': -1})
141  call WaitFor('g:called == 2')
142  call assert_equal(2, g:called)
143  call assert_equal([], timer_info(t))
144endfunc
145
146func StopTimer1(timer)
147  let g:timer2 = timer_start(10, 'StopTimer2')
148  " avoid maxfuncdepth error
149  call timer_pause(g:timer1, 1)
150  sleep 40m
151endfunc
152
153func StopTimer2(timer)
154  call timer_stop(g:timer1)
155endfunc
156
157func Test_stop_in_callback()
158  let g:timer1 = timer_start(10, 'StopTimer1')
159  sleep 40m
160endfunc
161
162func StopTimerAll(timer)
163  call timer_stopall()
164endfunc
165
166func Test_stop_all_in_callback()
167  let g:timer1 = timer_start(10, 'StopTimerAll')
168  let info = timer_info()
169  call assert_equal(1, len(info))
170  sleep 40m
171  let info = timer_info()
172  call assert_equal(0, len(info))
173endfunc
174
175
176" vim: shiftwidth=2 sts=2 expandtab
177