blob: 5709bf83255101f2880e03dc8db35ab147ca2942 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaare2469252016-02-04 10:54:34 +01003if !has('channel')
4 finish
5endif
6
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
8
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010011 " Can't run this test without Python.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaar37175402017-03-18 20:18:45 +010015" Uncomment the next line to see what happens. Output is in
16" src/testdir/channellog.
17" call ch_logfile('channellog', 'w')
18
Bram Moolenaare74e8e72016-02-16 22:01:30 +010019let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010020
Bram Moolenaard6a8d482016-02-10 20:32:20 +010021" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010022func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020023 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010024endfunc
25
Bram Moolenaar321efdd2016-07-15 17:09:11 +020026let g:Ch_responseMsg = ''
27func Ch_requestHandler(handle, msg)
28 let g:Ch_responseHandle = a:handle
29 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010030endfunc
31
Bram Moolenaar321efdd2016-07-15 17:09:11 +020032func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010033 " Avoid dropping messages, since we don't use a callback here.
34 let s:chopt.drop = 'never'
Bram Moolenaard6a8d482016-02-10 20:32:20 +010035 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010036 unlet s:chopt.drop
Bram Moolenaar77073442016-02-13 23:23:53 +010037 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010038 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010039 return
40 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010041 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010042 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010043 call assert_equal('no process', string(ch_getjob(handle)))
44 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010045 let dict = ch_info(handle)
46 call assert_true(dict.id != 0)
47 call assert_equal('open', dict.status)
48 call assert_equal(a:port, string(dict.port))
49 call assert_equal('open', dict.sock_status)
50 call assert_equal('socket', dict.sock_io)
51
Bram Moolenaard7ece102016-02-02 23:23:02 +010052 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010053 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010054
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010055 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010056 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
57 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
58 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
59
60 " split command should work
61 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020062 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010063 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010064
Bram Moolenaarf1f07922016-08-26 17:58:53 +020065 " string with ][ should work
66 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
67
Bram Moolenaar4b785f62016-11-29 21:54:44 +010068 " nothing to read now
69 call assert_equal(0, ch_canread(handle))
70
Bram Moolenaarf1f07922016-08-26 17:58:53 +020071 " sending three messages quickly then reading should work
72 for i in range(3)
73 call ch_sendexpr(handle, 'echo hello ' . i)
74 endfor
75 call assert_equal('hello 0', ch_read(handle)[1])
76 call assert_equal('hello 1', ch_read(handle)[1])
77 call assert_equal('hello 2', ch_read(handle)[1])
78
Bram Moolenaard7ece102016-02-02 23:23:02 +010079 " Request that triggers sending two ex commands. These will usually be
80 " handled before getting the response, but it's not guaranteed, thus wait a
81 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010082 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020083 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010084 call assert_equal('added1', getline(line('$') - 1))
85 call assert_equal('added2', getline('$'))
86
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010087 " Request command "foo bar", which fails silently.
88 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020089 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020090 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010091
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010092 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020093 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010094 call assert_equal('added more', getline('$'))
95
Bram Moolenaara07fec92016-02-05 21:04:08 +010096 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020097 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
98 call WaitFor('exists("g:Ch_responseHandle")')
99 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100100 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100101 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200102 call assert_equal(handle, g:Ch_responseHandle)
103 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100104 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200105 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100106
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200107 let g:Ch_responseMsg = ''
108 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
109 call WaitFor('exists("g:Ch_responseHandle")')
110 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100111 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100112 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200113 call assert_equal(handle, g:Ch_responseHandle)
114 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100115 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200116 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100117
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200118 " Using lambda.
119 let g:Ch_responseMsg = ''
120 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
121 call WaitFor('exists("g:Ch_responseHandle")')
122 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100123 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200124 else
125 call assert_equal(handle, g:Ch_responseHandle)
126 unlet g:Ch_responseHandle
127 endif
128 call assert_equal('got it', g:Ch_responseMsg)
129
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100130 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200131 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100132
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100133 " check setting options (without testing the effect)
134 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100135 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100136 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100137 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100138 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100139 call ch_setoptions(handle, {'drop': 'never'})
140 call ch_setoptions(handle, {'drop': 'auto'})
141 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100142
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100143 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100144 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100145 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100146 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100147
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100148 " Send an eval request with special characters.
149 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
150 sleep 10m
151 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
152
153 " Send an eval request to get a line with special characters.
154 call setline(3, "a\nb\<CR>c\x01d\x7fe")
155 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
156 sleep 10m
157 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
158
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100159 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100160 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100161 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100162 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163
Bram Moolenaar55fab432016-02-07 16:53:13 +0100164 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100165 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100166 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100167 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100168
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100169 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100170 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100171 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100172 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100173
Bram Moolenaarf4160862016-02-05 23:09:12 +0100174 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200176 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100177 call assert_equal('one', getline(line('$') - 2))
178 call assert_equal('two', getline(line('$') - 1))
179 call assert_equal('three', getline('$'))
180
181 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
183 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100184
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100186
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100187 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100188 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
189 let start = reltime()
190 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
191 let elapsed = reltime(start)
192 call assert_true(reltimefloat(elapsed) > 0.3)
193 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100194
195 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100197 let resp = ch_read(handle)
198 call assert_equal(type([]), type(resp))
199 call assert_equal(type(11), type(resp[0]))
200 call assert_equal('waited', resp[1])
201
Bram Moolenaard7ece102016-02-02 23:23:02 +0100202 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100204endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100205
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100206func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200208 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100209endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100210
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100211" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200212func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100213 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200214 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100215 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100216 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100217 return
218 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100221
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100222 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100223 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100224 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100225 return
226 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
228 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100229
230 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100231 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100232
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100233 call ch_close(newhandle)
234endfunc
235
236func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100237 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200238 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100239endfunc
240
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100241" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200242func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100243 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100245 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100246 return
247 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100249 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100250
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100251 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100252endfunc
253
254func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100255 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200256 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100257endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100258
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100259"""""""""
260
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200261func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100262 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200263 unlet g:Ch_reply
264 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100265endfunc
266
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200267func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100268 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100269 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100270 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100271 return
272 endif
273
274 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100275 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200276 call WaitFor('"we called you" == g:Ch_reply')
277 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100278
279 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100280 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200281 call WaitFor('"we did call you" == g:Ch_reply')
282 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100283endfunc
284
285func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100286 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200287 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200288 let s:chopt.callback = 'Ch_handler'
289 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200290 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200291 let s:chopt.callback = function('Ch_handler')
292 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100293 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100294endfunc
295
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100296"""""""""
297
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200298let g:Ch_reply = ''
299func Ch_zeroHandler(chan, msg)
300 unlet g:Ch_reply
301 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100302endfunc
303
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200304let g:Ch_zero_reply = ''
305func Ch_oneHandler(chan, msg)
306 unlet g:Ch_zero_reply
307 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100308endfunc
309
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100311 let handle = ch_open('localhost:' . a:port, s:chopt)
312 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100313 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100314 return
315 endif
316
317 " Check that eval works.
318 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
319
320 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200321 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100322 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200324 call WaitFor('"zero index" == g:Ch_reply')
325 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100327 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200328 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100329 endif
330
331 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200332 let g:Ch_reply = ''
333 let g:Ch_zero_reply = ''
334 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
335 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200337 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100338 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200341 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100342endfunc
343
344func Test_zero_reply()
345 call ch_log('Test_zero_reply()')
346 " Run with channel handler
347 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348 let s:chopt.callback = 'Ch_zeroHandler'
349 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 unlet s:chopt.callback
351
352 " Run without channel handler
353 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200354 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100355endfunc
356
357"""""""""
358
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200359let g:Ch_reply1 = ""
360func Ch_handleRaw1(chan, msg)
361 unlet g:Ch_reply1
362 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100363endfunc
364
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200365let g:Ch_reply2 = ""
366func Ch_handleRaw2(chan, msg)
367 unlet g:Ch_reply2
368 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100369endfunc
370
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200371let g:Ch_reply3 = ""
372func Ch_handleRaw3(chan, msg)
373 unlet g:Ch_reply3
374 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100375endfunc
376
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200377func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100378 let handle = ch_open('localhost:' . a:port, s:chopt)
379 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100380 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100381 return
382 endif
383 call ch_setoptions(handle, {'mode': 'raw'})
384
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100385 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200386 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200387 call WaitFor('g:Ch_reply1 != ""')
388 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200389 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
390 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200391 call WaitFor('g:Ch_reply2 != ""')
392 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100393 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200394 call WaitFor('g:Ch_reply3 != ""')
395 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100396endfunc
397
398func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100399 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200400 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100401endfunc
402
403"""""""""
404
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100405" Test that trying to connect to a non-existing port fails quickly.
406func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100407 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100408 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100409 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100410 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100411 " Oops, port does exists.
412 call ch_close(handle)
413 else
414 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100415 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100416 endif
417
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100418 " We intend to use a socket that doesn't exist and wait for half a second
419 " before giving up. If the socket does exist it can fail in various ways.
420 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100421 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100422 try
423 let handle = ch_open('localhost:9867', {'waittime': 500})
424 if ch_status(handle) != "fail"
425 " Oops, port does exists.
426 call ch_close(handle)
427 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100428 " Failed connection should wait about 500 msec. Can be longer if the
429 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100430 let elapsed = reltime(start)
431 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100432 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100433 endif
434 catch
435 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100436 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 endif
438 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100439endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100440
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100441"""""""""
442
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100443func Test_raw_pipe()
444 if !has('job')
445 return
446 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100447 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100448 " Add a dummy close callback to avoid that messages are dropped when calling
449 " ch_canread().
450 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100451 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200452 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100453 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200454
455 call assert_equal("open", ch_status(job))
456 call assert_equal("open", ch_status(job), {"part": "out"})
457 call assert_equal("open", ch_status(job), {"part": "err"})
458 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
459 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
460
461 let dict = ch_info(job)
462 call assert_true(dict.id != 0)
463 call assert_equal('open', dict.status)
464 call assert_equal('open', dict.out_status)
465 call assert_equal('RAW', dict.out_mode)
466 call assert_equal('pipe', dict.out_io)
467 call assert_equal('open', dict.err_status)
468 call assert_equal('RAW', dict.err_mode)
469 call assert_equal('pipe', dict.err_io)
470
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100472 " For a change use the job where a channel is expected.
473 call ch_sendraw(job, "echo something\n")
474 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100475 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
476
Bram Moolenaar151f6562016-03-07 21:19:38 +0100477 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100478 let g:handle = job_getchannel(job)
479 call WaitFor('ch_canread(g:handle)')
480 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100481 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100482 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
483
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200484 let g:Ch_reply = ""
485 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
486 call WaitFor('"" != g:Ch_reply')
487 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
488
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
491 finally
492 call job_stop(job)
493 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100494
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200495 let g:Ch_job = job
496 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100497 let info = job_info(job)
498 call assert_equal("dead", info.status)
499 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100500endfunc
501
502func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100503 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100504 return
505 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100506 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100507 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508 call assert_equal("run", job_status(job))
509 try
510 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100511 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100512 call assert_equal("something", ch_readraw(handle))
513
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100514 call ch_sendraw(handle, "echoerr wrong\n")
515 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
516
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100517 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100518 call assert_equal("this", ch_readraw(handle))
519 call assert_equal("AND this", ch_readraw(handle))
520
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200521 call ch_sendraw(handle, "split this line\n")
522 call assert_equal("this linethis linethis line", ch_readraw(handle))
523
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100524 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100525 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100526 finally
527 call job_stop(job)
528 endtry
529endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100530
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100531func Test_nl_err_to_out_pipe()
532 if !has('job')
533 return
534 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100535 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100536 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100537 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100538 call assert_equal("run", job_status(job))
539 try
540 let handle = job_getchannel(job)
541 call ch_sendraw(handle, "echo something\n")
542 call assert_equal("something", ch_readraw(handle))
543
544 call ch_sendraw(handle, "echoerr wrong\n")
545 call assert_equal("wrong", ch_readraw(handle))
546 finally
547 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100548 call ch_logfile('')
549 let loglines = readfile('Xlog')
550 call assert_true(len(loglines) > 10)
551 let found_test = 0
552 let found_send = 0
553 let found_recv = 0
554 let found_stop = 0
555 for l in loglines
556 if l =~ 'Test_nl_err_to_out_pipe'
557 let found_test = 1
558 endif
559 if l =~ 'SEND on.*echo something'
560 let found_send = 1
561 endif
562 if l =~ 'RECV on.*something'
563 let found_recv = 1
564 endif
565 if l =~ 'Stopping job with'
566 let found_stop = 1
567 endif
568 endfor
569 call assert_equal(1, found_test)
570 call assert_equal(1, found_send)
571 call assert_equal(1, found_recv)
572 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200573 " On MS-Windows need to sleep for a moment to be able to delete the file.
574 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100575 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100576 endtry
577endfunc
578
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200579func Stop_g_job()
580 call job_stop(g:job)
581 if has('win32')
582 " On MS-Windows the server must close the file handle before we are able
583 " to delete the file.
584 call WaitFor('job_status(g:job) == "dead"')
585 sleep 10m
586 endif
587endfunc
588
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100589func Test_nl_read_file()
590 if !has('job')
591 return
592 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100593 call ch_log('Test_nl_read_file()')
594 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200595 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100596 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200597 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100598 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200599 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100600 call assert_equal("something", ch_readraw(handle))
601 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
602 call assert_equal("this", ch_readraw(handle))
603 call assert_equal("AND this", ch_readraw(handle))
604 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200605 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100606 call delete('Xinput')
607 endtry
608endfunc
609
Bram Moolenaare98d1212016-03-08 15:37:41 +0100610func Test_nl_write_out_file()
611 if !has('job')
612 return
613 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100614 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200615 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100616 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200617 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100618 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200619 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100620 call ch_sendraw(handle, "echo line one\n")
621 call ch_sendraw(handle, "echo line two\n")
622 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200623 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
625 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200626 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100627 call delete('Xoutput')
628 endtry
629endfunc
630
631func Test_nl_write_err_file()
632 if !has('job')
633 return
634 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200636 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100637 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200638 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100639 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200640 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100641 call ch_sendraw(handle, "echoerr line one\n")
642 call ch_sendraw(handle, "echoerr line two\n")
643 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200644 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
646 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 call delete('Xoutput')
649 endtry
650endfunc
651
652func Test_nl_write_both_file()
653 if !has('job')
654 return
655 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200657 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100658 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100660 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200661 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100662 call ch_sendraw(handle, "echoerr line one\n")
663 call ch_sendraw(handle, "echo line two\n")
664 call ch_sendraw(handle, "double this\n")
665 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200666 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100667 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
668 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200669 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100670 call delete('Xoutput')
671 endtry
672endfunc
673
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200674func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200675 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200676endfunc
677
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200678func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100679 if !has('job')
680 return
681 endif
682 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200683 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200684 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200685 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100686 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100687 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200688 if a:do_msg
689 let expected[0] = 'Reading from channel output...'
690 else
691 let options['out_msg'] = 0
692 call remove(expected, 0)
693 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100694 else
695 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100696 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100697 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200698 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100699 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200700 if a:nomod
701 let options['out_modifiable'] = 0
702 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100703 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100704 call assert_equal("run", job_status(job))
705 try
706 let handle = job_getchannel(job)
707 call ch_sendraw(handle, "echo line one\n")
708 call ch_sendraw(handle, "echo line two\n")
709 call ch_sendraw(handle, "double this\n")
710 call ch_sendraw(handle, "quit\n")
711 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100712 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200713 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200714 if a:nomod
715 call assert_equal(0, &modifiable)
716 else
717 call assert_equal(1, &modifiable)
718 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200719 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100720 bwipe!
721 finally
722 call job_stop(job)
723 endtry
724endfunc
725
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200727 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100728endfunc
729
730func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200731 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100732endfunc
733
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200734func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200735 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200736endfunc
737
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200738func Test_pipe_to_buffer_name_nomsg()
739 call Run_test_pipe_to_buffer(1, 0, 1)
740endfunc
741
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200742func Test_close_output_buffer()
743 if !has('job')
744 return
745 endif
746 enew!
747 let test_lines = ['one', 'two']
748 call setline(1, test_lines)
749 call ch_log('Test_close_output_buffer()')
750 let options = {'out_io': 'buffer'}
751 let options['out_name'] = 'buffer-output'
752 let options['out_msg'] = 0
753 split buffer-output
754 let job = job_start(s:python . " test_channel_write.py", options)
755 call assert_equal("run", job_status(job))
756 try
757 call WaitFor('line("$") == 3')
758 call assert_equal(3, line('$'))
759 quit!
760 sleep 100m
761 " Make sure the write didn't happen to the wrong buffer.
762 call assert_equal(test_lines, getline(1, line('$')))
763 call assert_equal(-1, bufwinnr('buffer-output'))
764 sbuf buffer-output
765 call assert_notequal(-1, bufwinnr('buffer-output'))
766 sleep 100m
767 close " no more writes
768 bwipe!
769 finally
770 call job_stop(job)
771 endtry
772endfunc
773
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200774func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100775 if !has('job')
776 return
777 endif
778 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100779 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200780 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100781 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100782 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200783 if a:do_msg
784 let expected[0] = 'Reading from channel error...'
785 else
786 let options['err_msg'] = 0
787 call remove(expected, 0)
788 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100789 else
790 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100791 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100792 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200793 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100794 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200795 if a:nomod
796 let options['err_modifiable'] = 0
797 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100798 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100799 call assert_equal("run", job_status(job))
800 try
801 let handle = job_getchannel(job)
802 call ch_sendraw(handle, "echoerr line one\n")
803 call ch_sendraw(handle, "echoerr line two\n")
804 call ch_sendraw(handle, "doubleerr this\n")
805 call ch_sendraw(handle, "quit\n")
806 sp pipe-err
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100807 call WaitFor('line("$") == ' . len(expected))
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200808 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200809 if a:nomod
810 call assert_equal(0, &modifiable)
811 else
812 call assert_equal(1, &modifiable)
813 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100814 bwipe!
815 finally
816 call job_stop(job)
817 endtry
818endfunc
819
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100820func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200821 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100822endfunc
823
824func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200825 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200826endfunc
827
828func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200829 call Run_test_pipe_err_to_buffer(1, 1, 1)
830endfunc
831
832func Test_pipe_err_to_buffer_name_nomsg()
833 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834endfunc
835
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100836func Test_pipe_both_to_buffer()
837 if !has('job')
838 return
839 endif
840 call ch_log('Test_pipe_both_to_buffer()')
841 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100842 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100843 call assert_equal("run", job_status(job))
844 try
845 let handle = job_getchannel(job)
846 call ch_sendraw(handle, "echo line one\n")
847 call ch_sendraw(handle, "echoerr line two\n")
848 call ch_sendraw(handle, "double this\n")
849 call ch_sendraw(handle, "doubleerr that\n")
850 call ch_sendraw(handle, "quit\n")
851 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200852 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100853 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
854 bwipe!
855 finally
856 call job_stop(job)
857 endtry
858endfunc
859
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100860func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100861 if !has('job')
862 return
863 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100864 call ch_log('Test_pipe_from_buffer()')
865
866 sp pipe-input
867 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200868 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100869 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100870 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100871 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100872 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100873 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100874
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100875 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100876 call assert_equal("run", job_status(job))
877 try
878 let handle = job_getchannel(job)
879 call assert_equal('one', ch_read(handle))
880 call assert_equal('two', ch_read(handle))
881 call assert_equal('three', ch_read(handle))
882 bwipe!
883 finally
884 call job_stop(job)
885 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100886endfunc
887
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100888func Test_pipe_from_buffer_name()
889 call Run_test_pipe_from_buffer(1)
890endfunc
891
892func Test_pipe_from_buffer_nr()
893 call Run_test_pipe_from_buffer(0)
894endfunc
895
Bram Moolenaar0874a832016-09-01 15:11:51 +0200896func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200897 if !executable('sort') || !has('job')
898 return
899 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200900 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
901 if a:use_buffer
902 split sortin
903 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
904 let options.in_io = 'buffer'
905 let options.in_name = 'sortin'
906 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200907 if !a:all
908 let options.in_top = 2
909 let options.in_bot = 4
910 endif
911 let g:job = job_start('sort', options)
912 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200913
914 if !a:use_buffer
915 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
916 call ch_close_in(g:job)
917 endif
918
Bram Moolenaard8b55492016-09-01 14:35:22 +0200919 call WaitFor('job_status(g:job) == "dead"')
920 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200921
Bram Moolenaard8b55492016-09-01 14:35:22 +0200922 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200923 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200924 call assert_equal('Reading from channel output...', getline(1))
925 if a:all
926 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
927 else
928 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
929 endif
930
931 call job_stop(g:job)
932 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200933 if a:use_buffer
934 bwipe! sortin
935 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200936 bwipe! sortout
937endfunc
938
939func Test_pipe_through_sort_all()
940 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200941 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200942endfunc
943
944func Test_pipe_through_sort_some()
945 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200946 call Run_pipe_through_sort(0, 1)
947endfunc
948
949func Test_pipe_through_sort_feed()
950 call ch_log('Test_pipe_through_sort_feed()')
951 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200952endfunc
953
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100954func Test_pipe_to_nameless_buffer()
955 if !has('job')
956 return
957 endif
958 call ch_log('Test_pipe_to_nameless_buffer()')
959 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100960 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100961 call assert_equal("run", job_status(job))
962 try
963 let handle = job_getchannel(job)
964 call ch_sendraw(handle, "echo line one\n")
965 call ch_sendraw(handle, "echo line two\n")
966 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200967 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100968 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
969 bwipe!
970 finally
971 call job_stop(job)
972 endtry
973endfunc
974
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100975func Test_pipe_to_buffer_json()
976 if !has('job')
977 return
978 endif
979 call ch_log('Test_pipe_to_buffer_json()')
980 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100981 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100982 call assert_equal("run", job_status(job))
983 try
984 let handle = job_getchannel(job)
985 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
986 call ch_sendraw(handle, "echo [-2, 12.34]\n")
987 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200988 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100989 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
990 bwipe!
991 finally
992 call job_stop(job)
993 endtry
994endfunc
995
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100996" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100997func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100998 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100999 if getline(line('$') - a:offset) == a:line
1000 break
1001 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001002 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001003 endfor
1004endfunc
1005
1006func Test_pipe_io_two_buffers()
1007 if !has('job')
1008 return
1009 endif
1010 call ch_log('Test_pipe_io_two_buffers()')
1011
1012 " Create two buffers, one to read from and one to write to.
1013 split pipe-output
1014 set buftype=nofile
1015 split pipe-input
1016 set buftype=nofile
1017
1018 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001019 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001020 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1021 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001022 call assert_equal("run", job_status(job))
1023 try
1024 exe "normal Gaecho hello\<CR>"
1025 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001026 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001027 call assert_equal('hello', getline('$'))
1028
1029 exe bufwinnr('pipe-input') . "wincmd w"
1030 exe "normal Gadouble this\<CR>"
1031 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001032 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001033 call assert_equal('this', getline(line('$') - 1))
1034 call assert_equal('AND this', getline('$'))
1035
1036 bwipe!
1037 exe bufwinnr('pipe-input') . "wincmd w"
1038 bwipe!
1039 finally
1040 call job_stop(job)
1041 endtry
1042endfunc
1043
1044func Test_pipe_io_one_buffer()
1045 if !has('job')
1046 return
1047 endif
1048 call ch_log('Test_pipe_io_one_buffer()')
1049
1050 " Create one buffer to read from and to write to.
1051 split pipe-io
1052 set buftype=nofile
1053
1054 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001055 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001056 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1057 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001058 call assert_equal("run", job_status(job))
1059 try
1060 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001061 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001062 call assert_equal('hello', getline(line('$') - 1))
1063
1064 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001065 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001066 call assert_equal('this', getline(line('$') - 2))
1067 call assert_equal('AND this', getline(line('$') - 1))
1068
1069 bwipe!
1070 finally
1071 call job_stop(job)
1072 endtry
1073endfunc
1074
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001075func Test_pipe_null()
1076 if !has('job')
1077 return
1078 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001079 call ch_log('Test_pipe_null()')
1080
1081 " We cannot check that no I/O works, we only check that the job starts
1082 " properly.
1083 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001084 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001085 call assert_equal("run", job_status(job))
1086 try
1087 call assert_equal('something', ch_read(job))
1088 finally
1089 call job_stop(job)
1090 endtry
1091
1092 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001093 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001094 call assert_equal("run", job_status(job))
1095 try
1096 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1097 finally
1098 call job_stop(job)
1099 endtry
1100
1101 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001102 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001103 call assert_equal("run", job_status(job))
1104 try
1105 call assert_equal('something', ch_read(job))
1106 finally
1107 call job_stop(job)
1108 endtry
1109
1110 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001111 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001112 call assert_equal("run", job_status(job))
1113 call job_stop(job)
1114
1115 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001116 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001117 call assert_equal("run", job_status(job))
1118 call assert_equal('channel fail', string(job_getchannel(job)))
1119 call assert_equal('fail', ch_status(job))
1120 call job_stop(job)
1121endfunc
1122
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001123func Test_pipe_to_buffer_raw()
1124 if !has('job')
1125 return
1126 endif
1127 call ch_log('Test_raw_pipe_to_buffer()')
1128 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1129 split testout
1130 let job = job_start([s:python, '-c',
1131 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1132 call assert_equal("run", job_status(job))
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001133 call WaitFor('len(join(getline(1, "$"), "")) >= 10000', 3000)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001134 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001135 let totlen = 0
1136 for line in getline(1, '$')
1137 call assert_equal('', substitute(line, '^\.*', '', ''))
1138 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001139 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001140 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001141 finally
1142 call job_stop(job)
1143 bwipe!
1144 endtry
1145endfunc
1146
Bram Moolenaarde279892016-03-11 22:19:44 +01001147func Test_reuse_channel()
1148 if !has('job')
1149 return
1150 endif
1151 call ch_log('Test_reuse_channel()')
1152
1153 let job = job_start(s:python . " test_channel_pipe.py")
1154 call assert_equal("run", job_status(job))
1155 let handle = job_getchannel(job)
1156 try
1157 call ch_sendraw(handle, "echo something\n")
1158 call assert_equal("something", ch_readraw(handle))
1159 finally
1160 call job_stop(job)
1161 endtry
1162
1163 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1164 call assert_equal("run", job_status(job))
1165 let handle = job_getchannel(job)
1166 try
1167 call ch_sendraw(handle, "echo again\n")
1168 call assert_equal("again", ch_readraw(handle))
1169 finally
1170 call job_stop(job)
1171 endtry
1172endfunc
1173
Bram Moolenaar75f72652016-03-20 22:16:56 +01001174func Test_out_cb()
1175 if !has('job')
1176 return
1177 endif
1178 call ch_log('Test_out_cb()')
1179
1180 let dict = {'thisis': 'dict: '}
1181 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001182 if type(a:msg) == v:t_string
1183 let g:Ch_outmsg = self.thisis . a:msg
1184 else
1185 let g:Ch_outobj = a:msg
1186 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001187 endfunc
1188 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001189 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001190 endfunc
1191 let job = job_start(s:python . " test_channel_pipe.py",
1192 \ {'out_cb': dict.outHandler,
1193 \ 'out_mode': 'json',
1194 \ 'err_cb': dict.errHandler,
1195 \ 'err_mode': 'json'})
1196 call assert_equal("run", job_status(job))
1197 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001198 let g:Ch_outmsg = ''
1199 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001200 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1201 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001202 call WaitFor('g:Ch_outmsg != ""')
1203 call assert_equal("dict: hello", g:Ch_outmsg)
1204 call WaitFor('g:Ch_errmsg != ""')
1205 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001206
1207 " Receive a json object split in pieces
1208 unlet! g:Ch_outobj
1209 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
1210 call WaitFor('exists("g:Ch_outobj")')
1211 call assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001212 finally
1213 call job_stop(job)
1214 endtry
1215endfunc
1216
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001217func Test_out_close_cb()
1218 if !has('job')
1219 return
1220 endif
1221 call ch_log('Test_out_close_cb()')
1222
1223 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001224 let g:Ch_msg1 = ''
1225 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001226 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001227 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001228 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001229 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001230 let s:counter += 1
1231 endfunc
1232 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001233 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001234 let s:counter += 1
1235 endfunc
1236 let job = job_start(s:python . " test_channel_pipe.py quit now",
1237 \ {'out_cb': 'OutHandler',
1238 \ 'close_cb': 'CloseHandler'})
1239 call assert_equal("run", job_status(job))
1240 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001241 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1242 call assert_equal('quit', g:Ch_msg1)
1243 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001244 finally
1245 call job_stop(job)
1246 delfunc OutHandler
1247 delfunc CloseHandler
1248 endtry
1249endfunc
1250
Bram Moolenaar437905c2016-04-26 19:01:05 +02001251func Test_read_in_close_cb()
1252 if !has('job')
1253 return
1254 endif
1255 call ch_log('Test_read_in_close_cb()')
1256
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001257 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001258 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001259 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001260 endfunc
1261 let job = job_start(s:python . " test_channel_pipe.py quit now",
1262 \ {'close_cb': 'CloseHandler'})
1263 call assert_equal("run", job_status(job))
1264 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001265 call WaitFor('g:Ch_received != ""')
1266 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001267 finally
1268 call job_stop(job)
1269 delfunc CloseHandler
1270 endtry
1271endfunc
1272
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001273func Test_out_cb_lambda()
1274 if !has('job')
1275 return
1276 endif
1277 call ch_log('Test_out_cb_lambda()')
1278
1279 let job = job_start(s:python . " test_channel_pipe.py",
1280 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1281 \ 'out_mode': 'json',
1282 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1283 \ 'err_mode': 'json'})
1284 call assert_equal("run", job_status(job))
1285 try
1286 let g:Ch_outmsg = ''
1287 let g:Ch_errmsg = ''
1288 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1289 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1290 call WaitFor('g:Ch_outmsg != ""')
1291 call assert_equal("lambda: hello", g:Ch_outmsg)
1292 call WaitFor('g:Ch_errmsg != ""')
1293 call assert_equal("lambda: there", g:Ch_errmsg)
1294 finally
1295 call job_stop(job)
1296 endtry
1297endfunc
1298
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001299func Test_close_and_exit_cb()
1300 if !has('job')
1301 return
1302 endif
1303 call ch_log('Test_close_and_exit_cb')
1304
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001305 let g:retdict = {'ret': {}}
1306 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001307 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1308 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001309 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001310 let self.ret['exit_cb'] = job_status(a:job)
1311 endfunc
1312
1313 let g:job = job_start('echo', {
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001314 \ 'close_cb': g:retdict.close_cb,
1315 \ 'exit_cb': g:retdict.exit_cb,
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001316 \ })
1317 call assert_equal('run', job_status(g:job))
1318 unlet g:job
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001319 call WaitFor('len(g:retdict.ret) >= 2')
1320 call assert_equal(2, len(g:retdict.ret))
1321 call assert_match('^\%(dead\|run\)', g:retdict.ret['close_cb'])
1322 call assert_equal('dead', g:retdict.ret['exit_cb'])
1323 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001324endfunc
1325
Bram Moolenaard46ae142016-02-16 13:33:52 +01001326""""""""""
1327
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001328let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001329func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001330 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001331 unlet s:channelfd
1332endfunc
1333
1334" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001335func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001336 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001337 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001338 call WaitFor('"what?" == g:Ch_unletResponse')
1339 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001340endfunc
1341
1342func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001343 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001344 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001345endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001346
Bram Moolenaard46ae142016-02-16 13:33:52 +01001347""""""""""
1348
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001349let g:Ch_unletResponse = ''
1350func Ch_CloseHandler(handle, msg)
1351 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001352 call ch_close(s:channelfd)
1353endfunc
1354
1355" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001356func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001357 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001358 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1359 call WaitFor('"what?" == g:Ch_unletResponse')
1360 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001361endfunc
1362
1363func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001364 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001365 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001366endfunc
1367
1368""""""""""
1369
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001370func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001371 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001372 silent! let ch = ch_open("noserver")
1373 echo ch
1374 let d = ch
1375endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001376
1377""""""""""
1378
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001379func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001380 " Wait up to a second for the port to open.
1381 let s:chopt.waittime = 1000
1382 let channel = ch_open('localhost:' . a:port, s:chopt)
1383 unlet s:chopt.waittime
1384 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001385 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001386 return
1387 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001388 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001389 call ch_close(channel)
1390endfunc
1391
1392func Test_open_delay()
1393 call ch_log('Test_open_delay()')
1394 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001395 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001396endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001397
1398"""""""""
1399
1400function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001401 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001402endfunc
1403
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001404function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001405 let handle = ch_open('localhost:' . a:port, s:chopt)
1406 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001407 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001408 return
1409 endif
1410
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001411 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001412 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001413 call WaitFor('len(g:Ch_call_ret) > 0')
1414 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001415endfunc
1416
1417func Test_call()
1418 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001419 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001420endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001421
1422"""""""""
1423
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001424let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001425function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001426 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001427endfunc
1428
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001429function Ch_test_exit_callback(port)
1430 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1431 let g:Ch_exit_job = g:currentJob
1432 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001433endfunc
1434
1435func Test_exit_callback()
1436 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001437 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001438 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001439
Bram Moolenaar9730f742016-02-28 19:50:51 +01001440 " wait up to a second for the job to exit
1441 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001442 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001443 break
1444 endif
1445 sleep 10m
1446 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001447 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001448 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001449
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001450 call assert_equal('done', g:Ch_job_exit_ret)
1451 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1452 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001453 endif
1454endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001455
Bram Moolenaar97792de2016-10-15 18:36:49 +02001456function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001457 if job_info(a:job).process == g:exit_cb_val.process
1458 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1459 endif
1460 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001461endfunction
1462
1463func Test_exit_callback_interval()
1464 if !has('job')
1465 return
1466 endif
1467
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001468 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001469 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001470 let g:exit_cb_val.process = job_info(job).process
1471 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1472 let elapsed = reltimefloat(g:exit_cb_val.end)
1473 call assert_true(elapsed > 0.5)
1474 call assert_true(elapsed < 1.0)
1475
1476 " case: unreferenced job, using timer
1477 if !has('timers')
1478 return
1479 endif
1480
1481 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1482 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1483 let g:exit_cb_val.process = job_info(g:job).process
1484 unlet g:job
1485 call Standby(1000)
1486 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1487 let elapsed = reltimefloat(g:exit_cb_val.end)
1488 else
1489 let elapsed = 1.0
1490 endif
1491 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001492 call assert_true(elapsed < 1.0)
1493endfunc
1494
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001495"""""""""
1496
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001497let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001498function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001499 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001500endfunc
1501
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001502function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001503 let handle = ch_open('localhost:' . a:port, s:chopt)
1504 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001505 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001506 return
1507 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001508 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001509
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001510 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001511 call WaitFor('"closed" == g:Ch_close_ret')
1512 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001513endfunc
1514
1515func Test_close_callback()
1516 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001517 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001518endfunc
1519
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001520function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001521 let handle = ch_open('localhost:' . a:port, s:chopt)
1522 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001523 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001524 return
1525 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001526 let g:Ch_d = {}
1527 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001528 let self.close_ret = 'closed'
1529 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001530 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001531
1532 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001533 call WaitFor('"closed" == g:Ch_d.close_ret')
1534 call assert_equal('closed', g:Ch_d.close_ret)
1535 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001536endfunc
1537
1538func Test_close_partial()
1539 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001540 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001541endfunc
1542
Bram Moolenaar80385682016-03-27 19:13:35 +02001543func Test_job_start_invalid()
1544 call assert_fails('call job_start($x)', 'E474:')
1545 call assert_fails('call job_start("")', 'E474:')
1546endfunc
1547
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001548func Test_job_stop_immediately()
1549 if !has('job')
1550 return
1551 endif
1552
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001553 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001554 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001555 call job_stop(g:job)
1556 call WaitFor('"dead" == job_status(g:job)')
1557 call assert_equal('dead', job_status(g:job))
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001558 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001559 call job_stop(g:job, 'kill')
1560 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001561 endtry
1562endfunc
1563
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001564" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001565func Test_partial_in_channel_cycle()
1566 let d = {}
1567 let d.a = function('string', [d])
1568 try
1569 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1570 catch
1571 call assert_exception('E901:')
1572 endtry
1573 unlet d
1574endfunc
1575
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001576func Test_using_freed_memory()
1577 let g:a = job_start(['ls'])
1578 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001579 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001580endfunc
1581
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001582func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001583 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001584 return
1585 endif
1586 sp test_channel.vim
1587 let g:linecount = line('$')
1588 close
1589 split testout
1590 1,$delete
1591 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001592 call WaitFor('line("$") >= g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001593 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001594 bwipe!
1595endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001596
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001597func Test_cmd_parsing()
1598 if !has('unix')
1599 return
1600 endif
1601 call assert_false(filereadable("file with space"))
1602 let job = job_start('touch "file with space"')
1603 call WaitFor('filereadable("file with space")')
1604 call assert_true(filereadable("file with space"))
1605 call delete("file with space")
1606
1607 let job = job_start('touch file\ with\ space')
1608 call WaitFor('filereadable("file with space")')
1609 call assert_true(filereadable("file with space"))
1610 call delete("file with space")
1611endfunc
1612
Bram Moolenaar82117982016-08-27 19:21:48 +02001613func Test_raw_passes_nul()
1614 if !executable('cat') || !has('job')
1615 return
1616 endif
1617
1618 " Test lines from the job containing NUL are stored correctly in a buffer.
1619 new
1620 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1621 w! Xtestread
1622 bwipe!
1623 split testout
1624 1,$delete
1625 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1626 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001627 call assert_equal("asdf\nasdf", getline(1))
1628 call assert_equal("xxx\n", getline(2))
1629 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001630
1631 call delete('Xtestread')
1632 bwipe!
1633
1634 " Test lines from a buffer with NUL bytes are written correctly to the job.
1635 new mybuffer
1636 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1637 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1638 call WaitFor('"dead" == job_status(g:Ch_job)')
1639 bwipe!
1640 split Xtestwrite
1641 call assert_equal("asdf\nasdf", getline(1))
1642 call assert_equal("xxx\n", getline(2))
1643 call assert_equal("\nyyy", getline(3))
1644
1645 call delete('Xtestwrite')
1646 bwipe!
1647endfunc
1648
Bram Moolenaarec68a992016-10-03 21:37:41 +02001649func MyLineCountCb(ch, msg)
1650 let g:linecount += 1
1651endfunc
1652
1653func Test_read_nonl_line()
1654 if !has('job')
1655 return
1656 endif
1657
1658 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001659 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001660 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1661 call WaitFor('3 <= g:linecount')
1662 call assert_equal(3, g:linecount)
1663endfunc
1664
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001665func Test_read_from_terminated_job()
1666 if !has('job')
1667 return
1668 endif
1669
1670 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001671 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001672 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1673 call WaitFor('1 <= g:linecount')
1674 call assert_equal(1, g:linecount)
1675endfunc
1676
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001677func Test_env()
1678 if !has('job')
1679 return
1680 endif
1681
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001682 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001683 if has('win32')
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001684 call job_start(['cmd', '/c', 'echo %FOO%'], {'callback': {ch,msg->execute(":let g:envstr .= msg")}, 'env':{'FOO': 'bar'}})
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001685 else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001686 call job_start([&shell, &shellcmdflag, 'echo $FOO'], {'callback': {ch,msg->execute(":let g:envstr .= msg")}, 'env':{'FOO': 'bar'}})
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001687 endif
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001688 call WaitFor('"" != g:envstr')
1689 call assert_equal("bar", g:envstr)
1690 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001691endfunc
1692
1693func Test_cwd()
1694 if !has('job')
1695 return
1696 endif
1697
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001698 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001699 if has('win32')
1700 let expect = $TEMP
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001701 call job_start(['cmd', '/c', 'echo %CD%'], {'callback': {ch,msg->execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001702 else
1703 let expect = $HOME
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001704 call job_start(['pwd'], {'callback': {ch,msg->execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001705 endif
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001706 call WaitFor('"" != g:envstr')
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001707 let expect = substitute(expect, '[/\\]$', '', '')
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001708 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1709 if $CI != '' && stridx(g:envstr, '/private/') == 0
1710 let g:envstr = g:envstr[8:]
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001711 endif
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001712 call assert_equal(expect, g:envstr)
1713 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001714endfunc
1715
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001716function Ch_test_close_lambda(port)
1717 let handle = ch_open('localhost:' . a:port, s:chopt)
1718 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001719 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001720 return
1721 endif
1722 let g:Ch_close_ret = ''
1723 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1724
1725 call assert_equal('', ch_evalexpr(handle, 'close me'))
1726 call WaitFor('"closed" == g:Ch_close_ret')
1727 call assert_equal('closed', g:Ch_close_ret)
1728endfunc
1729
1730func Test_close_lambda()
1731 call ch_log('Test_close_lambda()')
1732 call s:run_server('Ch_test_close_lambda')
1733endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001734
1735func s:test_list_args(cmd, out, remove_lf)
1736 try
1737 let g:out = ''
1738 call job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
1739 call WaitFor('"" != g:out')
1740 if has('win32')
1741 let g:out = substitute(g:out, '\r', '', 'g')
1742 endif
1743 if a:remove_lf
1744 let g:out = substitute(g:out, '\n$', '', 'g')
1745 endif
1746 call assert_equal(a:out, g:out)
1747 finally
1748 unlet g:out
1749 endtry
1750endfunc
1751
1752func Test_list_args()
1753 if !has('job')
1754 return
1755 endif
1756
1757 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1758 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1759 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1760 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1761 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1762 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1763 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1764 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1765 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1766 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1767 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1768 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1769 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1770
1771 " tests which not contain spaces in the argument
1772 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1773 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1774 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1775 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1776 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1777 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1778 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1779 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1780 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1781endfunc