blob: 034bd3977807e806ab8fea2414c0f5e523bf997d [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')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100277
278 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100279 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200280 call WaitFor('"we did call you" == g:Ch_reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100281endfunc
282
283func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100284 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200285 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200286 let s:chopt.callback = 'Ch_handler'
287 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200288 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200289 let s:chopt.callback = function('Ch_handler')
290 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100291 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292endfunc
293
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100294"""""""""
295
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200296let g:Ch_reply = ''
297func Ch_zeroHandler(chan, msg)
298 unlet g:Ch_reply
299 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100300endfunc
301
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200302let g:Ch_zero_reply = ''
303func Ch_oneHandler(chan, msg)
304 unlet g:Ch_zero_reply
305 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100306endfunc
307
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200308func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100309 let handle = ch_open('localhost:' . a:port, s:chopt)
310 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100311 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100312 return
313 endif
314
315 " Check that eval works.
316 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
317
318 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200319 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100321 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322 call WaitFor('"zero index" == g:Ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100324 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200325 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 endif
327
328 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200329 let g:Ch_reply = ''
330 let g:Ch_zero_reply = ''
331 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
332 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100333 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200334 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200336 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100338endfunc
339
340func Test_zero_reply()
341 call ch_log('Test_zero_reply()')
342 " Run with channel handler
343 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200344 let s:chopt.callback = 'Ch_zeroHandler'
345 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100346 unlet s:chopt.callback
347
348 " Run without channel handler
349 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200350 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100351endfunc
352
353"""""""""
354
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200355let g:Ch_reply1 = ""
356func Ch_handleRaw1(chan, msg)
357 unlet g:Ch_reply1
358 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100359endfunc
360
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200361let g:Ch_reply2 = ""
362func Ch_handleRaw2(chan, msg)
363 unlet g:Ch_reply2
364 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100365endfunc
366
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200367let g:Ch_reply3 = ""
368func Ch_handleRaw3(chan, msg)
369 unlet g:Ch_reply3
370 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100371endfunc
372
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200373func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100374 let handle = ch_open('localhost:' . a:port, s:chopt)
375 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100376 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100377 return
378 endif
379 call ch_setoptions(handle, {'mode': 'raw'})
380
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100381 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200382 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200383 call WaitFor('g:Ch_reply1 != ""')
384 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200385 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
386 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200387 call WaitFor('g:Ch_reply2 != ""')
388 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100389 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200390 call WaitFor('g:Ch_reply3 != ""')
391 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100392endfunc
393
394func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100395 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200396 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100397endfunc
398
399"""""""""
400
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100401" Test that trying to connect to a non-existing port fails quickly.
402func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100403 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100404 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100405 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100406 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100407 " Oops, port does exists.
408 call ch_close(handle)
409 else
410 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100411 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100412 endif
413
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100414 " We intend to use a socket that doesn't exist and wait for half a second
415 " before giving up. If the socket does exist it can fail in various ways.
416 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100417 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100418 try
419 let handle = ch_open('localhost:9867', {'waittime': 500})
420 if ch_status(handle) != "fail"
421 " Oops, port does exists.
422 call ch_close(handle)
423 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100424 " Failed connection should wait about 500 msec. Can be longer if the
425 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100426 let elapsed = reltime(start)
427 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100428 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100429 endif
430 catch
431 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100432 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100433 endif
434 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100435endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100436
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100437"""""""""
438
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100439func Test_raw_pipe()
440 if !has('job')
441 return
442 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100443 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100444 " Add a dummy close callback to avoid that messages are dropped when calling
445 " ch_canread().
446 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100447 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200448 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100449 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200450
451 call assert_equal("open", ch_status(job))
452 call assert_equal("open", ch_status(job), {"part": "out"})
453 call assert_equal("open", ch_status(job), {"part": "err"})
454 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
455 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
456
457 let dict = ch_info(job)
458 call assert_true(dict.id != 0)
459 call assert_equal('open', dict.status)
460 call assert_equal('open', dict.out_status)
461 call assert_equal('RAW', dict.out_mode)
462 call assert_equal('pipe', dict.out_io)
463 call assert_equal('open', dict.err_status)
464 call assert_equal('RAW', dict.err_mode)
465 call assert_equal('pipe', dict.err_io)
466
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100467 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100468 " For a change use the job where a channel is expected.
469 call ch_sendraw(job, "echo something\n")
470 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
472
Bram Moolenaar151f6562016-03-07 21:19:38 +0100473 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100474 let g:handle = job_getchannel(job)
475 call WaitFor('ch_canread(g:handle)')
476 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100477 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100478 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
479
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200480 let g:Ch_reply = ""
481 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
482 call WaitFor('"" != g:Ch_reply')
483 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
484
Bram Moolenaar151f6562016-03-07 21:19:38 +0100485 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100486 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
487 finally
488 call job_stop(job)
489 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100490
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200491 let g:Ch_job = job
492 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100493 let info = job_info(job)
494 call assert_equal("dead", info.status)
495 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100496endfunc
497
498func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100499 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100500 return
501 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100502 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100503 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100504 call assert_equal("run", job_status(job))
505 try
506 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100507 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100508 call assert_equal("something", ch_readraw(handle))
509
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100510 call ch_sendraw(handle, "echoerr wrong\n")
511 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
512
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100514 call assert_equal("this", ch_readraw(handle))
515 call assert_equal("AND this", ch_readraw(handle))
516
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200517 call ch_sendraw(handle, "split this line\n")
518 call assert_equal("this linethis linethis line", ch_readraw(handle))
519
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100520 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100521 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522 finally
523 call job_stop(job)
524 endtry
525endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100526
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100527func Test_nl_err_to_out_pipe()
528 if !has('job')
529 return
530 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100531 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100532 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100533 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100534 call assert_equal("run", job_status(job))
535 try
536 let handle = job_getchannel(job)
537 call ch_sendraw(handle, "echo something\n")
538 call assert_equal("something", ch_readraw(handle))
539
540 call ch_sendraw(handle, "echoerr wrong\n")
541 call assert_equal("wrong", ch_readraw(handle))
542 finally
543 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100544 call ch_logfile('')
545 let loglines = readfile('Xlog')
546 call assert_true(len(loglines) > 10)
547 let found_test = 0
548 let found_send = 0
549 let found_recv = 0
550 let found_stop = 0
551 for l in loglines
552 if l =~ 'Test_nl_err_to_out_pipe'
553 let found_test = 1
554 endif
555 if l =~ 'SEND on.*echo something'
556 let found_send = 1
557 endif
558 if l =~ 'RECV on.*something'
559 let found_recv = 1
560 endif
561 if l =~ 'Stopping job with'
562 let found_stop = 1
563 endif
564 endfor
565 call assert_equal(1, found_test)
566 call assert_equal(1, found_send)
567 call assert_equal(1, found_recv)
568 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200569 " On MS-Windows need to sleep for a moment to be able to delete the file.
570 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100571 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100572 endtry
573endfunc
574
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200575func Stop_g_job()
576 call job_stop(g:job)
577 if has('win32')
578 " On MS-Windows the server must close the file handle before we are able
579 " to delete the file.
580 call WaitFor('job_status(g:job) == "dead"')
581 sleep 10m
582 endif
583endfunc
584
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100585func Test_nl_read_file()
586 if !has('job')
587 return
588 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100589 call ch_log('Test_nl_read_file()')
590 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200591 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100592 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200593 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100594 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200595 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100596 call assert_equal("something", ch_readraw(handle))
597 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
598 call assert_equal("this", ch_readraw(handle))
599 call assert_equal("AND this", ch_readraw(handle))
600 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200601 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100602 call delete('Xinput')
603 endtry
604endfunc
605
Bram Moolenaare98d1212016-03-08 15:37:41 +0100606func Test_nl_write_out_file()
607 if !has('job')
608 return
609 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100610 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200611 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100612 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200613 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100614 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200615 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100616 call ch_sendraw(handle, "echo line one\n")
617 call ch_sendraw(handle, "echo line two\n")
618 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200619 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100620 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
621 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200622 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100623 call delete('Xoutput')
624 endtry
625endfunc
626
627func Test_nl_write_err_file()
628 if !has('job')
629 return
630 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200632 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100633 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200634 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200636 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100637 call ch_sendraw(handle, "echoerr line one\n")
638 call ch_sendraw(handle, "echoerr line two\n")
639 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200640 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100641 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
642 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200643 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100644 call delete('Xoutput')
645 endtry
646endfunc
647
648func Test_nl_write_both_file()
649 if !has('job')
650 return
651 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100652 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100654 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200655 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200657 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100658 call ch_sendraw(handle, "echoerr line one\n")
659 call ch_sendraw(handle, "echo line two\n")
660 call ch_sendraw(handle, "double this\n")
661 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200662 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100663 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
664 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200665 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100666 call delete('Xoutput')
667 endtry
668endfunc
669
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200670func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200671 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200672endfunc
673
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200674func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100675 if !has('job')
676 return
677 endif
678 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200679 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200680 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200681 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100682 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100683 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200684 if a:do_msg
685 let expected[0] = 'Reading from channel output...'
686 else
687 let options['out_msg'] = 0
688 call remove(expected, 0)
689 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100690 else
691 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100692 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100693 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200694 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200696 if a:nomod
697 let options['out_modifiable'] = 0
698 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100699 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100700 call assert_equal("run", job_status(job))
701 try
702 let handle = job_getchannel(job)
703 call ch_sendraw(handle, "echo line one\n")
704 call ch_sendraw(handle, "echo line two\n")
705 call ch_sendraw(handle, "double this\n")
706 call ch_sendraw(handle, "quit\n")
707 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100708 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200709 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200710 if a:nomod
711 call assert_equal(0, &modifiable)
712 else
713 call assert_equal(1, &modifiable)
714 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200715 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100716 bwipe!
717 finally
718 call job_stop(job)
719 endtry
720endfunc
721
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100722func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200723 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100724endfunc
725
726func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200727 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100728endfunc
729
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200730func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200731 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200732endfunc
733
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200734func Test_pipe_to_buffer_name_nomsg()
735 call Run_test_pipe_to_buffer(1, 0, 1)
736endfunc
737
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200738func Test_close_output_buffer()
739 if !has('job')
740 return
741 endif
742 enew!
743 let test_lines = ['one', 'two']
744 call setline(1, test_lines)
745 call ch_log('Test_close_output_buffer()')
746 let options = {'out_io': 'buffer'}
747 let options['out_name'] = 'buffer-output'
748 let options['out_msg'] = 0
749 split buffer-output
750 let job = job_start(s:python . " test_channel_write.py", options)
751 call assert_equal("run", job_status(job))
752 try
753 call WaitFor('line("$") == 3')
754 call assert_equal(3, line('$'))
755 quit!
756 sleep 100m
757 " Make sure the write didn't happen to the wrong buffer.
758 call assert_equal(test_lines, getline(1, line('$')))
759 call assert_equal(-1, bufwinnr('buffer-output'))
760 sbuf buffer-output
761 call assert_notequal(-1, bufwinnr('buffer-output'))
762 sleep 100m
763 close " no more writes
764 bwipe!
765 finally
766 call job_stop(job)
767 endtry
768endfunc
769
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200770func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100771 if !has('job')
772 return
773 endif
774 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100775 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200776 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100777 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100778 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200779 if a:do_msg
780 let expected[0] = 'Reading from channel error...'
781 else
782 let options['err_msg'] = 0
783 call remove(expected, 0)
784 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100785 else
786 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100787 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100788 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200789 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100790 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200791 if a:nomod
792 let options['err_modifiable'] = 0
793 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100794 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100795 call assert_equal("run", job_status(job))
796 try
797 let handle = job_getchannel(job)
798 call ch_sendraw(handle, "echoerr line one\n")
799 call ch_sendraw(handle, "echoerr line two\n")
800 call ch_sendraw(handle, "doubleerr this\n")
801 call ch_sendraw(handle, "quit\n")
802 sp pipe-err
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100803 call WaitFor('line("$") == ' . len(expected))
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200804 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200805 if a:nomod
806 call assert_equal(0, &modifiable)
807 else
808 call assert_equal(1, &modifiable)
809 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100810 bwipe!
811 finally
812 call job_stop(job)
813 endtry
814endfunc
815
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100816func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200817 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100818endfunc
819
820func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200821 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200822endfunc
823
824func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200825 call Run_test_pipe_err_to_buffer(1, 1, 1)
826endfunc
827
828func Test_pipe_err_to_buffer_name_nomsg()
829 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100830endfunc
831
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100832func Test_pipe_both_to_buffer()
833 if !has('job')
834 return
835 endif
836 call ch_log('Test_pipe_both_to_buffer()')
837 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100838 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100839 call assert_equal("run", job_status(job))
840 try
841 let handle = job_getchannel(job)
842 call ch_sendraw(handle, "echo line one\n")
843 call ch_sendraw(handle, "echoerr line two\n")
844 call ch_sendraw(handle, "double this\n")
845 call ch_sendraw(handle, "doubleerr that\n")
846 call ch_sendraw(handle, "quit\n")
847 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200848 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100849 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
850 bwipe!
851 finally
852 call job_stop(job)
853 endtry
854endfunc
855
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100856func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100857 if !has('job')
858 return
859 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100860 call ch_log('Test_pipe_from_buffer()')
861
862 sp pipe-input
863 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200864 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100865 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100866 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100867 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100868 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100869 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100870
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100871 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100872 call assert_equal("run", job_status(job))
873 try
874 let handle = job_getchannel(job)
875 call assert_equal('one', ch_read(handle))
876 call assert_equal('two', ch_read(handle))
877 call assert_equal('three', ch_read(handle))
878 bwipe!
879 finally
880 call job_stop(job)
881 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100882endfunc
883
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100884func Test_pipe_from_buffer_name()
885 call Run_test_pipe_from_buffer(1)
886endfunc
887
888func Test_pipe_from_buffer_nr()
889 call Run_test_pipe_from_buffer(0)
890endfunc
891
Bram Moolenaar0874a832016-09-01 15:11:51 +0200892func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200893 if !executable('sort') || !has('job')
894 return
895 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200896 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
897 if a:use_buffer
898 split sortin
899 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
900 let options.in_io = 'buffer'
901 let options.in_name = 'sortin'
902 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200903 if !a:all
904 let options.in_top = 2
905 let options.in_bot = 4
906 endif
907 let g:job = job_start('sort', options)
908 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909
910 if !a:use_buffer
911 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
912 call ch_close_in(g:job)
913 endif
914
Bram Moolenaard8b55492016-09-01 14:35:22 +0200915 call WaitFor('job_status(g:job) == "dead"')
916 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200917
Bram Moolenaard8b55492016-09-01 14:35:22 +0200918 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200919 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200920 call assert_equal('Reading from channel output...', getline(1))
921 if a:all
922 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
923 else
924 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
925 endif
926
927 call job_stop(g:job)
928 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200929 if a:use_buffer
930 bwipe! sortin
931 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200932 bwipe! sortout
933endfunc
934
935func Test_pipe_through_sort_all()
936 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200937 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200938endfunc
939
940func Test_pipe_through_sort_some()
941 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200942 call Run_pipe_through_sort(0, 1)
943endfunc
944
945func Test_pipe_through_sort_feed()
946 call ch_log('Test_pipe_through_sort_feed()')
947 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200948endfunc
949
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100950func Test_pipe_to_nameless_buffer()
951 if !has('job')
952 return
953 endif
954 call ch_log('Test_pipe_to_nameless_buffer()')
955 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100956 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100957 call assert_equal("run", job_status(job))
958 try
959 let handle = job_getchannel(job)
960 call ch_sendraw(handle, "echo line one\n")
961 call ch_sendraw(handle, "echo line two\n")
962 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200963 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100964 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
965 bwipe!
966 finally
967 call job_stop(job)
968 endtry
969endfunc
970
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100971func Test_pipe_to_buffer_json()
972 if !has('job')
973 return
974 endif
975 call ch_log('Test_pipe_to_buffer_json()')
976 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100977 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100978 call assert_equal("run", job_status(job))
979 try
980 let handle = job_getchannel(job)
981 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
982 call ch_sendraw(handle, "echo [-2, 12.34]\n")
983 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200984 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100985 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
986 bwipe!
987 finally
988 call job_stop(job)
989 endtry
990endfunc
991
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100992" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100993func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100994 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100995 if getline(line('$') - a:offset) == a:line
996 break
997 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100998 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100999 endfor
1000endfunc
1001
1002func Test_pipe_io_two_buffers()
1003 if !has('job')
1004 return
1005 endif
1006 call ch_log('Test_pipe_io_two_buffers()')
1007
1008 " Create two buffers, one to read from and one to write to.
1009 split pipe-output
1010 set buftype=nofile
1011 split pipe-input
1012 set buftype=nofile
1013
1014 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001015 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001016 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1017 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001018 call assert_equal("run", job_status(job))
1019 try
1020 exe "normal Gaecho hello\<CR>"
1021 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001022 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001023 call assert_equal('hello', getline('$'))
1024
1025 exe bufwinnr('pipe-input') . "wincmd w"
1026 exe "normal Gadouble this\<CR>"
1027 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001028 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001029 call assert_equal('this', getline(line('$') - 1))
1030 call assert_equal('AND this', getline('$'))
1031
1032 bwipe!
1033 exe bufwinnr('pipe-input') . "wincmd w"
1034 bwipe!
1035 finally
1036 call job_stop(job)
1037 endtry
1038endfunc
1039
1040func Test_pipe_io_one_buffer()
1041 if !has('job')
1042 return
1043 endif
1044 call ch_log('Test_pipe_io_one_buffer()')
1045
1046 " Create one buffer to read from and to write to.
1047 split pipe-io
1048 set buftype=nofile
1049
1050 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001051 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001052 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1053 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001054 call assert_equal("run", job_status(job))
1055 try
1056 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001057 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001058 call assert_equal('hello', getline(line('$') - 1))
1059
1060 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001061 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001062 call assert_equal('this', getline(line('$') - 2))
1063 call assert_equal('AND this', getline(line('$') - 1))
1064
1065 bwipe!
1066 finally
1067 call job_stop(job)
1068 endtry
1069endfunc
1070
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001071func Test_pipe_null()
1072 if !has('job')
1073 return
1074 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001075 call ch_log('Test_pipe_null()')
1076
1077 " We cannot check that no I/O works, we only check that the job starts
1078 " properly.
1079 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001080 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001081 call assert_equal("run", job_status(job))
1082 try
1083 call assert_equal('something', ch_read(job))
1084 finally
1085 call job_stop(job)
1086 endtry
1087
1088 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001089 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001090 call assert_equal("run", job_status(job))
1091 try
1092 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1093 finally
1094 call job_stop(job)
1095 endtry
1096
1097 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001098 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001099 call assert_equal("run", job_status(job))
1100 try
1101 call assert_equal('something', ch_read(job))
1102 finally
1103 call job_stop(job)
1104 endtry
1105
1106 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001107 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001108 call assert_equal("run", job_status(job))
1109 call job_stop(job)
1110
1111 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001112 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001113 call assert_equal("run", job_status(job))
1114 call assert_equal('channel fail', string(job_getchannel(job)))
1115 call assert_equal('fail', ch_status(job))
1116 call job_stop(job)
1117endfunc
1118
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001119func Test_pipe_to_buffer_raw()
1120 if !has('job')
1121 return
1122 endif
1123 call ch_log('Test_raw_pipe_to_buffer()')
1124 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1125 split testout
1126 let job = job_start([s:python, '-c',
1127 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1128 call assert_equal("run", job_status(job))
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001129 call WaitFor('len(join(getline(1, "$"), "")) >= 10000', 3000)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001130 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001131 let totlen = 0
1132 for line in getline(1, '$')
1133 call assert_equal('', substitute(line, '^\.*', '', ''))
1134 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001135 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001136 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001137 finally
1138 call job_stop(job)
1139 bwipe!
1140 endtry
1141endfunc
1142
Bram Moolenaarde279892016-03-11 22:19:44 +01001143func Test_reuse_channel()
1144 if !has('job')
1145 return
1146 endif
1147 call ch_log('Test_reuse_channel()')
1148
1149 let job = job_start(s:python . " test_channel_pipe.py")
1150 call assert_equal("run", job_status(job))
1151 let handle = job_getchannel(job)
1152 try
1153 call ch_sendraw(handle, "echo something\n")
1154 call assert_equal("something", ch_readraw(handle))
1155 finally
1156 call job_stop(job)
1157 endtry
1158
1159 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1160 call assert_equal("run", job_status(job))
1161 let handle = job_getchannel(job)
1162 try
1163 call ch_sendraw(handle, "echo again\n")
1164 call assert_equal("again", ch_readraw(handle))
1165 finally
1166 call job_stop(job)
1167 endtry
1168endfunc
1169
Bram Moolenaar75f72652016-03-20 22:16:56 +01001170func Test_out_cb()
1171 if !has('job')
1172 return
1173 endif
1174 call ch_log('Test_out_cb()')
1175
1176 let dict = {'thisis': 'dict: '}
1177 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001178 if type(a:msg) == v:t_string
1179 let g:Ch_outmsg = self.thisis . a:msg
1180 else
1181 let g:Ch_outobj = a:msg
1182 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001183 endfunc
1184 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001185 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001186 endfunc
1187 let job = job_start(s:python . " test_channel_pipe.py",
1188 \ {'out_cb': dict.outHandler,
1189 \ 'out_mode': 'json',
1190 \ 'err_cb': dict.errHandler,
1191 \ 'err_mode': 'json'})
1192 call assert_equal("run", job_status(job))
1193 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001194 let g:Ch_outmsg = ''
1195 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001196 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1197 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001198 call WaitFor('g:Ch_outmsg != ""')
1199 call assert_equal("dict: hello", g:Ch_outmsg)
1200 call WaitFor('g:Ch_errmsg != ""')
1201 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001202
1203 " Receive a json object split in pieces
1204 unlet! g:Ch_outobj
1205 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
1206 call WaitFor('exists("g:Ch_outobj")')
1207 call assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001208 finally
1209 call job_stop(job)
1210 endtry
1211endfunc
1212
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001213func Test_out_close_cb()
1214 if !has('job')
1215 return
1216 endif
1217 call ch_log('Test_out_close_cb()')
1218
1219 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001220 let g:Ch_msg1 = ''
1221 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001222 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001223 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001224 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001225 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001226 let s:counter += 1
1227 endfunc
1228 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001229 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001230 let s:counter += 1
1231 endfunc
1232 let job = job_start(s:python . " test_channel_pipe.py quit now",
1233 \ {'out_cb': 'OutHandler',
1234 \ 'close_cb': 'CloseHandler'})
1235 call assert_equal("run", job_status(job))
1236 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001237 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1238 call assert_equal('quit', g:Ch_msg1)
1239 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001240 finally
1241 call job_stop(job)
1242 delfunc OutHandler
1243 delfunc CloseHandler
1244 endtry
1245endfunc
1246
Bram Moolenaar437905c2016-04-26 19:01:05 +02001247func Test_read_in_close_cb()
1248 if !has('job')
1249 return
1250 endif
1251 call ch_log('Test_read_in_close_cb()')
1252
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001253 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001254 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001255 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001256 endfunc
1257 let job = job_start(s:python . " test_channel_pipe.py quit now",
1258 \ {'close_cb': 'CloseHandler'})
1259 call assert_equal("run", job_status(job))
1260 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001261 call WaitFor('g:Ch_received != ""')
1262 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001263 finally
1264 call job_stop(job)
1265 delfunc CloseHandler
1266 endtry
1267endfunc
1268
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001269func Test_out_cb_lambda()
1270 if !has('job')
1271 return
1272 endif
1273 call ch_log('Test_out_cb_lambda()')
1274
1275 let job = job_start(s:python . " test_channel_pipe.py",
1276 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1277 \ 'out_mode': 'json',
1278 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1279 \ 'err_mode': 'json'})
1280 call assert_equal("run", job_status(job))
1281 try
1282 let g:Ch_outmsg = ''
1283 let g:Ch_errmsg = ''
1284 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1285 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1286 call WaitFor('g:Ch_outmsg != ""')
1287 call assert_equal("lambda: hello", g:Ch_outmsg)
1288 call WaitFor('g:Ch_errmsg != ""')
1289 call assert_equal("lambda: there", g:Ch_errmsg)
1290 finally
1291 call job_stop(job)
1292 endtry
1293endfunc
1294
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001295func Test_close_and_exit_cb()
1296 if !has('job')
1297 return
1298 endif
1299 call ch_log('Test_close_and_exit_cb')
1300
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001301 let g:retdict = {'ret': {}}
1302 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001303 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1304 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001305 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001306 let self.ret['exit_cb'] = job_status(a:job)
1307 endfunc
1308
1309 let g:job = job_start('echo', {
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001310 \ 'close_cb': g:retdict.close_cb,
1311 \ 'exit_cb': g:retdict.exit_cb,
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001312 \ })
1313 call assert_equal('run', job_status(g:job))
1314 unlet g:job
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001315 call WaitFor('len(g:retdict.ret) >= 2')
1316 call assert_equal(2, len(g:retdict.ret))
1317 call assert_match('^\%(dead\|run\)', g:retdict.ret['close_cb'])
1318 call assert_equal('dead', g:retdict.ret['exit_cb'])
1319 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001320endfunc
1321
Bram Moolenaard46ae142016-02-16 13:33:52 +01001322""""""""""
1323
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001324let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001325func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001326 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001327 unlet s:channelfd
1328endfunc
1329
1330" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001331func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001332 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001333 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001334 call WaitFor('"what?" == g:Ch_unletResponse')
1335 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001336endfunc
1337
1338func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001339 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001341endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001342
Bram Moolenaard46ae142016-02-16 13:33:52 +01001343""""""""""
1344
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001345let g:Ch_unletResponse = ''
1346func Ch_CloseHandler(handle, msg)
1347 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001348 call ch_close(s:channelfd)
1349endfunc
1350
1351" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001352func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001353 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001354 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1355 call WaitFor('"what?" == g:Ch_unletResponse')
1356 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001357endfunc
1358
1359func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001360 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001361 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001362endfunc
1363
1364""""""""""
1365
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001366func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001367 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001368 silent! let ch = ch_open("noserver")
1369 echo ch
1370 let d = ch
1371endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001372
1373""""""""""
1374
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001375func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001376 " Wait up to a second for the port to open.
1377 let s:chopt.waittime = 1000
1378 let channel = ch_open('localhost:' . a:port, s:chopt)
1379 unlet s:chopt.waittime
1380 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001381 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001382 return
1383 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001384 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001385 call ch_close(channel)
1386endfunc
1387
1388func Test_open_delay()
1389 call ch_log('Test_open_delay()')
1390 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001391 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001392endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001393
1394"""""""""
1395
1396function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001397 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001398endfunc
1399
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001400function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001401 let handle = ch_open('localhost:' . a:port, s:chopt)
1402 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001403 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001404 return
1405 endif
1406
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001407 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001408 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001409 call WaitFor('len(g:Ch_call_ret) > 0')
1410 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001411endfunc
1412
1413func Test_call()
1414 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001415 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001416endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001417
1418"""""""""
1419
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001420let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001421function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001422 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001423endfunc
1424
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001425function Ch_test_exit_callback(port)
1426 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1427 let g:Ch_exit_job = g:currentJob
1428 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001429endfunc
1430
1431func Test_exit_callback()
1432 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001433 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001434 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001435
Bram Moolenaar9730f742016-02-28 19:50:51 +01001436 " wait up to a second for the job to exit
1437 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001438 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001439 break
1440 endif
1441 sleep 10m
1442 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001443 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001444 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001445
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001446 call assert_equal('done', g:Ch_job_exit_ret)
1447 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1448 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001449 endif
1450endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001451
Bram Moolenaar97792de2016-10-15 18:36:49 +02001452function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001453 if job_info(a:job).process == g:exit_cb_val.process
1454 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1455 endif
1456 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001457endfunction
1458
1459func Test_exit_callback_interval()
1460 if !has('job')
1461 return
1462 endif
1463
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001464 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001465 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001466 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaarab8b1c12017-11-04 19:24:31 +01001467 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0', 2000)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001468 let elapsed = reltimefloat(g:exit_cb_val.end)
1469 call assert_true(elapsed > 0.5)
1470 call assert_true(elapsed < 1.0)
1471
1472 " case: unreferenced job, using timer
1473 if !has('timers')
1474 return
1475 endif
1476
1477 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1478 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1479 let g:exit_cb_val.process = job_info(g:job).process
1480 unlet g:job
1481 call Standby(1000)
1482 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1483 let elapsed = reltimefloat(g:exit_cb_val.end)
1484 else
1485 let elapsed = 1.0
1486 endif
1487 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001488 call assert_true(elapsed < 1.0)
1489endfunc
1490
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001491"""""""""
1492
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001493let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001494function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001495 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001496endfunc
1497
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001498function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001499 let handle = ch_open('localhost:' . a:port, s:chopt)
1500 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001501 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001502 return
1503 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001504 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001505
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001506 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001507 call WaitFor('"closed" == g:Ch_close_ret')
1508 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001509endfunc
1510
1511func Test_close_callback()
1512 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001513 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001514endfunc
1515
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001516function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001517 let handle = ch_open('localhost:' . a:port, s:chopt)
1518 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001519 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001520 return
1521 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001522 let g:Ch_d = {}
1523 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001524 let self.close_ret = 'closed'
1525 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001526 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001527
1528 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001529 call WaitFor('"closed" == g:Ch_d.close_ret')
1530 call assert_equal('closed', g:Ch_d.close_ret)
1531 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001532endfunc
1533
1534func Test_close_partial()
1535 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001536 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001537endfunc
1538
Bram Moolenaar80385682016-03-27 19:13:35 +02001539func Test_job_start_invalid()
1540 call assert_fails('call job_start($x)', 'E474:')
1541 call assert_fails('call job_start("")', 'E474:')
1542endfunc
1543
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001544func Test_job_stop_immediately()
1545 if !has('job')
1546 return
1547 endif
1548
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001549 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001550 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001551 call job_stop(g:job)
1552 call WaitFor('"dead" == job_status(g:job)')
1553 call assert_equal('dead', job_status(g:job))
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001554 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001555 call job_stop(g:job, 'kill')
1556 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001557 endtry
1558endfunc
1559
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001560" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001561func Test_partial_in_channel_cycle()
1562 let d = {}
1563 let d.a = function('string', [d])
1564 try
1565 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1566 catch
1567 call assert_exception('E901:')
1568 endtry
1569 unlet d
1570endfunc
1571
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001572func Test_using_freed_memory()
1573 let g:a = job_start(['ls'])
1574 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001575 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001576endfunc
1577
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001578func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001579 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001580 return
1581 endif
1582 sp test_channel.vim
1583 let g:linecount = line('$')
1584 close
1585 split testout
1586 1,$delete
1587 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001588 call WaitFor('line("$") >= g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001589 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001590 bwipe!
1591endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001592
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001593func Test_cmd_parsing()
1594 if !has('unix')
1595 return
1596 endif
1597 call assert_false(filereadable("file with space"))
1598 let job = job_start('touch "file with space"')
1599 call WaitFor('filereadable("file with space")')
1600 call assert_true(filereadable("file with space"))
1601 call delete("file with space")
1602
1603 let job = job_start('touch file\ with\ space')
1604 call WaitFor('filereadable("file with space")')
1605 call assert_true(filereadable("file with space"))
1606 call delete("file with space")
1607endfunc
1608
Bram Moolenaar82117982016-08-27 19:21:48 +02001609func Test_raw_passes_nul()
1610 if !executable('cat') || !has('job')
1611 return
1612 endif
1613
1614 " Test lines from the job containing NUL are stored correctly in a buffer.
1615 new
1616 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1617 w! Xtestread
1618 bwipe!
1619 split testout
1620 1,$delete
1621 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1622 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001623 call assert_equal("asdf\nasdf", getline(1))
1624 call assert_equal("xxx\n", getline(2))
1625 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001626
1627 call delete('Xtestread')
1628 bwipe!
1629
1630 " Test lines from a buffer with NUL bytes are written correctly to the job.
1631 new mybuffer
1632 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1633 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1634 call WaitFor('"dead" == job_status(g:Ch_job)')
1635 bwipe!
1636 split Xtestwrite
1637 call assert_equal("asdf\nasdf", getline(1))
1638 call assert_equal("xxx\n", getline(2))
1639 call assert_equal("\nyyy", getline(3))
1640
1641 call delete('Xtestwrite')
1642 bwipe!
1643endfunc
1644
Bram Moolenaarec68a992016-10-03 21:37:41 +02001645func MyLineCountCb(ch, msg)
1646 let g:linecount += 1
1647endfunc
1648
1649func Test_read_nonl_line()
1650 if !has('job')
1651 return
1652 endif
1653
1654 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001655 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001656 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1657 call WaitFor('3 <= g:linecount')
1658 call assert_equal(3, g:linecount)
1659endfunc
1660
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001661func Test_read_from_terminated_job()
1662 if !has('job')
1663 return
1664 endif
1665
1666 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001667 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001668 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1669 call WaitFor('1 <= g:linecount')
1670 call assert_equal(1, g:linecount)
1671endfunc
1672
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001673func Test_env()
1674 if !has('job')
1675 return
1676 endif
1677
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001678 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001679 if has('win32')
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001680 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 +02001681 else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001682 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 +02001683 endif
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001684 call WaitFor('"" != g:envstr')
1685 call assert_equal("bar", g:envstr)
1686 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001687endfunc
1688
1689func Test_cwd()
1690 if !has('job')
1691 return
1692 endif
1693
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001694 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001695 if has('win32')
1696 let expect = $TEMP
Bram Moolenaar24820692017-12-02 16:38:12 +01001697 let job = job_start(['cmd', '/c', 'echo %CD%'], {'callback': {ch,msg->execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001698 else
1699 let expect = $HOME
Bram Moolenaar24820692017-12-02 16:38:12 +01001700 let job = job_start(['pwd'], {'callback': {ch,msg->execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001701 endif
Bram Moolenaar24820692017-12-02 16:38:12 +01001702 try
1703 call WaitFor('"" != g:envstr')
1704 let expect = substitute(expect, '[/\\]$', '', '')
1705 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1706 if $CI != '' && stridx(g:envstr, '/private/') == 0
1707 let g:envstr = g:envstr[8:]
1708 endif
1709 call assert_equal(expect, g:envstr)
1710 finally
1711 call job_stop(job)
1712 unlet g:envstr
1713 endtry
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 = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001738 let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001739 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
Bram Moolenaar24820692017-12-02 16:38:12 +01001748 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001749 unlet g:out
1750 endtry
1751endfunc
1752
1753func Test_list_args()
1754 if !has('job')
1755 return
1756 endif
1757
1758 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 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\nworld'')', "hello\nworld", 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("hello\"world\"")', 'hello"world"', 0)
1767 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1768 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1769 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1770 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1771
1772 " tests which not contain spaces in the argument
1773 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1774 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 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\"world\"")', 'hello"world"', 1)
1781 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1782endfunc