blob: f5ee7c086c658730ea1431567dc81565025c752c [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 Moolenaar819524702018-02-27 19:10:00 +010026" Return a list of open files.
27" Can be used to make sure no resources leaked.
28" Returns an empty list on systems where this is not supported.
29func s:get_resources()
30 let pid = getpid()
31
32 if has('mac')
33 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
34 elseif isdirectory('/proc/' . pid . '/fd/')
35 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
36 else
37 return []
38 endif
39endfunc
40
Bram Moolenaar321efdd2016-07-15 17:09:11 +020041let g:Ch_responseMsg = ''
42func Ch_requestHandler(handle, msg)
43 let g:Ch_responseHandle = a:handle
44 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010045endfunc
46
Bram Moolenaar321efdd2016-07-15 17:09:11 +020047func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010048 " Avoid dropping messages, since we don't use a callback here.
49 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020050 " Also add the noblock flag to try it out.
51 let s:chopt.noblock = 1
Bram Moolenaard6a8d482016-02-10 20:32:20 +010052 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010053 unlet s:chopt.drop
Bram Moolenaar0b146882018-09-06 16:27:24 +020054 unlet s:chopt.noblock
Bram Moolenaar77073442016-02-13 23:23:53 +010055 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010056 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010057 return
58 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010059 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010060 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010061 call assert_equal('no process', string(ch_getjob(handle)))
62 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010063 let dict = ch_info(handle)
64 call assert_true(dict.id != 0)
65 call assert_equal('open', dict.status)
66 call assert_equal(a:port, string(dict.port))
67 call assert_equal('open', dict.sock_status)
68 call assert_equal('socket', dict.sock_io)
69
Bram Moolenaard7ece102016-02-02 23:23:02 +010070 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010071 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010072
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010073 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010074 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
75 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
76 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
77
78 " split command should work
79 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020080 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010081 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010082
Bram Moolenaarf1f07922016-08-26 17:58:53 +020083 " string with ][ should work
84 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
85
Bram Moolenaar4b785f62016-11-29 21:54:44 +010086 " nothing to read now
87 call assert_equal(0, ch_canread(handle))
88
Bram Moolenaarf1f07922016-08-26 17:58:53 +020089 " sending three messages quickly then reading should work
90 for i in range(3)
91 call ch_sendexpr(handle, 'echo hello ' . i)
92 endfor
93 call assert_equal('hello 0', ch_read(handle)[1])
94 call assert_equal('hello 1', ch_read(handle)[1])
95 call assert_equal('hello 2', ch_read(handle)[1])
96
Bram Moolenaard7ece102016-02-02 23:23:02 +010097 " Request that triggers sending two ex commands. These will usually be
98 " handled before getting the response, but it's not guaranteed, thus wait a
99 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100100 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200101 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100102 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100103
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100104 " Request command "foo bar", which fails silently.
105 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200106 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100107
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100108 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200109 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100110
Bram Moolenaara07fec92016-02-05 21:04:08 +0100111 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200112 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
113 call WaitFor('exists("g:Ch_responseHandle")')
114 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100115 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100116 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200117 call assert_equal(handle, g:Ch_responseHandle)
118 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100119 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200120 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100121
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200122 let g:Ch_responseMsg = ''
123 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
124 call WaitFor('exists("g:Ch_responseHandle")')
125 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100126 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100127 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200128 call assert_equal(handle, g:Ch_responseHandle)
129 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100130 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200131 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100132
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200133 " Using lambda.
134 let g:Ch_responseMsg = ''
135 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
136 call WaitFor('exists("g:Ch_responseHandle")')
137 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100138 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200139 else
140 call assert_equal(handle, g:Ch_responseHandle)
141 unlet g:Ch_responseHandle
142 endif
143 call assert_equal('got it', g:Ch_responseMsg)
144
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100145 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200146 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100147
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100148 " check setting options (without testing the effect)
149 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100150 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100151 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100152 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100153 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100154 call ch_setoptions(handle, {'drop': 'never'})
155 call ch_setoptions(handle, {'drop': 'auto'})
156 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100157
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100158 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100159 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100160 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100162
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100163 " Send an eval request with special characters.
164 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
165 sleep 10m
166 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
167
168 " Send an eval request to get a line with special characters.
169 call setline(3, "a\nb\<CR>c\x01d\x7fe")
170 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
171 sleep 10m
172 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
173
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100174 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100176 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100177 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100178
Bram Moolenaar55fab432016-02-07 16:53:13 +0100179 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100181 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100182 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100183
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100184 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100186 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100187 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100188
Bram Moolenaarf4160862016-02-05 23:09:12 +0100189 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200191 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100192 call assert_equal('one', getline(line('$') - 2))
193 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194
195 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
197 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100198
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100199 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100200
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100201 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100202 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
203 let start = reltime()
204 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
205 let elapsed = reltime(start)
206 call assert_true(reltimefloat(elapsed) > 0.3)
207 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100208
209 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100210 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100211 let resp = ch_read(handle)
212 call assert_equal(type([]), type(resp))
213 call assert_equal(type(11), type(resp[0]))
214 call assert_equal('waited', resp[1])
215
Bram Moolenaard7ece102016-02-02 23:23:02 +0100216 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100217 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100218endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100219
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100221 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200222 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100223endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100224
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100225" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200226func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100227 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200228 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100229 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100230 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100231 return
232 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100233
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100234 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100235
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100237 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100238 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239 return
240 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100241 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
242 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100243
244 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100245 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100246
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100247 call ch_close(newhandle)
248endfunc
249
250func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100251 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200252 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100253endfunc
254
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100255" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200256func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100257 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100259 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100260 return
261 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100262
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100263 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100264
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100265 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100266endfunc
267
268func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100269 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200270 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100271endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100272
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100273"""""""""
274
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200275func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100276 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200277 unlet g:Ch_reply
278 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100279endfunc
280
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200281func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100282 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100283 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100284 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100285 return
286 endif
287
288 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100289 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200290 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100291
292 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100293 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200294 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100295endfunc
296
297func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100298 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200299 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200300 let s:chopt.callback = 'Ch_handler'
301 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200302 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200303 let s:chopt.callback = function('Ch_handler')
304 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100305 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100306endfunc
307
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100308"""""""""
309
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310let g:Ch_reply = ''
311func Ch_zeroHandler(chan, msg)
312 unlet g:Ch_reply
313 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100314endfunc
315
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200316let g:Ch_zero_reply = ''
317func Ch_oneHandler(chan, msg)
318 unlet g:Ch_zero_reply
319 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320endfunc
321
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 let handle = ch_open('localhost:' . a:port, s:chopt)
324 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100325 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 return
327 endif
328
329 " Check that eval works.
330 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
331
332 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100334 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200336 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100338 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 endif
341
342 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200343 let g:Ch_reply = ''
344 let g:Ch_zero_reply = ''
345 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200346 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100349 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200350 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100351 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100352endfunc
353
354func Test_zero_reply()
355 call ch_log('Test_zero_reply()')
356 " Run with channel handler
357 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200358 let s:chopt.callback = 'Ch_zeroHandler'
359 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100360 unlet s:chopt.callback
361
362 " Run without channel handler
363 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200364 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100365endfunc
366
367"""""""""
368
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200369let g:Ch_reply1 = ""
370func Ch_handleRaw1(chan, msg)
371 unlet g:Ch_reply1
372 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100373endfunc
374
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200375let g:Ch_reply2 = ""
376func Ch_handleRaw2(chan, msg)
377 unlet g:Ch_reply2
378 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100379endfunc
380
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200381let g:Ch_reply3 = ""
382func Ch_handleRaw3(chan, msg)
383 unlet g:Ch_reply3
384 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100385endfunc
386
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200387func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100388 let handle = ch_open('localhost:' . a:port, s:chopt)
389 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100390 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100391 return
392 endif
393 call ch_setoptions(handle, {'mode': 'raw'})
394
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100395 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200396 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200397 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200398 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
399 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200400 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100401 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200402 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100403endfunc
404
405func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100406 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200407 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100408endfunc
409
410"""""""""
411
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100412" Test that trying to connect to a non-existing port fails quickly.
413func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100414 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100415 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100416 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100417 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100418 " Oops, port does exists.
419 call ch_close(handle)
420 else
421 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100422 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100423 endif
424
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100425 " We intend to use a socket that doesn't exist and wait for half a second
426 " before giving up. If the socket does exist it can fail in various ways.
427 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100429 try
430 let handle = ch_open('localhost:9867', {'waittime': 500})
431 if ch_status(handle) != "fail"
432 " Oops, port does exists.
433 call ch_close(handle)
434 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100435 " Failed connection should wait about 500 msec. Can be longer if the
436 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 let elapsed = reltime(start)
438 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100439 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100440 endif
441 catch
442 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100443 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100444 endif
445 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100446endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100447
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100448"""""""""
449
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100450func Test_raw_pipe()
451 if !has('job')
452 return
453 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100454 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100455 " Add a dummy close callback to avoid that messages are dropped when calling
456 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200457 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100458 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200459 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200460 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100461 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200462
463 call assert_equal("open", ch_status(job))
464 call assert_equal("open", ch_status(job), {"part": "out"})
465 call assert_equal("open", ch_status(job), {"part": "err"})
466 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
467 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
468
469 let dict = ch_info(job)
470 call assert_true(dict.id != 0)
471 call assert_equal('open', dict.status)
472 call assert_equal('open', dict.out_status)
473 call assert_equal('RAW', dict.out_mode)
474 call assert_equal('pipe', dict.out_io)
475 call assert_equal('open', dict.err_status)
476 call assert_equal('RAW', dict.err_mode)
477 call assert_equal('pipe', dict.err_io)
478
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100479 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100480 " For a change use the job where a channel is expected.
481 call ch_sendraw(job, "echo something\n")
482 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100483 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
484
Bram Moolenaar151f6562016-03-07 21:19:38 +0100485 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100486 let g:handle = job_getchannel(job)
487 call WaitFor('ch_canread(g:handle)')
488 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
491
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200492 let g:Ch_reply = ""
493 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200494 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200495
Bram Moolenaar151f6562016-03-07 21:19:38 +0100496 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100497 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
498 finally
499 call job_stop(job)
500 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100501
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200502 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200503 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar8950a562016-03-12 15:22:55 +0100504 let info = job_info(job)
505 call assert_equal("dead", info.status)
506 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200507 call assert_equal(2, len(info.cmd))
508 call assert_equal("test_channel_pipe.py", info.cmd[1])
509
510 let found = 0
511 for j in job_info()
512 if j == job
513 let found += 1
514 endif
515 endfor
516 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100517endfunc
518
519func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100520 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100521 return
522 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100523 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100524 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525 call assert_equal("run", job_status(job))
526 try
527 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100528 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100529 call assert_equal("something", ch_readraw(handle))
530
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100531 call ch_sendraw(handle, "echoerr wrong\n")
532 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
533
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100534 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100535 call assert_equal("this", ch_readraw(handle))
536 call assert_equal("AND this", ch_readraw(handle))
537
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200538 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar620ca2d2017-12-09 19:13:13 +0100539 call assert_equal("this linethis linethis line", ch_read(handle))
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200540
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100541 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100542 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100543 finally
544 call job_stop(job)
545 endtry
546endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100547
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100548func Test_nl_err_to_out_pipe()
549 if !has('job')
550 return
551 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100552 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100553 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100554 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100555 call assert_equal("run", job_status(job))
556 try
557 let handle = job_getchannel(job)
558 call ch_sendraw(handle, "echo something\n")
559 call assert_equal("something", ch_readraw(handle))
560
561 call ch_sendraw(handle, "echoerr wrong\n")
562 call assert_equal("wrong", ch_readraw(handle))
563 finally
564 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100565 call ch_logfile('')
566 let loglines = readfile('Xlog')
567 call assert_true(len(loglines) > 10)
568 let found_test = 0
569 let found_send = 0
570 let found_recv = 0
571 let found_stop = 0
572 for l in loglines
573 if l =~ 'Test_nl_err_to_out_pipe'
574 let found_test = 1
575 endif
576 if l =~ 'SEND on.*echo something'
577 let found_send = 1
578 endif
579 if l =~ 'RECV on.*something'
580 let found_recv = 1
581 endif
582 if l =~ 'Stopping job with'
583 let found_stop = 1
584 endif
585 endfor
586 call assert_equal(1, found_test)
587 call assert_equal(1, found_send)
588 call assert_equal(1, found_recv)
589 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200590 " On MS-Windows need to sleep for a moment to be able to delete the file.
591 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100592 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100593 endtry
594endfunc
595
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200596func Stop_g_job()
597 call job_stop(g:job)
598 if has('win32')
599 " On MS-Windows the server must close the file handle before we are able
600 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200601 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200602 sleep 10m
603 endif
604endfunc
605
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100606func Test_nl_read_file()
607 if !has('job')
608 return
609 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100610 call ch_log('Test_nl_read_file()')
611 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200612 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100613 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200614 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100615 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200616 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100617 call assert_equal("something", ch_readraw(handle))
618 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
619 call assert_equal("this", ch_readraw(handle))
620 call assert_equal("AND this", ch_readraw(handle))
621 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200622 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100623 call delete('Xinput')
624 endtry
625endfunc
626
Bram Moolenaare98d1212016-03-08 15:37:41 +0100627func Test_nl_write_out_file()
628 if !has('job')
629 return
630 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631 call ch_log('Test_nl_write_out_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 \ {'out_io': 'file', 'out_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, "echo line one\n")
638 call ch_sendraw(handle, "echo line two\n")
639 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200640 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100641 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200642 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100643 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100644 call delete('Xoutput')
645 endtry
646endfunc
647
648func Test_nl_write_err_file()
649 if !has('job')
650 return
651 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100652 call ch_log('Test_nl_write_err_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 \ {'err_io': 'file', 'err_name': 'Xoutput'})
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, "echoerr line two\n")
660 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200661 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100662 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200663 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100664 call delete('Xoutput')
665 endtry
666endfunc
667
668func Test_nl_write_both_file()
669 if !has('job')
670 return
671 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100672 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200673 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100674 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200675 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100676 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200677 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100678 call ch_sendraw(handle, "echoerr line one\n")
679 call ch_sendraw(handle, "echo line two\n")
680 call ch_sendraw(handle, "double this\n")
681 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200682 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100683 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200684 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100685 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100686 call delete('Xoutput')
687 endtry
688endfunc
689
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200690func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200691 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200692endfunc
693
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200694func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100695 if !has('job')
696 return
697 endif
698 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200699 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200700 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200701 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100702 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100703 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200704 if a:do_msg
705 let expected[0] = 'Reading from channel output...'
706 else
707 let options['out_msg'] = 0
708 call remove(expected, 0)
709 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100710 else
711 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100712 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100713 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200714 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100715 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200716 if a:nomod
717 let options['out_modifiable'] = 0
718 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100719 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100720 call assert_equal("run", job_status(job))
721 try
722 let handle = job_getchannel(job)
723 call ch_sendraw(handle, "echo line one\n")
724 call ch_sendraw(handle, "echo line two\n")
725 call ch_sendraw(handle, "double this\n")
726 call ch_sendraw(handle, "quit\n")
727 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100728 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200729 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200730 if a:nomod
731 call assert_equal(0, &modifiable)
732 else
733 call assert_equal(1, &modifiable)
734 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200735 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100736 bwipe!
737 finally
738 call job_stop(job)
739 endtry
740endfunc
741
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100742func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200743 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100744endfunc
745
746func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200747 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100748endfunc
749
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200750func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200751 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200752endfunc
753
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200754func Test_pipe_to_buffer_name_nomsg()
755 call Run_test_pipe_to_buffer(1, 0, 1)
756endfunc
757
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200758func Test_close_output_buffer()
759 if !has('job')
760 return
761 endif
762 enew!
763 let test_lines = ['one', 'two']
764 call setline(1, test_lines)
765 call ch_log('Test_close_output_buffer()')
766 let options = {'out_io': 'buffer'}
767 let options['out_name'] = 'buffer-output'
768 let options['out_msg'] = 0
769 split buffer-output
770 let job = job_start(s:python . " test_channel_write.py", options)
771 call assert_equal("run", job_status(job))
772 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200773 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200774 quit!
775 sleep 100m
776 " Make sure the write didn't happen to the wrong buffer.
777 call assert_equal(test_lines, getline(1, line('$')))
778 call assert_equal(-1, bufwinnr('buffer-output'))
779 sbuf buffer-output
780 call assert_notequal(-1, bufwinnr('buffer-output'))
781 sleep 100m
782 close " no more writes
783 bwipe!
784 finally
785 call job_stop(job)
786 endtry
787endfunc
788
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200789func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100790 if !has('job')
791 return
792 endif
793 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100794 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200795 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100796 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100797 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200798 if a:do_msg
799 let expected[0] = 'Reading from channel error...'
800 else
801 let options['err_msg'] = 0
802 call remove(expected, 0)
803 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100804 else
805 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100806 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100807 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200808 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100809 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200810 if a:nomod
811 let options['err_modifiable'] = 0
812 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100813 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100814 call assert_equal("run", job_status(job))
815 try
816 let handle = job_getchannel(job)
817 call ch_sendraw(handle, "echoerr line one\n")
818 call ch_sendraw(handle, "echoerr line two\n")
819 call ch_sendraw(handle, "doubleerr this\n")
820 call ch_sendraw(handle, "quit\n")
821 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200822 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200823 if a:nomod
824 call assert_equal(0, &modifiable)
825 else
826 call assert_equal(1, &modifiable)
827 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100828 bwipe!
829 finally
830 call job_stop(job)
831 endtry
832endfunc
833
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200835 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100836endfunc
837
838func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200839 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200840endfunc
841
842func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200843 call Run_test_pipe_err_to_buffer(1, 1, 1)
844endfunc
845
846func Test_pipe_err_to_buffer_name_nomsg()
847 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100848endfunc
849
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100850func Test_pipe_both_to_buffer()
851 if !has('job')
852 return
853 endif
854 call ch_log('Test_pipe_both_to_buffer()')
855 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100856 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100857 call assert_equal("run", job_status(job))
858 try
859 let handle = job_getchannel(job)
860 call ch_sendraw(handle, "echo line one\n")
861 call ch_sendraw(handle, "echoerr line two\n")
862 call ch_sendraw(handle, "double this\n")
863 call ch_sendraw(handle, "doubleerr that\n")
864 call ch_sendraw(handle, "quit\n")
865 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200866 call WaitForAssert({-> assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100867 bwipe!
868 finally
869 call job_stop(job)
870 endtry
871endfunc
872
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100873func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100874 if !has('job')
875 return
876 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100877 call ch_log('Test_pipe_from_buffer()')
878
879 sp pipe-input
880 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200881 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100882 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100883 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100884 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100885 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100886 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100887
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100888 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100889 call assert_equal("run", job_status(job))
890 try
891 let handle = job_getchannel(job)
892 call assert_equal('one', ch_read(handle))
893 call assert_equal('two', ch_read(handle))
894 call assert_equal('three', ch_read(handle))
895 bwipe!
896 finally
897 call job_stop(job)
898 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100899endfunc
900
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100901func Test_pipe_from_buffer_name()
902 call Run_test_pipe_from_buffer(1)
903endfunc
904
905func Test_pipe_from_buffer_nr()
906 call Run_test_pipe_from_buffer(0)
907endfunc
908
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200910 if !executable('sort') || !has('job')
911 return
912 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200913 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
914 if a:use_buffer
915 split sortin
916 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
917 let options.in_io = 'buffer'
918 let options.in_name = 'sortin'
919 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200920 if !a:all
921 let options.in_top = 2
922 let options.in_bot = 4
923 endif
924 let g:job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200925
926 if !a:use_buffer
Bram Moolenaar6c8dd392018-12-14 22:42:13 +0100927 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200928 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
929 call ch_close_in(g:job)
930 endif
931
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200932 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200933
Bram Moolenaard8b55492016-09-01 14:35:22 +0200934 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200935 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200936 call assert_equal('Reading from channel output...', getline(1))
937 if a:all
938 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
939 else
940 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
941 endif
942
943 call job_stop(g:job)
944 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200945 if a:use_buffer
946 bwipe! sortin
947 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200948 bwipe! sortout
949endfunc
950
951func Test_pipe_through_sort_all()
952 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200953 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200954endfunc
955
956func Test_pipe_through_sort_some()
957 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200958 call Run_pipe_through_sort(0, 1)
959endfunc
960
961func Test_pipe_through_sort_feed()
962 call ch_log('Test_pipe_through_sort_feed()')
963 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200964endfunc
965
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100966func Test_pipe_to_nameless_buffer()
967 if !has('job')
968 return
969 endif
970 call ch_log('Test_pipe_to_nameless_buffer()')
971 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100972 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100973 call assert_equal("run", job_status(job))
974 try
975 let handle = job_getchannel(job)
976 call ch_sendraw(handle, "echo line one\n")
977 call ch_sendraw(handle, "echo line two\n")
978 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200979 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100980 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
981 bwipe!
982 finally
983 call job_stop(job)
984 endtry
985endfunc
986
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100987func Test_pipe_to_buffer_json()
988 if !has('job')
989 return
990 endif
991 call ch_log('Test_pipe_to_buffer_json()')
992 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100993 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100994 call assert_equal("run", job_status(job))
995 try
996 let handle = job_getchannel(job)
997 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
998 call ch_sendraw(handle, "echo [-2, 12.34]\n")
999 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001000 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001001 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1002 bwipe!
1003 finally
1004 call job_stop(job)
1005 endtry
1006endfunc
1007
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001008" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001009func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001010 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001011 if getline(line('$') - a:offset) == a:line
1012 break
1013 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001014 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001015 endfor
1016endfunc
1017
1018func Test_pipe_io_two_buffers()
1019 if !has('job')
1020 return
1021 endif
1022 call ch_log('Test_pipe_io_two_buffers()')
1023
1024 " Create two buffers, one to read from and one to write to.
1025 split pipe-output
1026 set buftype=nofile
1027 split pipe-input
1028 set buftype=nofile
1029
1030 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001031 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001032 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1033 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001034 call assert_equal("run", job_status(job))
1035 try
1036 exe "normal Gaecho hello\<CR>"
1037 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001038 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001039 call assert_equal('hello', getline('$'))
1040
1041 exe bufwinnr('pipe-input') . "wincmd w"
1042 exe "normal Gadouble this\<CR>"
1043 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001044 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001045 call assert_equal('this', getline(line('$') - 1))
1046 call assert_equal('AND this', getline('$'))
1047
1048 bwipe!
1049 exe bufwinnr('pipe-input') . "wincmd w"
1050 bwipe!
1051 finally
1052 call job_stop(job)
1053 endtry
1054endfunc
1055
1056func Test_pipe_io_one_buffer()
1057 if !has('job')
1058 return
1059 endif
1060 call ch_log('Test_pipe_io_one_buffer()')
1061
1062 " Create one buffer to read from and to write to.
1063 split pipe-io
1064 set buftype=nofile
1065
1066 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001067 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001068 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1069 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001070 call assert_equal("run", job_status(job))
1071 try
1072 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001073 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001074 call assert_equal('hello', getline(line('$') - 1))
1075
1076 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001077 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001078 call assert_equal('this', getline(line('$') - 2))
1079 call assert_equal('AND this', getline(line('$') - 1))
1080
1081 bwipe!
1082 finally
1083 call job_stop(job)
1084 endtry
1085endfunc
1086
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001087func Test_pipe_null()
1088 if !has('job')
1089 return
1090 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001091 call ch_log('Test_pipe_null()')
1092
1093 " We cannot check that no I/O works, we only check that the job starts
1094 " properly.
1095 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001096 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001097 call assert_equal("run", job_status(job))
1098 try
1099 call assert_equal('something', ch_read(job))
1100 finally
1101 call job_stop(job)
1102 endtry
1103
1104 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001105 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001106 call assert_equal("run", job_status(job))
1107 try
1108 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1109 finally
1110 call job_stop(job)
1111 endtry
1112
1113 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001114 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001115 call assert_equal("run", job_status(job))
1116 try
1117 call assert_equal('something', ch_read(job))
1118 finally
1119 call job_stop(job)
1120 endtry
1121
1122 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001123 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001124 call assert_equal("run", job_status(job))
1125 call job_stop(job)
1126
1127 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001128 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001129 call assert_equal("run", job_status(job))
1130 call assert_equal('channel fail', string(job_getchannel(job)))
1131 call assert_equal('fail', ch_status(job))
1132 call job_stop(job)
1133endfunc
1134
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001135func Test_pipe_to_buffer_raw()
1136 if !has('job')
1137 return
1138 endif
1139 call ch_log('Test_raw_pipe_to_buffer()')
1140 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1141 split testout
1142 let job = job_start([s:python, '-c',
1143 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1144 call assert_equal("run", job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001145 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001146 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001147 let totlen = 0
1148 for line in getline(1, '$')
1149 call assert_equal('', substitute(line, '^\.*', '', ''))
1150 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001151 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001152 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001153 finally
1154 call job_stop(job)
1155 bwipe!
1156 endtry
1157endfunc
1158
Bram Moolenaarde279892016-03-11 22:19:44 +01001159func Test_reuse_channel()
1160 if !has('job')
1161 return
1162 endif
1163 call ch_log('Test_reuse_channel()')
1164
1165 let job = job_start(s:python . " test_channel_pipe.py")
1166 call assert_equal("run", job_status(job))
1167 let handle = job_getchannel(job)
1168 try
1169 call ch_sendraw(handle, "echo something\n")
1170 call assert_equal("something", ch_readraw(handle))
1171 finally
1172 call job_stop(job)
1173 endtry
1174
1175 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1176 call assert_equal("run", job_status(job))
1177 let handle = job_getchannel(job)
1178 try
1179 call ch_sendraw(handle, "echo again\n")
1180 call assert_equal("again", ch_readraw(handle))
1181 finally
1182 call job_stop(job)
1183 endtry
1184endfunc
1185
Bram Moolenaar75f72652016-03-20 22:16:56 +01001186func Test_out_cb()
1187 if !has('job')
1188 return
1189 endif
1190 call ch_log('Test_out_cb()')
1191
1192 let dict = {'thisis': 'dict: '}
1193 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001194 if type(a:msg) == v:t_string
1195 let g:Ch_outmsg = self.thisis . a:msg
1196 else
1197 let g:Ch_outobj = a:msg
1198 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001199 endfunc
1200 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001201 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001202 endfunc
1203 let job = job_start(s:python . " test_channel_pipe.py",
1204 \ {'out_cb': dict.outHandler,
1205 \ 'out_mode': 'json',
1206 \ 'err_cb': dict.errHandler,
1207 \ 'err_mode': 'json'})
1208 call assert_equal("run", job_status(job))
1209 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001210 let g:Ch_outmsg = ''
1211 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001212 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1213 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001214 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1215 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001216
1217 " Receive a json object split in pieces
1218 unlet! g:Ch_outobj
1219 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001220 let g:Ch_outobj = ''
1221 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001222 finally
1223 call job_stop(job)
1224 endtry
1225endfunc
1226
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001227func Test_out_close_cb()
1228 if !has('job')
1229 return
1230 endif
1231 call ch_log('Test_out_close_cb()')
1232
1233 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001234 let g:Ch_msg1 = ''
1235 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001236 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001237 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001238 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001239 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001240 let s:counter += 1
1241 endfunc
1242 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001243 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001244 let s:counter += 1
1245 endfunc
1246 let job = job_start(s:python . " test_channel_pipe.py quit now",
1247 \ {'out_cb': 'OutHandler',
1248 \ 'close_cb': 'CloseHandler'})
1249 call assert_equal("run", job_status(job))
1250 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001251 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1252 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001253 finally
1254 call job_stop(job)
1255 delfunc OutHandler
1256 delfunc CloseHandler
1257 endtry
1258endfunc
1259
Bram Moolenaar437905c2016-04-26 19:01:05 +02001260func Test_read_in_close_cb()
1261 if !has('job')
1262 return
1263 endif
1264 call ch_log('Test_read_in_close_cb()')
1265
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001266 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001267 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001268 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001269 endfunc
1270 let job = job_start(s:python . " test_channel_pipe.py quit now",
1271 \ {'close_cb': 'CloseHandler'})
1272 call assert_equal("run", job_status(job))
1273 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001274 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001275 finally
1276 call job_stop(job)
1277 delfunc CloseHandler
1278 endtry
1279endfunc
1280
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001281" Use channel in NL mode but received text does not end in NL.
1282func Test_read_in_close_cb_incomplete()
1283 if !has('job')
1284 return
1285 endif
1286 call ch_log('Test_read_in_close_cb_incomplete()')
1287
1288 let g:Ch_received = ''
1289 func! CloseHandler(chan)
1290 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1291 let g:Ch_received .= ch_read(a:chan)
1292 endwhile
1293 endfunc
1294 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1295 \ {'close_cb': 'CloseHandler'})
1296 call assert_equal("run", job_status(job))
1297 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001298 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001299 finally
1300 call job_stop(job)
1301 delfunc CloseHandler
1302 endtry
1303endfunc
1304
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001305func Test_out_cb_lambda()
1306 if !has('job')
1307 return
1308 endif
1309 call ch_log('Test_out_cb_lambda()')
1310
1311 let job = job_start(s:python . " test_channel_pipe.py",
1312 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1313 \ 'out_mode': 'json',
1314 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1315 \ 'err_mode': 'json'})
1316 call assert_equal("run", job_status(job))
1317 try
1318 let g:Ch_outmsg = ''
1319 let g:Ch_errmsg = ''
1320 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1321 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001322 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1323 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001324 finally
1325 call job_stop(job)
1326 endtry
1327endfunc
1328
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001329func Test_close_and_exit_cb()
1330 if !has('job')
1331 return
1332 endif
1333 call ch_log('Test_close_and_exit_cb')
1334
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001335 let g:retdict = {'ret': {}}
1336 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001337 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1338 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001339 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001340 let self.ret['exit_cb'] = job_status(a:job)
1341 endfunc
1342
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001343 let g:job = job_start(has('win32') ? 'cmd /c echo:' : 'echo', {
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001344 \ 'close_cb': g:retdict.close_cb,
1345 \ 'exit_cb': g:retdict.exit_cb,
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001346 \ })
1347 call assert_equal('run', job_status(g:job))
1348 unlet g:job
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001349 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001350 call assert_match('^\%(dead\|run\)', g:retdict.ret['close_cb'])
1351 call assert_equal('dead', g:retdict.ret['exit_cb'])
1352 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001353endfunc
1354
Bram Moolenaard46ae142016-02-16 13:33:52 +01001355""""""""""
1356
Bram Moolenaar0b146882018-09-06 16:27:24 +02001357function ExitCbWipe(job, status)
1358 exe g:wipe_buf 'bw!'
1359endfunction
1360
1361" This caused a crash, because messages were handled while peeking for a
1362" character.
1363func Test_exit_cb_wipes_buf()
1364 if !has('timers')
1365 return
1366 endif
1367 set cursorline lazyredraw
1368 call test_override('redraw_flag', 1)
1369 new
1370 let g:wipe_buf = bufnr('')
1371
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001372 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
1373 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001374 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1375 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1376 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1377 call timer_stop(timer)
1378
1379 set nocursorline nolazyredraw
1380 unlet g:wipe_buf
1381 call test_override('ALL', 0)
1382endfunc
1383
1384""""""""""
1385
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001386let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001387func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001388 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001389 unlet s:channelfd
1390endfunc
1391
1392" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001393func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001394 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001395 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001396 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001397endfunc
1398
1399func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001400 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001401 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001402endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001403
Bram Moolenaard46ae142016-02-16 13:33:52 +01001404""""""""""
1405
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001406let g:Ch_unletResponse = ''
1407func Ch_CloseHandler(handle, msg)
1408 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001409 call ch_close(s:channelfd)
1410endfunc
1411
1412" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001413func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001414 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001415 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001416 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001417endfunc
1418
1419func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001420 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001421 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001422endfunc
1423
1424""""""""""
1425
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001426func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001427 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001428 silent! let ch = ch_open("noserver")
1429 echo ch
1430 let d = ch
1431endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001432
1433""""""""""
1434
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001435func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001436 " Wait up to a second for the port to open.
1437 let s:chopt.waittime = 1000
1438 let channel = ch_open('localhost:' . a:port, s:chopt)
1439 unlet s:chopt.waittime
1440 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001441 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001442 return
1443 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001444 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001445 call ch_close(channel)
1446endfunc
1447
1448func Test_open_delay()
1449 call ch_log('Test_open_delay()')
1450 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001451 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001452endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001453
1454"""""""""
1455
1456function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001457 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001458endfunc
1459
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001461 let handle = ch_open('localhost:' . a:port, s:chopt)
1462 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001463 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001464 return
1465 endif
1466
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001467 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001468 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001469 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001470endfunc
1471
1472func Test_call()
1473 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001475endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001476
1477"""""""""
1478
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001480function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001481 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001482endfunc
1483
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001484function Ch_test_exit_callback(port)
1485 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1486 let g:Ch_exit_job = g:currentJob
1487 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001488endfunc
1489
1490func Test_exit_callback()
1491 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001492 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001493 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001494
Bram Moolenaar9730f742016-02-28 19:50:51 +01001495 " wait up to a second for the job to exit
1496 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001497 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001498 break
1499 endif
1500 sleep 10m
1501 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001502 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001503 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001504
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001505 call assert_equal('done', g:Ch_job_exit_ret)
1506 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1507 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001508 endif
1509endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001510
Bram Moolenaar97792de2016-10-15 18:36:49 +02001511function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001512 if job_info(a:job).process == g:exit_cb_val.process
1513 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1514 endif
1515 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001516endfunction
1517
1518func Test_exit_callback_interval()
1519 if !has('job')
1520 return
1521 endif
1522
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001523 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001524 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001525 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001526 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001527 let elapsed = reltimefloat(g:exit_cb_val.end)
1528 call assert_true(elapsed > 0.5)
1529 call assert_true(elapsed < 1.0)
1530
1531 " case: unreferenced job, using timer
1532 if !has('timers')
1533 return
1534 endif
1535
1536 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1537 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1538 let g:exit_cb_val.process = job_info(g:job).process
1539 unlet g:job
1540 call Standby(1000)
1541 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1542 let elapsed = reltimefloat(g:exit_cb_val.end)
1543 else
1544 let elapsed = 1.0
1545 endif
1546 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001547 call assert_true(elapsed < 1.0)
1548endfunc
1549
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001550"""""""""
1551
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001552let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001553function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001554 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001555endfunc
1556
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001557function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001558 let handle = ch_open('localhost:' . a:port, s:chopt)
1559 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001560 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001561 return
1562 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001563 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001564
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001565 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001566 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001567endfunc
1568
1569func Test_close_callback()
1570 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001571 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001572endfunc
1573
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001574function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001575 let handle = ch_open('localhost:' . a:port, s:chopt)
1576 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001577 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001578 return
1579 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001580 let g:Ch_d = {}
1581 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001582 let self.close_ret = 'closed'
1583 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001584 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001585
1586 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001587 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001588 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001589endfunc
1590
1591func Test_close_partial()
1592 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001593 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001594endfunc
1595
Bram Moolenaar80385682016-03-27 19:13:35 +02001596func Test_job_start_invalid()
1597 call assert_fails('call job_start($x)', 'E474:')
1598 call assert_fails('call job_start("")', 'E474:')
1599endfunc
1600
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001601func Test_job_stop_immediately()
1602 if !has('job')
1603 return
1604 endif
1605
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001606 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001607 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001608 call job_stop(g:job)
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001609 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001610 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001611 call job_stop(g:job, 'kill')
1612 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001613 endtry
1614endfunc
1615
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001616" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001617func Test_partial_in_channel_cycle()
1618 let d = {}
1619 let d.a = function('string', [d])
1620 try
1621 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1622 catch
1623 call assert_exception('E901:')
1624 endtry
1625 unlet d
1626endfunc
1627
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001628func Test_using_freed_memory()
1629 let g:a = job_start(['ls'])
1630 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001631 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001632endfunc
1633
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001634func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001635 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001636 return
1637 endif
1638 sp test_channel.vim
1639 let g:linecount = line('$')
1640 close
1641 split testout
1642 1,$delete
1643 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001644 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001645 bwipe!
1646endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001647
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001648func Test_write_to_deleted_buffer()
1649 if !executable('echo') || !has('job')
1650 return
1651 endif
1652 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001653 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001654 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001655 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1656 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001657
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001658 bdel test_buffer
1659 call assert_equal([], getbufline(bufnr, 1, '$'))
1660
1661 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001662 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001663 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1664 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1665
1666 bwipe! test_buffer
1667endfunc
1668
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001669func Test_cmd_parsing()
1670 if !has('unix')
1671 return
1672 endif
1673 call assert_false(filereadable("file with space"))
1674 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001675 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001676 call delete("file with space")
1677
1678 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001679 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001680 call delete("file with space")
1681endfunc
1682
Bram Moolenaar82117982016-08-27 19:21:48 +02001683func Test_raw_passes_nul()
1684 if !executable('cat') || !has('job')
1685 return
1686 endif
1687
1688 " Test lines from the job containing NUL are stored correctly in a buffer.
1689 new
1690 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1691 w! Xtestread
1692 bwipe!
1693 split testout
1694 1,$delete
1695 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1696 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001697 call assert_equal("asdf\nasdf", getline(1))
1698 call assert_equal("xxx\n", getline(2))
1699 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001700
1701 call delete('Xtestread')
1702 bwipe!
1703
1704 " Test lines from a buffer with NUL bytes are written correctly to the job.
1705 new mybuffer
1706 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1707 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001708 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001709 bwipe!
1710 split Xtestwrite
1711 call assert_equal("asdf\nasdf", getline(1))
1712 call assert_equal("xxx\n", getline(2))
1713 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001714 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001715
1716 call delete('Xtestwrite')
1717 bwipe!
1718endfunc
1719
Bram Moolenaarec68a992016-10-03 21:37:41 +02001720func MyLineCountCb(ch, msg)
1721 let g:linecount += 1
1722endfunc
1723
1724func Test_read_nonl_line()
1725 if !has('job')
1726 return
1727 endif
1728
1729 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001730 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001731 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001732 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaarec68a992016-10-03 21:37:41 +02001733endfunc
1734
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001735func Test_read_from_terminated_job()
1736 if !has('job')
1737 return
1738 endif
1739
1740 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001741 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001742 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001743 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001744endfunc
1745
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001746func Test_job_start_windows()
1747 if !has('job') || !has('win32')
1748 return
1749 endif
1750
1751 " Check that backslash in $COMSPEC is handled properly.
1752 let g:echostr = ''
1753 let cmd = $COMSPEC . ' /c echo 123'
1754 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1755 let info = job_info(job)
1756 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1757
1758 call WaitForAssert({-> assert_equal("123", g:echostr)})
1759 unlet g:echostr
1760endfunc
1761
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001762func Test_env()
1763 if !has('job')
1764 return
1765 endif
1766
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001767 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001768 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001769 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001770 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001771 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001772 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001773 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1774 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001775 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001776 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001777endfunc
1778
1779func Test_cwd()
1780 if !has('job')
1781 return
1782 endif
1783
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001784 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001785 if has('win32')
1786 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001787 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001788 else
1789 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001790 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001791 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001792 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001793 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001794 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001795 let expect = substitute(expect, '[/\\]$', '', '')
1796 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1797 if $CI != '' && stridx(g:envstr, '/private/') == 0
1798 let g:envstr = g:envstr[8:]
1799 endif
1800 call assert_equal(expect, g:envstr)
1801 finally
1802 call job_stop(job)
1803 unlet g:envstr
1804 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001805endfunc
1806
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001807function Ch_test_close_lambda(port)
1808 let handle = ch_open('localhost:' . a:port, s:chopt)
1809 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001810 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001811 return
1812 endif
1813 let g:Ch_close_ret = ''
1814 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1815
1816 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001817 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001818endfunc
1819
1820func Test_close_lambda()
1821 call ch_log('Test_close_lambda()')
1822 call s:run_server('Ch_test_close_lambda')
1823endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001824
1825func s:test_list_args(cmd, out, remove_lf)
1826 try
1827 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001828 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 +02001829 call WaitFor('"" != g:out')
1830 if has('win32')
1831 let g:out = substitute(g:out, '\r', '', 'g')
1832 endif
1833 if a:remove_lf
1834 let g:out = substitute(g:out, '\n$', '', 'g')
1835 endif
1836 call assert_equal(a:out, g:out)
1837 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001838 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001839 unlet g:out
1840 endtry
1841endfunc
1842
1843func Test_list_args()
1844 if !has('job')
1845 return
1846 endif
1847
1848 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1849 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1850 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1851 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1852 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1853 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1854 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1855 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1856 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1857 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1858 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1859 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1860 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1861
1862 " tests which not contain spaces in the argument
1863 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1864 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1865 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1866 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1867 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1868 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1869 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1870 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1871 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1872endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001873
1874" Do this last, it stops any channel log.
1875func Test_zz_ch_log()
1876 call ch_logfile('Xlog', 'w')
1877 call ch_log('hello there')
1878 call ch_log('%s%s')
1879 call ch_logfile('')
1880 let text = readfile('Xlog')
1881 call assert_match("hello there", text[1])
1882 call assert_match("%s%s", text[2])
1883 call delete('Xlog')
1884endfunc
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001885
1886func Test_keep_pty_open()
1887 if !has('unix')
1888 return
1889 endif
1890
1891 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"', {'out_io': 'null', 'err_io': 'null', 'pty': 1})
1892 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1893 call assert_inrange(200, 1000, elapsed)
1894 call job_stop(job)
1895endfunc