blob: 2b46566b7641bbe86109d71c6f4b7bfef2babdfb [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 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 Moolenaarda94fdf2016-03-03 18:09:10 +0100345 let reply = ch_evalraw(handle, "quit\n", {'timeout': 100})
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 Moolenaarc7f0ebc2016-02-27 21:10:09 +0100403func Test_pipe_to_nameless_buffer()
404 if !has('job')
405 return
406 endif
407 call ch_log('Test_pipe_to_nameless_buffer()')
408 let job = job_start(s:python . " test_channel_pipe.py",
409 \ {'out-io': 'buffer'})
410 call assert_equal("run", job_status(job))
411 try
412 let handle = job_getchannel(job)
413 call ch_sendraw(handle, "echo line one\n")
414 call ch_sendraw(handle, "echo line two\n")
415 exe ch_getbufnr(handle, "out") . 'sbuf'
416 for i in range(100)
417 sleep 10m
418 if line('$') >= 3
419 break
420 endif
421 endfor
422 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
423 bwipe!
424 finally
425 call job_stop(job)
426 endtry
427endfunc
428
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100429func Test_pipe_to_buffer_json()
430 if !has('job')
431 return
432 endif
433 call ch_log('Test_pipe_to_buffer_json()')
434 let job = job_start(s:python . " test_channel_pipe.py",
435 \ {'out-io': 'buffer', 'out-mode': 'json'})
436 call assert_equal("run", job_status(job))
437 try
438 let handle = job_getchannel(job)
439 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
440 call ch_sendraw(handle, "echo [-2, 12.34]\n")
441 exe ch_getbufnr(handle, "out") . 'sbuf'
442 for i in range(100)
443 sleep 10m
444 if line('$') >= 3
445 break
446 endif
447 endfor
448 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
449 bwipe!
450 finally
451 call job_stop(job)
452 endtry
453endfunc
454
Bram Moolenaard46ae142016-02-16 13:33:52 +0100455""""""""""
456
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100457let s:unletResponse = ''
458func s:UnletHandler(handle, msg)
459 let s:unletResponse = a:msg
460 unlet s:channelfd
461endfunc
462
463" Test that "unlet handle" in a handler doesn't crash Vim.
464func s:unlet_handle(port)
465 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100466 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100467 sleep 10m
468 call assert_equal('what?', s:unletResponse)
469endfunc
470
471func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100472 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100473 call s:run_server('s:unlet_handle')
474endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100475
Bram Moolenaard46ae142016-02-16 13:33:52 +0100476""""""""""
477
478let s:unletResponse = ''
479func s:CloseHandler(handle, msg)
480 let s:unletResponse = a:msg
481 call ch_close(s:channelfd)
482endfunc
483
484" Test that "unlet handle" in a handler doesn't crash Vim.
485func s:close_handle(port)
486 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100487 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaard46ae142016-02-16 13:33:52 +0100488 sleep 10m
489 call assert_equal('what?', s:unletResponse)
490endfunc
491
492func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100493 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100494 call s:run_server('s:close_handle')
495endfunc
496
497""""""""""
498
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100499func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100500 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100501 silent! let ch = ch_open("noserver")
502 echo ch
503 let d = ch
504endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100505
506""""""""""
507
508func s:open_delay(port)
509 " Wait up to a second for the port to open.
510 let s:chopt.waittime = 1000
511 let channel = ch_open('localhost:' . a:port, s:chopt)
512 unlet s:chopt.waittime
513 if ch_status(channel) == "fail"
514 call assert_false(1, "Can't open channel")
515 return
516 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100517 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518 call ch_close(channel)
519endfunc
520
521func Test_open_delay()
522 call ch_log('Test_open_delay()')
523 " The server will wait half a second before creating the port.
524 call s:run_server('s:open_delay', 'delay')
525endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100526
527"""""""""
528
529function MyFunction(a,b,c)
530 let s:call_ret = [a:a, a:b, a:c]
531endfunc
532
533function s:test_call(port)
534 let handle = ch_open('localhost:' . a:port, s:chopt)
535 if ch_status(handle) == "fail"
536 call assert_false(1, "Can't open channel")
537 return
538 endif
539
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100540 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100541 sleep 20m
542 call assert_equal([1, 2, 3], s:call_ret)
543endfunc
544
545func Test_call()
546 call ch_log('Test_call()')
547 call s:run_server('s:test_call')
548endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100549
550"""""""""
551
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100552let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100553function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100554 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100555endfunc
556
557function s:test_exit_callback(port)
558 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
559 let s:exit_job = s:job
560endfunc
561
562func Test_exit_callback()
563 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100564 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100565 call s:run_server('s:test_exit_callback')
566
Bram Moolenaar9730f742016-02-28 19:50:51 +0100567 " wait up to a second for the job to exit
568 for i in range(100)
569 if s:job_exit_ret == 'done'
570 break
571 endif
572 sleep 10m
573 " calling job_status() triggers the callback
574 call job_status(s:exit_job)
575 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100576
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100577 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100578 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100579 endif
580endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100581
582"""""""""
583
584let s:ch_close_ret = 'alive'
585function MyCloseCb(ch)
586 let s:ch_close_ret = 'closed'
587endfunc
588
589function s:test_close_callback(port)
590 let handle = ch_open('localhost:' . a:port, s:chopt)
591 if ch_status(handle) == "fail"
592 call assert_false(1, "Can't open channel")
593 return
594 endif
595 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
596
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100597 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100598 sleep 20m
599 call assert_equal('closed', s:ch_close_ret)
600endfunc
601
602func Test_close_callback()
603 call ch_log('Test_close_callback()')
604 call s:run_server('s:test_close_callback')
605endfunc
606
Bram Moolenaar9730f742016-02-28 19:50:51 +0100607" Uncomment this to see what happens, output is in src/testdir/channellog.
608" call ch_logfile('channellog', 'w')