blob: 982a0459a156cff9328d0f4df493a60e59cb5764 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaare2469252016-02-04 10:54:34 +01003if !has('channel')
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02004 throw 'Skipped: channel feature missing'
Bram Moolenaare2469252016-02-04 10:54:34 +01005endif
6
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
8
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010011 " Can't run this test without Python.
Bram 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 Moolenaarf65333c2016-03-08 18:27:21 +01001128func Test_pipe_null()
1129 if !has('job')
1130 return
1131 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001132 call ch_log('Test_pipe_null()')
1133
1134 " We cannot check that no I/O works, we only check that the job starts
1135 " properly.
1136 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001137 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001138 call assert_equal("run", job_status(job))
1139 try
1140 call assert_equal('something', ch_read(job))
1141 finally
1142 call job_stop(job)
1143 endtry
1144
1145 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001146 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001147 call assert_equal("run", job_status(job))
1148 try
1149 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1150 finally
1151 call job_stop(job)
1152 endtry
1153
1154 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001155 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001156 call assert_equal("run", job_status(job))
1157 try
1158 call assert_equal('something', ch_read(job))
1159 finally
1160 call job_stop(job)
1161 endtry
1162
1163 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001164 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001165 call assert_equal("run", job_status(job))
1166 call job_stop(job)
1167
1168 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001169 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001170 call assert_equal("run", job_status(job))
1171 call assert_equal('channel fail', string(job_getchannel(job)))
1172 call assert_equal('fail', ch_status(job))
1173 call job_stop(job)
1174endfunc
1175
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001176func Test_pipe_to_buffer_raw()
1177 if !has('job')
1178 return
1179 endif
1180 call ch_log('Test_raw_pipe_to_buffer()')
1181 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1182 split testout
1183 let job = job_start([s:python, '-c',
1184 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001185 " the job may be done quickly, also accept "dead"
1186 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001187 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001188 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001189 let totlen = 0
1190 for line in getline(1, '$')
1191 call assert_equal('', substitute(line, '^\.*', '', ''))
1192 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001193 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001194 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001195 finally
1196 call job_stop(job)
1197 bwipe!
1198 endtry
1199endfunc
1200
Bram Moolenaarde279892016-03-11 22:19:44 +01001201func Test_reuse_channel()
1202 if !has('job')
1203 return
1204 endif
1205 call ch_log('Test_reuse_channel()')
1206
1207 let job = job_start(s:python . " test_channel_pipe.py")
1208 call assert_equal("run", job_status(job))
1209 let handle = job_getchannel(job)
1210 try
1211 call ch_sendraw(handle, "echo something\n")
1212 call assert_equal("something", ch_readraw(handle))
1213 finally
1214 call job_stop(job)
1215 endtry
1216
1217 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1218 call assert_equal("run", job_status(job))
1219 let handle = job_getchannel(job)
1220 try
1221 call ch_sendraw(handle, "echo again\n")
1222 call assert_equal("again", ch_readraw(handle))
1223 finally
1224 call job_stop(job)
1225 endtry
1226endfunc
1227
Bram Moolenaar75f72652016-03-20 22:16:56 +01001228func Test_out_cb()
1229 if !has('job')
1230 return
1231 endif
1232 call ch_log('Test_out_cb()')
1233
1234 let dict = {'thisis': 'dict: '}
1235 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001236 if type(a:msg) == v:t_string
1237 let g:Ch_outmsg = self.thisis . a:msg
1238 else
1239 let g:Ch_outobj = a:msg
1240 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001241 endfunc
1242 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001243 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001244 endfunc
1245 let job = job_start(s:python . " test_channel_pipe.py",
1246 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001247 \ 'out_mode': 'json',
1248 \ 'err_cb': dict.errHandler,
1249 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001250 call assert_equal("run", job_status(job))
1251 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001252 let g:Ch_outmsg = ''
1253 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001254 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1255 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001256 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1257 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001258
1259 " Receive a json object split in pieces
1260 unlet! g:Ch_outobj
1261 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001262 let g:Ch_outobj = ''
1263 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001264 finally
1265 call job_stop(job)
1266 endtry
1267endfunc
1268
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001269func Test_out_close_cb()
1270 if !has('job')
1271 return
1272 endif
1273 call ch_log('Test_out_close_cb()')
1274
1275 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001276 let g:Ch_msg1 = ''
1277 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001278 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001279 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001280 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001281 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001282 let s:counter += 1
1283 endfunc
1284 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001285 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001286 let s:counter += 1
1287 endfunc
1288 let job = job_start(s:python . " test_channel_pipe.py quit now",
1289 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001290 \ 'close_cb': 'CloseHandler'})
1291 " the job may be done quickly, also accept "dead"
1292 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001293 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001294 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1295 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001296 finally
1297 call job_stop(job)
1298 delfunc OutHandler
1299 delfunc CloseHandler
1300 endtry
1301endfunc
1302
Bram Moolenaar437905c2016-04-26 19:01:05 +02001303func Test_read_in_close_cb()
1304 if !has('job')
1305 return
1306 endif
1307 call ch_log('Test_read_in_close_cb()')
1308
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001309 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001310 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001311 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001312 endfunc
1313 let job = job_start(s:python . " test_channel_pipe.py quit now",
1314 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001315 " the job may be done quickly, also accept "dead"
1316 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001317 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001318 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001319 finally
1320 call job_stop(job)
1321 delfunc CloseHandler
1322 endtry
1323endfunc
1324
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001325" Use channel in NL mode but received text does not end in NL.
1326func Test_read_in_close_cb_incomplete()
1327 if !has('job')
1328 return
1329 endif
1330 call ch_log('Test_read_in_close_cb_incomplete()')
1331
1332 let g:Ch_received = ''
1333 func! CloseHandler(chan)
1334 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1335 let g:Ch_received .= ch_read(a:chan)
1336 endwhile
1337 endfunc
1338 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1339 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001340 " the job may be done quickly, also accept "dead"
1341 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001342 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001343 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001344 finally
1345 call job_stop(job)
1346 delfunc CloseHandler
1347 endtry
1348endfunc
1349
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001350func Test_out_cb_lambda()
1351 if !has('job')
1352 return
1353 endif
1354 call ch_log('Test_out_cb_lambda()')
1355
1356 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001357 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1358 \ 'out_mode': 'json',
1359 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1360 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001361 call assert_equal("run", job_status(job))
1362 try
1363 let g:Ch_outmsg = ''
1364 let g:Ch_errmsg = ''
1365 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1366 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001367 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1368 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001369 finally
1370 call job_stop(job)
1371 endtry
1372endfunc
1373
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001374func Test_close_and_exit_cb()
1375 if !has('job')
1376 return
1377 endif
1378 call ch_log('Test_close_and_exit_cb')
1379
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001380 let g:retdict = {'ret': {}}
1381 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001382 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1383 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001384 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001385 let self.ret['exit_cb'] = job_status(a:job)
1386 endfunc
1387
Bram Moolenaare2956092019-01-25 21:01:17 +01001388 let job = job_start([&shell, &shellcmdflag, 'echo'],
1389 \ {'close_cb': g:retdict.close_cb,
1390 \ 'exit_cb': g:retdict.exit_cb})
1391 " the job may be done quickly, also accept "dead"
1392 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001393 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001394 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001395 call assert_equal('dead', g:retdict.ret['exit_cb'])
1396 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001397endfunc
1398
Bram Moolenaard46ae142016-02-16 13:33:52 +01001399""""""""""
1400
Bram Moolenaar0b146882018-09-06 16:27:24 +02001401function ExitCbWipe(job, status)
1402 exe g:wipe_buf 'bw!'
1403endfunction
1404
1405" This caused a crash, because messages were handled while peeking for a
1406" character.
1407func Test_exit_cb_wipes_buf()
1408 if !has('timers')
1409 return
1410 endif
1411 set cursorline lazyredraw
1412 call test_override('redraw_flag', 1)
1413 new
1414 let g:wipe_buf = bufnr('')
1415
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001416 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001417 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001418 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1419 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1420 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1421 call timer_stop(timer)
1422
1423 set nocursorline nolazyredraw
1424 unlet g:wipe_buf
1425 call test_override('ALL', 0)
1426endfunc
1427
1428""""""""""
1429
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001430let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001431func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001432 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001433 unlet s:channelfd
1434endfunc
1435
1436" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001437func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001438 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001439 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001440 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001441endfunc
1442
1443func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001444 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001445 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001446endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001447
Bram Moolenaard46ae142016-02-16 13:33:52 +01001448""""""""""
1449
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001450let g:Ch_unletResponse = ''
1451func Ch_CloseHandler(handle, msg)
1452 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001453 call ch_close(s:channelfd)
1454endfunc
1455
1456" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001457func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001458 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001459 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001460 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001461endfunc
1462
1463func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001464 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001465 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001466endfunc
1467
1468""""""""""
1469
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001470func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001471 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001472 silent! let ch = ch_open("noserver")
1473 echo ch
1474 let d = ch
1475endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001476
1477""""""""""
1478
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001480 " Wait up to a second for the port to open.
1481 let s:chopt.waittime = 1000
1482 let channel = ch_open('localhost:' . a:port, s:chopt)
1483 unlet s:chopt.waittime
1484 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001485 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001486 return
1487 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001488 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001489 call ch_close(channel)
1490endfunc
1491
1492func Test_open_delay()
1493 call ch_log('Test_open_delay()')
1494 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001495 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001496endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001497
1498"""""""""
1499
1500function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001501 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001502endfunc
1503
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001504function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001505 let handle = ch_open('localhost:' . a:port, s:chopt)
1506 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001507 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001508 return
1509 endif
1510
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001511 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001512 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001513 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001514endfunc
1515
1516func Test_call()
1517 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001518 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001519endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001520
1521"""""""""
1522
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001523let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001524function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001525 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001526endfunc
1527
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001528function Ch_test_exit_callback(port)
1529 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1530 let g:Ch_exit_job = g:currentJob
1531 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001532endfunc
1533
1534func Test_exit_callback()
1535 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001536 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001537 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001538
Bram Moolenaar9730f742016-02-28 19:50:51 +01001539 " wait up to a second for the job to exit
1540 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001541 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001542 break
1543 endif
1544 sleep 10m
1545 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001546 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001547 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001548
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001549 call assert_equal('done', g:Ch_job_exit_ret)
1550 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1551 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001552 endif
1553endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001554
Bram Moolenaar97792de2016-10-15 18:36:49 +02001555function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001556 if job_info(a:job).process == g:exit_cb_val.process
1557 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1558 endif
1559 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001560endfunction
1561
1562func Test_exit_callback_interval()
1563 if !has('job')
1564 return
1565 endif
1566
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001567 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001568 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001569 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001570 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001571 let elapsed = reltimefloat(g:exit_cb_val.end)
1572 call assert_true(elapsed > 0.5)
1573 call assert_true(elapsed < 1.0)
1574
1575 " case: unreferenced job, using timer
1576 if !has('timers')
1577 return
1578 endif
1579
1580 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1581 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1582 let g:exit_cb_val.process = job_info(g:job).process
1583 unlet g:job
1584 call Standby(1000)
1585 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1586 let elapsed = reltimefloat(g:exit_cb_val.end)
1587 else
1588 let elapsed = 1.0
1589 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001590 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001591endfunc
1592
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001593"""""""""
1594
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001595let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001596function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001597 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001598endfunc
1599
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001600function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001601 let handle = ch_open('localhost:' . a:port, s:chopt)
1602 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001603 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001604 return
1605 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001606 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001607
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001608 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001609 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001610endfunc
1611
1612func Test_close_callback()
1613 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001614 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001615endfunc
1616
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001617function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001618 let handle = ch_open('localhost:' . a:port, s:chopt)
1619 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001620 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001621 return
1622 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001623 let g:Ch_d = {}
1624 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001625 let self.close_ret = 'closed'
1626 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001627 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001628
1629 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001630 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001631 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001632endfunc
1633
1634func Test_close_partial()
1635 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001636 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001637endfunc
1638
Bram Moolenaar80385682016-03-27 19:13:35 +02001639func Test_job_start_invalid()
1640 call assert_fails('call job_start($x)', 'E474:')
1641 call assert_fails('call job_start("")', 'E474:')
1642endfunc
1643
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001644func Test_job_stop_immediately()
1645 if !has('job')
1646 return
1647 endif
1648
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001649 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001650 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001651 call job_stop(g:job)
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001652 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001653 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001654 call job_stop(g:job, 'kill')
1655 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001656 endtry
1657endfunc
1658
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001659" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001660func Test_partial_in_channel_cycle()
1661 let d = {}
1662 let d.a = function('string', [d])
1663 try
1664 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1665 catch
1666 call assert_exception('E901:')
1667 endtry
1668 unlet d
1669endfunc
1670
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001671func Test_using_freed_memory()
1672 let g:a = job_start(['ls'])
1673 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001674 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001675endfunc
1676
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001677func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001678 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001679 return
1680 endif
1681 sp test_channel.vim
1682 let g:linecount = line('$')
1683 close
1684 split testout
1685 1,$delete
1686 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001687 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001688 bwipe!
1689endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001690
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001691func Test_write_to_deleted_buffer()
1692 if !executable('echo') || !has('job')
1693 return
1694 endif
1695 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001696 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001697 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001698 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1699 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001700
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001701 bdel test_buffer
1702 call assert_equal([], getbufline(bufnr, 1, '$'))
1703
1704 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001705 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001706 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1707 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1708
1709 bwipe! test_buffer
1710endfunc
1711
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001712func Test_cmd_parsing()
1713 if !has('unix')
1714 return
1715 endif
1716 call assert_false(filereadable("file with space"))
1717 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001718 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001719 call delete("file with space")
1720
1721 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001722 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001723 call delete("file with space")
1724endfunc
1725
Bram Moolenaar82117982016-08-27 19:21:48 +02001726func Test_raw_passes_nul()
1727 if !executable('cat') || !has('job')
1728 return
1729 endif
1730
1731 " Test lines from the job containing NUL are stored correctly in a buffer.
1732 new
1733 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1734 w! Xtestread
1735 bwipe!
1736 split testout
1737 1,$delete
1738 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1739 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001740 call assert_equal("asdf\nasdf", getline(1))
1741 call assert_equal("xxx\n", getline(2))
1742 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001743
1744 call delete('Xtestread')
1745 bwipe!
1746
1747 " Test lines from a buffer with NUL bytes are written correctly to the job.
1748 new mybuffer
1749 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1750 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 +02001751 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001752 bwipe!
1753 split Xtestwrite
1754 call assert_equal("asdf\nasdf", getline(1))
1755 call assert_equal("xxx\n", getline(2))
1756 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001757 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001758
1759 call delete('Xtestwrite')
1760 bwipe!
1761endfunc
1762
Bram Moolenaarec68a992016-10-03 21:37:41 +02001763func Test_read_nonl_line()
1764 if !has('job')
1765 return
1766 endif
1767
1768 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001769 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001770 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001771 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001772 unlet g:linecount
1773endfunc
1774
1775func Test_read_nonl_in_close_cb()
1776 if !has('job')
1777 return
1778 endif
1779
1780 func s:close_cb(ch)
1781 while ch_status(a:ch) == 'buffered'
1782 let g:out .= ch_read(a:ch)
1783 endwhile
1784 endfunc
1785
1786 let g:out = ''
1787 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1788 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1789 call WaitForAssert({-> assert_equal('123', g:out)})
1790 unlet g:out
1791 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001792endfunc
1793
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001794func Test_read_from_terminated_job()
1795 if !has('job')
1796 return
1797 endif
1798
1799 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001800 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001801 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001802 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001803 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001804endfunc
1805
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001806func Test_job_start_windows()
1807 if !has('job') || !has('win32')
1808 return
1809 endif
1810
1811 " Check that backslash in $COMSPEC is handled properly.
1812 let g:echostr = ''
1813 let cmd = $COMSPEC . ' /c echo 123'
1814 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1815 let info = job_info(job)
1816 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1817
1818 call WaitForAssert({-> assert_equal("123", g:echostr)})
1819 unlet g:echostr
1820endfunc
1821
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001822func Test_env()
1823 if !has('job')
1824 return
1825 endif
1826
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001827 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001828 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001829 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001830 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001831 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001832 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001833 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1834 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001835 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001836 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001837endfunc
1838
1839func Test_cwd()
1840 if !has('job')
1841 return
1842 endif
1843
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001844 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001845 if has('win32')
1846 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001847 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001848 else
1849 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001850 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001851 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001852 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001853 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001854 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001855 let expect = substitute(expect, '[/\\]$', '', '')
1856 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1857 if $CI != '' && stridx(g:envstr, '/private/') == 0
1858 let g:envstr = g:envstr[8:]
1859 endif
1860 call assert_equal(expect, g:envstr)
1861 finally
1862 call job_stop(job)
1863 unlet g:envstr
1864 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001865endfunc
1866
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001867function Ch_test_close_lambda(port)
1868 let handle = ch_open('localhost:' . a:port, s:chopt)
1869 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001870 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001871 return
1872 endif
1873 let g:Ch_close_ret = ''
1874 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1875
1876 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001877 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001878endfunc
1879
1880func Test_close_lambda()
1881 call ch_log('Test_close_lambda()')
1882 call s:run_server('Ch_test_close_lambda')
1883endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001884
1885func s:test_list_args(cmd, out, remove_lf)
1886 try
1887 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001888 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 +02001889 call WaitFor('"" != g:out')
1890 if has('win32')
1891 let g:out = substitute(g:out, '\r', '', 'g')
1892 endif
1893 if a:remove_lf
1894 let g:out = substitute(g:out, '\n$', '', 'g')
1895 endif
1896 call assert_equal(a:out, g:out)
1897 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001898 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001899 unlet g:out
1900 endtry
1901endfunc
1902
1903func Test_list_args()
1904 if !has('job')
1905 return
1906 endif
1907
1908 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1909 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1910 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1911 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1912 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1913 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1914 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1915 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1916 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1917 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1918 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1919 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1920 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1921
1922 " tests which not contain spaces in the argument
1923 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1924 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1925 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1926 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1927 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1928 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1929 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1930 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1931 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1932endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001933
1934" Do this last, it stops any channel log.
1935func Test_zz_ch_log()
1936 call ch_logfile('Xlog', 'w')
1937 call ch_log('hello there')
1938 call ch_log('%s%s')
1939 call ch_logfile('')
1940 let text = readfile('Xlog')
1941 call assert_match("hello there", text[1])
1942 call assert_match("%s%s", text[2])
1943 call delete('Xlog')
1944endfunc
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001945
1946func Test_keep_pty_open()
1947 if !has('unix')
1948 return
1949 endif
1950
Bram Moolenaare2956092019-01-25 21:01:17 +01001951 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1952 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001953 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1954 call assert_inrange(200, 1000, elapsed)
1955 call job_stop(job)
1956endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001957
1958func Test_job_start_in_timer()
1959 if !has('job') || !has('timers')
1960 return
1961 endif
1962
1963 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001964 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001965 endfunc
1966
1967 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001968 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001969 call Resume()
1970 endfunc
1971
1972 func TimerCb(timer)
1973 if has('win32')
1974 let cmd = ['cmd', '/c', 'echo.']
1975 else
1976 let cmd = ['echo']
1977 endif
1978 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1979 call substitute(repeat('a', 100000), '.', '', 'g')
1980 endfunc
1981
1982 " We should be interrupted before 'updatetime' elapsed.
1983 let g:val = 0
1984 call timer_start(1, 'TimerCb')
1985 let elapsed = Standby(&ut)
1986 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001987
1988 " Wait for both OutCb() and ExitCb() to have been called before deleting
1989 " them.
1990 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001991 call job_stop(g:job)
1992
1993 delfunc OutCb
1994 delfunc ExitCb
1995 delfunc TimerCb
1996 unlet! g:val
1997 unlet! g:job
1998endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001999
2000func Test_raw_large_data()
2001 try
2002 let g:out = ''
2003 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002004 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2005 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002006
Bram Moolenaare2956092019-01-25 21:01:17 +01002007 let outlen = 79999
2008 let want = repeat('X', outlen) . "\n"
Bram Moolenaar24058382019-01-24 23:11:49 +01002009 call ch_sendraw(job, want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002010 call WaitFor({-> len(g:out) >= outlen}, 10000)
2011 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002012 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2013 finally
2014 call job_stop(job)
2015 unlet g:out
2016 endtry
2017endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002018
Bram Moolenaar65240682019-02-10 22:23:26 +01002019func Test_no_hang_windows()
2020 if !has('job') || !has('win32')
2021 return
2022 endif
2023
2024 try
2025 let job = job_start(s:python . " test_channel_pipe.py busy",
2026 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2027 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2028 finally
2029 call job_stop(job)
2030 endtry
2031endfunc
2032
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002033func Test_job_exitval_and_termsig()
2034 if !has('unix')
2035 return
2036 endif
2037
2038 " Terminate job normally
2039 let cmd = ['echo']
2040 let job = job_start(cmd)
2041 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2042 let info = job_info(job)
2043 call assert_equal(0, info.exitval)
2044 call assert_equal("", info.termsig)
2045
2046 " Terminate job by signal
2047 let cmd = ['sleep', '10']
2048 let job = job_start(cmd)
2049 sleep 10m
2050 call job_stop(job)
2051 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2052 let info = job_info(job)
2053 call assert_equal(-1, info.exitval)
2054 call assert_equal("term", info.termsig)
2055endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002056
2057func Test_job_tty_in_out()
2058 if !has('job') || !has('unix')
2059 return
2060 endif
2061
2062 call writefile(['test'], 'Xtestin')
2063 let in_opts = [{},
2064 \ {'in_io': 'null'},
2065 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2066 let out_opts = [{},
2067 \ {'out_io': 'null'},
2068 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2069 let err_opts = [{},
2070 \ {'err_io': 'null'},
2071 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2072 \ {'err_io': 'out'}]
2073 let opts = []
2074
2075 for in_opt in in_opts
2076 let x = copy(in_opt)
2077 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002078 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002079 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002080 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002081 let opts += [extend({'pty': 1}, x)]
2082 endfor
2083 endfor
2084 endfor
2085
2086 for opt in opts
2087 let job = job_start('echo', opt)
2088 let info = job_info(job)
2089 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2090 \ get(opt, 'in_io', 'tty'),
2091 \ get(opt, 'out_io', 'tty'),
2092 \ get(opt, 'err_io', 'tty'))
2093
2094 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2095 call assert_notequal('', info.tty_in, msg)
2096 else
2097 call assert_equal('', info.tty_in, msg)
2098 endif
2099 call assert_equal(info.tty_in, info.tty_out, msg)
2100
2101 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2102 endfor
2103
2104 call delete('Xtestin')
2105 call delete('Xtestout')
2106 call delete('Xtesterr')
2107endfunc