blob: dd40187f757161b54fa7d6dc9bfc7674bfd86e42 [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')
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100123 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +0100124 call assert_equal('no process', string(ch_getjob(handle)))
125 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100126 let dict = ch_info(handle)
127 call assert_true(dict.id != 0)
128 call assert_equal('open', dict.status)
129 call assert_equal(a:port, string(dict.port))
130 call assert_equal('open', dict.sock_status)
131 call assert_equal('socket', dict.sock_io)
132
Bram Moolenaard7ece102016-02-02 23:23:02 +0100133 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100134 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100135
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100136 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100137 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
138 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
139 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
140
141 " split command should work
142 call assert_equal('ok', ch_evalexpr(handle, 'split'))
143 call s:waitFor('exists("g:split")')
144 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100145
Bram Moolenaard7ece102016-02-02 23:23:02 +0100146 " Request that triggers sending two ex commands. These will usually be
147 " handled before getting the response, but it's not guaranteed, thus wait a
148 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100149 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100150 call s:waitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100151 call assert_equal('added1', getline(line('$') - 1))
152 call assert_equal('added2', getline('$'))
153
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100154 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100155 call s:waitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100156 call assert_equal('added more', getline('$'))
157
Bram Moolenaara07fec92016-02-05 21:04:08 +0100158 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100159 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100160 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 if !exists('s:responseHandle')
162 call assert_false(1, 's:responseHandle was not set')
163 else
164 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100165 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100167 call assert_equal('got it', s:responseMsg)
168
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100169 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100170 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100171 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100172 if !exists('s:responseHandle')
173 call assert_false(1, 's:responseHandle was not set')
174 else
175 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100176 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100177 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100178 call assert_equal('got it', s:responseMsg)
179
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100180 " Collect garbage, tests that our handle isn't collected.
181 call garbagecollect()
182
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100183 " check setting options (without testing the effect)
184 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100185 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100186 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100187 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100188 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100189
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100190 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100191 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100192 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100193 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100194
195 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100197 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100199
Bram Moolenaar55fab432016-02-07 16:53:13 +0100200 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100202 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100204
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100205 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100206 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100207 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100208 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100209
Bram Moolenaarf4160862016-02-05 23:09:12 +0100210 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100211 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100212 call s:waitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100213 call assert_equal('one', getline(line('$') - 2))
214 call assert_equal('two', getline(line('$') - 1))
215 call assert_equal('three', getline('$'))
216
217 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
219 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100220
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100221 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100222
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100223 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100224 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
225 let start = reltime()
226 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
227 let elapsed = reltime(start)
228 call assert_true(reltimefloat(elapsed) > 0.3)
229 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100230
231 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100232 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100233 let resp = ch_read(handle)
234 call assert_equal(type([]), type(resp))
235 call assert_equal(type(11), type(resp[0]))
236 call assert_equal('waited', resp[1])
237
Bram Moolenaard7ece102016-02-02 23:23:02 +0100238 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100240endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100241
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100242func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100243 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100244 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100245endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100246
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100247" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100249 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100250 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100251 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100252 return
253 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100254
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100255 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100256
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100257 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100259 call assert_false(1, "Can't open second channel")
260 return
261 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100262 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
263 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100264
265 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100266 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100267
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100268 call ch_close(newhandle)
269endfunc
270
271func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100272 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100273 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100274endfunc
275
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100276" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100277func s:server_crash(port)
278 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100279 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100280 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100281 return
282 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100283
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100284 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100285
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100286 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100287endfunc
288
289func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100290 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100291 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100292endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100293
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100294"""""""""
295
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296let s:reply = ""
297func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100298 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100299 let s:reply = a:msg
300endfunc
301
302func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100303 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100304 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100305 call assert_false(1, "Can't open channel")
306 return
307 endif
308
309 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100310 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100311 call s:waitFor('"we called you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100312 call assert_equal('we called you', s:reply)
313
314 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100315 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100316 call s:waitFor('"we did call you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100317 call assert_equal('we did call you', s:reply)
318endfunc
319
320func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100321 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100322 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100323 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100324 let s:chopt.callback = function('s:Handler')
325 call s:run_server('s:channel_handler')
326 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100327endfunc
328
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100329"""""""""
330
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100331let s:ch_reply = ''
332func s:ChHandler(chan, msg)
333 unlet s:ch_reply
334 let s:ch_reply = a:msg
335endfunc
336
337let s:zero_reply = ''
338func s:OneHandler(chan, msg)
339 unlet s:zero_reply
340 let s:zero_reply = a:msg
341endfunc
342
343func s:channel_zero(port)
344 let handle = ch_open('localhost:' . a:port, s:chopt)
345 if ch_status(handle) == "fail"
346 call assert_false(1, "Can't open channel")
347 return
348 endif
349
350 " Check that eval works.
351 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
352
353 " Check that eval works if a zero id message is sent back.
354 let s:ch_reply = ''
355 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100356 if s:has_handler
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100357 call s:waitFor('"zero index" == s:ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100358 call assert_equal('zero index', s:ch_reply)
359 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100360 sleep 20m
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100361 call assert_equal('', s:ch_reply)
362 endif
363
364 " Check that handler works if a zero id message is sent back.
365 let s:ch_reply = ''
366 let s:zero_reply = ''
367 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100368 call s:waitFor('"sent zero" == s:zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100369 if s:has_handler
370 call assert_equal('zero index', s:ch_reply)
371 else
372 call assert_equal('', s:ch_reply)
373 endif
374 call assert_equal('sent zero', s:zero_reply)
375endfunc
376
377func Test_zero_reply()
378 call ch_log('Test_zero_reply()')
379 " Run with channel handler
380 let s:has_handler = 1
381 let s:chopt.callback = 's:ChHandler'
382 call s:run_server('s:channel_zero')
383 unlet s:chopt.callback
384
385 " Run without channel handler
386 let s:has_handler = 0
387 call s:run_server('s:channel_zero')
388endfunc
389
390"""""""""
391
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100392let s:reply1 = ""
393func s:HandleRaw1(chan, msg)
394 unlet s:reply1
395 let s:reply1 = a:msg
396endfunc
397
398let s:reply2 = ""
399func s:HandleRaw2(chan, msg)
400 unlet s:reply2
401 let s:reply2 = a:msg
402endfunc
403
404let s:reply3 = ""
405func s:HandleRaw3(chan, msg)
406 unlet s:reply3
407 let s:reply3 = a:msg
408endfunc
409
410func s:raw_one_time_callback(port)
411 let handle = ch_open('localhost:' . a:port, s:chopt)
412 if ch_status(handle) == "fail"
413 call assert_false(1, "Can't open channel")
414 return
415 endif
416 call ch_setoptions(handle, {'mode': 'raw'})
417
418 " The message are sent raw, we do our own JSON strings here.
419 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100420 call s:waitFor('s:reply1 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100421 call assert_equal("[1, \"got it\"]", s:reply1)
422 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
423 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100424 call s:waitFor('s:reply2 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100425 call assert_equal("[2, \"something\"]", s:reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100426 " wait for the 200 msec delayed reply
427 call s:waitFor('s:reply3 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100428 call assert_equal("[3, \"waited\"]", s:reply3)
429endfunc
430
431func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100432 call ch_log('Test_raw_one_time_callback()')
433 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100434endfunc
435
436"""""""""
437
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100438" Test that trying to connect to a non-existing port fails quickly.
439func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100440 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100441 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100442 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100443 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100444 " Oops, port does exists.
445 call ch_close(handle)
446 else
447 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100448 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100449 endif
450
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100451 " We intend to use a socket that doesn't exist and wait for half a second
452 " before giving up. If the socket does exist it can fail in various ways.
453 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100454 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100455 try
456 let handle = ch_open('localhost:9867', {'waittime': 500})
457 if ch_status(handle) != "fail"
458 " Oops, port does exists.
459 call ch_close(handle)
460 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100461 " Failed connection should wait about 500 msec. Can be longer if the
462 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100463 let elapsed = reltime(start)
464 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100465 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100466 endif
467 catch
468 if v:exception !~ 'Connection reset by peer'
469 call assert_false(1, "Caught exception: " . v:exception)
470 endif
471 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100472endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100473
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100474"""""""""
475
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100476func Test_raw_pipe()
477 if !has('job')
478 return
479 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100480 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100481 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
482 call assert_equal("run", job_status(job))
483 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100484 " For a change use the job where a channel is expected.
485 call ch_sendraw(job, "echo something\n")
486 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100487 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
488
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 call ch_sendraw(job, "double this\n")
490 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100491 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
492
Bram Moolenaar151f6562016-03-07 21:19:38 +0100493 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
495 finally
496 call job_stop(job)
497 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100498
499 let s:job = job
500 call s:waitFor('"dead" == job_status(s:job)')
501 let info = job_info(job)
502 call assert_equal("dead", info.status)
503 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100504endfunc
505
506func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100507 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508 return
509 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100510 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100511 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100512 call assert_equal("run", job_status(job))
513 try
514 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100515 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100516 call assert_equal("something", ch_readraw(handle))
517
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100518 call ch_sendraw(handle, "echoerr wrong\n")
519 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
520
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100521 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100522 call assert_equal("this", ch_readraw(handle))
523 call assert_equal("AND this", ch_readraw(handle))
524
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100525 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100526 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100527 finally
528 call job_stop(job)
529 endtry
530endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100531
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100532func Test_nl_err_to_out_pipe()
533 if !has('job')
534 return
535 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100536 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100537 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100538 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100539 call assert_equal("run", job_status(job))
540 try
541 let handle = job_getchannel(job)
542 call ch_sendraw(handle, "echo something\n")
543 call assert_equal("something", ch_readraw(handle))
544
545 call ch_sendraw(handle, "echoerr wrong\n")
546 call assert_equal("wrong", ch_readraw(handle))
547 finally
548 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100549 call ch_logfile('')
550 let loglines = readfile('Xlog')
551 call assert_true(len(loglines) > 10)
552 let found_test = 0
553 let found_send = 0
554 let found_recv = 0
555 let found_stop = 0
556 for l in loglines
557 if l =~ 'Test_nl_err_to_out_pipe'
558 let found_test = 1
559 endif
560 if l =~ 'SEND on.*echo something'
561 let found_send = 1
562 endif
563 if l =~ 'RECV on.*something'
564 let found_recv = 1
565 endif
566 if l =~ 'Stopping job with'
567 let found_stop = 1
568 endif
569 endfor
570 call assert_equal(1, found_test)
571 call assert_equal(1, found_send)
572 call assert_equal(1, found_recv)
573 call assert_equal(1, found_stop)
574 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100575 endtry
576endfunc
577
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100578func Test_nl_read_file()
579 if !has('job')
580 return
581 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100582 call ch_log('Test_nl_read_file()')
583 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
584 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100585 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100586 call assert_equal("run", job_status(job))
587 try
588 let handle = job_getchannel(job)
589 call assert_equal("something", ch_readraw(handle))
590 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
591 call assert_equal("this", ch_readraw(handle))
592 call assert_equal("AND this", ch_readraw(handle))
593 finally
594 call job_stop(job)
595 call delete('Xinput')
596 endtry
597endfunc
598
Bram Moolenaare98d1212016-03-08 15:37:41 +0100599func Test_nl_write_out_file()
600 if !has('job')
601 return
602 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100603 call ch_log('Test_nl_write_out_file()')
604 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100605 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100606 call assert_equal("run", job_status(job))
607 try
608 let handle = job_getchannel(job)
609 call ch_sendraw(handle, "echo line one\n")
610 call ch_sendraw(handle, "echo line two\n")
611 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100612 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100613 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
614 finally
615 call job_stop(job)
616 call delete('Xoutput')
617 endtry
618endfunc
619
620func Test_nl_write_err_file()
621 if !has('job')
622 return
623 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call ch_log('Test_nl_write_err_file()')
625 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100626 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100627 call assert_equal("run", job_status(job))
628 try
629 let handle = job_getchannel(job)
630 call ch_sendraw(handle, "echoerr line one\n")
631 call ch_sendraw(handle, "echoerr line two\n")
632 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100633 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100634 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
635 finally
636 call job_stop(job)
637 call delete('Xoutput')
638 endtry
639endfunc
640
641func Test_nl_write_both_file()
642 if !has('job')
643 return
644 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call ch_log('Test_nl_write_both_file()')
646 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100647 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 call assert_equal("run", job_status(job))
649 try
650 let handle = job_getchannel(job)
651 call ch_sendraw(handle, "echoerr line one\n")
652 call ch_sendraw(handle, "echo line two\n")
653 call ch_sendraw(handle, "double this\n")
654 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100655 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
657 finally
658 call job_stop(job)
659 call delete('Xoutput')
660 endtry
661endfunc
662
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100663func Run_test_pipe_to_buffer(use_name)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100664 if !has('job')
665 return
666 endif
667 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100668 let options = {'out_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100669 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100670 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100671 let firstline = 'Reading from channel output...'
672 else
673 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100674 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100675 quit
676 let firstline = ''
677 endif
678 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100679 call assert_equal("run", job_status(job))
680 try
681 let handle = job_getchannel(job)
682 call ch_sendraw(handle, "echo line one\n")
683 call ch_sendraw(handle, "echo line two\n")
684 call ch_sendraw(handle, "double this\n")
685 call ch_sendraw(handle, "quit\n")
686 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100687 call s:waitFor('line("$") >= 6')
Bram Moolenaar6a063632016-03-21 23:18:54 +0100688 if getline('$') == 'DETACH'
689 $del
690 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100691 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100692 bwipe!
693 finally
694 call job_stop(job)
695 endtry
696endfunc
697
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100698func Test_pipe_to_buffer_name()
699 call Run_test_pipe_to_buffer(1)
700endfunc
701
702func Test_pipe_to_buffer_nr()
703 call Run_test_pipe_to_buffer(0)
704endfunc
705
706func Run_test_pipe_err_to_buffer(use_name)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100707 if !has('job')
708 return
709 endif
710 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100711 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100712 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100713 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100714 let firstline = 'Reading from channel error...'
715 else
716 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100717 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100718 quit
719 let firstline = ''
720 endif
721 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100722 call assert_equal("run", job_status(job))
723 try
724 let handle = job_getchannel(job)
725 call ch_sendraw(handle, "echoerr line one\n")
726 call ch_sendraw(handle, "echoerr line two\n")
727 call ch_sendraw(handle, "doubleerr this\n")
728 call ch_sendraw(handle, "quit\n")
729 sp pipe-err
730 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100731 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100732 bwipe!
733 finally
734 call job_stop(job)
735 endtry
736endfunc
737
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100738func Test_pipe_err_to_buffer_name()
739 call Run_test_pipe_err_to_buffer(1)
740endfunc
741
742func Test_pipe_err_to_buffer_nr()
743 call Run_test_pipe_err_to_buffer(0)
744endfunc
745
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100746func Test_pipe_both_to_buffer()
747 if !has('job')
748 return
749 endif
750 call ch_log('Test_pipe_both_to_buffer()')
751 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100752 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100753 call assert_equal("run", job_status(job))
754 try
755 let handle = job_getchannel(job)
756 call ch_sendraw(handle, "echo line one\n")
757 call ch_sendraw(handle, "echoerr line two\n")
758 call ch_sendraw(handle, "double this\n")
759 call ch_sendraw(handle, "doubleerr that\n")
760 call ch_sendraw(handle, "quit\n")
761 sp pipe-err
762 call s:waitFor('line("$") >= 7')
763 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
764 bwipe!
765 finally
766 call job_stop(job)
767 endtry
768endfunc
769
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100771 if !has('job')
772 return
773 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100774 call ch_log('Test_pipe_from_buffer()')
775
776 sp pipe-input
777 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100778 let options = {'in_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100779 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100780 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100781 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100782 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100783 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100784
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100785 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100786 call assert_equal("run", job_status(job))
787 try
788 let handle = job_getchannel(job)
789 call assert_equal('one', ch_read(handle))
790 call assert_equal('two', ch_read(handle))
791 call assert_equal('three', ch_read(handle))
792 bwipe!
793 finally
794 call job_stop(job)
795 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100796endfunc
797
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100798func Test_pipe_from_buffer_name()
799 call Run_test_pipe_from_buffer(1)
800endfunc
801
802func Test_pipe_from_buffer_nr()
803 call Run_test_pipe_from_buffer(0)
804endfunc
805
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100806func Test_pipe_to_nameless_buffer()
807 if !has('job')
808 return
809 endif
810 call ch_log('Test_pipe_to_nameless_buffer()')
811 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100812 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100813 call assert_equal("run", job_status(job))
814 try
815 let handle = job_getchannel(job)
816 call ch_sendraw(handle, "echo line one\n")
817 call ch_sendraw(handle, "echo line two\n")
818 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100819 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100820 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
821 bwipe!
822 finally
823 call job_stop(job)
824 endtry
825endfunc
826
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100827func Test_pipe_to_buffer_json()
828 if !has('job')
829 return
830 endif
831 call ch_log('Test_pipe_to_buffer_json()')
832 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100833 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100834 call assert_equal("run", job_status(job))
835 try
836 let handle = job_getchannel(job)
837 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
838 call ch_sendraw(handle, "echo [-2, 12.34]\n")
839 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100840 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100841 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
842 bwipe!
843 finally
844 call job_stop(job)
845 endtry
846endfunc
847
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100848" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100849func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100850 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100851 if getline(line('$') - a:offset) == a:line
852 break
853 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100854 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100855 endfor
856endfunc
857
858func Test_pipe_io_two_buffers()
859 if !has('job')
860 return
861 endif
862 call ch_log('Test_pipe_io_two_buffers()')
863
864 " Create two buffers, one to read from and one to write to.
865 split pipe-output
866 set buftype=nofile
867 split pipe-input
868 set buftype=nofile
869
870 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100871 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
872 \ 'out_io': 'buffer', 'out_name': 'pipe-output'})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100873 call assert_equal("run", job_status(job))
874 try
875 exe "normal Gaecho hello\<CR>"
876 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100877 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100878 call assert_equal('hello', getline('$'))
879
880 exe bufwinnr('pipe-input') . "wincmd w"
881 exe "normal Gadouble this\<CR>"
882 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100883 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100884 call assert_equal('this', getline(line('$') - 1))
885 call assert_equal('AND this', getline('$'))
886
887 bwipe!
888 exe bufwinnr('pipe-input') . "wincmd w"
889 bwipe!
890 finally
891 call job_stop(job)
892 endtry
893endfunc
894
895func Test_pipe_io_one_buffer()
896 if !has('job')
897 return
898 endif
899 call ch_log('Test_pipe_io_one_buffer()')
900
901 " Create one buffer to read from and to write to.
902 split pipe-io
903 set buftype=nofile
904
905 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100906 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
907 \ 'out_io': 'buffer', 'out_name': 'pipe-io'})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100908 call assert_equal("run", job_status(job))
909 try
910 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100911 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100912 call assert_equal('hello', getline(line('$') - 1))
913
914 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100915 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100916 call assert_equal('this', getline(line('$') - 2))
917 call assert_equal('AND this', getline(line('$') - 1))
918
919 bwipe!
920 finally
921 call job_stop(job)
922 endtry
923endfunc
924
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100925func Test_pipe_null()
926 if !has('job')
927 return
928 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100929 call ch_log('Test_pipe_null()')
930
931 " We cannot check that no I/O works, we only check that the job starts
932 " properly.
933 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100934 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100935 call assert_equal("run", job_status(job))
936 try
937 call assert_equal('something', ch_read(job))
938 finally
939 call job_stop(job)
940 endtry
941
942 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100943 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100944 call assert_equal("run", job_status(job))
945 try
946 call assert_equal('err-out', ch_read(job, {"part": "err"}))
947 finally
948 call job_stop(job)
949 endtry
950
951 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100952 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100953 call assert_equal("run", job_status(job))
954 try
955 call assert_equal('something', ch_read(job))
956 finally
957 call job_stop(job)
958 endtry
959
960 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100961 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100962 call assert_equal("run", job_status(job))
963 call job_stop(job)
964
965 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100966 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100967 call assert_equal("run", job_status(job))
968 call assert_equal('channel fail', string(job_getchannel(job)))
969 call assert_equal('fail', ch_status(job))
970 call job_stop(job)
971endfunc
972
Bram Moolenaarde279892016-03-11 22:19:44 +0100973func Test_reuse_channel()
974 if !has('job')
975 return
976 endif
977 call ch_log('Test_reuse_channel()')
978
979 let job = job_start(s:python . " test_channel_pipe.py")
980 call assert_equal("run", job_status(job))
981 let handle = job_getchannel(job)
982 try
983 call ch_sendraw(handle, "echo something\n")
984 call assert_equal("something", ch_readraw(handle))
985 finally
986 call job_stop(job)
987 endtry
988
989 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
990 call assert_equal("run", job_status(job))
991 let handle = job_getchannel(job)
992 try
993 call ch_sendraw(handle, "echo again\n")
994 call assert_equal("again", ch_readraw(handle))
995 finally
996 call job_stop(job)
997 endtry
998endfunc
999
Bram Moolenaar75f72652016-03-20 22:16:56 +01001000func Test_out_cb()
1001 if !has('job')
1002 return
1003 endif
1004 call ch_log('Test_out_cb()')
1005
1006 let dict = {'thisis': 'dict: '}
1007 func dict.outHandler(chan, msg) dict
1008 let s:outmsg = self.thisis . a:msg
1009 endfunc
1010 func dict.errHandler(chan, msg) dict
1011 let s:errmsg = self.thisis . a:msg
1012 endfunc
1013 let job = job_start(s:python . " test_channel_pipe.py",
1014 \ {'out_cb': dict.outHandler,
1015 \ 'out_mode': 'json',
1016 \ 'err_cb': dict.errHandler,
1017 \ 'err_mode': 'json'})
1018 call assert_equal("run", job_status(job))
1019 try
1020 let s:outmsg = ''
1021 let s:errmsg = ''
1022 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1023 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1024 call s:waitFor('s:outmsg != ""')
1025 call assert_equal("dict: hello", s:outmsg)
1026 call s:waitFor('s:errmsg != ""')
1027 call assert_equal("dict: there", s:errmsg)
1028 finally
1029 call job_stop(job)
1030 endtry
1031endfunc
1032
Bram Moolenaard46ae142016-02-16 13:33:52 +01001033""""""""""
1034
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001035let s:unletResponse = ''
1036func s:UnletHandler(handle, msg)
1037 let s:unletResponse = a:msg
1038 unlet s:channelfd
1039endfunc
1040
1041" Test that "unlet handle" in a handler doesn't crash Vim.
1042func s:unlet_handle(port)
1043 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001044 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001045 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001046 call assert_equal('what?', s:unletResponse)
1047endfunc
1048
1049func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001050 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001051 call s:run_server('s:unlet_handle')
1052endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001053
Bram Moolenaard46ae142016-02-16 13:33:52 +01001054""""""""""
1055
1056let s:unletResponse = ''
1057func s:CloseHandler(handle, msg)
1058 let s:unletResponse = a:msg
1059 call ch_close(s:channelfd)
1060endfunc
1061
1062" Test that "unlet handle" in a handler doesn't crash Vim.
1063func s:close_handle(port)
1064 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001065 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001066 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001067 call assert_equal('what?', s:unletResponse)
1068endfunc
1069
1070func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001071 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001072 call s:run_server('s:close_handle')
1073endfunc
1074
1075""""""""""
1076
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001077func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001078 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001079 silent! let ch = ch_open("noserver")
1080 echo ch
1081 let d = ch
1082endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001083
1084""""""""""
1085
1086func s:open_delay(port)
1087 " Wait up to a second for the port to open.
1088 let s:chopt.waittime = 1000
1089 let channel = ch_open('localhost:' . a:port, s:chopt)
1090 unlet s:chopt.waittime
1091 if ch_status(channel) == "fail"
1092 call assert_false(1, "Can't open channel")
1093 return
1094 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001095 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001096 call ch_close(channel)
1097endfunc
1098
1099func Test_open_delay()
1100 call ch_log('Test_open_delay()')
1101 " The server will wait half a second before creating the port.
1102 call s:run_server('s:open_delay', 'delay')
1103endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001104
1105"""""""""
1106
1107function MyFunction(a,b,c)
1108 let s:call_ret = [a:a, a:b, a:c]
1109endfunc
1110
1111function s:test_call(port)
1112 let handle = ch_open('localhost:' . a:port, s:chopt)
1113 if ch_status(handle) == "fail"
1114 call assert_false(1, "Can't open channel")
1115 return
1116 endif
1117
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001118 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001119 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001120 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001121 call assert_equal([1, 2, 3], s:call_ret)
1122endfunc
1123
1124func Test_call()
1125 call ch_log('Test_call()')
1126 call s:run_server('s:test_call')
1127endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001128
1129"""""""""
1130
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001131let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001132function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001133 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001134endfunc
1135
1136function s:test_exit_callback(port)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001137 call job_setoptions(s:job, {'exit_cb': 'MyExitCb'})
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001138 let s:exit_job = s:job
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001139 call assert_equal('MyExitCb', job_info(s:job)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001140endfunc
1141
1142func Test_exit_callback()
1143 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001144 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001145 call s:run_server('s:test_exit_callback')
1146
Bram Moolenaar9730f742016-02-28 19:50:51 +01001147 " wait up to a second for the job to exit
1148 for i in range(100)
1149 if s:job_exit_ret == 'done'
1150 break
1151 endif
1152 sleep 10m
1153 " calling job_status() triggers the callback
1154 call job_status(s:exit_job)
1155 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001156
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001157 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar8950a562016-03-12 15:22:55 +01001158 call assert_equal('dead', job_info(s:exit_job).status)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001159 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001160 endif
1161endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001162
1163"""""""""
1164
1165let s:ch_close_ret = 'alive'
1166function MyCloseCb(ch)
1167 let s:ch_close_ret = 'closed'
1168endfunc
1169
1170function s:test_close_callback(port)
1171 let handle = ch_open('localhost:' . a:port, s:chopt)
1172 if ch_status(handle) == "fail"
1173 call assert_false(1, "Can't open channel")
1174 return
1175 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001176 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001177
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001178 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001179 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001180 call assert_equal('closed', s:ch_close_ret)
1181endfunc
1182
1183func Test_close_callback()
1184 call ch_log('Test_close_callback()')
1185 call s:run_server('s:test_close_callback')
1186endfunc
1187
Bram Moolenaar9730f742016-02-28 19:50:51 +01001188" Uncomment this to see what happens, output is in src/testdir/channellog.
1189" call ch_logfile('channellog', 'w')