blob: a05272749b84029e1eac1da7d8073f6d836a4fe9 [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 Moolenaarb6a77372016-02-15 22:55:28 +010016 let s:python = 'python'
Bram Moolenaara8343c12016-02-04 22:09:48 +010017elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010018 " Use Python Launcher for Windows (py.exe) if available.
19 if executable('py.exe')
20 let s:python = 'py.exe'
21 elseif executable('python.exe')
22 let s:python = 'python.exe'
23 else
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010024 finish
25 endif
26else
Bram Moolenaard6a8d482016-02-10 20:32:20 +010027 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010028 finish
29endif
30
Bram Moolenaare74e8e72016-02-16 22:01:30 +010031let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010032
Bram Moolenaard6a8d482016-02-10 20:32:20 +010033" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010034func s:run_server(testfunc, ...)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010035 " The Python program writes the port number in Xportnr.
36 call delete("Xportnr")
37
Bram Moolenaar81661fb2016-02-18 22:23:34 +010038 if a:0 == 1
39 let arg = ' ' . a:1
40 else
41 let arg = ''
42 endif
43 let cmd = s:python . " test_channel.py" . arg
44
Bram Moolenaard6a8d482016-02-10 20:32:20 +010045 try
46 if has('job')
Bram Moolenaar81661fb2016-02-18 22:23:34 +010047 let s:job = job_start(cmd)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010048 elseif has('win32')
Bram Moolenaar81661fb2016-02-18 22:23:34 +010049 exe 'silent !start cmd /c start "test_channel" ' . cmd
Bram Moolenaard6a8d482016-02-10 20:32:20 +010050 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010051 exe 'silent !' . cmd . '&'
Bram Moolenaard7ece102016-02-02 23:23:02 +010052 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010053
Bram Moolenaard6a8d482016-02-10 20:32:20 +010054 " Wait for up to 2 seconds for the port number to be there.
55 let cnt = 20
56 let l = []
57 while cnt > 0
58 try
59 let l = readfile("Xportnr")
60 catch
61 endtry
62 if len(l) >= 1
63 break
64 endif
65 sleep 100m
66 let cnt -= 1
67 endwhile
68 call delete("Xportnr")
69
70 if len(l) == 0
71 " Can't make the connection, give up.
72 call assert_false(1, "Can't start test_channel.py")
73 return -1
74 endif
75 let port = l[0]
76
77 call call(function(a:testfunc), [port])
78 catch
79 call assert_false(1, "Caught exception: " . v:exception)
80 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010081 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010082 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010083endfunc
84
85func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010086 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010087 if exists('s:job')
88 call job_stop(s:job)
89 unlet s:job
90 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010091 elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010092 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010093 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010094 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010095 endif
96endfunc
97
Bram Moolenaara07fec92016-02-05 21:04:08 +010098let s:responseMsg = ''
99func s:RequestHandler(handle, msg)
100 let s:responseHandle = a:handle
101 let s:responseMsg = a:msg
102endfunc
103
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100104func s:communicate(port)
105 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100106 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100107 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100108 return
109 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100110
111 " Simple string request and reply.
112 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
113
114 " Request that triggers sending two ex commands. These will usually be
115 " handled before getting the response, but it's not guaranteed, thus wait a
116 " tiny bit for the commands to get executed.
117 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
118 sleep 10m
119 call assert_equal('added1', getline(line('$') - 1))
120 call assert_equal('added2', getline('$'))
121
Bram Moolenaarf4160862016-02-05 23:09:12 +0100122 call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
123 sleep 10m
124 call assert_equal('added more', getline('$'))
125
Bram Moolenaara07fec92016-02-05 21:04:08 +0100126 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100127 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaara07fec92016-02-05 21:04:08 +0100128 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100129 if !exists('s:responseHandle')
130 call assert_false(1, 's:responseHandle was not set')
131 else
132 call assert_equal(handle, s:responseHandle)
133 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100134 call assert_equal('got it', s:responseMsg)
135
Bram Moolenaar77073442016-02-13 23:23:53 +0100136 unlet s:responseHandle
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100137 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100138 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100139 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100140 if !exists('s:responseHandle')
141 call assert_false(1, 's:responseHandle was not set')
142 else
143 call assert_equal(handle, s:responseHandle)
144 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100145 call assert_equal('got it', s:responseMsg)
146
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100147 " check setting options (without testing the effect)
148 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100149 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100150 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100151 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100152 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100153
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100154 " Send an eval request that works.
155 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100156 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100157 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
158
159 " Send an eval request that fails.
160 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100161 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100162 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
163
Bram Moolenaar55fab432016-02-07 16:53:13 +0100164 " Send an eval request that works but can't be encoded.
165 call assert_equal('ok', ch_sendexpr(handle, 'eval-error'))
166 sleep 10m
167 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
168
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100169 " Send a bad eval request. There will be no response.
170 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100171 sleep 10m
Bram Moolenaar55fab432016-02-07 16:53:13 +0100172 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100173
Bram Moolenaarf4160862016-02-05 23:09:12 +0100174 " Send an expr request
175 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
176 sleep 10m
177 call assert_equal('one', getline(line('$') - 2))
178 call assert_equal('two', getline(line('$') - 1))
179 call assert_equal('three', getline('$'))
180
181 " Request a redraw, we don't check for the effect.
182 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
183 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
184
185 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
186
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100187 " Reading while there is nothing available.
Bram Moolenaaraf7559f2016-02-20 21:48:25 +0100188 " TODO: make this work for MS-Windows
189 if has('unix')
190 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
191 let start = reltime()
192 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
193 let elapsed = reltime(start)
194 call assert_true(reltimefloat(elapsed) > 0.3)
195 call assert_true(reltimefloat(elapsed) < 0.6)
196 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100197
198 " Send without waiting for a response, then wait for a response.
199 call ch_sendexpr(handle, 'wait a bit', {'callback': 0})
200 let resp = ch_read(handle)
201 call assert_equal(type([]), type(resp))
202 call assert_equal(type(11), type(resp[0]))
203 call assert_equal('waited', resp[1])
204
Bram Moolenaard7ece102016-02-02 23:23:02 +0100205 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100206 call ch_sendexpr(handle, '!quit!', {'callback': 0})
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100207endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100208
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100210 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100211 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100212endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100213
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100214" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100215func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100216 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100217 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100218 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100219 return
220 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100222 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
223
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100224 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100225 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226 call assert_false(1, "Can't open second channel")
227 return
228 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100229 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
230 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
231
232 call ch_close(handle)
233 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
234
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235 call ch_close(newhandle)
236endfunc
237
238func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100239 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100240 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100241endfunc
242
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100243" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100244func s:server_crash(port)
245 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100246 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100247 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100248 return
249 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100250
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100251 call ch_sendexpr(handle, '!crash!')
252
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100253 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100254endfunc
255
256func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100257 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100258 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100259endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100260
Bram Moolenaarf6157282016-02-10 21:07:14 +0100261let s:reply = ""
262func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100263 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100264 let s:reply = a:msg
265endfunc
266
267func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100268 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100269 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100270 call assert_false(1, "Can't open channel")
271 return
272 endif
273
274 " Test that it works while waiting on a numbered message.
275 call assert_equal('ok', ch_sendexpr(handle, 'call me'))
276 sleep 10m
277 call assert_equal('we called you', s:reply)
278
279 " Test that it works while not waiting on a numbered message.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100280 call ch_sendexpr(handle, 'call me again', {'callback': 0})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100281 sleep 10m
282 call assert_equal('we did call you', s:reply)
283endfunc
284
285func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100286 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100287 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100288 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100289 let s:chopt.callback = function('s:Handler')
290 call s:run_server('s:channel_handler')
291 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292endfunc
293
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100294" Test that trying to connect to a non-existing port fails quickly.
295func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100296 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100297 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100298 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100299 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100300 " Oops, port does exists.
301 call ch_close(handle)
302 else
303 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100304 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100305 endif
306
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100307 " We intend to use a socket that doesn't exist and wait for half a second
308 " before giving up. If the socket does exist it can fail in various ways.
309 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100310 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100311 try
312 let handle = ch_open('localhost:9867', {'waittime': 500})
313 if ch_status(handle) != "fail"
314 " Oops, port does exists.
315 call ch_close(handle)
316 else
317 " Failed connection should wait about 500 msec.
318 let elapsed = reltime(start)
319 call assert_true(reltimefloat(elapsed) > 0.3)
320 call assert_true(reltimefloat(elapsed) < 1.0)
321 endif
322 catch
323 if v:exception !~ 'Connection reset by peer'
324 call assert_false(1, "Caught exception: " . v:exception)
325 endif
326 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100327endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100328
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100329func Test_raw_pipe()
330 if !has('job')
331 return
332 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100333 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100334 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
335 call assert_equal("run", job_status(job))
336 try
337 let handle = job_getchannel(job)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100338 call ch_sendraw(handle, "echo something\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100339 let msg = ch_readraw(handle)
340 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
341
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100342 call ch_sendraw(handle, "double this\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100343 let msg = ch_readraw(handle)
344 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
345
346 let reply = ch_sendraw(handle, "quit\n")
347 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
348 finally
349 call job_stop(job)
350 endtry
351endfunc
352
353func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100354 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100355 return
356 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100357 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100358 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100359 call assert_equal("run", job_status(job))
360 try
361 let handle = job_getchannel(job)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100362 call ch_sendraw(handle, "echo something\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100363 call assert_equal("something", ch_readraw(handle))
364
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100365 call ch_sendraw(handle, "double this\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100366 call assert_equal("this", ch_readraw(handle))
367 call assert_equal("AND this", ch_readraw(handle))
368
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100369 let reply = ch_sendraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100370 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100371 finally
372 call job_stop(job)
373 endtry
374endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100375
Bram Moolenaard46ae142016-02-16 13:33:52 +0100376""""""""""
377
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100378let s:unletResponse = ''
379func s:UnletHandler(handle, msg)
380 let s:unletResponse = a:msg
381 unlet s:channelfd
382endfunc
383
384" Test that "unlet handle" in a handler doesn't crash Vim.
385func s:unlet_handle(port)
386 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100387 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100388 sleep 10m
389 call assert_equal('what?', s:unletResponse)
390endfunc
391
392func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100393 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100394 call s:run_server('s:unlet_handle')
395endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100396
Bram Moolenaard46ae142016-02-16 13:33:52 +0100397""""""""""
398
399let s:unletResponse = ''
400func s:CloseHandler(handle, msg)
401 let s:unletResponse = a:msg
402 call ch_close(s:channelfd)
403endfunc
404
405" Test that "unlet handle" in a handler doesn't crash Vim.
406func s:close_handle(port)
407 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100408 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100409 sleep 10m
410 call assert_equal('what?', s:unletResponse)
411endfunc
412
413func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100414 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100415 call s:run_server('s:close_handle')
416endfunc
417
418""""""""""
419
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100420func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100421 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100422 silent! let ch = ch_open("noserver")
423 echo ch
424 let d = ch
425endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100426
427""""""""""
428
429func s:open_delay(port)
430 " Wait up to a second for the port to open.
431 let s:chopt.waittime = 1000
432 let channel = ch_open('localhost:' . a:port, s:chopt)
433 unlet s:chopt.waittime
434 if ch_status(channel) == "fail"
435 call assert_false(1, "Can't open channel")
436 return
437 endif
438 call assert_equal('got it', ch_sendexpr(channel, 'hello!'))
439 call ch_close(channel)
440endfunc
441
442func Test_open_delay()
443 call ch_log('Test_open_delay()')
444 " The server will wait half a second before creating the port.
445 call s:run_server('s:open_delay', 'delay')
446endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100447
448"""""""""
449
450function MyFunction(a,b,c)
451 let s:call_ret = [a:a, a:b, a:c]
452endfunc
453
454function s:test_call(port)
455 let handle = ch_open('localhost:' . a:port, s:chopt)
456 if ch_status(handle) == "fail"
457 call assert_false(1, "Can't open channel")
458 return
459 endif
460
461 call assert_equal('ok', ch_sendexpr(handle, 'call-func'))
462 sleep 20m
463 call assert_equal([1, 2, 3], s:call_ret)
464endfunc
465
466func Test_call()
467 call ch_log('Test_call()')
468 call s:run_server('s:test_call')
469endfunc