blob: 61ca9b73213aeb4ec3c433d29f0abc9b948bed80 [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 Moolenaar65edff82016-02-21 16:40:11 +010047 let s:job = job_start(cmd, {"stoponexit": "hup"})
48 call job_setoptions(s:job, {"stoponexit": "kill"})
Bram Moolenaard6a8d482016-02-10 20:32:20 +010049 elseif has('win32')
Bram Moolenaar81661fb2016-02-18 22:23:34 +010050 exe 'silent !start cmd /c start "test_channel" ' . cmd
Bram Moolenaard6a8d482016-02-10 20:32:20 +010051 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010052 exe 'silent !' . cmd . '&'
Bram Moolenaard7ece102016-02-02 23:23:02 +010053 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010054
Bram Moolenaard6a8d482016-02-10 20:32:20 +010055 " Wait for up to 2 seconds for the port number to be there.
56 let cnt = 20
57 let l = []
58 while cnt > 0
59 try
60 let l = readfile("Xportnr")
61 catch
62 endtry
63 if len(l) >= 1
64 break
65 endif
66 sleep 100m
67 let cnt -= 1
68 endwhile
69 call delete("Xportnr")
70
71 if len(l) == 0
72 " Can't make the connection, give up.
73 call assert_false(1, "Can't start test_channel.py")
74 return -1
75 endif
76 let port = l[0]
77
78 call call(function(a:testfunc), [port])
79 catch
80 call assert_false(1, "Caught exception: " . v:exception)
81 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010082 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010083 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010084endfunc
85
86func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010087 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010088 if exists('s:job')
89 call job_stop(s:job)
90 unlet s:job
91 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010092 elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010093 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010094 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010095 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010096 endif
97endfunc
98
Bram Moolenaara07fec92016-02-05 21:04:08 +010099let s:responseMsg = ''
100func s:RequestHandler(handle, msg)
101 let s:responseHandle = a:handle
102 let s:responseMsg = a:msg
103endfunc
104
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100105func s:communicate(port)
106 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100107 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100108 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100109 return
110 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100111
112 " Simple string request and reply.
113 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
114
115 " Request that triggers sending two ex commands. These will usually be
116 " handled before getting the response, but it's not guaranteed, thus wait a
117 " tiny bit for the commands to get executed.
118 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
119 sleep 10m
120 call assert_equal('added1', getline(line('$') - 1))
121 call assert_equal('added2', getline('$'))
122
Bram Moolenaarf4160862016-02-05 23:09:12 +0100123 call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
124 sleep 10m
125 call assert_equal('added more', getline('$'))
126
Bram Moolenaara07fec92016-02-05 21:04:08 +0100127 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100128 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaara07fec92016-02-05 21:04:08 +0100129 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100130 if !exists('s:responseHandle')
131 call assert_false(1, 's:responseHandle was not set')
132 else
133 call assert_equal(handle, s:responseHandle)
134 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100135 call assert_equal('got it', s:responseMsg)
136
Bram Moolenaar77073442016-02-13 23:23:53 +0100137 unlet s:responseHandle
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100138 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100139 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100140 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100141 if !exists('s:responseHandle')
142 call assert_false(1, 's:responseHandle was not set')
143 else
144 call assert_equal(handle, s:responseHandle)
145 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100146 call assert_equal('got it', s:responseMsg)
147
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100148 " check setting options (without testing the effect)
149 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100150 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100151 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100152 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100153 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100154
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100155 " Send an eval request that works.
156 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100157 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100158 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
159
160 " Send an eval request that fails.
161 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100162 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
164
Bram Moolenaar55fab432016-02-07 16:53:13 +0100165 " Send an eval request that works but can't be encoded.
166 call assert_equal('ok', ch_sendexpr(handle, 'eval-error'))
167 sleep 10m
168 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
169
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100170 " Send a bad eval request. There will be no response.
171 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100172 sleep 10m
Bram Moolenaar55fab432016-02-07 16:53:13 +0100173 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100174
Bram Moolenaarf4160862016-02-05 23:09:12 +0100175 " Send an expr request
176 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
177 sleep 10m
178 call assert_equal('one', getline(line('$') - 2))
179 call assert_equal('two', getline(line('$') - 1))
180 call assert_equal('three', getline('$'))
181
182 " Request a redraw, we don't check for the effect.
183 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
184 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
185
186 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
187
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100188 " Reading while there is nothing available.
Bram Moolenaaraf7559f2016-02-20 21:48:25 +0100189 " TODO: make this work for MS-Windows
190 if has('unix')
191 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
192 let start = reltime()
193 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
194 let elapsed = reltime(start)
195 call assert_true(reltimefloat(elapsed) > 0.3)
196 call assert_true(reltimefloat(elapsed) < 0.6)
197 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100198
199 " Send without waiting for a response, then wait for a response.
200 call ch_sendexpr(handle, 'wait a bit', {'callback': 0})
201 let resp = ch_read(handle)
202 call assert_equal(type([]), type(resp))
203 call assert_equal(type(11), type(resp[0]))
204 call assert_equal('waited', resp[1])
205
Bram Moolenaard7ece102016-02-02 23:23:02 +0100206 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100207 call ch_sendexpr(handle, '!quit!', {'callback': 0})
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100208endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100209
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100210func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100211 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100212 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100213endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100214
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100215" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100216func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100217 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100220 return
221 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100222
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100223 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
224
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100225 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100226 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100227 call assert_false(1, "Can't open second channel")
228 return
229 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100230 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
231 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
232
233 call ch_close(handle)
234 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
235
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236 call ch_close(newhandle)
237endfunc
238
239func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100240 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100241 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100242endfunc
243
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100244" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100245func s:server_crash(port)
246 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100247 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100249 return
250 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100251
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100252 call ch_sendexpr(handle, '!crash!')
253
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100254 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100255endfunc
256
257func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100258 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100259 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100260endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100261
Bram Moolenaarf6157282016-02-10 21:07:14 +0100262let s:reply = ""
263func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100264 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100265 let s:reply = a:msg
266endfunc
267
268func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100269 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100270 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100271 call assert_false(1, "Can't open channel")
272 return
273 endif
274
275 " Test that it works while waiting on a numbered message.
276 call assert_equal('ok', ch_sendexpr(handle, 'call me'))
277 sleep 10m
278 call assert_equal('we called you', s:reply)
279
280 " Test that it works while not waiting on a numbered message.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100281 call ch_sendexpr(handle, 'call me again', {'callback': 0})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100282 sleep 10m
283 call assert_equal('we did call you', s:reply)
284endfunc
285
286func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100287 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100288 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100289 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100290 let s:chopt.callback = function('s:Handler')
291 call s:run_server('s:channel_handler')
292 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100293endfunc
294
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100295" Test that trying to connect to a non-existing port fails quickly.
296func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100297 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100298 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100299 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100300 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100301 " Oops, port does exists.
302 call ch_close(handle)
303 else
304 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100305 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100306 endif
307
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100308 " We intend to use a socket that doesn't exist and wait for half a second
309 " before giving up. If the socket does exist it can fail in various ways.
310 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100311 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100312 try
313 let handle = ch_open('localhost:9867', {'waittime': 500})
314 if ch_status(handle) != "fail"
315 " Oops, port does exists.
316 call ch_close(handle)
317 else
318 " Failed connection should wait about 500 msec.
319 let elapsed = reltime(start)
320 call assert_true(reltimefloat(elapsed) > 0.3)
321 call assert_true(reltimefloat(elapsed) < 1.0)
322 endif
323 catch
324 if v:exception !~ 'Connection reset by peer'
325 call assert_false(1, "Caught exception: " . v:exception)
326 endif
327 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100328endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100329
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100330func Test_raw_pipe()
331 if !has('job')
332 return
333 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100334 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100335 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
336 call assert_equal("run", job_status(job))
337 try
338 let handle = job_getchannel(job)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100339 call ch_sendraw(handle, "echo something\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100340 let msg = ch_readraw(handle)
341 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
342
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100343 call ch_sendraw(handle, "double this\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100344 let msg = ch_readraw(handle)
345 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
346
347 let reply = ch_sendraw(handle, "quit\n")
348 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
349 finally
350 call job_stop(job)
351 endtry
352endfunc
353
354func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100355 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100356 return
357 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100358 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100359 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100360 call assert_equal("run", job_status(job))
361 try
362 let handle = job_getchannel(job)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100363 call ch_sendraw(handle, "echo something\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100364 call assert_equal("something", ch_readraw(handle))
365
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100366 call ch_sendraw(handle, "double this\n", {'callback': 0})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100367 call assert_equal("this", ch_readraw(handle))
368 call assert_equal("AND this", ch_readraw(handle))
369
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100370 let reply = ch_sendraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100371 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100372 finally
373 call job_stop(job)
374 endtry
375endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100376
Bram Moolenaard46ae142016-02-16 13:33:52 +0100377""""""""""
378
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100379let s:unletResponse = ''
380func s:UnletHandler(handle, msg)
381 let s:unletResponse = a:msg
382 unlet s:channelfd
383endfunc
384
385" Test that "unlet handle" in a handler doesn't crash Vim.
386func s:unlet_handle(port)
387 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100388 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100389 sleep 10m
390 call assert_equal('what?', s:unletResponse)
391endfunc
392
393func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100394 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100395 call s:run_server('s:unlet_handle')
396endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100397
Bram Moolenaard46ae142016-02-16 13:33:52 +0100398""""""""""
399
400let s:unletResponse = ''
401func s:CloseHandler(handle, msg)
402 let s:unletResponse = a:msg
403 call ch_close(s:channelfd)
404endfunc
405
406" Test that "unlet handle" in a handler doesn't crash Vim.
407func s:close_handle(port)
408 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100409 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100410 sleep 10m
411 call assert_equal('what?', s:unletResponse)
412endfunc
413
414func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100415 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100416 call s:run_server('s:close_handle')
417endfunc
418
419""""""""""
420
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100421func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100422 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100423 silent! let ch = ch_open("noserver")
424 echo ch
425 let d = ch
426endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100427
428""""""""""
429
430func s:open_delay(port)
431 " Wait up to a second for the port to open.
432 let s:chopt.waittime = 1000
433 let channel = ch_open('localhost:' . a:port, s:chopt)
434 unlet s:chopt.waittime
435 if ch_status(channel) == "fail"
436 call assert_false(1, "Can't open channel")
437 return
438 endif
439 call assert_equal('got it', ch_sendexpr(channel, 'hello!'))
440 call ch_close(channel)
441endfunc
442
443func Test_open_delay()
444 call ch_log('Test_open_delay()')
445 " The server will wait half a second before creating the port.
446 call s:run_server('s:open_delay', 'delay')
447endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100448
449"""""""""
450
451function MyFunction(a,b,c)
452 let s:call_ret = [a:a, a:b, a:c]
453endfunc
454
455function s:test_call(port)
456 let handle = ch_open('localhost:' . a:port, s:chopt)
457 if ch_status(handle) == "fail"
458 call assert_false(1, "Can't open channel")
459 return
460 endif
461
462 call assert_equal('ok', ch_sendexpr(handle, 'call-func'))
463 sleep 20m
464 call assert_equal([1, 2, 3], s:call_ret)
465endfunc
466
467func Test_call()
468 call ch_log('Test_call()')
469 call s:run_server('s:test_call')
470endfunc