blob: 31a4ff6eb29730c496a0f0fa4407e8cbf71ffe51 [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.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100113 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100114
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.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100118 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100119 sleep 10m
120 call assert_equal('added1', getline(line('$') - 1))
121 call assert_equal('added2', getline('$'))
122
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100123 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100124 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)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100134 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100135 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100136 call assert_equal('got it', s:responseMsg)
137
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)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100145 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100146 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100147 call assert_equal('got it', s:responseMsg)
148
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100149 " Collect garbage, tests that our handle isn't collected.
150 call garbagecollect()
151
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100152 " check setting options (without testing the effect)
153 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100154 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100155 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100156 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100157 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100158
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100159 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100160 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100161 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100162 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163
164 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100165 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100166 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100167 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100168
Bram Moolenaar55fab432016-02-07 16:53:13 +0100169 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100170 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100171 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100173
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100174 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100176 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100178
Bram Moolenaarf4160862016-02-05 23:09:12 +0100179 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100181 sleep 10m
182 call assert_equal('one', getline(line('$') - 2))
183 call assert_equal('two', getline(line('$') - 1))
184 call assert_equal('three', getline('$'))
185
186 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
188 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100189
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100191
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100192 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100193 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
194 let start = reltime()
195 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
196 let elapsed = reltime(start)
197 call assert_true(reltimefloat(elapsed) > 0.3)
198 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100199
200 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100202 let resp = ch_read(handle)
203 call assert_equal(type([]), type(resp))
204 call assert_equal(type(11), type(resp[0]))
205 call assert_equal('waited', resp[1])
206
Bram Moolenaard7ece102016-02-02 23:23:02 +0100207 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100208 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100210
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100211func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100212 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100213 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100214endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100215
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100216" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100217func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100218 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100219 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100221 return
222 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100223
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100224 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100225
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100227 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100228 call assert_false(1, "Can't open second channel")
229 return
230 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100231 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
232 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100233
234 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100235 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100236
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100237 call ch_close(newhandle)
238endfunc
239
240func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100241 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100242 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100243endfunc
244
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100245" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100246func s:server_crash(port)
247 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100248 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100249 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100250 return
251 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100252
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100253 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100254
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100255 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100256endfunc
257
258func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100259 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100260 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100261endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100262
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100263"""""""""
264
Bram Moolenaarf6157282016-02-10 21:07:14 +0100265let s:reply = ""
266func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100267 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100268 let s:reply = a:msg
269endfunc
270
271func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100272 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100273 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100274 call assert_false(1, "Can't open channel")
275 return
276 endif
277
278 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100279 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280 sleep 10m
281 call assert_equal('we called you', s:reply)
282
283 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100284 call ch_sendexpr(handle, 'call me again')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100285 sleep 10m
286 call assert_equal('we did call you', s:reply)
287endfunc
288
289func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100290 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100291 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100293 let s:chopt.callback = function('s:Handler')
294 call s:run_server('s:channel_handler')
295 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296endfunc
297
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100298"""""""""
299
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100300let s:ch_reply = ''
301func s:ChHandler(chan, msg)
302 unlet s:ch_reply
303 let s:ch_reply = a:msg
304endfunc
305
306let s:zero_reply = ''
307func s:OneHandler(chan, msg)
308 unlet s:zero_reply
309 let s:zero_reply = a:msg
310endfunc
311
312func s:channel_zero(port)
313 let handle = ch_open('localhost:' . a:port, s:chopt)
314 if ch_status(handle) == "fail"
315 call assert_false(1, "Can't open channel")
316 return
317 endif
318
319 " Check that eval works.
320 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
321
322 " Check that eval works if a zero id message is sent back.
323 let s:ch_reply = ''
324 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
325 sleep 10m
326 if s:has_handler
327 call assert_equal('zero index', s:ch_reply)
328 else
329 call assert_equal('', s:ch_reply)
330 endif
331
332 " Check that handler works if a zero id message is sent back.
333 let s:ch_reply = ''
334 let s:zero_reply = ''
335 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
336 " Somehow the second message takes a bit of time.
337 for i in range(50)
338 if s:zero_reply == 'sent zero'
339 break
340 endif
341 sleep 10m
342 endfor
343 if s:has_handler
344 call assert_equal('zero index', s:ch_reply)
345 else
346 call assert_equal('', s:ch_reply)
347 endif
348 call assert_equal('sent zero', s:zero_reply)
349endfunc
350
351func Test_zero_reply()
352 call ch_log('Test_zero_reply()')
353 " Run with channel handler
354 let s:has_handler = 1
355 let s:chopt.callback = 's:ChHandler'
356 call s:run_server('s:channel_zero')
357 unlet s:chopt.callback
358
359 " Run without channel handler
360 let s:has_handler = 0
361 call s:run_server('s:channel_zero')
362endfunc
363
364"""""""""
365
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100366let s:reply1 = ""
367func s:HandleRaw1(chan, msg)
368 unlet s:reply1
369 let s:reply1 = a:msg
370endfunc
371
372let s:reply2 = ""
373func s:HandleRaw2(chan, msg)
374 unlet s:reply2
375 let s:reply2 = a:msg
376endfunc
377
378let s:reply3 = ""
379func s:HandleRaw3(chan, msg)
380 unlet s:reply3
381 let s:reply3 = a:msg
382endfunc
383
384func s:raw_one_time_callback(port)
385 let handle = ch_open('localhost:' . a:port, s:chopt)
386 if ch_status(handle) == "fail"
387 call assert_false(1, "Can't open channel")
388 return
389 endif
390 call ch_setoptions(handle, {'mode': 'raw'})
391
392 " The message are sent raw, we do our own JSON strings here.
393 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
394 sleep 10m
395 call assert_equal("[1, \"got it\"]", s:reply1)
396 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
397 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
398 sleep 10m
399 call assert_equal("[2, \"something\"]", s:reply2)
400 " wait for up to 500 msec for the 200 msec delayed reply
401 for i in range(50)
402 sleep 10m
403 if s:reply3 != ''
404 break
405 endif
406 endfor
407 call assert_equal("[3, \"waited\"]", s:reply3)
408endfunc
409
410func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100411 call ch_log('Test_raw_one_time_callback()')
412 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100413endfunc
414
415"""""""""
416
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100417" Test that trying to connect to a non-existing port fails quickly.
418func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100419 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100420 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100421 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100422 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100423 " Oops, port does exists.
424 call ch_close(handle)
425 else
426 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100427 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428 endif
429
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100430 " We intend to use a socket that doesn't exist and wait for half a second
431 " before giving up. If the socket does exist it can fail in various ways.
432 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100433 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100434 try
435 let handle = ch_open('localhost:9867', {'waittime': 500})
436 if ch_status(handle) != "fail"
437 " Oops, port does exists.
438 call ch_close(handle)
439 else
440 " Failed connection should wait about 500 msec.
441 let elapsed = reltime(start)
442 call assert_true(reltimefloat(elapsed) > 0.3)
443 call assert_true(reltimefloat(elapsed) < 1.0)
444 endif
445 catch
446 if v:exception !~ 'Connection reset by peer'
447 call assert_false(1, "Caught exception: " . v:exception)
448 endif
449 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100450endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100451
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100452"""""""""
453
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100454func Test_raw_pipe()
455 if !has('job')
456 return
457 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100458 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100459 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
460 call assert_equal("run", job_status(job))
461 try
462 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100463 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100464 let msg = ch_readraw(handle)
465 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
466
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100467 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100468 let msg = ch_readraw(handle)
469 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
470
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100471 let reply = ch_evalraw(handle, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100472 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
473 finally
474 call job_stop(job)
475 endtry
476endfunc
477
478func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100479 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100480 return
481 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100482 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100483 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100484 call assert_equal("run", job_status(job))
485 try
486 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100487 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100488 call assert_equal("something", ch_readraw(handle))
489
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100490 call ch_sendraw(handle, "echoerr wrong\n")
491 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
492
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100493 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494 call assert_equal("this", ch_readraw(handle))
495 call assert_equal("AND this", ch_readraw(handle))
496
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100497 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100498 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100499 finally
500 call job_stop(job)
501 endtry
502endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100503
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100504func Test_nl_err_to_out_pipe()
505 if !has('job')
506 return
507 endif
508 call ch_log('Test_nl_err_to_out_pipe()')
509 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
510 call assert_equal("run", job_status(job))
511 try
512 let handle = job_getchannel(job)
513 call ch_sendraw(handle, "echo something\n")
514 call assert_equal("something", ch_readraw(handle))
515
516 call ch_sendraw(handle, "echoerr wrong\n")
517 call assert_equal("wrong", ch_readraw(handle))
518 finally
519 call job_stop(job)
520 endtry
521endfunc
522
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100523func Test_pipe_to_buffer()
524 if !has('job')
525 return
526 endif
527 call ch_log('Test_pipe_to_buffer()')
528 let job = job_start(s:python . " test_channel_pipe.py",
529 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
530 call assert_equal("run", job_status(job))
531 try
532 let handle = job_getchannel(job)
533 call ch_sendraw(handle, "echo line one\n")
534 call ch_sendraw(handle, "echo line two\n")
535 call ch_sendraw(handle, "double this\n")
536 call ch_sendraw(handle, "quit\n")
537 sp pipe-output
538 for i in range(100)
539 sleep 10m
540 if line('$') >= 6
541 break
542 endif
543 endfor
544 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
545 bwipe!
546 finally
547 call job_stop(job)
548 endtry
549endfunc
550
Bram Moolenaar014069a2016-03-03 22:51:40 +0100551func Test_pipe_from_buffer()
552 if !has('job')
553 return
554 endif
555call ch_logfile('channellog', 'w')
556 call ch_log('Test_pipe_from_buffer()')
557
558 sp pipe-input
559 call setline(1, ['echo one', 'echo two', 'echo three'])
560
561 let job = job_start(s:python . " test_channel_pipe.py",
562 \ {'in-io': 'buffer', 'in-name': 'pipe-input'})
563 call assert_equal("run", job_status(job))
564 try
565 let handle = job_getchannel(job)
566 call assert_equal('one', ch_read(handle))
567 call assert_equal('two', ch_read(handle))
568 call assert_equal('three', ch_read(handle))
569 bwipe!
570 finally
571 call job_stop(job)
572 endtry
573call ch_logfile('')
574endfunc
575
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100576func Test_pipe_to_nameless_buffer()
577 if !has('job')
578 return
579 endif
580 call ch_log('Test_pipe_to_nameless_buffer()')
581 let job = job_start(s:python . " test_channel_pipe.py",
582 \ {'out-io': 'buffer'})
583 call assert_equal("run", job_status(job))
584 try
585 let handle = job_getchannel(job)
586 call ch_sendraw(handle, "echo line one\n")
587 call ch_sendraw(handle, "echo line two\n")
588 exe ch_getbufnr(handle, "out") . 'sbuf'
589 for i in range(100)
590 sleep 10m
591 if line('$') >= 3
592 break
593 endif
594 endfor
595 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
596 bwipe!
597 finally
598 call job_stop(job)
599 endtry
600endfunc
601
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100602func Test_pipe_to_buffer_json()
603 if !has('job')
604 return
605 endif
606 call ch_log('Test_pipe_to_buffer_json()')
607 let job = job_start(s:python . " test_channel_pipe.py",
608 \ {'out-io': 'buffer', 'out-mode': 'json'})
609 call assert_equal("run", job_status(job))
610 try
611 let handle = job_getchannel(job)
612 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
613 call ch_sendraw(handle, "echo [-2, 12.34]\n")
614 exe ch_getbufnr(handle, "out") . 'sbuf'
615 for i in range(100)
616 sleep 10m
617 if line('$') >= 3
618 break
619 endif
620 endfor
621 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
622 bwipe!
623 finally
624 call job_stop(job)
625 endtry
626endfunc
627
Bram Moolenaard46ae142016-02-16 13:33:52 +0100628""""""""""
629
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100630let s:unletResponse = ''
631func s:UnletHandler(handle, msg)
632 let s:unletResponse = a:msg
633 unlet s:channelfd
634endfunc
635
636" Test that "unlet handle" in a handler doesn't crash Vim.
637func s:unlet_handle(port)
638 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100639 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100640 sleep 10m
641 call assert_equal('what?', s:unletResponse)
642endfunc
643
644func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100645 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100646 call s:run_server('s:unlet_handle')
647endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100648
Bram Moolenaard46ae142016-02-16 13:33:52 +0100649""""""""""
650
651let s:unletResponse = ''
652func s:CloseHandler(handle, msg)
653 let s:unletResponse = a:msg
654 call ch_close(s:channelfd)
655endfunc
656
657" Test that "unlet handle" in a handler doesn't crash Vim.
658func s:close_handle(port)
659 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100660 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100661 sleep 10m
662 call assert_equal('what?', s:unletResponse)
663endfunc
664
665func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100666 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100667 call s:run_server('s:close_handle')
668endfunc
669
670""""""""""
671
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100672func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100673 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100674 silent! let ch = ch_open("noserver")
675 echo ch
676 let d = ch
677endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100678
679""""""""""
680
681func s:open_delay(port)
682 " Wait up to a second for the port to open.
683 let s:chopt.waittime = 1000
684 let channel = ch_open('localhost:' . a:port, s:chopt)
685 unlet s:chopt.waittime
686 if ch_status(channel) == "fail"
687 call assert_false(1, "Can't open channel")
688 return
689 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100690 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 call ch_close(channel)
692endfunc
693
694func Test_open_delay()
695 call ch_log('Test_open_delay()')
696 " The server will wait half a second before creating the port.
697 call s:run_server('s:open_delay', 'delay')
698endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100699
700"""""""""
701
702function MyFunction(a,b,c)
703 let s:call_ret = [a:a, a:b, a:c]
704endfunc
705
706function s:test_call(port)
707 let handle = ch_open('localhost:' . a:port, s:chopt)
708 if ch_status(handle) == "fail"
709 call assert_false(1, "Can't open channel")
710 return
711 endif
712
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100713 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100714 sleep 20m
715 call assert_equal([1, 2, 3], s:call_ret)
716endfunc
717
718func Test_call()
719 call ch_log('Test_call()')
720 call s:run_server('s:test_call')
721endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100722
723"""""""""
724
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100725let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100726function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100727 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100728endfunc
729
730function s:test_exit_callback(port)
731 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
732 let s:exit_job = s:job
733endfunc
734
735func Test_exit_callback()
736 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100737 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100738 call s:run_server('s:test_exit_callback')
739
Bram Moolenaar9730f742016-02-28 19:50:51 +0100740 " wait up to a second for the job to exit
741 for i in range(100)
742 if s:job_exit_ret == 'done'
743 break
744 endif
745 sleep 10m
746 " calling job_status() triggers the callback
747 call job_status(s:exit_job)
748 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100749
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100750 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100751 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100752 endif
753endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100754
755"""""""""
756
757let s:ch_close_ret = 'alive'
758function MyCloseCb(ch)
759 let s:ch_close_ret = 'closed'
760endfunc
761
762function s:test_close_callback(port)
763 let handle = ch_open('localhost:' . a:port, s:chopt)
764 if ch_status(handle) == "fail"
765 call assert_false(1, "Can't open channel")
766 return
767 endif
768 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
769
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100770 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100771 sleep 20m
772 call assert_equal('closed', s:ch_close_ret)
773endfunc
774
775func Test_close_callback()
776 call ch_log('Test_close_callback()')
777 call s:run_server('s:test_close_callback')
778endfunc
779
Bram Moolenaar9730f742016-02-28 19:50:51 +0100780" Uncomment this to see what happens, output is in src/testdir/channellog.
781" call ch_logfile('channellog', 'w')