blob: dd7ef4ec40549f92772618a82cda0bf28a020c76 [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'})
Bram Moolenaar304563c2016-03-07 22:26:28 +0100398 for i in range(50)
399 sleep 10m
400 if s:reply1 != ''
401 break
402 endif
403 endfor
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100404 call assert_equal("[1, \"got it\"]", s:reply1)
405 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
406 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar304563c2016-03-07 22:26:28 +0100407 for i in range(50)
408 sleep 10m
409 if s:reply2 != ''
410 break
411 endif
412 endfor
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100413 call assert_equal("[2, \"something\"]", s:reply2)
414 " wait for up to 500 msec for the 200 msec delayed reply
415 for i in range(50)
416 sleep 10m
417 if s:reply3 != ''
418 break
419 endif
420 endfor
421 call assert_equal("[3, \"waited\"]", s:reply3)
422endfunc
423
424func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100425 call ch_log('Test_raw_one_time_callback()')
426 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100427endfunc
428
429"""""""""
430
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100431" Test that trying to connect to a non-existing port fails quickly.
432func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100433 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100434 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100435 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100436 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100437 " Oops, port does exists.
438 call ch_close(handle)
439 else
440 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100441 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100442 endif
443
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100444 " We intend to use a socket that doesn't exist and wait for half a second
445 " before giving up. If the socket does exist it can fail in various ways.
446 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100447 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100448 try
449 let handle = ch_open('localhost:9867', {'waittime': 500})
450 if ch_status(handle) != "fail"
451 " Oops, port does exists.
452 call ch_close(handle)
453 else
454 " Failed connection should wait about 500 msec.
455 let elapsed = reltime(start)
456 call assert_true(reltimefloat(elapsed) > 0.3)
457 call assert_true(reltimefloat(elapsed) < 1.0)
458 endif
459 catch
460 if v:exception !~ 'Connection reset by peer'
461 call assert_false(1, "Caught exception: " . v:exception)
462 endif
463 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100464endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100465
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100466"""""""""
467
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100468func Test_raw_pipe()
469 if !has('job')
470 return
471 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100472 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100473 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
474 call assert_equal("run", job_status(job))
475 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100476 " For a change use the job where a channel is expected.
477 call ch_sendraw(job, "echo something\n")
478 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100479 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
480
Bram Moolenaar151f6562016-03-07 21:19:38 +0100481 call ch_sendraw(job, "double this\n")
482 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100483 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
484
Bram Moolenaar151f6562016-03-07 21:19:38 +0100485 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100486 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
487 finally
488 call job_stop(job)
489 endtry
490endfunc
491
492func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100493 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100494 return
495 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100496 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100497 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100498 call assert_equal("run", job_status(job))
499 try
500 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100502 call assert_equal("something", ch_readraw(handle))
503
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100504 call ch_sendraw(handle, "echoerr wrong\n")
505 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
506
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100507 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100508 call assert_equal("this", ch_readraw(handle))
509 call assert_equal("AND this", ch_readraw(handle))
510
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100511 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100512 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100513 finally
514 call job_stop(job)
515 endtry
516endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100517
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100518func Test_nl_err_to_out_pipe()
519 if !has('job')
520 return
521 endif
522 call ch_log('Test_nl_err_to_out_pipe()')
523 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
524 call assert_equal("run", job_status(job))
525 try
526 let handle = job_getchannel(job)
527 call ch_sendraw(handle, "echo something\n")
528 call assert_equal("something", ch_readraw(handle))
529
530 call ch_sendraw(handle, "echoerr wrong\n")
531 call assert_equal("wrong", ch_readraw(handle))
532 finally
533 call job_stop(job)
534 endtry
535endfunc
536
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100537func Test_nl_read_file()
538 if !has('job')
539 return
540 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100541 call ch_log('Test_nl_read_file()')
542 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
543 let job = job_start(s:python . " test_channel_pipe.py",
544 \ {'in-io': 'file', 'in-name': 'Xinput'})
545 call assert_equal("run", job_status(job))
546 try
547 let handle = job_getchannel(job)
548 call assert_equal("something", ch_readraw(handle))
549 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
550 call assert_equal("this", ch_readraw(handle))
551 call assert_equal("AND this", ch_readraw(handle))
552 finally
553 call job_stop(job)
554 call delete('Xinput')
555 endtry
556endfunc
557
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100558func Test_pipe_to_buffer()
559 if !has('job')
560 return
561 endif
562 call ch_log('Test_pipe_to_buffer()')
563 let job = job_start(s:python . " test_channel_pipe.py",
564 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
565 call assert_equal("run", job_status(job))
566 try
567 let handle = job_getchannel(job)
568 call ch_sendraw(handle, "echo line one\n")
569 call ch_sendraw(handle, "echo line two\n")
570 call ch_sendraw(handle, "double this\n")
571 call ch_sendraw(handle, "quit\n")
572 sp pipe-output
573 for i in range(100)
574 sleep 10m
575 if line('$') >= 6
576 break
577 endif
578 endfor
579 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
580 bwipe!
581 finally
582 call job_stop(job)
583 endtry
584endfunc
585
Bram Moolenaar014069a2016-03-03 22:51:40 +0100586func Test_pipe_from_buffer()
587 if !has('job')
588 return
589 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100590 call ch_log('Test_pipe_from_buffer()')
591
592 sp pipe-input
593 call setline(1, ['echo one', 'echo two', 'echo three'])
594
595 let job = job_start(s:python . " test_channel_pipe.py",
596 \ {'in-io': 'buffer', 'in-name': 'pipe-input'})
597 call assert_equal("run", job_status(job))
598 try
599 let handle = job_getchannel(job)
600 call assert_equal('one', ch_read(handle))
601 call assert_equal('two', ch_read(handle))
602 call assert_equal('three', ch_read(handle))
603 bwipe!
604 finally
605 call job_stop(job)
606 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100607endfunc
608
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100609func Test_pipe_to_nameless_buffer()
610 if !has('job')
611 return
612 endif
613 call ch_log('Test_pipe_to_nameless_buffer()')
614 let job = job_start(s:python . " test_channel_pipe.py",
615 \ {'out-io': 'buffer'})
616 call assert_equal("run", job_status(job))
617 try
618 let handle = job_getchannel(job)
619 call ch_sendraw(handle, "echo line one\n")
620 call ch_sendraw(handle, "echo line two\n")
621 exe ch_getbufnr(handle, "out") . 'sbuf'
622 for i in range(100)
623 sleep 10m
624 if line('$') >= 3
625 break
626 endif
627 endfor
628 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
629 bwipe!
630 finally
631 call job_stop(job)
632 endtry
633endfunc
634
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100635func Test_pipe_to_buffer_json()
636 if !has('job')
637 return
638 endif
639 call ch_log('Test_pipe_to_buffer_json()')
640 let job = job_start(s:python . " test_channel_pipe.py",
641 \ {'out-io': 'buffer', 'out-mode': 'json'})
642 call assert_equal("run", job_status(job))
643 try
644 let handle = job_getchannel(job)
645 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
646 call ch_sendraw(handle, "echo [-2, 12.34]\n")
647 exe ch_getbufnr(handle, "out") . 'sbuf'
648 for i in range(100)
649 sleep 10m
650 if line('$') >= 3
651 break
652 endif
653 endfor
654 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
655 bwipe!
656 finally
657 call job_stop(job)
658 endtry
659endfunc
660
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100661" Wait a little while for the last line, minus "offset", to equal "line".
662func Wait_for_last_line(line, offset)
663 for i in range(100)
664 sleep 10m
665 if getline(line('$') - a:offset) == a:line
666 break
667 endif
668 endfor
669endfunc
670
671func Test_pipe_io_two_buffers()
672 if !has('job')
673 return
674 endif
675 call ch_log('Test_pipe_io_two_buffers()')
676
677 " Create two buffers, one to read from and one to write to.
678 split pipe-output
679 set buftype=nofile
680 split pipe-input
681 set buftype=nofile
682
683 let job = job_start(s:python . " test_channel_pipe.py",
684 \ {'in-io': 'buffer', 'in-name': 'pipe-input', 'in-top': 0,
685 \ 'out-io': 'buffer', 'out-name': 'pipe-output'})
686 call assert_equal("run", job_status(job))
687 try
688 exe "normal Gaecho hello\<CR>"
689 exe bufwinnr('pipe-output') . "wincmd w"
690 call Wait_for_last_line('hello', 0)
691 call assert_equal('hello', getline('$'))
692
693 exe bufwinnr('pipe-input') . "wincmd w"
694 exe "normal Gadouble this\<CR>"
695 exe bufwinnr('pipe-output') . "wincmd w"
696 call Wait_for_last_line('AND this', 0)
697 call assert_equal('this', getline(line('$') - 1))
698 call assert_equal('AND this', getline('$'))
699
700 bwipe!
701 exe bufwinnr('pipe-input') . "wincmd w"
702 bwipe!
703 finally
704 call job_stop(job)
705 endtry
706endfunc
707
708func Test_pipe_io_one_buffer()
709 if !has('job')
710 return
711 endif
712 call ch_log('Test_pipe_io_one_buffer()')
713
714 " Create one buffer to read from and to write to.
715 split pipe-io
716 set buftype=nofile
717
718 let job = job_start(s:python . " test_channel_pipe.py",
719 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0,
720 \ 'out-io': 'buffer', 'out-name': 'pipe-io'})
721 call assert_equal("run", job_status(job))
722 try
723 exe "normal Goecho hello\<CR>"
724 call Wait_for_last_line('hello', 1)
725 call assert_equal('hello', getline(line('$') - 1))
726
727 exe "normal Gadouble this\<CR>"
728 call Wait_for_last_line('AND this', 1)
729 call assert_equal('this', getline(line('$') - 2))
730 call assert_equal('AND this', getline(line('$') - 1))
731
732 bwipe!
733 finally
734 call job_stop(job)
735 endtry
736endfunc
737
Bram Moolenaard46ae142016-02-16 13:33:52 +0100738""""""""""
739
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100740let s:unletResponse = ''
741func s:UnletHandler(handle, msg)
742 let s:unletResponse = a:msg
743 unlet s:channelfd
744endfunc
745
746" Test that "unlet handle" in a handler doesn't crash Vim.
747func s:unlet_handle(port)
748 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100749 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100750 sleep 10m
751 call assert_equal('what?', s:unletResponse)
752endfunc
753
754func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100755 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100756 call s:run_server('s:unlet_handle')
757endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100758
Bram Moolenaard46ae142016-02-16 13:33:52 +0100759""""""""""
760
761let s:unletResponse = ''
762func s:CloseHandler(handle, msg)
763 let s:unletResponse = a:msg
764 call ch_close(s:channelfd)
765endfunc
766
767" Test that "unlet handle" in a handler doesn't crash Vim.
768func s:close_handle(port)
769 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100770 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100771 sleep 10m
772 call assert_equal('what?', s:unletResponse)
773endfunc
774
775func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100776 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100777 call s:run_server('s:close_handle')
778endfunc
779
780""""""""""
781
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100782func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100783 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100784 silent! let ch = ch_open("noserver")
785 echo ch
786 let d = ch
787endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100788
789""""""""""
790
791func s:open_delay(port)
792 " Wait up to a second for the port to open.
793 let s:chopt.waittime = 1000
794 let channel = ch_open('localhost:' . a:port, s:chopt)
795 unlet s:chopt.waittime
796 if ch_status(channel) == "fail"
797 call assert_false(1, "Can't open channel")
798 return
799 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100800 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801 call ch_close(channel)
802endfunc
803
804func Test_open_delay()
805 call ch_log('Test_open_delay()')
806 " The server will wait half a second before creating the port.
807 call s:run_server('s:open_delay', 'delay')
808endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100809
810"""""""""
811
812function MyFunction(a,b,c)
813 let s:call_ret = [a:a, a:b, a:c]
814endfunc
815
816function s:test_call(port)
817 let handle = ch_open('localhost:' . a:port, s:chopt)
818 if ch_status(handle) == "fail"
819 call assert_false(1, "Can't open channel")
820 return
821 endif
822
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100823 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100824 sleep 20m
825 call assert_equal([1, 2, 3], s:call_ret)
826endfunc
827
828func Test_call()
829 call ch_log('Test_call()')
830 call s:run_server('s:test_call')
831endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100832
833"""""""""
834
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100835let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100836function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100837 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100838endfunc
839
840function s:test_exit_callback(port)
841 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
842 let s:exit_job = s:job
843endfunc
844
845func Test_exit_callback()
846 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100847 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100848 call s:run_server('s:test_exit_callback')
849
Bram Moolenaar9730f742016-02-28 19:50:51 +0100850 " wait up to a second for the job to exit
851 for i in range(100)
852 if s:job_exit_ret == 'done'
853 break
854 endif
855 sleep 10m
856 " calling job_status() triggers the callback
857 call job_status(s:exit_job)
858 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100859
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100860 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100861 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100862 endif
863endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100864
865"""""""""
866
867let s:ch_close_ret = 'alive'
868function MyCloseCb(ch)
869 let s:ch_close_ret = 'closed'
870endfunc
871
872function s:test_close_callback(port)
873 let handle = ch_open('localhost:' . a:port, s:chopt)
874 if ch_status(handle) == "fail"
875 call assert_false(1, "Can't open channel")
876 return
877 endif
878 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
879
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100880 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100881 sleep 20m
882 call assert_equal('closed', s:ch_close_ret)
883endfunc
884
885func Test_close_callback()
886 call ch_log('Test_close_callback()')
887 call s:run_server('s:test_close_callback')
888endfunc
889
Bram Moolenaar9730f742016-02-28 19:50:51 +0100890" Uncomment this to see what happens, output is in src/testdir/channellog.
891" call ch_logfile('channellog', 'w')