blob: 62eaa2089c8fe241fd09ab3fac30fb23dcdffd0c [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaar4641a122019-07-29 22:10:23 +02003source check.vim
4CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01005
Bram Moolenaar321efdd2016-07-15 17:09:11 +02006source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02007source screendump.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +02008
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010011 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020012 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010013endif
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
Bram Moolenaar39536dd2019-01-29 22:58:21 +010032 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010033 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)
Bram Moolenaar772153f2019-03-04 12:09:49 +0100206 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100207
208 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100209 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100210 let resp = ch_read(handle)
211 call assert_equal(type([]), type(resp))
212 call assert_equal(type(11), type(resp[0]))
213 call assert_equal('waited', resp[1])
214
Bram Moolenaard7ece102016-02-02 23:23:02 +0100215 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100216 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100217endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100218
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200221 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100222endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100223
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100224" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200225func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100226 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200227 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100228 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100229 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100230 return
231 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100233 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100234
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100236 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100237 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100238 return
239 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100240 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
241 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100242
243 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100244 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100245
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100246 call ch_close(newhandle)
247endfunc
248
249func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100250 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200251 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100252endfunc
253
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100254" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200255func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100256 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100257 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100258 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100259 return
260 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100261
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100262 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100263
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100264 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100265endfunc
266
267func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100268 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200269 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100270endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100271
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100272"""""""""
273
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200274func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100275 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200276 unlet g:Ch_reply
277 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100278endfunc
279
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200280func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100281 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100282 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100283 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100284 return
285 endif
286
287 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100288 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200289 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100290
291 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100292 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200293 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100294endfunc
295
296func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100297 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200298 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200299 let s:chopt.callback = 'Ch_handler'
300 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200301 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200302 let s:chopt.callback = function('Ch_handler')
303 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100304 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100305endfunc
306
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100307"""""""""
308
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200309let g:Ch_reply = ''
310func Ch_zeroHandler(chan, msg)
311 unlet g:Ch_reply
312 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100313endfunc
314
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200315let g:Ch_zero_reply = ''
316func Ch_oneHandler(chan, msg)
317 unlet g:Ch_zero_reply
318 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100319endfunc
320
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200321func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100322 let handle = ch_open('localhost:' . a:port, s:chopt)
323 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100324 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100325 return
326 endif
327
328 " Check that eval works.
329 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
330
331 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200332 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100333 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100334 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200335 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100337 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200338 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100339 endif
340
341 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200342 let g:Ch_reply = ''
343 let g:Ch_zero_reply = ''
344 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200345 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100346 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200347 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100348 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200349 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100351endfunc
352
353func Test_zero_reply()
354 call ch_log('Test_zero_reply()')
355 " Run with channel handler
356 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200357 let s:chopt.callback = 'Ch_zeroHandler'
358 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100359 unlet s:chopt.callback
360
361 " Run without channel handler
362 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200363 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100364endfunc
365
366"""""""""
367
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200368let g:Ch_reply1 = ""
369func Ch_handleRaw1(chan, msg)
370 unlet g:Ch_reply1
371 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100372endfunc
373
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200374let g:Ch_reply2 = ""
375func Ch_handleRaw2(chan, msg)
376 unlet g:Ch_reply2
377 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100378endfunc
379
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200380let g:Ch_reply3 = ""
381func Ch_handleRaw3(chan, msg)
382 unlet g:Ch_reply3
383 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100384endfunc
385
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200386func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100387 let handle = ch_open('localhost:' . a:port, s:chopt)
388 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100389 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100390 return
391 endif
392 call ch_setoptions(handle, {'mode': 'raw'})
393
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100394 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200395 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200396 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200397 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
398 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200399 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100400 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200401 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100402endfunc
403
404func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100405 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200406 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100407endfunc
408
409"""""""""
410
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100411" Test that trying to connect to a non-existing port fails quickly.
412func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100413 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100414 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100415 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100416 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100417 " Oops, port does exists.
418 call ch_close(handle)
419 else
420 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100421 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100422 endif
423
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100424 " We intend to use a socket that doesn't exist and wait for half a second
425 " before giving up. If the socket does exist it can fail in various ways.
426 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100427 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100428 try
429 let handle = ch_open('localhost:9867', {'waittime': 500})
430 if ch_status(handle) != "fail"
431 " Oops, port does exists.
432 call ch_close(handle)
433 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100434 " Failed connection should wait about 500 msec. Can be longer if the
435 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100436 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 endif
438 catch
439 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100440 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100441 endif
442 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100443endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100444
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100445"""""""""
446
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100447func Test_raw_pipe()
448 if !has('job')
449 return
450 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100451 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100452 " Add a dummy close callback to avoid that messages are dropped when calling
453 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200454 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100455 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200456 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200457 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100458 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200459
460 call assert_equal("open", ch_status(job))
461 call assert_equal("open", ch_status(job), {"part": "out"})
462 call assert_equal("open", ch_status(job), {"part": "err"})
463 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
464 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
465
466 let dict = ch_info(job)
467 call assert_true(dict.id != 0)
468 call assert_equal('open', dict.status)
469 call assert_equal('open', dict.out_status)
470 call assert_equal('RAW', dict.out_mode)
471 call assert_equal('pipe', dict.out_io)
472 call assert_equal('open', dict.err_status)
473 call assert_equal('RAW', dict.err_mode)
474 call assert_equal('pipe', dict.err_io)
475
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100476 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100477 " For a change use the job where a channel is expected.
478 call ch_sendraw(job, "echo something\n")
479 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100480 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
481
Bram Moolenaar151f6562016-03-07 21:19:38 +0100482 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100483 let g:handle = job_getchannel(job)
484 call WaitFor('ch_canread(g:handle)')
485 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100486 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100487 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
488
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200489 let g:Ch_reply = ""
490 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200491 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200492
Bram Moolenaar151f6562016-03-07 21:19:38 +0100493 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
495 finally
496 call job_stop(job)
497 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100498
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200499 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200500 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar8950a562016-03-12 15:22:55 +0100501 let info = job_info(job)
502 call assert_equal("dead", info.status)
503 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200504 call assert_equal(2, len(info.cmd))
505 call assert_equal("test_channel_pipe.py", info.cmd[1])
506
507 let found = 0
508 for j in job_info()
509 if j == job
510 let found += 1
511 endif
512 endfor
513 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100514endfunc
515
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100516func Test_raw_pipe_blob()
517 if !has('job')
518 return
519 endif
520 call ch_log('Test_raw_pipe_blob()')
521 " Add a dummy close callback to avoid that messages are dropped when calling
522 " ch_canread().
523 " Also test the non-blocking option.
524 let job = job_start(s:python . " test_channel_pipe.py",
525 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
526 call assert_equal(v:t_job, type(job))
527 call assert_equal("run", job_status(job))
528
529 call assert_equal("open", ch_status(job))
530 call assert_equal("open", ch_status(job), {"part": "out"})
531
532 try
533 " Create a blob with the echo command and write it.
534 let blob = 0z00
535 let cmd = "echo something\n"
536 for i in range(0, len(cmd) - 1)
537 let blob[i] = char2nr(cmd[i])
538 endfor
539 call assert_equal(len(cmd), len(blob))
540 call ch_sendraw(job, blob)
541
542 " Read a blob with the reply.
543 let msg = ch_readblob(job)
544 let expected = 'something'
545 for i in range(0, len(expected) - 1)
546 call assert_equal(char2nr(expected[i]), msg[i])
547 endfor
548
549 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
550 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
551 finally
552 call job_stop(job)
553 endtry
554
555 let g:Ch_job = job
556 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
557 let info = job_info(job)
558 call assert_equal("dead", info.status)
559endfunc
560
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100561func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100562 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100563 return
564 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100565 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100566 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100567 call assert_equal("run", job_status(job))
568 try
569 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100570 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100571 call assert_equal("something", ch_readraw(handle))
572
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100573 call ch_sendraw(handle, "echoerr wrong\n")
574 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
575
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100576 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100577 call assert_equal("this", ch_readraw(handle))
578 call assert_equal("AND this", ch_readraw(handle))
579
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200580 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar620ca2d2017-12-09 19:13:13 +0100581 call assert_equal("this linethis linethis line", ch_read(handle))
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200582
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100583 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100584 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100585 finally
586 call job_stop(job)
587 endtry
588endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100589
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100590func Test_nl_err_to_out_pipe()
591 if !has('job')
592 return
593 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100594 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100595 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100596 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100597 call assert_equal("run", job_status(job))
598 try
599 let handle = job_getchannel(job)
600 call ch_sendraw(handle, "echo something\n")
601 call assert_equal("something", ch_readraw(handle))
602
603 call ch_sendraw(handle, "echoerr wrong\n")
604 call assert_equal("wrong", ch_readraw(handle))
605 finally
606 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100607 call ch_logfile('')
608 let loglines = readfile('Xlog')
609 call assert_true(len(loglines) > 10)
610 let found_test = 0
611 let found_send = 0
612 let found_recv = 0
613 let found_stop = 0
614 for l in loglines
615 if l =~ 'Test_nl_err_to_out_pipe'
616 let found_test = 1
617 endif
618 if l =~ 'SEND on.*echo something'
619 let found_send = 1
620 endif
621 if l =~ 'RECV on.*something'
622 let found_recv = 1
623 endif
624 if l =~ 'Stopping job with'
625 let found_stop = 1
626 endif
627 endfor
628 call assert_equal(1, found_test)
629 call assert_equal(1, found_send)
630 call assert_equal(1, found_recv)
631 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200632 " On MS-Windows need to sleep for a moment to be able to delete the file.
633 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100634 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100635 endtry
636endfunc
637
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200638func Stop_g_job()
639 call job_stop(g:job)
640 if has('win32')
641 " On MS-Windows the server must close the file handle before we are able
642 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200643 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200644 sleep 10m
645 endif
646endfunc
647
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100648func Test_nl_read_file()
649 if !has('job')
650 return
651 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100652 call ch_log('Test_nl_read_file()')
653 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200654 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100655 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200656 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100657 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200658 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100659 call assert_equal("something", ch_readraw(handle))
660 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
661 call assert_equal("this", ch_readraw(handle))
662 call assert_equal("AND this", ch_readraw(handle))
663 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200664 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100665 call delete('Xinput')
666 endtry
667endfunc
668
Bram Moolenaare98d1212016-03-08 15:37:41 +0100669func Test_nl_write_out_file()
670 if !has('job')
671 return
672 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100673 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200674 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100675 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200676 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100677 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200678 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100679 call ch_sendraw(handle, "echo line one\n")
680 call ch_sendraw(handle, "echo line two\n")
681 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200682 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], 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
690func Test_nl_write_err_file()
691 if !has('job')
692 return
693 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100694 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200695 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100696 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200697 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100698 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200699 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100700 call ch_sendraw(handle, "echoerr line one\n")
701 call ch_sendraw(handle, "echoerr line two\n")
702 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200703 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100704 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200705 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100706 call delete('Xoutput')
707 endtry
708endfunc
709
710func Test_nl_write_both_file()
711 if !has('job')
712 return
713 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100714 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200715 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100716 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200717 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100718 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200719 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100720 call ch_sendraw(handle, "echoerr line one\n")
721 call ch_sendraw(handle, "echo line two\n")
722 call ch_sendraw(handle, "double this\n")
723 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200724 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100725 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200726 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100727 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100728 call delete('Xoutput')
729 endtry
730endfunc
731
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200732func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200733 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200734endfunc
735
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200736func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100737 if !has('job')
738 return
739 endif
740 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200741 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200742 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200743 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100744 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100745 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200746 if a:do_msg
747 let expected[0] = 'Reading from channel output...'
748 else
749 let options['out_msg'] = 0
750 call remove(expected, 0)
751 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100752 else
753 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100754 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100755 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200756 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200758 if a:nomod
759 let options['out_modifiable'] = 0
760 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100761 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100762 call assert_equal("run", job_status(job))
763 try
764 let handle = job_getchannel(job)
765 call ch_sendraw(handle, "echo line one\n")
766 call ch_sendraw(handle, "echo line two\n")
767 call ch_sendraw(handle, "double this\n")
768 call ch_sendraw(handle, "quit\n")
769 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100770 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200771 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200772 if a:nomod
773 call assert_equal(0, &modifiable)
774 else
775 call assert_equal(1, &modifiable)
776 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200777 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100778 bwipe!
779 finally
780 call job_stop(job)
781 endtry
782endfunc
783
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100784func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200785 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100786endfunc
787
788func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200789 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100790endfunc
791
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200792func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200793 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200794endfunc
795
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200796func Test_pipe_to_buffer_name_nomsg()
797 call Run_test_pipe_to_buffer(1, 0, 1)
798endfunc
799
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200800func Test_close_output_buffer()
801 if !has('job')
802 return
803 endif
804 enew!
805 let test_lines = ['one', 'two']
806 call setline(1, test_lines)
807 call ch_log('Test_close_output_buffer()')
808 let options = {'out_io': 'buffer'}
809 let options['out_name'] = 'buffer-output'
810 let options['out_msg'] = 0
811 split buffer-output
812 let job = job_start(s:python . " test_channel_write.py", options)
813 call assert_equal("run", job_status(job))
814 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200815 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200816 quit!
817 sleep 100m
818 " Make sure the write didn't happen to the wrong buffer.
819 call assert_equal(test_lines, getline(1, line('$')))
820 call assert_equal(-1, bufwinnr('buffer-output'))
821 sbuf buffer-output
822 call assert_notequal(-1, bufwinnr('buffer-output'))
823 sleep 100m
824 close " no more writes
825 bwipe!
826 finally
827 call job_stop(job)
828 endtry
829endfunc
830
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200831func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100832 if !has('job')
833 return
834 endif
835 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100836 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200837 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100838 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100839 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200840 if a:do_msg
841 let expected[0] = 'Reading from channel error...'
842 else
843 let options['err_msg'] = 0
844 call remove(expected, 0)
845 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100846 else
847 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100848 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100849 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200850 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100851 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200852 if a:nomod
853 let options['err_modifiable'] = 0
854 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100855 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100856 call assert_equal("run", job_status(job))
857 try
858 let handle = job_getchannel(job)
859 call ch_sendraw(handle, "echoerr line one\n")
860 call ch_sendraw(handle, "echoerr line two\n")
861 call ch_sendraw(handle, "doubleerr this\n")
862 call ch_sendraw(handle, "quit\n")
863 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200864 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200865 if a:nomod
866 call assert_equal(0, &modifiable)
867 else
868 call assert_equal(1, &modifiable)
869 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100870 bwipe!
871 finally
872 call job_stop(job)
873 endtry
874endfunc
875
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100876func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200877 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100878endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100879
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100880func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200881 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200882endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100883
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200884func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200885 call Run_test_pipe_err_to_buffer(1, 1, 1)
886endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100887
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200888func Test_pipe_err_to_buffer_name_nomsg()
889 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100890endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100891
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100892func Test_pipe_both_to_buffer()
893 if !has('job')
894 return
895 endif
896 call ch_log('Test_pipe_both_to_buffer()')
897 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100898 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100899 call assert_equal("run", job_status(job))
900 try
901 let handle = job_getchannel(job)
902 call ch_sendraw(handle, "echo line one\n")
903 call ch_sendraw(handle, "echoerr line two\n")
904 call ch_sendraw(handle, "double this\n")
905 call ch_sendraw(handle, "doubleerr that\n")
906 call ch_sendraw(handle, "quit\n")
907 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200908 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 +0100909 bwipe!
910 finally
911 call job_stop(job)
912 endtry
913endfunc
914
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100915func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100916 if !has('job')
917 return
918 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100919 call ch_log('Test_pipe_from_buffer()')
920
921 sp pipe-input
922 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200923 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100924 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100925 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100926 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100927 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100928 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100929
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100930 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100931 call assert_equal("run", job_status(job))
932 try
933 let handle = job_getchannel(job)
934 call assert_equal('one', ch_read(handle))
935 call assert_equal('two', ch_read(handle))
936 call assert_equal('three', ch_read(handle))
937 bwipe!
938 finally
939 call job_stop(job)
940 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100941endfunc
942
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100943func Test_pipe_from_buffer_name()
944 call Run_test_pipe_from_buffer(1)
945endfunc
946
947func Test_pipe_from_buffer_nr()
948 call Run_test_pipe_from_buffer(0)
949endfunc
950
Bram Moolenaar0874a832016-09-01 15:11:51 +0200951func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200952 if !executable('sort') || !has('job')
953 return
954 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200955 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
956 if a:use_buffer
957 split sortin
958 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
959 let options.in_io = 'buffer'
960 let options.in_name = 'sortin'
961 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200962 if !a:all
963 let options.in_top = 2
964 let options.in_bot = 4
965 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100966 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200967
968 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100969 call assert_equal("run", job_status(job))
970 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
971 call ch_close_in(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200972 endif
973
Bram Moolenaare2956092019-01-25 21:01:17 +0100974 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200975
Bram Moolenaard8b55492016-09-01 14:35:22 +0200976 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200977 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200978 call assert_equal('Reading from channel output...', getline(1))
979 if a:all
980 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
981 else
982 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
983 endif
984
Bram Moolenaare2956092019-01-25 21:01:17 +0100985 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200986 if a:use_buffer
987 bwipe! sortin
988 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200989 bwipe! sortout
990endfunc
991
992func Test_pipe_through_sort_all()
993 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200994 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200995endfunc
996
997func Test_pipe_through_sort_some()
998 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200999 call Run_pipe_through_sort(0, 1)
1000endfunc
1001
1002func Test_pipe_through_sort_feed()
1003 call ch_log('Test_pipe_through_sort_feed()')
1004 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001005endfunc
1006
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001007func Test_pipe_to_nameless_buffer()
1008 if !has('job')
1009 return
1010 endif
1011 call ch_log('Test_pipe_to_nameless_buffer()')
1012 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001013 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001014 call assert_equal("run", job_status(job))
1015 try
1016 let handle = job_getchannel(job)
1017 call ch_sendraw(handle, "echo line one\n")
1018 call ch_sendraw(handle, "echo line two\n")
1019 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001020 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001021 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
1022 bwipe!
1023 finally
1024 call job_stop(job)
1025 endtry
1026endfunc
1027
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001028func Test_pipe_to_buffer_json()
1029 if !has('job')
1030 return
1031 endif
1032 call ch_log('Test_pipe_to_buffer_json()')
1033 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001034 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001035 call assert_equal("run", job_status(job))
1036 try
1037 let handle = job_getchannel(job)
1038 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1039 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1040 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001041 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001042 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1043 bwipe!
1044 finally
1045 call job_stop(job)
1046 endtry
1047endfunc
1048
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001049" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001050func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001051 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001052 if getline(line('$') - a:offset) == a:line
1053 break
1054 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001055 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001056 endfor
1057endfunc
1058
1059func Test_pipe_io_two_buffers()
1060 if !has('job')
1061 return
1062 endif
1063 call ch_log('Test_pipe_io_two_buffers()')
1064
1065 " Create two buffers, one to read from and one to write to.
1066 split pipe-output
1067 set buftype=nofile
1068 split pipe-input
1069 set buftype=nofile
1070
1071 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001072 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001073 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1074 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001075 call assert_equal("run", job_status(job))
1076 try
1077 exe "normal Gaecho hello\<CR>"
1078 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001079 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001080 call assert_equal('hello', getline('$'))
1081
1082 exe bufwinnr('pipe-input') . "wincmd w"
1083 exe "normal Gadouble this\<CR>"
1084 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001085 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001086 call assert_equal('this', getline(line('$') - 1))
1087 call assert_equal('AND this', getline('$'))
1088
1089 bwipe!
1090 exe bufwinnr('pipe-input') . "wincmd w"
1091 bwipe!
1092 finally
1093 call job_stop(job)
1094 endtry
1095endfunc
1096
1097func Test_pipe_io_one_buffer()
1098 if !has('job')
1099 return
1100 endif
1101 call ch_log('Test_pipe_io_one_buffer()')
1102
1103 " Create one buffer to read from and to write to.
1104 split pipe-io
1105 set buftype=nofile
1106
1107 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001108 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001109 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1110 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001111 call assert_equal("run", job_status(job))
1112 try
1113 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001114 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001115 call assert_equal('hello', getline(line('$') - 1))
1116
1117 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001118 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001119 call assert_equal('this', getline(line('$') - 2))
1120 call assert_equal('AND this', getline(line('$') - 1))
1121
1122 bwipe!
1123 finally
1124 call job_stop(job)
1125 endtry
1126endfunc
1127
Bram Moolenaar4641a122019-07-29 22:10:23 +02001128func Test_write_to_buffer_and_scroll()
1129 CheckFeature job
1130 if !CanRunVimInTerminal()
1131 throw 'Skipped: cannot make screendumps'
1132 endif
1133 let lines =<< trim END
1134 new Xscrollbuffer
1135 call setline(1, range(1, 200))
1136 $
1137 redraw
1138 wincmd w
1139 call deletebufline('Xscrollbuffer', 1, '$')
1140 if has('win32')
1141 let cmd = ['cmd', '/c', 'echo sometext']
1142 else
1143 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1144 endif
1145 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1146 END
1147 call writefile(lines, 'XtestBufferScroll')
1148 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
1149 sleep 500m
1150 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1151
1152 " clean up
1153 call StopVimInTerminal(buf)
1154 call delete('XtestBufferScroll')
1155endfunc
1156
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001157func Test_pipe_null()
1158 if !has('job')
1159 return
1160 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001161 call ch_log('Test_pipe_null()')
1162
1163 " We cannot check that no I/O works, we only check that the job starts
1164 " properly.
1165 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001166 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001167 call assert_equal("run", job_status(job))
1168 try
1169 call assert_equal('something', ch_read(job))
1170 finally
1171 call job_stop(job)
1172 endtry
1173
1174 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001175 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001176 call assert_equal("run", job_status(job))
1177 try
1178 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1179 finally
1180 call job_stop(job)
1181 endtry
1182
1183 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001184 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001185 call assert_equal("run", job_status(job))
1186 try
1187 call assert_equal('something', ch_read(job))
1188 finally
1189 call job_stop(job)
1190 endtry
1191
1192 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001193 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001194 call assert_equal("run", job_status(job))
1195 call job_stop(job)
1196
1197 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001198 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001199 call assert_equal("run", job_status(job))
1200 call assert_equal('channel fail', string(job_getchannel(job)))
1201 call assert_equal('fail', ch_status(job))
1202 call job_stop(job)
1203endfunc
1204
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001205func Test_pipe_to_buffer_raw()
1206 if !has('job')
1207 return
1208 endif
1209 call ch_log('Test_raw_pipe_to_buffer()')
1210 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1211 split testout
1212 let job = job_start([s:python, '-c',
1213 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001214 " the job may be done quickly, also accept "dead"
1215 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001216 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001217 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001218 let totlen = 0
1219 for line in getline(1, '$')
1220 call assert_equal('', substitute(line, '^\.*', '', ''))
1221 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001222 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001223 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001224 finally
1225 call job_stop(job)
1226 bwipe!
1227 endtry
1228endfunc
1229
Bram Moolenaarde279892016-03-11 22:19:44 +01001230func Test_reuse_channel()
1231 if !has('job')
1232 return
1233 endif
1234 call ch_log('Test_reuse_channel()')
1235
1236 let job = job_start(s:python . " test_channel_pipe.py")
1237 call assert_equal("run", job_status(job))
1238 let handle = job_getchannel(job)
1239 try
1240 call ch_sendraw(handle, "echo something\n")
1241 call assert_equal("something", ch_readraw(handle))
1242 finally
1243 call job_stop(job)
1244 endtry
1245
1246 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1247 call assert_equal("run", job_status(job))
1248 let handle = job_getchannel(job)
1249 try
1250 call ch_sendraw(handle, "echo again\n")
1251 call assert_equal("again", ch_readraw(handle))
1252 finally
1253 call job_stop(job)
1254 endtry
1255endfunc
1256
Bram Moolenaar75f72652016-03-20 22:16:56 +01001257func Test_out_cb()
1258 if !has('job')
1259 return
1260 endif
1261 call ch_log('Test_out_cb()')
1262
1263 let dict = {'thisis': 'dict: '}
1264 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001265 if type(a:msg) == v:t_string
1266 let g:Ch_outmsg = self.thisis . a:msg
1267 else
1268 let g:Ch_outobj = a:msg
1269 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001270 endfunc
1271 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001272 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001273 endfunc
1274 let job = job_start(s:python . " test_channel_pipe.py",
1275 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001276 \ 'out_mode': 'json',
1277 \ 'err_cb': dict.errHandler,
1278 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001279 call assert_equal("run", job_status(job))
1280 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001281 let g:Ch_outmsg = ''
1282 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001283 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1284 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001285 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1286 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001287
1288 " Receive a json object split in pieces
1289 unlet! g:Ch_outobj
1290 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001291 let g:Ch_outobj = ''
1292 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001293 finally
1294 call job_stop(job)
1295 endtry
1296endfunc
1297
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001298func Test_out_close_cb()
1299 if !has('job')
1300 return
1301 endif
1302 call ch_log('Test_out_close_cb()')
1303
1304 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001305 let g:Ch_msg1 = ''
1306 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001307 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001308 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001309 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001310 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001311 let s:counter += 1
1312 endfunc
1313 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001314 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001315 let s:counter += 1
1316 endfunc
1317 let job = job_start(s:python . " test_channel_pipe.py quit now",
1318 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001319 \ 'close_cb': 'CloseHandler'})
1320 " the job may be done quickly, also accept "dead"
1321 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001322 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001323 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1324 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001325 finally
1326 call job_stop(job)
1327 delfunc OutHandler
1328 delfunc CloseHandler
1329 endtry
1330endfunc
1331
Bram Moolenaar437905c2016-04-26 19:01:05 +02001332func Test_read_in_close_cb()
1333 if !has('job')
1334 return
1335 endif
1336 call ch_log('Test_read_in_close_cb()')
1337
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001338 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001339 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001341 endfunc
1342 let job = job_start(s:python . " test_channel_pipe.py quit now",
1343 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001344 " the job may be done quickly, also accept "dead"
1345 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001346 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001347 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001348 finally
1349 call job_stop(job)
1350 delfunc CloseHandler
1351 endtry
1352endfunc
1353
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001354" Use channel in NL mode but received text does not end in NL.
1355func Test_read_in_close_cb_incomplete()
1356 if !has('job')
1357 return
1358 endif
1359 call ch_log('Test_read_in_close_cb_incomplete()')
1360
1361 let g:Ch_received = ''
1362 func! CloseHandler(chan)
1363 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1364 let g:Ch_received .= ch_read(a:chan)
1365 endwhile
1366 endfunc
1367 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1368 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001369 " the job may be done quickly, also accept "dead"
1370 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001371 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001372 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001373 finally
1374 call job_stop(job)
1375 delfunc CloseHandler
1376 endtry
1377endfunc
1378
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001379func Test_out_cb_lambda()
1380 if !has('job')
1381 return
1382 endif
1383 call ch_log('Test_out_cb_lambda()')
1384
1385 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001386 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1387 \ 'out_mode': 'json',
1388 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1389 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001390 call assert_equal("run", job_status(job))
1391 try
1392 let g:Ch_outmsg = ''
1393 let g:Ch_errmsg = ''
1394 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1395 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001396 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1397 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001398 finally
1399 call job_stop(job)
1400 endtry
1401endfunc
1402
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001403func Test_close_and_exit_cb()
1404 if !has('job')
1405 return
1406 endif
1407 call ch_log('Test_close_and_exit_cb')
1408
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001409 let g:retdict = {'ret': {}}
1410 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001411 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1412 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001413 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001414 let self.ret['exit_cb'] = job_status(a:job)
1415 endfunc
1416
Bram Moolenaare2956092019-01-25 21:01:17 +01001417 let job = job_start([&shell, &shellcmdflag, 'echo'],
1418 \ {'close_cb': g:retdict.close_cb,
1419 \ 'exit_cb': g:retdict.exit_cb})
1420 " the job may be done quickly, also accept "dead"
1421 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001422 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001423 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001424 call assert_equal('dead', g:retdict.ret['exit_cb'])
1425 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001426endfunc
1427
Bram Moolenaard46ae142016-02-16 13:33:52 +01001428""""""""""
1429
Bram Moolenaar0b146882018-09-06 16:27:24 +02001430function ExitCbWipe(job, status)
1431 exe g:wipe_buf 'bw!'
1432endfunction
1433
1434" This caused a crash, because messages were handled while peeking for a
1435" character.
1436func Test_exit_cb_wipes_buf()
1437 if !has('timers')
1438 return
1439 endif
1440 set cursorline lazyredraw
1441 call test_override('redraw_flag', 1)
1442 new
1443 let g:wipe_buf = bufnr('')
1444
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001445 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001446 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001447 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1448 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1449 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1450 call timer_stop(timer)
1451
1452 set nocursorline nolazyredraw
1453 unlet g:wipe_buf
1454 call test_override('ALL', 0)
1455endfunc
1456
1457""""""""""
1458
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001459let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001460func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001461 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001462 unlet s:channelfd
1463endfunc
1464
1465" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001466func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001467 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001468 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001469 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001470endfunc
1471
1472func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001473 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001475endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001476
Bram Moolenaard46ae142016-02-16 13:33:52 +01001477""""""""""
1478
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479let g:Ch_unletResponse = ''
1480func Ch_CloseHandler(handle, msg)
1481 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001482 call ch_close(s:channelfd)
1483endfunc
1484
1485" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001486func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001487 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001488 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001489 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001490endfunc
1491
1492func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001493 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001495endfunc
1496
1497""""""""""
1498
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001499func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001500 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001501 silent! let ch = ch_open("noserver")
1502 echo ch
1503 let d = ch
1504endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001505
1506""""""""""
1507
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001508func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001509 " Wait up to a second for the port to open.
1510 let s:chopt.waittime = 1000
1511 let channel = ch_open('localhost:' . a:port, s:chopt)
1512 unlet s:chopt.waittime
1513 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001514 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001515 return
1516 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001517 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001518 call ch_close(channel)
1519endfunc
1520
1521func Test_open_delay()
1522 call ch_log('Test_open_delay()')
1523 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001524 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001525endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001526
1527"""""""""
1528
1529function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001530 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001531endfunc
1532
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001533function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001534 let handle = ch_open('localhost:' . a:port, s:chopt)
1535 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001536 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001537 return
1538 endif
1539
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001540 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001541 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001542 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001543endfunc
1544
1545func Test_call()
1546 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001547 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001548endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001549
1550"""""""""
1551
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001552let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001553function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001554 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001555endfunc
1556
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001557function Ch_test_exit_callback(port)
1558 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1559 let g:Ch_exit_job = g:currentJob
1560 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001561endfunc
1562
1563func Test_exit_callback()
1564 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001565 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001566 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001567
Bram Moolenaar9730f742016-02-28 19:50:51 +01001568 " wait up to a second for the job to exit
1569 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001570 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001571 break
1572 endif
1573 sleep 10m
1574 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001575 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001576 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001577
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001578 call assert_equal('done', g:Ch_job_exit_ret)
1579 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1580 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001581 endif
1582endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001583
Bram Moolenaar97792de2016-10-15 18:36:49 +02001584function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001585 if job_info(a:job).process == g:exit_cb_val.process
1586 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1587 endif
1588 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001589endfunction
1590
1591func Test_exit_callback_interval()
1592 if !has('job')
1593 return
1594 endif
1595
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001596 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001597 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001598 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001599 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001600 let elapsed = reltimefloat(g:exit_cb_val.end)
1601 call assert_true(elapsed > 0.5)
1602 call assert_true(elapsed < 1.0)
1603
1604 " case: unreferenced job, using timer
1605 if !has('timers')
1606 return
1607 endif
1608
1609 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1610 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1611 let g:exit_cb_val.process = job_info(g:job).process
1612 unlet g:job
1613 call Standby(1000)
1614 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1615 let elapsed = reltimefloat(g:exit_cb_val.end)
1616 else
1617 let elapsed = 1.0
1618 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001619 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001620endfunc
1621
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001622"""""""""
1623
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001624let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001625function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001626 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001627endfunc
1628
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001629function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001630 let handle = ch_open('localhost:' . a:port, s:chopt)
1631 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001632 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001633 return
1634 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001635 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001636
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001637 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001638 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001639endfunc
1640
1641func Test_close_callback()
1642 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001643 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001644endfunc
1645
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001646function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001647 let handle = ch_open('localhost:' . a:port, s:chopt)
1648 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001649 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001650 return
1651 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001652 let g:Ch_d = {}
1653 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001654 let self.close_ret = 'closed'
1655 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001656 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001657
1658 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001659 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001660 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001661endfunc
1662
1663func Test_close_partial()
1664 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001665 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001666endfunc
1667
Bram Moolenaar80385682016-03-27 19:13:35 +02001668func Test_job_start_invalid()
1669 call assert_fails('call job_start($x)', 'E474:')
1670 call assert_fails('call job_start("")', 'E474:')
1671endfunc
1672
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001673func Test_job_stop_immediately()
1674 if !has('job')
1675 return
1676 endif
1677
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001678 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001679 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001680 call job_stop(g:job)
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001681 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001682 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001683 call job_stop(g:job, 'kill')
1684 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001685 endtry
1686endfunc
1687
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001688" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001689func Test_partial_in_channel_cycle()
1690 let d = {}
1691 let d.a = function('string', [d])
1692 try
1693 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1694 catch
1695 call assert_exception('E901:')
1696 endtry
1697 unlet d
1698endfunc
1699
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001700func Test_using_freed_memory()
1701 let g:a = job_start(['ls'])
1702 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001703 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001704endfunc
1705
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001706func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001707 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001708 return
1709 endif
1710 sp test_channel.vim
1711 let g:linecount = line('$')
1712 close
1713 split testout
1714 1,$delete
1715 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001716 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001717 bwipe!
1718endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001719
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001720func Test_write_to_deleted_buffer()
1721 if !executable('echo') || !has('job')
1722 return
1723 endif
1724 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001725 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001726 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001727 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1728 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001729
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001730 bdel test_buffer
1731 call assert_equal([], getbufline(bufnr, 1, '$'))
1732
1733 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001734 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001735 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1736 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1737
1738 bwipe! test_buffer
1739endfunc
1740
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001741func Test_cmd_parsing()
1742 if !has('unix')
1743 return
1744 endif
1745 call assert_false(filereadable("file with space"))
1746 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001747 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001748 call delete("file with space")
1749
1750 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001751 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001752 call delete("file with space")
1753endfunc
1754
Bram Moolenaar82117982016-08-27 19:21:48 +02001755func Test_raw_passes_nul()
1756 if !executable('cat') || !has('job')
1757 return
1758 endif
1759
1760 " Test lines from the job containing NUL are stored correctly in a buffer.
1761 new
1762 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1763 w! Xtestread
1764 bwipe!
1765 split testout
1766 1,$delete
1767 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1768 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001769 call assert_equal("asdf\nasdf", getline(1))
1770 call assert_equal("xxx\n", getline(2))
1771 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001772
1773 call delete('Xtestread')
1774 bwipe!
1775
1776 " Test lines from a buffer with NUL bytes are written correctly to the job.
1777 new mybuffer
1778 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1779 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 +02001780 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001781 bwipe!
1782 split Xtestwrite
1783 call assert_equal("asdf\nasdf", getline(1))
1784 call assert_equal("xxx\n", getline(2))
1785 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001786 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001787
1788 call delete('Xtestwrite')
1789 bwipe!
1790endfunc
1791
Bram Moolenaarec68a992016-10-03 21:37:41 +02001792func Test_read_nonl_line()
1793 if !has('job')
1794 return
1795 endif
1796
1797 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001798 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001799 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001800 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001801 unlet g:linecount
1802endfunc
1803
1804func Test_read_nonl_in_close_cb()
1805 if !has('job')
1806 return
1807 endif
1808
1809 func s:close_cb(ch)
1810 while ch_status(a:ch) == 'buffered'
1811 let g:out .= ch_read(a:ch)
1812 endwhile
1813 endfunc
1814
1815 let g:out = ''
1816 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1817 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1818 call WaitForAssert({-> assert_equal('123', g:out)})
1819 unlet g:out
1820 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001821endfunc
1822
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001823func Test_read_from_terminated_job()
1824 if !has('job')
1825 return
1826 endif
1827
1828 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001829 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001830 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001831 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001832 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001833endfunc
1834
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001835func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001836 CheckFeature job
1837 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001838
1839 " Check that backslash in $COMSPEC is handled properly.
1840 let g:echostr = ''
1841 let cmd = $COMSPEC . ' /c echo 123'
1842 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1843 let info = job_info(job)
1844 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1845
1846 call WaitForAssert({-> assert_equal("123", g:echostr)})
1847 unlet g:echostr
1848endfunc
1849
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001850func Test_env()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001851 CheckFeature job
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001852
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001853 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001854 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001855 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001856 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001857 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001858 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001859 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1860 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001861 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001862 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001863endfunc
1864
1865func Test_cwd()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001866 CheckFeature job
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001867
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001868 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001869 if has('win32')
1870 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001871 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001872 else
1873 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001874 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001875 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001876 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001877 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001878 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001879 let expect = substitute(expect, '[/\\]$', '', '')
1880 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1881 if $CI != '' && stridx(g:envstr, '/private/') == 0
1882 let g:envstr = g:envstr[8:]
1883 endif
1884 call assert_equal(expect, g:envstr)
1885 finally
1886 call job_stop(job)
1887 unlet g:envstr
1888 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001889endfunc
1890
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001891function Ch_test_close_lambda(port)
1892 let handle = ch_open('localhost:' . a:port, s:chopt)
1893 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001894 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001895 return
1896 endif
1897 let g:Ch_close_ret = ''
1898 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1899
1900 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001901 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001902endfunc
1903
1904func Test_close_lambda()
1905 call ch_log('Test_close_lambda()')
1906 call s:run_server('Ch_test_close_lambda')
1907endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001908
1909func s:test_list_args(cmd, out, remove_lf)
1910 try
1911 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001912 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 +02001913 call WaitFor('"" != g:out')
1914 if has('win32')
1915 let g:out = substitute(g:out, '\r', '', 'g')
1916 endif
1917 if a:remove_lf
1918 let g:out = substitute(g:out, '\n$', '', 'g')
1919 endif
1920 call assert_equal(a:out, g:out)
1921 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001922 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001923 unlet g:out
1924 endtry
1925endfunc
1926
1927func Test_list_args()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001928 CheckFeature job
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001929
1930 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1931 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1932 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1933 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1934 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1935 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1936 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1937 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1938 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1939 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1940 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1941 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1942 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1943
1944 " tests which not contain spaces in the argument
1945 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1946 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1947 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1948 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1949 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1950 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1951 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1952 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1953 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1954endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001955
1956" Do this last, it stops any channel log.
1957func Test_zz_ch_log()
1958 call ch_logfile('Xlog', 'w')
1959 call ch_log('hello there')
1960 call ch_log('%s%s')
1961 call ch_logfile('')
1962 let text = readfile('Xlog')
1963 call assert_match("hello there", text[1])
1964 call assert_match("%s%s", text[2])
1965 call delete('Xlog')
1966endfunc
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001967
1968func Test_keep_pty_open()
1969 if !has('unix')
1970 return
1971 endif
1972
Bram Moolenaare2956092019-01-25 21:01:17 +01001973 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1974 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001975 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1976 call assert_inrange(200, 1000, elapsed)
1977 call job_stop(job)
1978endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001979
1980func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001981 CheckFeature job
1982 CheckFeature timers
Bram Moolenaarc46af532019-01-09 22:24:49 +01001983
1984 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001985 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001986 endfunc
1987
1988 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001989 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001990 call Resume()
1991 endfunc
1992
1993 func TimerCb(timer)
1994 if has('win32')
1995 let cmd = ['cmd', '/c', 'echo.']
1996 else
1997 let cmd = ['echo']
1998 endif
1999 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
2000 call substitute(repeat('a', 100000), '.', '', 'g')
2001 endfunc
2002
2003 " We should be interrupted before 'updatetime' elapsed.
2004 let g:val = 0
2005 call timer_start(1, 'TimerCb')
2006 let elapsed = Standby(&ut)
2007 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002008
2009 " Wait for both OutCb() and ExitCb() to have been called before deleting
2010 " them.
2011 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002012 call job_stop(g:job)
2013
2014 delfunc OutCb
2015 delfunc ExitCb
2016 delfunc TimerCb
2017 unlet! g:val
2018 unlet! g:job
2019endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002020
2021func Test_raw_large_data()
2022 try
2023 let g:out = ''
2024 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002025 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2026 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002027
Bram Moolenaare2956092019-01-25 21:01:17 +01002028 let outlen = 79999
2029 let want = repeat('X', outlen) . "\n"
Bram Moolenaar24058382019-01-24 23:11:49 +01002030 call ch_sendraw(job, want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002031 call WaitFor({-> len(g:out) >= outlen}, 10000)
2032 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002033 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2034 finally
2035 call job_stop(job)
2036 unlet g:out
2037 endtry
2038endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002039
Bram Moolenaar65240682019-02-10 22:23:26 +01002040func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002041 CheckFeature job
2042 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002043
2044 try
2045 let job = job_start(s:python . " test_channel_pipe.py busy",
2046 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2047 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2048 finally
2049 call job_stop(job)
2050 endtry
2051endfunc
2052
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002053func Test_job_exitval_and_termsig()
2054 if !has('unix')
2055 return
2056 endif
2057
2058 " Terminate job normally
2059 let cmd = ['echo']
2060 let job = job_start(cmd)
2061 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2062 let info = job_info(job)
2063 call assert_equal(0, info.exitval)
2064 call assert_equal("", info.termsig)
2065
2066 " Terminate job by signal
2067 let cmd = ['sleep', '10']
2068 let job = job_start(cmd)
2069 sleep 10m
2070 call job_stop(job)
2071 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2072 let info = job_info(job)
2073 call assert_equal(-1, info.exitval)
2074 call assert_equal("term", info.termsig)
2075endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002076
2077func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002078 CheckFeature job
2079 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002080
2081 call writefile(['test'], 'Xtestin')
2082 let in_opts = [{},
2083 \ {'in_io': 'null'},
2084 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2085 let out_opts = [{},
2086 \ {'out_io': 'null'},
2087 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2088 let err_opts = [{},
2089 \ {'err_io': 'null'},
2090 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2091 \ {'err_io': 'out'}]
2092 let opts = []
2093
2094 for in_opt in in_opts
2095 let x = copy(in_opt)
2096 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002097 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002098 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002099 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002100 let opts += [extend({'pty': 1}, x)]
2101 endfor
2102 endfor
2103 endfor
2104
2105 for opt in opts
2106 let job = job_start('echo', opt)
2107 let info = job_info(job)
2108 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2109 \ get(opt, 'in_io', 'tty'),
2110 \ get(opt, 'out_io', 'tty'),
2111 \ get(opt, 'err_io', 'tty'))
2112
2113 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2114 call assert_notequal('', info.tty_in, msg)
2115 else
2116 call assert_equal('', info.tty_in, msg)
2117 endif
2118 call assert_equal(info.tty_in, info.tty_out, msg)
2119
2120 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2121 endfor
2122
2123 call delete('Xtestin')
2124 call delete('Xtestout')
2125 call delete('Xtesterr')
2126endfunc