blob: c628bbe88124490d12b7cc67e869f3b41da76315 [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.
Bram Moolenaard6a8d482016-02-10 20:32:20 +010056 let l = []
Bram Moolenaar9fe885e2016-03-08 16:06:55 +010057 for i in range(200)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010058 try
59 let l = readfile("Xportnr")
60 catch
61 endtry
62 if len(l) >= 1
63 break
64 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +010065 sleep 10m
66 endfor
Bram Moolenaard6a8d482016-02-10 20:32:20 +010067 call delete("Xportnr")
68
69 if len(l) == 0
70 " Can't make the connection, give up.
71 call assert_false(1, "Can't start test_channel.py")
72 return -1
73 endif
74 let port = l[0]
75
76 call call(function(a:testfunc), [port])
77 catch
78 call assert_false(1, "Caught exception: " . v:exception)
79 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010080 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010081 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010082endfunc
83
84func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010085 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010086 if exists('s:job')
87 call job_stop(s:job)
88 unlet s:job
89 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010090 elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010091 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010092 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010093 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010094 endif
95endfunc
96
Bram Moolenaara07fec92016-02-05 21:04:08 +010097let s:responseMsg = ''
98func s:RequestHandler(handle, msg)
99 let s:responseHandle = a:handle
100 let s:responseMsg = a:msg
101endfunc
102
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100103" Wait for up to a second for "expr" to become true.
104func s:waitFor(expr)
105 for i in range(100)
Bram Moolenaard9d473e2016-03-08 19:07:22 +0100106 try
107 if eval(a:expr)
108 return
109 endif
110 catch
111 endtry
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100112 sleep 10m
113 endfor
114endfunc
115
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100116func s:communicate(port)
117 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100118 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100119 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100120 return
121 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +0100122 if has('job')
123 " check that no job is handled correctly
124 call assert_equal('no process', string(ch_getjob(handle)))
125 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100126 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100127 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100128
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100129 " Malformed command should be ignored.
130 call assert_equal('ok', ch_evalexpr(handle, 'malformed'))
131
Bram Moolenaard7ece102016-02-02 23:23:02 +0100132 " Request that triggers sending two ex commands. These will usually be
133 " handled before getting the response, but it's not guaranteed, thus wait a
134 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100135 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100136 call s:waitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100137 call assert_equal('added1', getline(line('$') - 1))
138 call assert_equal('added2', getline('$'))
139
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100140 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100141 call s:waitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100142 call assert_equal('added more', getline('$'))
143
Bram Moolenaara07fec92016-02-05 21:04:08 +0100144 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100145 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100146 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100147 if !exists('s:responseHandle')
148 call assert_false(1, 's:responseHandle was not set')
149 else
150 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100151 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100153 call assert_equal('got it', s:responseMsg)
154
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100155 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100156 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100157 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100158 if !exists('s:responseHandle')
159 call assert_false(1, 's:responseHandle was not set')
160 else
161 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100162 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100163 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100164 call assert_equal('got it', s:responseMsg)
165
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100166 " Collect garbage, tests that our handle isn't collected.
167 call garbagecollect()
168
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100169 " check setting options (without testing the effect)
170 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100171 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100172 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100173 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100174 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100175
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100176 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100178 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100179 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100180
181 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100183 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100184 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100185
Bram Moolenaar55fab432016-02-07 16:53:13 +0100186 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100188 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100189 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100190
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100191 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100192 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100193 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100194 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100195
Bram Moolenaarf4160862016-02-05 23:09:12 +0100196 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100197 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100198 call s:waitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100199 call assert_equal('one', getline(line('$') - 2))
200 call assert_equal('two', getline(line('$') - 1))
201 call assert_equal('three', getline('$'))
202
203 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100204 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
205 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100206
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100207 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100208
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100209 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100210 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
211 let start = reltime()
212 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
213 let elapsed = reltime(start)
214 call assert_true(reltimefloat(elapsed) > 0.3)
215 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100216
217 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100219 let resp = ch_read(handle)
220 call assert_equal(type([]), type(resp))
221 call assert_equal(type(11), type(resp[0]))
222 call assert_equal('waited', resp[1])
223
Bram Moolenaard7ece102016-02-02 23:23:02 +0100224 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100225 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100227
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100228func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100229 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100230 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100231endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100232
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100233" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100234func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100235 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100236 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100237 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100238 return
239 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100240
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100241 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100242
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100243 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100245 call assert_false(1, "Can't open second channel")
246 return
247 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100248 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
249 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100250
251 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100252 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100253
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100254 call ch_close(newhandle)
255endfunc
256
257func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100258 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100259 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100260endfunc
261
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100262" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100263func s:server_crash(port)
264 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100265 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100266 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100267 return
268 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100269
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100270 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100271
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100272 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100273endfunc
274
275func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100276 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100277 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100278endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100279
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100280"""""""""
281
Bram Moolenaarf6157282016-02-10 21:07:14 +0100282let s:reply = ""
283func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100284 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100285 let s:reply = a:msg
286endfunc
287
288func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100289 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100290 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100291 call assert_false(1, "Can't open channel")
292 return
293 endif
294
295 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100296 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100297 call s:waitFor('"we called you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100298 call assert_equal('we called you', s:reply)
299
300 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100301 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100302 call s:waitFor('"we did call you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100303 call assert_equal('we did call you', s:reply)
304endfunc
305
306func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100307 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100308 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100309 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100310 let s:chopt.callback = function('s:Handler')
311 call s:run_server('s:channel_handler')
312 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100313endfunc
314
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100315"""""""""
316
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100317let s:ch_reply = ''
318func s:ChHandler(chan, msg)
319 unlet s:ch_reply
320 let s:ch_reply = a:msg
321endfunc
322
323let s:zero_reply = ''
324func s:OneHandler(chan, msg)
325 unlet s:zero_reply
326 let s:zero_reply = a:msg
327endfunc
328
329func s:channel_zero(port)
330 let handle = ch_open('localhost:' . a:port, s:chopt)
331 if ch_status(handle) == "fail"
332 call assert_false(1, "Can't open channel")
333 return
334 endif
335
336 " Check that eval works.
337 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
338
339 " Check that eval works if a zero id message is sent back.
340 let s:ch_reply = ''
341 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100342 if s:has_handler
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100343 call s:waitFor('"zero index" == s:ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100344 call assert_equal('zero index', s:ch_reply)
345 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100346 sleep 20m
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347 call assert_equal('', s:ch_reply)
348 endif
349
350 " Check that handler works if a zero id message is sent back.
351 let s:ch_reply = ''
352 let s:zero_reply = ''
353 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100354 call s:waitFor('"sent zero" == s:zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100355 if s:has_handler
356 call assert_equal('zero index', s:ch_reply)
357 else
358 call assert_equal('', s:ch_reply)
359 endif
360 call assert_equal('sent zero', s:zero_reply)
361endfunc
362
363func Test_zero_reply()
364 call ch_log('Test_zero_reply()')
365 " Run with channel handler
366 let s:has_handler = 1
367 let s:chopt.callback = 's:ChHandler'
368 call s:run_server('s:channel_zero')
369 unlet s:chopt.callback
370
371 " Run without channel handler
372 let s:has_handler = 0
373 call s:run_server('s:channel_zero')
374endfunc
375
376"""""""""
377
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100378let s:reply1 = ""
379func s:HandleRaw1(chan, msg)
380 unlet s:reply1
381 let s:reply1 = a:msg
382endfunc
383
384let s:reply2 = ""
385func s:HandleRaw2(chan, msg)
386 unlet s:reply2
387 let s:reply2 = a:msg
388endfunc
389
390let s:reply3 = ""
391func s:HandleRaw3(chan, msg)
392 unlet s:reply3
393 let s:reply3 = a:msg
394endfunc
395
396func s:raw_one_time_callback(port)
397 let handle = ch_open('localhost:' . a:port, s:chopt)
398 if ch_status(handle) == "fail"
399 call assert_false(1, "Can't open channel")
400 return
401 endif
402 call ch_setoptions(handle, {'mode': 'raw'})
403
404 " The message are sent raw, we do our own JSON strings here.
405 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100406 call s:waitFor('s:reply1 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100407 call assert_equal("[1, \"got it\"]", s:reply1)
408 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
409 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100410 call s:waitFor('s:reply2 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100411 call assert_equal("[2, \"something\"]", s:reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100412 " wait for the 200 msec delayed reply
413 call s:waitFor('s:reply3 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100414 call assert_equal("[3, \"waited\"]", s:reply3)
415endfunc
416
417func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100418 call ch_log('Test_raw_one_time_callback()')
419 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100420endfunc
421
422"""""""""
423
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100424" Test that trying to connect to a non-existing port fails quickly.
425func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100426 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100427 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100428 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100429 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100430 " Oops, port does exists.
431 call ch_close(handle)
432 else
433 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100434 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100435 endif
436
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 " We intend to use a socket that doesn't exist and wait for half a second
438 " before giving up. If the socket does exist it can fail in various ways.
439 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100440 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100441 try
442 let handle = ch_open('localhost:9867', {'waittime': 500})
443 if ch_status(handle) != "fail"
444 " Oops, port does exists.
445 call ch_close(handle)
446 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100447 " Failed connection should wait about 500 msec. Can be longer if the
448 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100449 let elapsed = reltime(start)
450 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100451 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100452 endif
453 catch
454 if v:exception !~ 'Connection reset by peer'
455 call assert_false(1, "Caught exception: " . v:exception)
456 endif
457 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100458endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100459
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100460"""""""""
461
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100462func Test_raw_pipe()
463 if !has('job')
464 return
465 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100466 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100467 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
468 call assert_equal("run", job_status(job))
469 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100470 " For a change use the job where a channel is expected.
471 call ch_sendraw(job, "echo something\n")
472 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100473 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
474
Bram Moolenaar151f6562016-03-07 21:19:38 +0100475 call ch_sendraw(job, "double this\n")
476 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100477 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
478
Bram Moolenaar151f6562016-03-07 21:19:38 +0100479 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100480 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
481 finally
482 call job_stop(job)
483 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100484
485 let s:job = job
486 call s:waitFor('"dead" == job_status(s:job)')
487 let info = job_info(job)
488 call assert_equal("dead", info.status)
489 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490endfunc
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 Moolenaar1adda342016-03-12 15:39:40 +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
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100522 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100523 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100524 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100525 call assert_equal("run", job_status(job))
526 try
527 let handle = job_getchannel(job)
528 call ch_sendraw(handle, "echo something\n")
529 call assert_equal("something", ch_readraw(handle))
530
531 call ch_sendraw(handle, "echoerr wrong\n")
532 call assert_equal("wrong", ch_readraw(handle))
533 finally
534 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100535 call ch_logfile('')
536 let loglines = readfile('Xlog')
537 call assert_true(len(loglines) > 10)
538 let found_test = 0
539 let found_send = 0
540 let found_recv = 0
541 let found_stop = 0
542 for l in loglines
543 if l =~ 'Test_nl_err_to_out_pipe'
544 let found_test = 1
545 endif
546 if l =~ 'SEND on.*echo something'
547 let found_send = 1
548 endif
549 if l =~ 'RECV on.*something'
550 let found_recv = 1
551 endif
552 if l =~ 'Stopping job with'
553 let found_stop = 1
554 endif
555 endfor
556 call assert_equal(1, found_test)
557 call assert_equal(1, found_send)
558 call assert_equal(1, found_recv)
559 call assert_equal(1, found_stop)
560 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100561 endtry
562endfunc
563
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100564func Test_nl_read_file()
565 if !has('job')
566 return
567 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100568 call ch_log('Test_nl_read_file()')
569 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
570 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100571 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100572 call assert_equal("run", job_status(job))
573 try
574 let handle = job_getchannel(job)
575 call assert_equal("something", ch_readraw(handle))
576 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
577 call assert_equal("this", ch_readraw(handle))
578 call assert_equal("AND this", ch_readraw(handle))
579 finally
580 call job_stop(job)
581 call delete('Xinput')
582 endtry
583endfunc
584
Bram Moolenaare98d1212016-03-08 15:37:41 +0100585func Test_nl_write_out_file()
586 if !has('job')
587 return
588 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100589 call ch_log('Test_nl_write_out_file()')
590 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100591 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100592 call assert_equal("run", job_status(job))
593 try
594 let handle = job_getchannel(job)
595 call ch_sendraw(handle, "echo line one\n")
596 call ch_sendraw(handle, "echo line two\n")
597 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100598 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100599 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
600 finally
601 call job_stop(job)
602 call delete('Xoutput')
603 endtry
604endfunc
605
606func Test_nl_write_err_file()
607 if !has('job')
608 return
609 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100610 call ch_log('Test_nl_write_err_file()')
611 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100612 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100613 call assert_equal("run", job_status(job))
614 try
615 let handle = job_getchannel(job)
616 call ch_sendraw(handle, "echoerr line one\n")
617 call ch_sendraw(handle, "echoerr line two\n")
618 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100619 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100620 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
621 finally
622 call job_stop(job)
623 call delete('Xoutput')
624 endtry
625endfunc
626
627func Test_nl_write_both_file()
628 if !has('job')
629 return
630 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631 call ch_log('Test_nl_write_both_file()')
632 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100633 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100634 call assert_equal("run", job_status(job))
635 try
636 let handle = job_getchannel(job)
637 call ch_sendraw(handle, "echoerr line one\n")
638 call ch_sendraw(handle, "echo line two\n")
639 call ch_sendraw(handle, "double this\n")
640 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100641 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100642 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
643 finally
644 call job_stop(job)
645 call delete('Xoutput')
646 endtry
647endfunc
648
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100649func Run_test_pipe_to_buffer(use_name)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100650 if !has('job')
651 return
652 endif
653 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100654 let options = {'out_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100655 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100656 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100657 let firstline = 'Reading from channel output...'
658 else
659 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100660 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100661 quit
662 let firstline = ''
663 endif
664 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100665 call assert_equal("run", job_status(job))
666 try
667 let handle = job_getchannel(job)
668 call ch_sendraw(handle, "echo line one\n")
669 call ch_sendraw(handle, "echo line two\n")
670 call ch_sendraw(handle, "double this\n")
671 call ch_sendraw(handle, "quit\n")
672 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100673 call s:waitFor('line("$") >= 6')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100674 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100675 bwipe!
676 finally
677 call job_stop(job)
678 endtry
679endfunc
680
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100681func Test_pipe_to_buffer_name()
682 call Run_test_pipe_to_buffer(1)
683endfunc
684
685func Test_pipe_to_buffer_nr()
686 call Run_test_pipe_to_buffer(0)
687endfunc
688
689func Run_test_pipe_err_to_buffer(use_name)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100690 if !has('job')
691 return
692 endif
693 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100694 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100696 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100697 let firstline = 'Reading from channel error...'
698 else
699 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100700 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100701 quit
702 let firstline = ''
703 endif
704 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100705 call assert_equal("run", job_status(job))
706 try
707 let handle = job_getchannel(job)
708 call ch_sendraw(handle, "echoerr line one\n")
709 call ch_sendraw(handle, "echoerr line two\n")
710 call ch_sendraw(handle, "doubleerr this\n")
711 call ch_sendraw(handle, "quit\n")
712 sp pipe-err
713 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100714 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100715 bwipe!
716 finally
717 call job_stop(job)
718 endtry
719endfunc
720
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100721func Test_pipe_err_to_buffer_name()
722 call Run_test_pipe_err_to_buffer(1)
723endfunc
724
725func Test_pipe_err_to_buffer_nr()
726 call Run_test_pipe_err_to_buffer(0)
727endfunc
728
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100729func Test_pipe_both_to_buffer()
730 if !has('job')
731 return
732 endif
733 call ch_log('Test_pipe_both_to_buffer()')
734 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100735 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100736 call assert_equal("run", job_status(job))
737 try
738 let handle = job_getchannel(job)
739 call ch_sendraw(handle, "echo line one\n")
740 call ch_sendraw(handle, "echoerr line two\n")
741 call ch_sendraw(handle, "double this\n")
742 call ch_sendraw(handle, "doubleerr that\n")
743 call ch_sendraw(handle, "quit\n")
744 sp pipe-err
745 call s:waitFor('line("$") >= 7')
746 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
747 bwipe!
748 finally
749 call job_stop(job)
750 endtry
751endfunc
752
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100753func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100754 if !has('job')
755 return
756 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100757 call ch_log('Test_pipe_from_buffer()')
758
759 sp pipe-input
760 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100761 let options = {'in_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100762 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100763 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100764 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100765 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100766 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100767
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100768 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100769 call assert_equal("run", job_status(job))
770 try
771 let handle = job_getchannel(job)
772 call assert_equal('one', ch_read(handle))
773 call assert_equal('two', ch_read(handle))
774 call assert_equal('three', ch_read(handle))
775 bwipe!
776 finally
777 call job_stop(job)
778 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100779endfunc
780
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100781func Test_pipe_from_buffer_name()
782 call Run_test_pipe_from_buffer(1)
783endfunc
784
785func Test_pipe_from_buffer_nr()
786 call Run_test_pipe_from_buffer(0)
787endfunc
788
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100789func Test_pipe_to_nameless_buffer()
790 if !has('job')
791 return
792 endif
793 call ch_log('Test_pipe_to_nameless_buffer()')
794 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100795 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100796 call assert_equal("run", job_status(job))
797 try
798 let handle = job_getchannel(job)
799 call ch_sendraw(handle, "echo line one\n")
800 call ch_sendraw(handle, "echo line two\n")
801 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100802 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100803 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
804 bwipe!
805 finally
806 call job_stop(job)
807 endtry
808endfunc
809
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100810func Test_pipe_to_buffer_json()
811 if !has('job')
812 return
813 endif
814 call ch_log('Test_pipe_to_buffer_json()')
815 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100816 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100817 call assert_equal("run", job_status(job))
818 try
819 let handle = job_getchannel(job)
820 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
821 call ch_sendraw(handle, "echo [-2, 12.34]\n")
822 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100823 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100824 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
825 bwipe!
826 finally
827 call job_stop(job)
828 endtry
829endfunc
830
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100831" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100832func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100833 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100834 if getline(line('$') - a:offset) == a:line
835 break
836 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100837 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100838 endfor
839endfunc
840
841func Test_pipe_io_two_buffers()
842 if !has('job')
843 return
844 endif
845 call ch_log('Test_pipe_io_two_buffers()')
846
847 " Create two buffers, one to read from and one to write to.
848 split pipe-output
849 set buftype=nofile
850 split pipe-input
851 set buftype=nofile
852
853 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100854 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
855 \ 'out_io': 'buffer', 'out_name': 'pipe-output'})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100856 call assert_equal("run", job_status(job))
857 try
858 exe "normal Gaecho hello\<CR>"
859 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100860 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100861 call assert_equal('hello', getline('$'))
862
863 exe bufwinnr('pipe-input') . "wincmd w"
864 exe "normal Gadouble this\<CR>"
865 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100866 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100867 call assert_equal('this', getline(line('$') - 1))
868 call assert_equal('AND this', getline('$'))
869
870 bwipe!
871 exe bufwinnr('pipe-input') . "wincmd w"
872 bwipe!
873 finally
874 call job_stop(job)
875 endtry
876endfunc
877
878func Test_pipe_io_one_buffer()
879 if !has('job')
880 return
881 endif
882 call ch_log('Test_pipe_io_one_buffer()')
883
884 " Create one buffer to read from and to write to.
885 split pipe-io
886 set buftype=nofile
887
888 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100889 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
890 \ 'out_io': 'buffer', 'out_name': 'pipe-io'})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100891 call assert_equal("run", job_status(job))
892 try
893 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100894 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100895 call assert_equal('hello', getline(line('$') - 1))
896
897 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100898 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100899 call assert_equal('this', getline(line('$') - 2))
900 call assert_equal('AND this', getline(line('$') - 1))
901
902 bwipe!
903 finally
904 call job_stop(job)
905 endtry
906endfunc
907
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100908func Test_pipe_null()
909 if !has('job')
910 return
911 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100912 call ch_log('Test_pipe_null()')
913
914 " We cannot check that no I/O works, we only check that the job starts
915 " properly.
916 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100917 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100918 call assert_equal("run", job_status(job))
919 try
920 call assert_equal('something', ch_read(job))
921 finally
922 call job_stop(job)
923 endtry
924
925 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100926 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100927 call assert_equal("run", job_status(job))
928 try
929 call assert_equal('err-out', ch_read(job, {"part": "err"}))
930 finally
931 call job_stop(job)
932 endtry
933
934 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100935 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100936 call assert_equal("run", job_status(job))
937 try
938 call assert_equal('something', ch_read(job))
939 finally
940 call job_stop(job)
941 endtry
942
943 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100944 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100945 call assert_equal("run", job_status(job))
946 call job_stop(job)
947
948 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100949 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100950 call assert_equal("run", job_status(job))
951 call assert_equal('channel fail', string(job_getchannel(job)))
952 call assert_equal('fail', ch_status(job))
953 call job_stop(job)
954endfunc
955
Bram Moolenaarde279892016-03-11 22:19:44 +0100956func Test_reuse_channel()
957 if !has('job')
958 return
959 endif
960 call ch_log('Test_reuse_channel()')
961
962 let job = job_start(s:python . " test_channel_pipe.py")
963 call assert_equal("run", job_status(job))
964 let handle = job_getchannel(job)
965 try
966 call ch_sendraw(handle, "echo something\n")
967 call assert_equal("something", ch_readraw(handle))
968 finally
969 call job_stop(job)
970 endtry
971
972 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
973 call assert_equal("run", job_status(job))
974 let handle = job_getchannel(job)
975 try
976 call ch_sendraw(handle, "echo again\n")
977 call assert_equal("again", ch_readraw(handle))
978 finally
979 call job_stop(job)
980 endtry
981endfunc
982
Bram Moolenaard46ae142016-02-16 13:33:52 +0100983""""""""""
984
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100985let s:unletResponse = ''
986func s:UnletHandler(handle, msg)
987 let s:unletResponse = a:msg
988 unlet s:channelfd
989endfunc
990
991" Test that "unlet handle" in a handler doesn't crash Vim.
992func s:unlet_handle(port)
993 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100994 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100995 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100996 call assert_equal('what?', s:unletResponse)
997endfunc
998
999func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001000 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001001 call s:run_server('s:unlet_handle')
1002endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001003
Bram Moolenaard46ae142016-02-16 13:33:52 +01001004""""""""""
1005
1006let s:unletResponse = ''
1007func s:CloseHandler(handle, msg)
1008 let s:unletResponse = a:msg
1009 call ch_close(s:channelfd)
1010endfunc
1011
1012" Test that "unlet handle" in a handler doesn't crash Vim.
1013func s:close_handle(port)
1014 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001015 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001016 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001017 call assert_equal('what?', s:unletResponse)
1018endfunc
1019
1020func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001021 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001022 call s:run_server('s:close_handle')
1023endfunc
1024
1025""""""""""
1026
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001027func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001028 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001029 silent! let ch = ch_open("noserver")
1030 echo ch
1031 let d = ch
1032endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001033
1034""""""""""
1035
1036func s:open_delay(port)
1037 " Wait up to a second for the port to open.
1038 let s:chopt.waittime = 1000
1039 let channel = ch_open('localhost:' . a:port, s:chopt)
1040 unlet s:chopt.waittime
1041 if ch_status(channel) == "fail"
1042 call assert_false(1, "Can't open channel")
1043 return
1044 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001045 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001046 call ch_close(channel)
1047endfunc
1048
1049func Test_open_delay()
1050 call ch_log('Test_open_delay()')
1051 " The server will wait half a second before creating the port.
1052 call s:run_server('s:open_delay', 'delay')
1053endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001054
1055"""""""""
1056
1057function MyFunction(a,b,c)
1058 let s:call_ret = [a:a, a:b, a:c]
1059endfunc
1060
1061function s:test_call(port)
1062 let handle = ch_open('localhost:' . a:port, s:chopt)
1063 if ch_status(handle) == "fail"
1064 call assert_false(1, "Can't open channel")
1065 return
1066 endif
1067
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001068 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001069 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001070 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001071 call assert_equal([1, 2, 3], s:call_ret)
1072endfunc
1073
1074func Test_call()
1075 call ch_log('Test_call()')
1076 call s:run_server('s:test_call')
1077endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001078
1079"""""""""
1080
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001081let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001082function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001083 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001084endfunc
1085
1086function s:test_exit_callback(port)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001087 call job_setoptions(s:job, {'exit_cb': 'MyExitCb'})
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001088 let s:exit_job = s:job
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001089 call assert_equal('MyExitCb', job_info(s:job)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001090endfunc
1091
1092func Test_exit_callback()
1093 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001094 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001095 call s:run_server('s:test_exit_callback')
1096
Bram Moolenaar9730f742016-02-28 19:50:51 +01001097 " wait up to a second for the job to exit
1098 for i in range(100)
1099 if s:job_exit_ret == 'done'
1100 break
1101 endif
1102 sleep 10m
1103 " calling job_status() triggers the callback
1104 call job_status(s:exit_job)
1105 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001106
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001107 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar8950a562016-03-12 15:22:55 +01001108 call assert_equal('dead', job_info(s:exit_job).status)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001109 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001110 endif
1111endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001112
1113"""""""""
1114
1115let s:ch_close_ret = 'alive'
1116function MyCloseCb(ch)
1117 let s:ch_close_ret = 'closed'
1118endfunc
1119
1120function s:test_close_callback(port)
1121 let handle = ch_open('localhost:' . a:port, s:chopt)
1122 if ch_status(handle) == "fail"
1123 call assert_false(1, "Can't open channel")
1124 return
1125 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001126 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001127
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001128 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001129 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001130 call assert_equal('closed', s:ch_close_ret)
1131endfunc
1132
1133func Test_close_callback()
1134 call ch_log('Test_close_callback()')
1135 call s:run_server('s:test_close_callback')
1136endfunc
1137
Bram Moolenaar9730f742016-02-28 19:50:51 +01001138" Uncomment this to see what happens, output is in src/testdir/channellog.
1139" call ch_logfile('channellog', 'w')