blob: 5133a249654b4e98cc7bbeb468abe6c23e62b694 [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 Moolenaar839fd112016-03-06 21:34:03 +0100111 if has('job')
112 " check that no job is handled correctly
113 call assert_equal('no process', string(ch_getjob(handle)))
114 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100115
116 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100117 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100118
119 " Request that triggers sending two ex commands. These will usually be
120 " handled before getting the response, but it's not guaranteed, thus wait a
121 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100122 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100123 sleep 10m
124 call assert_equal('added1', getline(line('$') - 1))
125 call assert_equal('added2', getline('$'))
126
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100127 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100128 sleep 10m
129 call assert_equal('added more', getline('$'))
130
Bram Moolenaara07fec92016-02-05 21:04:08 +0100131 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100132 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaara07fec92016-02-05 21:04:08 +0100133 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100134 if !exists('s:responseHandle')
135 call assert_false(1, 's:responseHandle was not set')
136 else
137 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100138 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100139 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100140 call assert_equal('got it', s:responseMsg)
141
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100142 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100143 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100144 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100145 if !exists('s:responseHandle')
146 call assert_false(1, 's:responseHandle was not set')
147 else
148 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100149 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100150 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100151 call assert_equal('got it', s:responseMsg)
152
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100153 " Collect garbage, tests that our handle isn't collected.
154 call garbagecollect()
155
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100156 " check setting options (without testing the effect)
157 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100158 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100159 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100160 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100161 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100162
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100164 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100165 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100166 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100167
168 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100169 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100170 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100171 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100172
Bram Moolenaar55fab432016-02-07 16:53:13 +0100173 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100174 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100175 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100176 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100177
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100178 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100179 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100180 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100181 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100182
Bram Moolenaarf4160862016-02-05 23:09:12 +0100183 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100184 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100185 sleep 10m
186 call assert_equal('one', getline(line('$') - 2))
187 call assert_equal('two', getline(line('$') - 1))
188 call assert_equal('three', getline('$'))
189
190 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100191 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
192 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100193
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100194 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100195
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100196 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100197 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
198 let start = reltime()
199 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
200 let elapsed = reltime(start)
201 call assert_true(reltimefloat(elapsed) > 0.3)
202 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100203
204 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100205 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100206 let resp = ch_read(handle)
207 call assert_equal(type([]), type(resp))
208 call assert_equal(type(11), type(resp[0]))
209 call assert_equal('waited', resp[1])
210
Bram Moolenaard7ece102016-02-02 23:23:02 +0100211 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100212 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100213endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100214
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100215func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100216 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100217 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100218endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100219
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100220" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100222 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100223 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100224 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100225 return
226 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100227
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100228 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100229
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100230 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232 call assert_false(1, "Can't open second channel")
233 return
234 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100235 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
236 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100237
238 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100240
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100241 call ch_close(newhandle)
242endfunc
243
244func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100245 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100246 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100247endfunc
248
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100249" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100250func s:server_crash(port)
251 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100252 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100253 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100254 return
255 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100256
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100257 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100258
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100259 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100260endfunc
261
262func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100263 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100264 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100265endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100266
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100267"""""""""
268
Bram Moolenaarf6157282016-02-10 21:07:14 +0100269let s:reply = ""
270func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100271 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100272 let s:reply = a:msg
273endfunc
274
275func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100276 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100277 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100278 call assert_false(1, "Can't open channel")
279 return
280 endif
281
282 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100283 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100284 sleep 10m
285 call assert_equal('we called you', s:reply)
286
287 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100288 call ch_sendexpr(handle, 'call me again')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100289 sleep 10m
290 call assert_equal('we did call you', s:reply)
291endfunc
292
293func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100294 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100295 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100297 let s:chopt.callback = function('s:Handler')
298 call s:run_server('s:channel_handler')
299 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100300endfunc
301
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100302"""""""""
303
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100304let s:ch_reply = ''
305func s:ChHandler(chan, msg)
306 unlet s:ch_reply
307 let s:ch_reply = a:msg
308endfunc
309
310let s:zero_reply = ''
311func s:OneHandler(chan, msg)
312 unlet s:zero_reply
313 let s:zero_reply = a:msg
314endfunc
315
316func s:channel_zero(port)
317 let handle = ch_open('localhost:' . a:port, s:chopt)
318 if ch_status(handle) == "fail"
319 call assert_false(1, "Can't open channel")
320 return
321 endif
322
323 " Check that eval works.
324 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
325
326 " Check that eval works if a zero id message is sent back.
327 let s:ch_reply = ''
328 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
329 sleep 10m
330 if s:has_handler
331 call assert_equal('zero index', s:ch_reply)
332 else
333 call assert_equal('', s:ch_reply)
334 endif
335
336 " Check that handler works if a zero id message is sent back.
337 let s:ch_reply = ''
338 let s:zero_reply = ''
339 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
340 " Somehow the second message takes a bit of time.
341 for i in range(50)
342 if s:zero_reply == 'sent zero'
343 break
344 endif
345 sleep 10m
346 endfor
347 if s:has_handler
348 call assert_equal('zero index', s:ch_reply)
349 else
350 call assert_equal('', s:ch_reply)
351 endif
352 call assert_equal('sent zero', s:zero_reply)
353endfunc
354
355func Test_zero_reply()
356 call ch_log('Test_zero_reply()')
357 " Run with channel handler
358 let s:has_handler = 1
359 let s:chopt.callback = 's:ChHandler'
360 call s:run_server('s:channel_zero')
361 unlet s:chopt.callback
362
363 " Run without channel handler
364 let s:has_handler = 0
365 call s:run_server('s:channel_zero')
366endfunc
367
368"""""""""
369
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100370let s:reply1 = ""
371func s:HandleRaw1(chan, msg)
372 unlet s:reply1
373 let s:reply1 = a:msg
374endfunc
375
376let s:reply2 = ""
377func s:HandleRaw2(chan, msg)
378 unlet s:reply2
379 let s:reply2 = a:msg
380endfunc
381
382let s:reply3 = ""
383func s:HandleRaw3(chan, msg)
384 unlet s:reply3
385 let s:reply3 = a:msg
386endfunc
387
388func s:raw_one_time_callback(port)
389 let handle = ch_open('localhost:' . a:port, s:chopt)
390 if ch_status(handle) == "fail"
391 call assert_false(1, "Can't open channel")
392 return
393 endif
394 call ch_setoptions(handle, {'mode': 'raw'})
395
396 " The message are sent raw, we do our own JSON strings here.
397 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
398 sleep 10m
399 call assert_equal("[1, \"got it\"]", s:reply1)
400 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
401 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
402 sleep 10m
403 call assert_equal("[2, \"something\"]", s:reply2)
404 " wait for up to 500 msec for the 200 msec delayed reply
405 for i in range(50)
406 sleep 10m
407 if s:reply3 != ''
408 break
409 endif
410 endfor
411 call assert_equal("[3, \"waited\"]", s:reply3)
412endfunc
413
414func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100415 call ch_log('Test_raw_one_time_callback()')
416 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100417endfunc
418
419"""""""""
420
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100421" Test that trying to connect to a non-existing port fails quickly.
422func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100423 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100424 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100425 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100426 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100427 " Oops, port does exists.
428 call ch_close(handle)
429 else
430 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100431 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100432 endif
433
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100434 " We intend to use a socket that doesn't exist and wait for half a second
435 " before giving up. If the socket does exist it can fail in various ways.
436 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100437 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100438 try
439 let handle = ch_open('localhost:9867', {'waittime': 500})
440 if ch_status(handle) != "fail"
441 " Oops, port does exists.
442 call ch_close(handle)
443 else
444 " Failed connection should wait about 500 msec.
445 let elapsed = reltime(start)
446 call assert_true(reltimefloat(elapsed) > 0.3)
447 call assert_true(reltimefloat(elapsed) < 1.0)
448 endif
449 catch
450 if v:exception !~ 'Connection reset by peer'
451 call assert_false(1, "Caught exception: " . v:exception)
452 endif
453 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100454endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100455
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100456"""""""""
457
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100458func Test_raw_pipe()
459 if !has('job')
460 return
461 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100462 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100463 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
464 call assert_equal("run", job_status(job))
465 try
466 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100467 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100468 let msg = ch_readraw(handle)
469 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
470
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100471 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100472 let msg = ch_readraw(handle)
473 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
474
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100475 let reply = ch_evalraw(handle, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100476 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
477 finally
478 call job_stop(job)
479 endtry
480endfunc
481
482func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100483 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100484 return
485 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100486 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100487 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100488 call assert_equal("run", job_status(job))
489 try
490 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100491 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100492 call assert_equal("something", ch_readraw(handle))
493
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100494 call ch_sendraw(handle, "echoerr wrong\n")
495 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
496
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100497 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100498 call assert_equal("this", ch_readraw(handle))
499 call assert_equal("AND this", ch_readraw(handle))
500
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100502 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100503 finally
504 call job_stop(job)
505 endtry
506endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100507
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100508func Test_nl_err_to_out_pipe()
509 if !has('job')
510 return
511 endif
512 call ch_log('Test_nl_err_to_out_pipe()')
513 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
514 call assert_equal("run", job_status(job))
515 try
516 let handle = job_getchannel(job)
517 call ch_sendraw(handle, "echo something\n")
518 call assert_equal("something", ch_readraw(handle))
519
520 call ch_sendraw(handle, "echoerr wrong\n")
521 call assert_equal("wrong", ch_readraw(handle))
522 finally
523 call job_stop(job)
524 endtry
525endfunc
526
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100527func Test_pipe_to_buffer()
528 if !has('job')
529 return
530 endif
531 call ch_log('Test_pipe_to_buffer()')
532 let job = job_start(s:python . " test_channel_pipe.py",
533 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
534 call assert_equal("run", job_status(job))
535 try
536 let handle = job_getchannel(job)
537 call ch_sendraw(handle, "echo line one\n")
538 call ch_sendraw(handle, "echo line two\n")
539 call ch_sendraw(handle, "double this\n")
540 call ch_sendraw(handle, "quit\n")
541 sp pipe-output
542 for i in range(100)
543 sleep 10m
544 if line('$') >= 6
545 break
546 endif
547 endfor
548 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
549 bwipe!
550 finally
551 call job_stop(job)
552 endtry
553endfunc
554
Bram Moolenaar014069a2016-03-03 22:51:40 +0100555func Test_pipe_from_buffer()
556 if !has('job')
557 return
558 endif
559call ch_logfile('channellog', 'w')
560 call ch_log('Test_pipe_from_buffer()')
561
562 sp pipe-input
563 call setline(1, ['echo one', 'echo two', 'echo three'])
564
565 let job = job_start(s:python . " test_channel_pipe.py",
566 \ {'in-io': 'buffer', 'in-name': 'pipe-input'})
567 call assert_equal("run", job_status(job))
568 try
569 let handle = job_getchannel(job)
570 call assert_equal('one', ch_read(handle))
571 call assert_equal('two', ch_read(handle))
572 call assert_equal('three', ch_read(handle))
573 bwipe!
574 finally
575 call job_stop(job)
576 endtry
577call ch_logfile('')
578endfunc
579
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100580func Test_pipe_to_nameless_buffer()
581 if !has('job')
582 return
583 endif
584 call ch_log('Test_pipe_to_nameless_buffer()')
585 let job = job_start(s:python . " test_channel_pipe.py",
586 \ {'out-io': 'buffer'})
587 call assert_equal("run", job_status(job))
588 try
589 let handle = job_getchannel(job)
590 call ch_sendraw(handle, "echo line one\n")
591 call ch_sendraw(handle, "echo line two\n")
592 exe ch_getbufnr(handle, "out") . 'sbuf'
593 for i in range(100)
594 sleep 10m
595 if line('$') >= 3
596 break
597 endif
598 endfor
599 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
600 bwipe!
601 finally
602 call job_stop(job)
603 endtry
604endfunc
605
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100606func Test_pipe_to_buffer_json()
607 if !has('job')
608 return
609 endif
610 call ch_log('Test_pipe_to_buffer_json()')
611 let job = job_start(s:python . " test_channel_pipe.py",
612 \ {'out-io': 'buffer', 'out-mode': 'json'})
613 call assert_equal("run", job_status(job))
614 try
615 let handle = job_getchannel(job)
616 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
617 call ch_sendraw(handle, "echo [-2, 12.34]\n")
618 exe ch_getbufnr(handle, "out") . 'sbuf'
619 for i in range(100)
620 sleep 10m
621 if line('$') >= 3
622 break
623 endif
624 endfor
625 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
626 bwipe!
627 finally
628 call job_stop(job)
629 endtry
630endfunc
631
Bram Moolenaard46ae142016-02-16 13:33:52 +0100632""""""""""
633
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100634let s:unletResponse = ''
635func s:UnletHandler(handle, msg)
636 let s:unletResponse = a:msg
637 unlet s:channelfd
638endfunc
639
640" Test that "unlet handle" in a handler doesn't crash Vim.
641func s:unlet_handle(port)
642 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100643 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100644 sleep 10m
645 call assert_equal('what?', s:unletResponse)
646endfunc
647
648func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100649 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100650 call s:run_server('s:unlet_handle')
651endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100652
Bram Moolenaard46ae142016-02-16 13:33:52 +0100653""""""""""
654
655let s:unletResponse = ''
656func s:CloseHandler(handle, msg)
657 let s:unletResponse = a:msg
658 call ch_close(s:channelfd)
659endfunc
660
661" Test that "unlet handle" in a handler doesn't crash Vim.
662func s:close_handle(port)
663 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100664 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100665 sleep 10m
666 call assert_equal('what?', s:unletResponse)
667endfunc
668
669func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100671 call s:run_server('s:close_handle')
672endfunc
673
674""""""""""
675
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100676func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100677 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100678 silent! let ch = ch_open("noserver")
679 echo ch
680 let d = ch
681endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682
683""""""""""
684
685func s:open_delay(port)
686 " Wait up to a second for the port to open.
687 let s:chopt.waittime = 1000
688 let channel = ch_open('localhost:' . a:port, s:chopt)
689 unlet s:chopt.waittime
690 if ch_status(channel) == "fail"
691 call assert_false(1, "Can't open channel")
692 return
693 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100694 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100695 call ch_close(channel)
696endfunc
697
698func Test_open_delay()
699 call ch_log('Test_open_delay()')
700 " The server will wait half a second before creating the port.
701 call s:run_server('s:open_delay', 'delay')
702endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100703
704"""""""""
705
706function MyFunction(a,b,c)
707 let s:call_ret = [a:a, a:b, a:c]
708endfunc
709
710function s:test_call(port)
711 let handle = ch_open('localhost:' . a:port, s:chopt)
712 if ch_status(handle) == "fail"
713 call assert_false(1, "Can't open channel")
714 return
715 endif
716
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100717 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100718 sleep 20m
719 call assert_equal([1, 2, 3], s:call_ret)
720endfunc
721
722func Test_call()
723 call ch_log('Test_call()')
724 call s:run_server('s:test_call')
725endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100726
727"""""""""
728
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100729let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100730function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100731 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100732endfunc
733
734function s:test_exit_callback(port)
735 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
736 let s:exit_job = s:job
737endfunc
738
739func Test_exit_callback()
740 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100741 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100742 call s:run_server('s:test_exit_callback')
743
Bram Moolenaar9730f742016-02-28 19:50:51 +0100744 " wait up to a second for the job to exit
745 for i in range(100)
746 if s:job_exit_ret == 'done'
747 break
748 endif
749 sleep 10m
750 " calling job_status() triggers the callback
751 call job_status(s:exit_job)
752 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100753
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100754 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100755 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100756 endif
757endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100758
759"""""""""
760
761let s:ch_close_ret = 'alive'
762function MyCloseCb(ch)
763 let s:ch_close_ret = 'closed'
764endfunc
765
766function s:test_close_callback(port)
767 let handle = ch_open('localhost:' . a:port, s:chopt)
768 if ch_status(handle) == "fail"
769 call assert_false(1, "Can't open channel")
770 return
771 endif
772 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
773
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100774 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100775 sleep 20m
776 call assert_equal('closed', s:ch_close_ret)
777endfunc
778
779func Test_close_callback()
780 call ch_log('Test_close_callback()')
781 call s:run_server('s:test_close_callback')
782endfunc
783
Bram Moolenaar9730f742016-02-28 19:50:51 +0100784" Uncomment this to see what happens, output is in src/testdir/channellog.
785" call ch_logfile('channellog', 'w')