blob: 299bb2ba7b53f49965ab9db1acabf1d75d949d6f [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 Moolenaarb69fccf2016-03-06 23:06:25 +0100527func Test_nl_read_file()
528 if !has('job')
529 return
530 endif
531 " TODO: make this work for MS-Windows.
532 if !has('unix')
533 return
534 endif
535 call ch_log('Test_nl_read_file()')
536 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
537 let job = job_start(s:python . " test_channel_pipe.py",
538 \ {'in-io': 'file', 'in-name': 'Xinput'})
539 call assert_equal("run", job_status(job))
540 try
541 let handle = job_getchannel(job)
542 call assert_equal("something", ch_readraw(handle))
543 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
544 call assert_equal("this", ch_readraw(handle))
545 call assert_equal("AND this", ch_readraw(handle))
546 finally
547 call job_stop(job)
548 call delete('Xinput')
549 endtry
550endfunc
551
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100552func Test_pipe_to_buffer()
553 if !has('job')
554 return
555 endif
556 call ch_log('Test_pipe_to_buffer()')
557 let job = job_start(s:python . " test_channel_pipe.py",
558 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
559 call assert_equal("run", job_status(job))
560 try
561 let handle = job_getchannel(job)
562 call ch_sendraw(handle, "echo line one\n")
563 call ch_sendraw(handle, "echo line two\n")
564 call ch_sendraw(handle, "double this\n")
565 call ch_sendraw(handle, "quit\n")
566 sp pipe-output
567 for i in range(100)
568 sleep 10m
569 if line('$') >= 6
570 break
571 endif
572 endfor
573 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
574 bwipe!
575 finally
576 call job_stop(job)
577 endtry
578endfunc
579
Bram Moolenaar014069a2016-03-03 22:51:40 +0100580func Test_pipe_from_buffer()
581 if !has('job')
582 return
583 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100584 call ch_log('Test_pipe_from_buffer()')
585
586 sp pipe-input
587 call setline(1, ['echo one', 'echo two', 'echo three'])
588
589 let job = job_start(s:python . " test_channel_pipe.py",
590 \ {'in-io': 'buffer', 'in-name': 'pipe-input'})
591 call assert_equal("run", job_status(job))
592 try
593 let handle = job_getchannel(job)
594 call assert_equal('one', ch_read(handle))
595 call assert_equal('two', ch_read(handle))
596 call assert_equal('three', ch_read(handle))
597 bwipe!
598 finally
599 call job_stop(job)
600 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100601endfunc
602
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100603func Test_pipe_to_nameless_buffer()
604 if !has('job')
605 return
606 endif
607 call ch_log('Test_pipe_to_nameless_buffer()')
608 let job = job_start(s:python . " test_channel_pipe.py",
609 \ {'out-io': 'buffer'})
610 call assert_equal("run", job_status(job))
611 try
612 let handle = job_getchannel(job)
613 call ch_sendraw(handle, "echo line one\n")
614 call ch_sendraw(handle, "echo line two\n")
615 exe ch_getbufnr(handle, "out") . 'sbuf'
616 for i in range(100)
617 sleep 10m
618 if line('$') >= 3
619 break
620 endif
621 endfor
622 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
623 bwipe!
624 finally
625 call job_stop(job)
626 endtry
627endfunc
628
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100629func Test_pipe_to_buffer_json()
630 if !has('job')
631 return
632 endif
633 call ch_log('Test_pipe_to_buffer_json()')
634 let job = job_start(s:python . " test_channel_pipe.py",
635 \ {'out-io': 'buffer', 'out-mode': 'json'})
636 call assert_equal("run", job_status(job))
637 try
638 let handle = job_getchannel(job)
639 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
640 call ch_sendraw(handle, "echo [-2, 12.34]\n")
641 exe ch_getbufnr(handle, "out") . 'sbuf'
642 for i in range(100)
643 sleep 10m
644 if line('$') >= 3
645 break
646 endif
647 endfor
648 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
649 bwipe!
650 finally
651 call job_stop(job)
652 endtry
653endfunc
654
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100655" Wait a little while for the last line, minus "offset", to equal "line".
656func Wait_for_last_line(line, offset)
657 for i in range(100)
658 sleep 10m
659 if getline(line('$') - a:offset) == a:line
660 break
661 endif
662 endfor
663endfunc
664
665func Test_pipe_io_two_buffers()
666 if !has('job')
667 return
668 endif
669 call ch_log('Test_pipe_io_two_buffers()')
670
671 " Create two buffers, one to read from and one to write to.
672 split pipe-output
673 set buftype=nofile
674 split pipe-input
675 set buftype=nofile
676
677 let job = job_start(s:python . " test_channel_pipe.py",
678 \ {'in-io': 'buffer', 'in-name': 'pipe-input', 'in-top': 0,
679 \ 'out-io': 'buffer', 'out-name': 'pipe-output'})
680 call assert_equal("run", job_status(job))
681 try
682 exe "normal Gaecho hello\<CR>"
683 exe bufwinnr('pipe-output') . "wincmd w"
684 call Wait_for_last_line('hello', 0)
685 call assert_equal('hello', getline('$'))
686
687 exe bufwinnr('pipe-input') . "wincmd w"
688 exe "normal Gadouble this\<CR>"
689 exe bufwinnr('pipe-output') . "wincmd w"
690 call Wait_for_last_line('AND this', 0)
691 call assert_equal('this', getline(line('$') - 1))
692 call assert_equal('AND this', getline('$'))
693
694 bwipe!
695 exe bufwinnr('pipe-input') . "wincmd w"
696 bwipe!
697 finally
698 call job_stop(job)
699 endtry
700endfunc
701
702func Test_pipe_io_one_buffer()
703 if !has('job')
704 return
705 endif
706 call ch_log('Test_pipe_io_one_buffer()')
707
708 " Create one buffer to read from and to write to.
709 split pipe-io
710 set buftype=nofile
711
712 let job = job_start(s:python . " test_channel_pipe.py",
713 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0,
714 \ 'out-io': 'buffer', 'out-name': 'pipe-io'})
715 call assert_equal("run", job_status(job))
716 try
717 exe "normal Goecho hello\<CR>"
718 call Wait_for_last_line('hello', 1)
719 call assert_equal('hello', getline(line('$') - 1))
720
721 exe "normal Gadouble this\<CR>"
722 call Wait_for_last_line('AND this', 1)
723 call assert_equal('this', getline(line('$') - 2))
724 call assert_equal('AND this', getline(line('$') - 1))
725
726 bwipe!
727 finally
728 call job_stop(job)
729 endtry
730endfunc
731
Bram Moolenaard46ae142016-02-16 13:33:52 +0100732""""""""""
733
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100734let s:unletResponse = ''
735func s:UnletHandler(handle, msg)
736 let s:unletResponse = a:msg
737 unlet s:channelfd
738endfunc
739
740" Test that "unlet handle" in a handler doesn't crash Vim.
741func s:unlet_handle(port)
742 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100743 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100744 sleep 10m
745 call assert_equal('what?', s:unletResponse)
746endfunc
747
748func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100749 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100750 call s:run_server('s:unlet_handle')
751endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100752
Bram Moolenaard46ae142016-02-16 13:33:52 +0100753""""""""""
754
755let s:unletResponse = ''
756func s:CloseHandler(handle, msg)
757 let s:unletResponse = a:msg
758 call ch_close(s:channelfd)
759endfunc
760
761" Test that "unlet handle" in a handler doesn't crash Vim.
762func s:close_handle(port)
763 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100764 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100765 sleep 10m
766 call assert_equal('what?', s:unletResponse)
767endfunc
768
769func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100771 call s:run_server('s:close_handle')
772endfunc
773
774""""""""""
775
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100776func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100777 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100778 silent! let ch = ch_open("noserver")
779 echo ch
780 let d = ch
781endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100782
783""""""""""
784
785func s:open_delay(port)
786 " Wait up to a second for the port to open.
787 let s:chopt.waittime = 1000
788 let channel = ch_open('localhost:' . a:port, s:chopt)
789 unlet s:chopt.waittime
790 if ch_status(channel) == "fail"
791 call assert_false(1, "Can't open channel")
792 return
793 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100794 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100795 call ch_close(channel)
796endfunc
797
798func Test_open_delay()
799 call ch_log('Test_open_delay()')
800 " The server will wait half a second before creating the port.
801 call s:run_server('s:open_delay', 'delay')
802endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100803
804"""""""""
805
806function MyFunction(a,b,c)
807 let s:call_ret = [a:a, a:b, a:c]
808endfunc
809
810function s:test_call(port)
811 let handle = ch_open('localhost:' . a:port, s:chopt)
812 if ch_status(handle) == "fail"
813 call assert_false(1, "Can't open channel")
814 return
815 endif
816
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100817 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100818 sleep 20m
819 call assert_equal([1, 2, 3], s:call_ret)
820endfunc
821
822func Test_call()
823 call ch_log('Test_call()')
824 call s:run_server('s:test_call')
825endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100826
827"""""""""
828
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100829let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100830function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100831 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100832endfunc
833
834function s:test_exit_callback(port)
835 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
836 let s:exit_job = s:job
837endfunc
838
839func Test_exit_callback()
840 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100841 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100842 call s:run_server('s:test_exit_callback')
843
Bram Moolenaar9730f742016-02-28 19:50:51 +0100844 " wait up to a second for the job to exit
845 for i in range(100)
846 if s:job_exit_ret == 'done'
847 break
848 endif
849 sleep 10m
850 " calling job_status() triggers the callback
851 call job_status(s:exit_job)
852 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100853
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100854 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100855 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100856 endif
857endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100858
859"""""""""
860
861let s:ch_close_ret = 'alive'
862function MyCloseCb(ch)
863 let s:ch_close_ret = 'closed'
864endfunc
865
866function s:test_close_callback(port)
867 let handle = ch_open('localhost:' . a:port, s:chopt)
868 if ch_status(handle) == "fail"
869 call assert_false(1, "Can't open channel")
870 return
871 endif
872 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
873
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100874 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100875 sleep 20m
876 call assert_equal('closed', s:ch_close_ret)
877endfunc
878
879func Test_close_callback()
880 call ch_log('Test_close_callback()')
881 call s:run_server('s:test_close_callback')
882endfunc
883
Bram Moolenaar9730f742016-02-28 19:50:51 +0100884" Uncomment this to see what happens, output is in src/testdir/channellog.
885" call ch_logfile('channellog', 'w')