blob: c8779344d1f0af22efc130d36518f11492f217b2 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
Bram Moolenaare2469252016-02-04 10:54:34 +01004if !has('channel')
5 finish
6endif
7
8" This test requires the Python command to run the test server.
Bram Moolenaara8343c12016-02-04 22:09:48 +01009" This most likely only works on Unix and Windows.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010010if has('unix')
Bram Moolenaar835dc632016-02-07 14:27:38 +010011 " We also need the job feature or the pkill command to make sure the server
12 " can be stopped.
13 if !(executable('python') && (has('job') || executable('pkill')))
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010014 finish
15 endif
Bram Moolenaarb6a77372016-02-15 22:55:28 +010016 let s:python = 'python'
Bram Moolenaara8343c12016-02-04 22:09:48 +010017elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010018 " Use Python Launcher for Windows (py.exe) if available.
19 if executable('py.exe')
20 let s:python = 'py.exe'
21 elseif executable('python.exe')
22 let s:python = 'python.exe'
23 else
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010024 finish
25 endif
26else
Bram Moolenaard6a8d482016-02-10 20:32:20 +010027 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010028 finish
29endif
30
Bram Moolenaare74e8e72016-02-16 22:01:30 +010031let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010032
Bram Moolenaard6a8d482016-02-10 20:32:20 +010033" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010034func s:run_server(testfunc, ...)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010035 " The Python program writes the port number in Xportnr.
36 call delete("Xportnr")
37
Bram Moolenaar81661fb2016-02-18 22:23:34 +010038 if a:0 == 1
39 let arg = ' ' . a:1
40 else
41 let arg = ''
42 endif
43 let cmd = s:python . " test_channel.py" . arg
44
Bram Moolenaard6a8d482016-02-10 20:32:20 +010045 try
46 if has('job')
Bram Moolenaar65edff82016-02-21 16:40:11 +010047 let s:job = job_start(cmd, {"stoponexit": "hup"})
48 call job_setoptions(s:job, {"stoponexit": "kill"})
Bram Moolenaard6a8d482016-02-10 20:32:20 +010049 elseif has('win32')
Bram Moolenaar81661fb2016-02-18 22:23:34 +010050 exe 'silent !start cmd /c start "test_channel" ' . cmd
Bram Moolenaard6a8d482016-02-10 20:32:20 +010051 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010052 exe 'silent !' . cmd . '&'
Bram Moolenaard7ece102016-02-02 23:23:02 +010053 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010054
Bram Moolenaard6a8d482016-02-10 20:32:20 +010055 " Wait for up to 2 seconds for the port number to be there.
56 let cnt = 20
57 let l = []
58 while cnt > 0
59 try
60 let l = readfile("Xportnr")
61 catch
62 endtry
63 if len(l) >= 1
64 break
65 endif
66 sleep 100m
67 let cnt -= 1
68 endwhile
69 call delete("Xportnr")
70
71 if len(l) == 0
72 " Can't make the connection, give up.
73 call assert_false(1, "Can't start test_channel.py")
74 return -1
75 endif
76 let port = l[0]
77
78 call call(function(a:testfunc), [port])
79 catch
80 call assert_false(1, "Caught exception: " . v:exception)
81 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010082 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010083 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010084endfunc
85
86func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010087 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010088 if exists('s:job')
89 call job_stop(s:job)
90 unlet s:job
91 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010092 elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010093 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010094 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010095 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010096 endif
97endfunc
98
Bram Moolenaara07fec92016-02-05 21:04:08 +010099let s:responseMsg = ''
100func s:RequestHandler(handle, msg)
101 let s:responseHandle = a:handle
102 let s:responseMsg = a:msg
103endfunc
104
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100105func s:communicate(port)
106 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100107 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100108 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100109 return
110 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100111
112 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100113 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100114
115 " Request that triggers sending two ex commands. These will usually be
116 " handled before getting the response, but it's not guaranteed, thus wait a
117 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100118 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100119 sleep 10m
120 call assert_equal('added1', getline(line('$') - 1))
121 call assert_equal('added2', getline('$'))
122
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100123 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100124 sleep 10m
125 call assert_equal('added more', getline('$'))
126
Bram Moolenaara07fec92016-02-05 21:04:08 +0100127 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100128 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaara07fec92016-02-05 21:04:08 +0100129 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100130 if !exists('s:responseHandle')
131 call assert_false(1, 's:responseHandle was not set')
132 else
133 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100134 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100135 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100136 call assert_equal('got it', s:responseMsg)
137
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100138 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100139 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100140 sleep 10m
Bram Moolenaar77073442016-02-13 23:23:53 +0100141 if !exists('s:responseHandle')
142 call assert_false(1, 's:responseHandle was not set')
143 else
144 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100145 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100146 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100147 call assert_equal('got it', s:responseMsg)
148
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100149 " check setting options (without testing the effect)
150 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100151 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100152 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100153 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100154 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100155
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100156 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100157 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100158 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100159 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100160
161 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100162 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100163 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100164 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100165
Bram Moolenaar55fab432016-02-07 16:53:13 +0100166 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100167 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100168 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100169 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100170
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100171 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100173 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100174 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100175
Bram Moolenaarf4160862016-02-05 23:09:12 +0100176 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100178 sleep 10m
179 call assert_equal('one', getline(line('$') - 2))
180 call assert_equal('two', getline(line('$') - 1))
181 call assert_equal('three', getline('$'))
182
183 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100184 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
185 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100186
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100188
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100189 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100190 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
191 let start = reltime()
192 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
193 let elapsed = reltime(start)
194 call assert_true(reltimefloat(elapsed) > 0.3)
195 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100196
197 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100199 let resp = ch_read(handle)
200 call assert_equal(type([]), type(resp))
201 call assert_equal(type(11), type(resp[0]))
202 call assert_equal('waited', resp[1])
203
Bram Moolenaard7ece102016-02-02 23:23:02 +0100204 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100205 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100206endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100207
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100208func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100209 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100210 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100211endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100212
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100213" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100214func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100215 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100216 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100217 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100218 return
219 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100221 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100222
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100223 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100224 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100225 call assert_false(1, "Can't open second channel")
226 return
227 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100228 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
229 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100230
231 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100232 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100233
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100234 call ch_close(newhandle)
235endfunc
236
237func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100238 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100239 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100240endfunc
241
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100242" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100243func s:server_crash(port)
244 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100245 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100246 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100247 return
248 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100249
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100250 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100251
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100252 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100253endfunc
254
255func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100256 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100257 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100258endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100259
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100260"""""""""
261
Bram Moolenaarf6157282016-02-10 21:07:14 +0100262let s:reply = ""
263func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100264 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100265 let s:reply = a:msg
266endfunc
267
268func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100269 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100270 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100271 call assert_false(1, "Can't open channel")
272 return
273 endif
274
275 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100276 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100277 sleep 10m
278 call assert_equal('we called you', s:reply)
279
280 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100281 call ch_sendexpr(handle, 'call me again')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100282 sleep 10m
283 call assert_equal('we did call you', s:reply)
284endfunc
285
286func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100287 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100288 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100289 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100290 let s:chopt.callback = function('s:Handler')
291 call s:run_server('s:channel_handler')
292 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100293endfunc
294
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100295"""""""""
296
297let s:reply1 = ""
298func s:HandleRaw1(chan, msg)
299 unlet s:reply1
300 let s:reply1 = a:msg
301endfunc
302
303let s:reply2 = ""
304func s:HandleRaw2(chan, msg)
305 unlet s:reply2
306 let s:reply2 = a:msg
307endfunc
308
309let s:reply3 = ""
310func s:HandleRaw3(chan, msg)
311 unlet s:reply3
312 let s:reply3 = a:msg
313endfunc
314
315func s:raw_one_time_callback(port)
316 let handle = ch_open('localhost:' . a:port, s:chopt)
317 if ch_status(handle) == "fail"
318 call assert_false(1, "Can't open channel")
319 return
320 endif
321 call ch_setoptions(handle, {'mode': 'raw'})
322
323 " The message are sent raw, we do our own JSON strings here.
324 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
325 sleep 10m
326 call assert_equal("[1, \"got it\"]", s:reply1)
327 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
328 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
329 sleep 10m
330 call assert_equal("[2, \"something\"]", s:reply2)
331 " wait for up to 500 msec for the 200 msec delayed reply
332 for i in range(50)
333 sleep 10m
334 if s:reply3 != ''
335 break
336 endif
337 endfor
338 call assert_equal("[3, \"waited\"]", s:reply3)
339endfunc
340
341func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100342 call ch_log('Test_raw_one_time_callback()')
343 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100344endfunc
345
346"""""""""
347
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100348" Test that trying to connect to a non-existing port fails quickly.
349func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100350 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100351 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100352 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100353 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100354 " Oops, port does exists.
355 call ch_close(handle)
356 else
357 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100358 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100359 endif
360
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100361 " We intend to use a socket that doesn't exist and wait for half a second
362 " before giving up. If the socket does exist it can fail in various ways.
363 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100364 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100365 try
366 let handle = ch_open('localhost:9867', {'waittime': 500})
367 if ch_status(handle) != "fail"
368 " Oops, port does exists.
369 call ch_close(handle)
370 else
371 " Failed connection should wait about 500 msec.
372 let elapsed = reltime(start)
373 call assert_true(reltimefloat(elapsed) > 0.3)
374 call assert_true(reltimefloat(elapsed) < 1.0)
375 endif
376 catch
377 if v:exception !~ 'Connection reset by peer'
378 call assert_false(1, "Caught exception: " . v:exception)
379 endif
380 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100381endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100382
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100383"""""""""
384
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100385func Test_raw_pipe()
386 if !has('job')
387 return
388 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100389 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100390 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
391 call assert_equal("run", job_status(job))
392 try
393 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100394 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100395 let msg = ch_readraw(handle)
396 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
397
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100398 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100399 let msg = ch_readraw(handle)
400 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
401
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100402 let reply = ch_evalraw(handle, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100403 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
404 finally
405 call job_stop(job)
406 endtry
407endfunc
408
409func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100410 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100411 return
412 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100413 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100414 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100415 call assert_equal("run", job_status(job))
416 try
417 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100418 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100419 call assert_equal("something", ch_readraw(handle))
420
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100421 call ch_sendraw(handle, "echoerr wrong\n")
422 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
423
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100424 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100425 call assert_equal("this", ch_readraw(handle))
426 call assert_equal("AND this", ch_readraw(handle))
427
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100428 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100429 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100430 finally
431 call job_stop(job)
432 endtry
433endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100434
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100435func Test_nl_err_to_out_pipe()
436 if !has('job')
437 return
438 endif
439 call ch_log('Test_nl_err_to_out_pipe()')
440 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
441 call assert_equal("run", job_status(job))
442 try
443 let handle = job_getchannel(job)
444 call ch_sendraw(handle, "echo something\n")
445 call assert_equal("something", ch_readraw(handle))
446
447 call ch_sendraw(handle, "echoerr wrong\n")
448 call assert_equal("wrong", ch_readraw(handle))
449 finally
450 call job_stop(job)
451 endtry
452endfunc
453
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100454func Test_pipe_to_buffer()
455 if !has('job')
456 return
457 endif
458 call ch_log('Test_pipe_to_buffer()')
459 let job = job_start(s:python . " test_channel_pipe.py",
460 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
461 call assert_equal("run", job_status(job))
462 try
463 let handle = job_getchannel(job)
464 call ch_sendraw(handle, "echo line one\n")
465 call ch_sendraw(handle, "echo line two\n")
466 call ch_sendraw(handle, "double this\n")
467 call ch_sendraw(handle, "quit\n")
468 sp pipe-output
469 for i in range(100)
470 sleep 10m
471 if line('$') >= 6
472 break
473 endif
474 endfor
475 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
476 bwipe!
477 finally
478 call job_stop(job)
479 endtry
480endfunc
481
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100482func Test_pipe_to_nameless_buffer()
483 if !has('job')
484 return
485 endif
486 call ch_log('Test_pipe_to_nameless_buffer()')
487 let job = job_start(s:python . " test_channel_pipe.py",
488 \ {'out-io': 'buffer'})
489 call assert_equal("run", job_status(job))
490 try
491 let handle = job_getchannel(job)
492 call ch_sendraw(handle, "echo line one\n")
493 call ch_sendraw(handle, "echo line two\n")
494 exe ch_getbufnr(handle, "out") . 'sbuf'
495 for i in range(100)
496 sleep 10m
497 if line('$') >= 3
498 break
499 endif
500 endfor
501 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
502 bwipe!
503 finally
504 call job_stop(job)
505 endtry
506endfunc
507
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100508func Test_pipe_to_buffer_json()
509 if !has('job')
510 return
511 endif
512 call ch_log('Test_pipe_to_buffer_json()')
513 let job = job_start(s:python . " test_channel_pipe.py",
514 \ {'out-io': 'buffer', 'out-mode': 'json'})
515 call assert_equal("run", job_status(job))
516 try
517 let handle = job_getchannel(job)
518 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
519 call ch_sendraw(handle, "echo [-2, 12.34]\n")
520 exe ch_getbufnr(handle, "out") . 'sbuf'
521 for i in range(100)
522 sleep 10m
523 if line('$') >= 3
524 break
525 endif
526 endfor
527 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
528 bwipe!
529 finally
530 call job_stop(job)
531 endtry
532endfunc
533
Bram Moolenaard46ae142016-02-16 13:33:52 +0100534""""""""""
535
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100536let s:unletResponse = ''
537func s:UnletHandler(handle, msg)
538 let s:unletResponse = a:msg
539 unlet s:channelfd
540endfunc
541
542" Test that "unlet handle" in a handler doesn't crash Vim.
543func s:unlet_handle(port)
544 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100545 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100546 sleep 10m
547 call assert_equal('what?', s:unletResponse)
548endfunc
549
550func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100551 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100552 call s:run_server('s:unlet_handle')
553endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100554
Bram Moolenaard46ae142016-02-16 13:33:52 +0100555""""""""""
556
557let s:unletResponse = ''
558func s:CloseHandler(handle, msg)
559 let s:unletResponse = a:msg
560 call ch_close(s:channelfd)
561endfunc
562
563" Test that "unlet handle" in a handler doesn't crash Vim.
564func s:close_handle(port)
565 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100566 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100567 sleep 10m
568 call assert_equal('what?', s:unletResponse)
569endfunc
570
571func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100572 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100573 call s:run_server('s:close_handle')
574endfunc
575
576""""""""""
577
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100578func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100579 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100580 silent! let ch = ch_open("noserver")
581 echo ch
582 let d = ch
583endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100584
585""""""""""
586
587func s:open_delay(port)
588 " Wait up to a second for the port to open.
589 let s:chopt.waittime = 1000
590 let channel = ch_open('localhost:' . a:port, s:chopt)
591 unlet s:chopt.waittime
592 if ch_status(channel) == "fail"
593 call assert_false(1, "Can't open channel")
594 return
595 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100596 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100597 call ch_close(channel)
598endfunc
599
600func Test_open_delay()
601 call ch_log('Test_open_delay()')
602 " The server will wait half a second before creating the port.
603 call s:run_server('s:open_delay', 'delay')
604endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100605
606"""""""""
607
608function MyFunction(a,b,c)
609 let s:call_ret = [a:a, a:b, a:c]
610endfunc
611
612function s:test_call(port)
613 let handle = ch_open('localhost:' . a:port, s:chopt)
614 if ch_status(handle) == "fail"
615 call assert_false(1, "Can't open channel")
616 return
617 endif
618
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100619 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100620 sleep 20m
621 call assert_equal([1, 2, 3], s:call_ret)
622endfunc
623
624func Test_call()
625 call ch_log('Test_call()')
626 call s:run_server('s:test_call')
627endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100628
629"""""""""
630
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100631let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100632function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100633 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100634endfunc
635
636function s:test_exit_callback(port)
637 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
638 let s:exit_job = s:job
639endfunc
640
641func Test_exit_callback()
642 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100643 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100644 call s:run_server('s:test_exit_callback')
645
Bram Moolenaar9730f742016-02-28 19:50:51 +0100646 " wait up to a second for the job to exit
647 for i in range(100)
648 if s:job_exit_ret == 'done'
649 break
650 endif
651 sleep 10m
652 " calling job_status() triggers the callback
653 call job_status(s:exit_job)
654 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100655
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100656 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100657 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100658 endif
659endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100660
661"""""""""
662
663let s:ch_close_ret = 'alive'
664function MyCloseCb(ch)
665 let s:ch_close_ret = 'closed'
666endfunc
667
668function s:test_close_callback(port)
669 let handle = ch_open('localhost:' . a:port, s:chopt)
670 if ch_status(handle) == "fail"
671 call assert_false(1, "Can't open channel")
672 return
673 endif
674 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
675
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100676 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100677 sleep 20m
678 call assert_equal('closed', s:ch_close_ret)
679endfunc
680
681func Test_close_callback()
682 call ch_log('Test_close_callback()')
683 call s:run_server('s:test_close_callback')
684endfunc
685
Bram Moolenaar9730f742016-02-28 19:50:51 +0100686" Uncomment this to see what happens, output is in src/testdir/channellog.
687" call ch_logfile('channellog', 'w')