blob: a3955e16ac22d066b3ea057f40b3bddb47ab4877 [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 Moolenaar8b1862a2016-02-27 19:21:24 +0100123 call assert_equal('ok', ch_evalexpr(handle, 'do normal'))
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 Moolenaarf6157282016-02-10 21:07:14 +0100260let s:reply = ""
261func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100262 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100263 let s:reply = a:msg
264endfunc
265
266func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100267 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100268 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100269 call assert_false(1, "Can't open channel")
270 return
271 endif
272
273 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100274 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100275 sleep 10m
276 call assert_equal('we called you', s:reply)
277
278 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100279 call ch_sendexpr(handle, 'call me again')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280 sleep 10m
281 call assert_equal('we did call you', s:reply)
282endfunc
283
284func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100285 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100286 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100287 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100288 let s:chopt.callback = function('s:Handler')
289 call s:run_server('s:channel_handler')
290 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100291endfunc
292
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100293" Test that trying to connect to a non-existing port fails quickly.
294func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100295 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100296 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100297 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100298 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100299 " Oops, port does exists.
300 call ch_close(handle)
301 else
302 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100303 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100304 endif
305
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100306 " We intend to use a socket that doesn't exist and wait for half a second
307 " before giving up. If the socket does exist it can fail in various ways.
308 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100309 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100310 try
311 let handle = ch_open('localhost:9867', {'waittime': 500})
312 if ch_status(handle) != "fail"
313 " Oops, port does exists.
314 call ch_close(handle)
315 else
316 " Failed connection should wait about 500 msec.
317 let elapsed = reltime(start)
318 call assert_true(reltimefloat(elapsed) > 0.3)
319 call assert_true(reltimefloat(elapsed) < 1.0)
320 endif
321 catch
322 if v:exception !~ 'Connection reset by peer'
323 call assert_false(1, "Caught exception: " . v:exception)
324 endif
325 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100326endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100327
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100328func Test_raw_pipe()
329 if !has('job')
330 return
331 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100332 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100333 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
334 call assert_equal("run", job_status(job))
335 try
336 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100337 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100338 let msg = ch_readraw(handle)
339 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
340
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100341 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100342 let msg = ch_readraw(handle)
343 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
344
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100345 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100346 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
347 finally
348 call job_stop(job)
349 endtry
350endfunc
351
352func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100353 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100354 return
355 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100356 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100357 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100358 call assert_equal("run", job_status(job))
359 try
360 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100361 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100362 call assert_equal("something", ch_readraw(handle))
363
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100364 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100365 call assert_equal("this", ch_readraw(handle))
366 call assert_equal("AND this", ch_readraw(handle))
367
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100368 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100369 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100370 finally
371 call job_stop(job)
372 endtry
373endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100374
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100375func Test_pipe_to_buffer()
376 if !has('job')
377 return
378 endif
379 call ch_log('Test_pipe_to_buffer()')
380 let job = job_start(s:python . " test_channel_pipe.py",
381 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
382 call assert_equal("run", job_status(job))
383 try
384 let handle = job_getchannel(job)
385 call ch_sendraw(handle, "echo line one\n")
386 call ch_sendraw(handle, "echo line two\n")
387 call ch_sendraw(handle, "double this\n")
388 call ch_sendraw(handle, "quit\n")
389 sp pipe-output
390 for i in range(100)
391 sleep 10m
392 if line('$') >= 6
393 break
394 endif
395 endfor
396 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
397 bwipe!
398 finally
399 call job_stop(job)
400 endtry
401endfunc
402
Bram Moolenaard46ae142016-02-16 13:33:52 +0100403""""""""""
404
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100405let s:unletResponse = ''
406func s:UnletHandler(handle, msg)
407 let s:unletResponse = a:msg
408 unlet s:channelfd
409endfunc
410
411" Test that "unlet handle" in a handler doesn't crash Vim.
412func s:unlet_handle(port)
413 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100414 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100415 sleep 10m
416 call assert_equal('what?', s:unletResponse)
417endfunc
418
419func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100420 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100421 call s:run_server('s:unlet_handle')
422endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100423
Bram Moolenaard46ae142016-02-16 13:33:52 +0100424""""""""""
425
426let s:unletResponse = ''
427func s:CloseHandler(handle, msg)
428 let s:unletResponse = a:msg
429 call ch_close(s:channelfd)
430endfunc
431
432" Test that "unlet handle" in a handler doesn't crash Vim.
433func s:close_handle(port)
434 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100435 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100436 sleep 10m
437 call assert_equal('what?', s:unletResponse)
438endfunc
439
440func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100441 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100442 call s:run_server('s:close_handle')
443endfunc
444
445""""""""""
446
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100447func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100448 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100449 silent! let ch = ch_open("noserver")
450 echo ch
451 let d = ch
452endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100453
454""""""""""
455
456func s:open_delay(port)
457 " Wait up to a second for the port to open.
458 let s:chopt.waittime = 1000
459 let channel = ch_open('localhost:' . a:port, s:chopt)
460 unlet s:chopt.waittime
461 if ch_status(channel) == "fail"
462 call assert_false(1, "Can't open channel")
463 return
464 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100465 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100466 call ch_close(channel)
467endfunc
468
469func Test_open_delay()
470 call ch_log('Test_open_delay()')
471 " The server will wait half a second before creating the port.
472 call s:run_server('s:open_delay', 'delay')
473endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100474
475"""""""""
476
477function MyFunction(a,b,c)
478 let s:call_ret = [a:a, a:b, a:c]
479endfunc
480
481function s:test_call(port)
482 let handle = ch_open('localhost:' . a:port, s:chopt)
483 if ch_status(handle) == "fail"
484 call assert_false(1, "Can't open channel")
485 return
486 endif
487
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100488 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100489 sleep 20m
490 call assert_equal([1, 2, 3], s:call_ret)
491endfunc
492
493func Test_call()
494 call ch_log('Test_call()')
495 call s:run_server('s:test_call')
496endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100497
498"""""""""
499
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100500let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100501function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100502 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100503endfunc
504
505function s:test_exit_callback(port)
506 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
507 let s:exit_job = s:job
508endfunc
509
510func Test_exit_callback()
511 if has('job')
512 call s:run_server('s:test_exit_callback')
513
514 " the job may take a little while to exit
515 sleep 50m
516
517 " calling job_status() triggers the callback
518 call job_status(s:exit_job)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100519 call assert_equal('done', s:job_exit_ret)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100520 endif
521endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100522
523"""""""""
524
525let s:ch_close_ret = 'alive'
526function MyCloseCb(ch)
527 let s:ch_close_ret = 'closed'
528endfunc
529
530function s:test_close_callback(port)
531 let handle = ch_open('localhost:' . a:port, s:chopt)
532 if ch_status(handle) == "fail"
533 call assert_false(1, "Can't open channel")
534 return
535 endif
536 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
537
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100538 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100539 sleep 20m
540 call assert_equal('closed', s:ch_close_ret)
541endfunc
542
543func Test_close_callback()
544 call ch_log('Test_close_callback()')
545 call s:run_server('s:test_close_callback')
546endfunc
547