blob: a0532ca4678dc36b8090023a4238c145ac0c18f9 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
Bram Moolenaare2469252016-02-04 10:54:34 +01004if !has('channel')
5 finish
6endif
7
8" This test requires the Python command to run the test server.
Bram Moolenaara8343c12016-02-04 22:09:48 +01009" This most likely only works on Unix and Windows.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010010if has('unix')
Bram Moolenaar835dc632016-02-07 14:27:38 +010011 " We also need the job feature or the pkill command to make sure the server
12 " can be stopped.
13 if !(executable('python') && (has('job') || executable('pkill')))
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010014 finish
15 endif
Bram Moolenaara8343c12016-02-04 22:09:48 +010016elseif has('win32')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010017 " Use Python Launcher for Windows (py.exe).
18 if !executable('py')
19 finish
20 endif
21else
Bram Moolenaard6a8d482016-02-10 20:32:20 +010022 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010023 finish
24endif
25
Bram Moolenaara4833262016-02-09 23:33:25 +010026let s:chopt = has('macunix') ? {'waittime' : 1} : {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010027
Bram Moolenaard6a8d482016-02-10 20:32:20 +010028" Run "testfunc" after sarting the server and stop the server afterwards.
29func s:run_server(testfunc)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010030 " The Python program writes the port number in Xportnr.
31 call delete("Xportnr")
32
Bram Moolenaard6a8d482016-02-10 20:32:20 +010033 try
34 if has('job')
35 let s:job = job_start("python test_channel.py")
36 elseif has('win32')
37 silent !start cmd /c start "test_channel" py test_channel.py
38 else
39 silent !python test_channel.py&
Bram Moolenaard7ece102016-02-02 23:23:02 +010040 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010041
Bram Moolenaard6a8d482016-02-10 20:32:20 +010042 " Wait for up to 2 seconds for the port number to be there.
43 let cnt = 20
44 let l = []
45 while cnt > 0
46 try
47 let l = readfile("Xportnr")
48 catch
49 endtry
50 if len(l) >= 1
51 break
52 endif
53 sleep 100m
54 let cnt -= 1
55 endwhile
56 call delete("Xportnr")
57
58 if len(l) == 0
59 " Can't make the connection, give up.
60 call assert_false(1, "Can't start test_channel.py")
61 return -1
62 endif
63 let port = l[0]
64
65 call call(function(a:testfunc), [port])
66 catch
67 call assert_false(1, "Caught exception: " . v:exception)
68 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010069 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010070 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010071endfunc
72
73func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010074 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010075 if exists('s:job')
76 call job_stop(s:job)
77 unlet s:job
78 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010079 elseif has('win32')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010080 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
81 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010082 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010083 endif
84endfunc
85
Bram Moolenaara07fec92016-02-05 21:04:08 +010086let s:responseHandle = -1
87let s:responseMsg = ''
88func s:RequestHandler(handle, msg)
89 let s:responseHandle = a:handle
90 let s:responseMsg = a:msg
91endfunc
92
Bram Moolenaard6a8d482016-02-10 20:32:20 +010093func s:communicate(port)
94 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010095 if handle < 0
Bram Moolenaard6a8d482016-02-10 20:32:20 +010096 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010097 return
98 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010099
100 " Simple string request and reply.
101 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
102
103 " Request that triggers sending two ex commands. These will usually be
104 " handled before getting the response, but it's not guaranteed, thus wait a
105 " tiny bit for the commands to get executed.
106 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
107 sleep 10m
108 call assert_equal('added1', getline(line('$') - 1))
109 call assert_equal('added2', getline('$'))
110
Bram Moolenaarf4160862016-02-05 23:09:12 +0100111 call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
112 sleep 10m
113 call assert_equal('added more', getline('$'))
114
Bram Moolenaara07fec92016-02-05 21:04:08 +0100115 " Send a request with a specific handler.
116 call ch_sendexpr(handle, 'hello!', 's:RequestHandler')
117 sleep 10m
118 call assert_equal(handle, s:responseHandle)
119 call assert_equal('got it', s:responseMsg)
120
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100121 let s:responseHandle = -1
122 let s:responseMsg = ''
123 call ch_sendexpr(handle, 'hello!', function('s:RequestHandler'))
124 sleep 10m
125 call assert_equal(handle, s:responseHandle)
126 call assert_equal('got it', s:responseMsg)
127
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100128 " Send an eval request that works.
129 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100130 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100131 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
132
133 " Send an eval request that fails.
134 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100135 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100136 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
137
Bram Moolenaar55fab432016-02-07 16:53:13 +0100138 " Send an eval request that works but can't be encoded.
139 call assert_equal('ok', ch_sendexpr(handle, 'eval-error'))
140 sleep 10m
141 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
142
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100143 " Send a bad eval request. There will be no response.
144 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100145 sleep 10m
Bram Moolenaar55fab432016-02-07 16:53:13 +0100146 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100147
Bram Moolenaarf4160862016-02-05 23:09:12 +0100148 " Send an expr request
149 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
150 sleep 10m
151 call assert_equal('one', getline(line('$') - 2))
152 call assert_equal('two', getline(line('$') - 1))
153 call assert_equal('three', getline('$'))
154
155 " Request a redraw, we don't check for the effect.
156 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
157 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
158
159 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
160
Bram Moolenaard7ece102016-02-02 23:23:02 +0100161 " make the server quit, can't check if this works, should not hang.
162 call ch_sendexpr(handle, '!quit!', 0)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100163endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100164
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100165func Test_communicate()
166 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100167endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100168
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100169" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100170func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100171 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100172 if handle < 0
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100173 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100174 return
175 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100176
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100177 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
178
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100179 let newhandle = ch_open('localhost:' . a:port, s:chopt)
180 if newhandle < 0
181 call assert_false(1, "Can't open second channel")
182 return
183 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100184 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
185 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
186
187 call ch_close(handle)
188 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
189
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100190 call ch_close(newhandle)
191endfunc
192
193func Test_two_channels()
Bram Moolenaarf02c5cf2016-02-12 22:25:56 +0100194 " TODO: make this work again with MS-Windows
195 if has('unix')
196 call s:run_server('s:two_channels')
197 endf
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100198endfunc
199
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100200" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100201func s:server_crash(port)
202 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100203 if handle < 0
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100204 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100205 return
206 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100207
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100208 call ch_sendexpr(handle, '!crash!')
209
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100210 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100211endfunc
212
213func Test_server_crash()
Bram Moolenaarf02c5cf2016-02-12 22:25:56 +0100214 " TODO: make this work again with MS-Windows
215 if has('unix')
216 call s:run_server('s:server_crash')
217 endif
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100218endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100219
Bram Moolenaarf6157282016-02-10 21:07:14 +0100220let s:reply = ""
221func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100222 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100223 let s:reply = a:msg
224endfunc
225
226func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100227 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100228 if handle < 0
229 call assert_false(1, "Can't open channel")
230 return
231 endif
232
233 " Test that it works while waiting on a numbered message.
234 call assert_equal('ok', ch_sendexpr(handle, 'call me'))
235 sleep 10m
236 call assert_equal('we called you', s:reply)
237
238 " Test that it works while not waiting on a numbered message.
239 call ch_sendexpr(handle, 'call me again', 0)
240 sleep 10m
241 call assert_equal('we did call you', s:reply)
242endfunc
243
244func Test_channel_handler()
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100245 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100246 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100247 let s:chopt.callback = function('s:Handler')
248 call s:run_server('s:channel_handler')
249 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100250endfunc
251
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100252" Test that trying to connect to a non-existing port fails quickly.
253func Test_connect_waittime()
254 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100255 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100256 if handle >= 0
257 " Oops, port does exists.
258 call ch_close(handle)
259 else
260 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100261 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100262 endif
263
264 let start = reltime()
265 let handle = ch_open('localhost:9867', {'waittime': 2000})
266 if handle >= 0
267 " Oops, port does exists.
268 call ch_close(handle)
269 else
Bram Moolenaar0fa98e72016-02-07 22:21:19 +0100270 " Failed connection doesn't wait the full time on Unix.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100271 " TODO: why is MS-Windows different?
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100272 let elapsed = reltime(start)
Bram Moolenaar0fa98e72016-02-07 22:21:19 +0100273 call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0))
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100274 endif
275endfunc