blob: f81701a076026221f31249389af131910daaf3e6 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaare2469252016-02-04 10:54:34 +01003if !has('channel')
4 finish
5endif
6
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
8
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010011 " Can't run this test without Python.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaar37175402017-03-18 20:18:45 +010015" Uncomment the next line to see what happens. Output is in
16" src/testdir/channellog.
17" call ch_logfile('channellog', 'w')
18
Bram Moolenaare74e8e72016-02-16 22:01:30 +010019let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010020
Bram Moolenaard6a8d482016-02-10 20:32:20 +010021" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010022func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020023 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010024endfunc
25
Bram Moolenaar819524702018-02-27 19:10:00 +010026" Return a list of open files.
27" Can be used to make sure no resources leaked.
28" Returns an empty list on systems where this is not supported.
29func s:get_resources()
30 let pid = getpid()
31
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)
206 call assert_true(reltimefloat(elapsed) > 0.3)
207 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100208
209 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100210 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100211 let resp = ch_read(handle)
212 call assert_equal(type([]), type(resp))
213 call assert_equal(type(11), type(resp[0]))
214 call assert_equal('waited', resp[1])
215
Bram Moolenaard7ece102016-02-02 23:23:02 +0100216 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100217 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100218endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100219
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100221 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200222 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100223endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100224
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100225" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200226func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100227 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200228 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100229 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100230 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100231 return
232 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100233
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100234 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100235
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100237 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100238 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239 return
240 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100241 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
242 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100243
244 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100245 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100246
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100247 call ch_close(newhandle)
248endfunc
249
250func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100251 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200252 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100253endfunc
254
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100255" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200256func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100257 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100259 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100260 return
261 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100262
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100263 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100264
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100265 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100266endfunc
267
268func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100269 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200270 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100271endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100272
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100273"""""""""
274
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200275func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100276 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200277 unlet g:Ch_reply
278 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100279endfunc
280
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200281func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100282 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100283 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100284 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100285 return
286 endif
287
288 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100289 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200290 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100291
292 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100293 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200294 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100295endfunc
296
297func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100298 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200299 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200300 let s:chopt.callback = 'Ch_handler'
301 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200302 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200303 let s:chopt.callback = function('Ch_handler')
304 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100305 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100306endfunc
307
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100308"""""""""
309
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310let g:Ch_reply = ''
311func Ch_zeroHandler(chan, msg)
312 unlet g:Ch_reply
313 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100314endfunc
315
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200316let g:Ch_zero_reply = ''
317func Ch_oneHandler(chan, msg)
318 unlet g:Ch_zero_reply
319 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320endfunc
321
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 let handle = ch_open('localhost:' . a:port, s:chopt)
324 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100325 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 return
327 endif
328
329 " Check that eval works.
330 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
331
332 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100334 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200336 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100338 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 endif
341
342 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200343 let g:Ch_reply = ''
344 let g:Ch_zero_reply = ''
345 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200346 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100349 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200350 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100351 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100352endfunc
353
354func Test_zero_reply()
355 call ch_log('Test_zero_reply()')
356 " Run with channel handler
357 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200358 let s:chopt.callback = 'Ch_zeroHandler'
359 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100360 unlet s:chopt.callback
361
362 " Run without channel handler
363 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200364 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100365endfunc
366
367"""""""""
368
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200369let g:Ch_reply1 = ""
370func Ch_handleRaw1(chan, msg)
371 unlet g:Ch_reply1
372 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100373endfunc
374
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200375let g:Ch_reply2 = ""
376func Ch_handleRaw2(chan, msg)
377 unlet g:Ch_reply2
378 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100379endfunc
380
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200381let g:Ch_reply3 = ""
382func Ch_handleRaw3(chan, msg)
383 unlet g:Ch_reply3
384 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100385endfunc
386
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200387func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100388 let handle = ch_open('localhost:' . a:port, s:chopt)
389 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100390 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100391 return
392 endif
393 call ch_setoptions(handle, {'mode': 'raw'})
394
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100395 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200396 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200397 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200398 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
399 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200400 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100401 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200402 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100403endfunc
404
405func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100406 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200407 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100408endfunc
409
410"""""""""
411
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100412" Test that trying to connect to a non-existing port fails quickly.
413func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100414 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100415 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100416 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100417 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100418 " Oops, port does exists.
419 call ch_close(handle)
420 else
421 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100422 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100423 endif
424
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100425 " We intend to use a socket that doesn't exist and wait for half a second
426 " before giving up. If the socket does exist it can fail in various ways.
427 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100429 try
430 let handle = ch_open('localhost:9867', {'waittime': 500})
431 if ch_status(handle) != "fail"
432 " Oops, port does exists.
433 call ch_close(handle)
434 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100435 " Failed connection should wait about 500 msec. Can be longer if the
436 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 let elapsed = reltime(start)
438 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100439 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100440 endif
441 catch
442 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100443 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100444 endif
445 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100446endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100447
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100448"""""""""
449
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100450func Test_raw_pipe()
451 if !has('job')
452 return
453 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100454 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100455 " Add a dummy close callback to avoid that messages are dropped when calling
456 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200457 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100458 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200459 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200460 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100461 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200462
463 call assert_equal("open", ch_status(job))
464 call assert_equal("open", ch_status(job), {"part": "out"})
465 call assert_equal("open", ch_status(job), {"part": "err"})
466 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
467 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
468
469 let dict = ch_info(job)
470 call assert_true(dict.id != 0)
471 call assert_equal('open', dict.status)
472 call assert_equal('open', dict.out_status)
473 call assert_equal('RAW', dict.out_mode)
474 call assert_equal('pipe', dict.out_io)
475 call assert_equal('open', dict.err_status)
476 call assert_equal('RAW', dict.err_mode)
477 call assert_equal('pipe', dict.err_io)
478
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100479 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100480 " For a change use the job where a channel is expected.
481 call ch_sendraw(job, "echo something\n")
482 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100483 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
484
Bram Moolenaar151f6562016-03-07 21:19:38 +0100485 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100486 let g:handle = job_getchannel(job)
487 call WaitFor('ch_canread(g:handle)')
488 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
491
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200492 let g:Ch_reply = ""
493 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200494 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200495
Bram Moolenaar151f6562016-03-07 21:19:38 +0100496 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100497 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
498 finally
499 call job_stop(job)
500 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100501
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200502 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200503 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar8950a562016-03-12 15:22:55 +0100504 let info = job_info(job)
505 call assert_equal("dead", info.status)
506 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200507 call assert_equal(2, len(info.cmd))
508 call assert_equal("test_channel_pipe.py", info.cmd[1])
509
510 let found = 0
511 for j in job_info()
512 if j == job
513 let found += 1
514 endif
515 endfor
516 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100517endfunc
518
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100519func Test_raw_pipe_blob()
520 if !has('job')
521 return
522 endif
523 call ch_log('Test_raw_pipe_blob()')
524 " Add a dummy close callback to avoid that messages are dropped when calling
525 " ch_canread().
526 " Also test the non-blocking option.
527 let job = job_start(s:python . " test_channel_pipe.py",
528 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
529 call assert_equal(v:t_job, type(job))
530 call assert_equal("run", job_status(job))
531
532 call assert_equal("open", ch_status(job))
533 call assert_equal("open", ch_status(job), {"part": "out"})
534
535 try
536 " Create a blob with the echo command and write it.
537 let blob = 0z00
538 let cmd = "echo something\n"
539 for i in range(0, len(cmd) - 1)
540 let blob[i] = char2nr(cmd[i])
541 endfor
542 call assert_equal(len(cmd), len(blob))
543 call ch_sendraw(job, blob)
544
545 " Read a blob with the reply.
546 let msg = ch_readblob(job)
547 let expected = 'something'
548 for i in range(0, len(expected) - 1)
549 call assert_equal(char2nr(expected[i]), msg[i])
550 endfor
551
552 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
553 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
554 finally
555 call job_stop(job)
556 endtry
557
558 let g:Ch_job = job
559 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
560 let info = job_info(job)
561 call assert_equal("dead", info.status)
562endfunc
563
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100564func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100565 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100566 return
567 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100568 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100569 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100570 call assert_equal("run", job_status(job))
571 try
572 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100573 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100574 call assert_equal("something", ch_readraw(handle))
575
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100576 call ch_sendraw(handle, "echoerr wrong\n")
577 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
578
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100579 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100580 call assert_equal("this", ch_readraw(handle))
581 call assert_equal("AND this", ch_readraw(handle))
582
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200583 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar620ca2d2017-12-09 19:13:13 +0100584 call assert_equal("this linethis linethis line", ch_read(handle))
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200585
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100586 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100587 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100588 finally
589 call job_stop(job)
590 endtry
591endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100592
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100593func Test_nl_err_to_out_pipe()
594 if !has('job')
595 return
596 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100597 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100598 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100599 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100600 call assert_equal("run", job_status(job))
601 try
602 let handle = job_getchannel(job)
603 call ch_sendraw(handle, "echo something\n")
604 call assert_equal("something", ch_readraw(handle))
605
606 call ch_sendraw(handle, "echoerr wrong\n")
607 call assert_equal("wrong", ch_readraw(handle))
608 finally
609 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100610 call ch_logfile('')
611 let loglines = readfile('Xlog')
612 call assert_true(len(loglines) > 10)
613 let found_test = 0
614 let found_send = 0
615 let found_recv = 0
616 let found_stop = 0
617 for l in loglines
618 if l =~ 'Test_nl_err_to_out_pipe'
619 let found_test = 1
620 endif
621 if l =~ 'SEND on.*echo something'
622 let found_send = 1
623 endif
624 if l =~ 'RECV on.*something'
625 let found_recv = 1
626 endif
627 if l =~ 'Stopping job with'
628 let found_stop = 1
629 endif
630 endfor
631 call assert_equal(1, found_test)
632 call assert_equal(1, found_send)
633 call assert_equal(1, found_recv)
634 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200635 " On MS-Windows need to sleep for a moment to be able to delete the file.
636 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100637 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100638 endtry
639endfunc
640
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200641func Stop_g_job()
642 call job_stop(g:job)
643 if has('win32')
644 " On MS-Windows the server must close the file handle before we are able
645 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200646 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 sleep 10m
648 endif
649endfunc
650
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100651func Test_nl_read_file()
652 if !has('job')
653 return
654 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100655 call ch_log('Test_nl_read_file()')
656 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200657 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100658 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100660 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200661 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100662 call assert_equal("something", ch_readraw(handle))
663 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
664 call assert_equal("this", ch_readraw(handle))
665 call assert_equal("AND this", ch_readraw(handle))
666 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200667 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100668 call delete('Xinput')
669 endtry
670endfunc
671
Bram Moolenaare98d1212016-03-08 15:37:41 +0100672func Test_nl_write_out_file()
673 if !has('job')
674 return
675 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100676 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200677 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100678 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200679 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100680 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200681 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100682 call ch_sendraw(handle, "echo line one\n")
683 call ch_sendraw(handle, "echo line two\n")
684 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200685 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100686 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200687 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100688 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100689 call delete('Xoutput')
690 endtry
691endfunc
692
693func Test_nl_write_err_file()
694 if !has('job')
695 return
696 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100697 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200698 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100699 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200700 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100701 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200702 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100703 call ch_sendraw(handle, "echoerr line one\n")
704 call ch_sendraw(handle, "echoerr line two\n")
705 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200706 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100707 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200708 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100709 call delete('Xoutput')
710 endtry
711endfunc
712
713func Test_nl_write_both_file()
714 if !has('job')
715 return
716 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100717 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200718 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100719 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200720 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100721 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200722 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100723 call ch_sendraw(handle, "echoerr line one\n")
724 call ch_sendraw(handle, "echo line two\n")
725 call ch_sendraw(handle, "double this\n")
726 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200727 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100728 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200729 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100730 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100731 call delete('Xoutput')
732 endtry
733endfunc
734
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200735func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200736 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200737endfunc
738
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200739func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100740 if !has('job')
741 return
742 endif
743 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200744 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200745 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200746 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100747 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100748 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200749 if a:do_msg
750 let expected[0] = 'Reading from channel output...'
751 else
752 let options['out_msg'] = 0
753 call remove(expected, 0)
754 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100755 else
756 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100757 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100758 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200759 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100760 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200761 if a:nomod
762 let options['out_modifiable'] = 0
763 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100764 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100765 call assert_equal("run", job_status(job))
766 try
767 let handle = job_getchannel(job)
768 call ch_sendraw(handle, "echo line one\n")
769 call ch_sendraw(handle, "echo line two\n")
770 call ch_sendraw(handle, "double this\n")
771 call ch_sendraw(handle, "quit\n")
772 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100773 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200774 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200775 if a:nomod
776 call assert_equal(0, &modifiable)
777 else
778 call assert_equal(1, &modifiable)
779 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200780 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100781 bwipe!
782 finally
783 call job_stop(job)
784 endtry
785endfunc
786
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100787func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200788 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100789endfunc
790
791func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200792 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100793endfunc
794
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200795func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200796 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200797endfunc
798
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200799func Test_pipe_to_buffer_name_nomsg()
800 call Run_test_pipe_to_buffer(1, 0, 1)
801endfunc
802
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200803func Test_close_output_buffer()
804 if !has('job')
805 return
806 endif
807 enew!
808 let test_lines = ['one', 'two']
809 call setline(1, test_lines)
810 call ch_log('Test_close_output_buffer()')
811 let options = {'out_io': 'buffer'}
812 let options['out_name'] = 'buffer-output'
813 let options['out_msg'] = 0
814 split buffer-output
815 let job = job_start(s:python . " test_channel_write.py", options)
816 call assert_equal("run", job_status(job))
817 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200818 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200819 quit!
820 sleep 100m
821 " Make sure the write didn't happen to the wrong buffer.
822 call assert_equal(test_lines, getline(1, line('$')))
823 call assert_equal(-1, bufwinnr('buffer-output'))
824 sbuf buffer-output
825 call assert_notequal(-1, bufwinnr('buffer-output'))
826 sleep 100m
827 close " no more writes
828 bwipe!
829 finally
830 call job_stop(job)
831 endtry
832endfunc
833
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200834func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100835 if !has('job')
836 return
837 endif
838 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100839 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200840 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100841 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100842 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200843 if a:do_msg
844 let expected[0] = 'Reading from channel error...'
845 else
846 let options['err_msg'] = 0
847 call remove(expected, 0)
848 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100849 else
850 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100851 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100852 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200853 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100854 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200855 if a:nomod
856 let options['err_modifiable'] = 0
857 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100858 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100859 call assert_equal("run", job_status(job))
860 try
861 let handle = job_getchannel(job)
862 call ch_sendraw(handle, "echoerr line one\n")
863 call ch_sendraw(handle, "echoerr line two\n")
864 call ch_sendraw(handle, "doubleerr this\n")
865 call ch_sendraw(handle, "quit\n")
866 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200867 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200868 if a:nomod
869 call assert_equal(0, &modifiable)
870 else
871 call assert_equal(1, &modifiable)
872 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100873 bwipe!
874 finally
875 call job_stop(job)
876 endtry
877endfunc
878
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100879func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200880 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100881endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100882
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100883func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200884 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200885endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100886
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200887func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200888 call Run_test_pipe_err_to_buffer(1, 1, 1)
889endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100890
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200891func Test_pipe_err_to_buffer_name_nomsg()
892 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100893endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100894
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100895func Test_pipe_both_to_buffer()
896 if !has('job')
897 return
898 endif
899 call ch_log('Test_pipe_both_to_buffer()')
900 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100901 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100902 call assert_equal("run", job_status(job))
903 try
904 let handle = job_getchannel(job)
905 call ch_sendraw(handle, "echo line one\n")
906 call ch_sendraw(handle, "echoerr line two\n")
907 call ch_sendraw(handle, "double this\n")
908 call ch_sendraw(handle, "doubleerr that\n")
909 call ch_sendraw(handle, "quit\n")
910 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200911 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 +0100912 bwipe!
913 finally
914 call job_stop(job)
915 endtry
916endfunc
917
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100918func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100919 if !has('job')
920 return
921 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100922 call ch_log('Test_pipe_from_buffer()')
923
924 sp pipe-input
925 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200926 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100927 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100928 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100929 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100930 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100931 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100932
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100933 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100934 call assert_equal("run", job_status(job))
935 try
936 let handle = job_getchannel(job)
937 call assert_equal('one', ch_read(handle))
938 call assert_equal('two', ch_read(handle))
939 call assert_equal('three', ch_read(handle))
940 bwipe!
941 finally
942 call job_stop(job)
943 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100944endfunc
945
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100946func Test_pipe_from_buffer_name()
947 call Run_test_pipe_from_buffer(1)
948endfunc
949
950func Test_pipe_from_buffer_nr()
951 call Run_test_pipe_from_buffer(0)
952endfunc
953
Bram Moolenaar0874a832016-09-01 15:11:51 +0200954func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200955 if !executable('sort') || !has('job')
956 return
957 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200958 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
959 if a:use_buffer
960 split sortin
961 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
962 let options.in_io = 'buffer'
963 let options.in_name = 'sortin'
964 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200965 if !a:all
966 let options.in_top = 2
967 let options.in_bot = 4
968 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100969 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200970
971 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100972 call assert_equal("run", job_status(job))
973 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
974 call ch_close_in(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200975 endif
976
Bram Moolenaare2956092019-01-25 21:01:17 +0100977 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200978
Bram Moolenaard8b55492016-09-01 14:35:22 +0200979 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200980 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200981 call assert_equal('Reading from channel output...', getline(1))
982 if a:all
983 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
984 else
985 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
986 endif
987
Bram Moolenaare2956092019-01-25 21:01:17 +0100988 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200989 if a:use_buffer
990 bwipe! sortin
991 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200992 bwipe! sortout
993endfunc
994
995func Test_pipe_through_sort_all()
996 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200997 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200998endfunc
999
1000func Test_pipe_through_sort_some()
1001 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +02001002 call Run_pipe_through_sort(0, 1)
1003endfunc
1004
1005func Test_pipe_through_sort_feed()
1006 call ch_log('Test_pipe_through_sort_feed()')
1007 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001008endfunc
1009
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001010func Test_pipe_to_nameless_buffer()
1011 if !has('job')
1012 return
1013 endif
1014 call ch_log('Test_pipe_to_nameless_buffer()')
1015 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001016 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001017 call assert_equal("run", job_status(job))
1018 try
1019 let handle = job_getchannel(job)
1020 call ch_sendraw(handle, "echo line one\n")
1021 call ch_sendraw(handle, "echo line two\n")
1022 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001023 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001024 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
1025 bwipe!
1026 finally
1027 call job_stop(job)
1028 endtry
1029endfunc
1030
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001031func Test_pipe_to_buffer_json()
1032 if !has('job')
1033 return
1034 endif
1035 call ch_log('Test_pipe_to_buffer_json()')
1036 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001037 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001038 call assert_equal("run", job_status(job))
1039 try
1040 let handle = job_getchannel(job)
1041 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1042 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1043 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001044 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001045 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1046 bwipe!
1047 finally
1048 call job_stop(job)
1049 endtry
1050endfunc
1051
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001052" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001053func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001054 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001055 if getline(line('$') - a:offset) == a:line
1056 break
1057 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001058 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001059 endfor
1060endfunc
1061
1062func Test_pipe_io_two_buffers()
1063 if !has('job')
1064 return
1065 endif
1066 call ch_log('Test_pipe_io_two_buffers()')
1067
1068 " Create two buffers, one to read from and one to write to.
1069 split pipe-output
1070 set buftype=nofile
1071 split pipe-input
1072 set buftype=nofile
1073
1074 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001075 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001076 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1077 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001078 call assert_equal("run", job_status(job))
1079 try
1080 exe "normal Gaecho hello\<CR>"
1081 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001082 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001083 call assert_equal('hello', getline('$'))
1084
1085 exe bufwinnr('pipe-input') . "wincmd w"
1086 exe "normal Gadouble this\<CR>"
1087 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001088 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001089 call assert_equal('this', getline(line('$') - 1))
1090 call assert_equal('AND this', getline('$'))
1091
1092 bwipe!
1093 exe bufwinnr('pipe-input') . "wincmd w"
1094 bwipe!
1095 finally
1096 call job_stop(job)
1097 endtry
1098endfunc
1099
1100func Test_pipe_io_one_buffer()
1101 if !has('job')
1102 return
1103 endif
1104 call ch_log('Test_pipe_io_one_buffer()')
1105
1106 " Create one buffer to read from and to write to.
1107 split pipe-io
1108 set buftype=nofile
1109
1110 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001111 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001112 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1113 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001114 call assert_equal("run", job_status(job))
1115 try
1116 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001117 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001118 call assert_equal('hello', getline(line('$') - 1))
1119
1120 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001121 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001122 call assert_equal('this', getline(line('$') - 2))
1123 call assert_equal('AND this', getline(line('$') - 1))
1124
1125 bwipe!
1126 finally
1127 call job_stop(job)
1128 endtry
1129endfunc
1130
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001131func Test_pipe_null()
1132 if !has('job')
1133 return
1134 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001135 call ch_log('Test_pipe_null()')
1136
1137 " We cannot check that no I/O works, we only check that the job starts
1138 " properly.
1139 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001140 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001141 call assert_equal("run", job_status(job))
1142 try
1143 call assert_equal('something', ch_read(job))
1144 finally
1145 call job_stop(job)
1146 endtry
1147
1148 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001149 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001150 call assert_equal("run", job_status(job))
1151 try
1152 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1153 finally
1154 call job_stop(job)
1155 endtry
1156
1157 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001158 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001159 call assert_equal("run", job_status(job))
1160 try
1161 call assert_equal('something', ch_read(job))
1162 finally
1163 call job_stop(job)
1164 endtry
1165
1166 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001167 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001168 call assert_equal("run", job_status(job))
1169 call job_stop(job)
1170
1171 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001172 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001173 call assert_equal("run", job_status(job))
1174 call assert_equal('channel fail', string(job_getchannel(job)))
1175 call assert_equal('fail', ch_status(job))
1176 call job_stop(job)
1177endfunc
1178
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001179func Test_pipe_to_buffer_raw()
1180 if !has('job')
1181 return
1182 endif
1183 call ch_log('Test_raw_pipe_to_buffer()')
1184 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1185 split testout
1186 let job = job_start([s:python, '-c',
1187 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001188 " the job may be done quickly, also accept "dead"
1189 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001190 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001191 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001192 let totlen = 0
1193 for line in getline(1, '$')
1194 call assert_equal('', substitute(line, '^\.*', '', ''))
1195 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001196 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001197 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001198 finally
1199 call job_stop(job)
1200 bwipe!
1201 endtry
1202endfunc
1203
Bram Moolenaarde279892016-03-11 22:19:44 +01001204func Test_reuse_channel()
1205 if !has('job')
1206 return
1207 endif
1208 call ch_log('Test_reuse_channel()')
1209
1210 let job = job_start(s:python . " test_channel_pipe.py")
1211 call assert_equal("run", job_status(job))
1212 let handle = job_getchannel(job)
1213 try
1214 call ch_sendraw(handle, "echo something\n")
1215 call assert_equal("something", ch_readraw(handle))
1216 finally
1217 call job_stop(job)
1218 endtry
1219
1220 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1221 call assert_equal("run", job_status(job))
1222 let handle = job_getchannel(job)
1223 try
1224 call ch_sendraw(handle, "echo again\n")
1225 call assert_equal("again", ch_readraw(handle))
1226 finally
1227 call job_stop(job)
1228 endtry
1229endfunc
1230
Bram Moolenaar75f72652016-03-20 22:16:56 +01001231func Test_out_cb()
1232 if !has('job')
1233 return
1234 endif
1235 call ch_log('Test_out_cb()')
1236
1237 let dict = {'thisis': 'dict: '}
1238 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001239 if type(a:msg) == v:t_string
1240 let g:Ch_outmsg = self.thisis . a:msg
1241 else
1242 let g:Ch_outobj = a:msg
1243 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001244 endfunc
1245 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001246 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001247 endfunc
1248 let job = job_start(s:python . " test_channel_pipe.py",
1249 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001250 \ 'out_mode': 'json',
1251 \ 'err_cb': dict.errHandler,
1252 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001253 call assert_equal("run", job_status(job))
1254 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001255 let g:Ch_outmsg = ''
1256 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001257 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1258 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001259 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1260 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001261
1262 " Receive a json object split in pieces
1263 unlet! g:Ch_outobj
1264 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001265 let g:Ch_outobj = ''
1266 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001267 finally
1268 call job_stop(job)
1269 endtry
1270endfunc
1271
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001272func Test_out_close_cb()
1273 if !has('job')
1274 return
1275 endif
1276 call ch_log('Test_out_close_cb()')
1277
1278 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001279 let g:Ch_msg1 = ''
1280 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001281 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001282 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001283 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001284 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001285 let s:counter += 1
1286 endfunc
1287 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001288 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001289 let s:counter += 1
1290 endfunc
1291 let job = job_start(s:python . " test_channel_pipe.py quit now",
1292 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001293 \ 'close_cb': 'CloseHandler'})
1294 " the job may be done quickly, also accept "dead"
1295 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001296 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001297 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1298 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001299 finally
1300 call job_stop(job)
1301 delfunc OutHandler
1302 delfunc CloseHandler
1303 endtry
1304endfunc
1305
Bram Moolenaar437905c2016-04-26 19:01:05 +02001306func Test_read_in_close_cb()
1307 if !has('job')
1308 return
1309 endif
1310 call ch_log('Test_read_in_close_cb()')
1311
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001312 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001313 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001314 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001315 endfunc
1316 let job = job_start(s:python . " test_channel_pipe.py quit now",
1317 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001318 " the job may be done quickly, also accept "dead"
1319 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001320 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001321 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001322 finally
1323 call job_stop(job)
1324 delfunc CloseHandler
1325 endtry
1326endfunc
1327
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001328" Use channel in NL mode but received text does not end in NL.
1329func Test_read_in_close_cb_incomplete()
1330 if !has('job')
1331 return
1332 endif
1333 call ch_log('Test_read_in_close_cb_incomplete()')
1334
1335 let g:Ch_received = ''
1336 func! CloseHandler(chan)
1337 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1338 let g:Ch_received .= ch_read(a:chan)
1339 endwhile
1340 endfunc
1341 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1342 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001343 " the job may be done quickly, also accept "dead"
1344 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001345 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001346 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001347 finally
1348 call job_stop(job)
1349 delfunc CloseHandler
1350 endtry
1351endfunc
1352
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001353func Test_out_cb_lambda()
1354 if !has('job')
1355 return
1356 endif
1357 call ch_log('Test_out_cb_lambda()')
1358
1359 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001360 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1361 \ 'out_mode': 'json',
1362 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1363 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001364 call assert_equal("run", job_status(job))
1365 try
1366 let g:Ch_outmsg = ''
1367 let g:Ch_errmsg = ''
1368 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1369 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001370 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1371 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001372 finally
1373 call job_stop(job)
1374 endtry
1375endfunc
1376
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001377func Test_close_and_exit_cb()
1378 if !has('job')
1379 return
1380 endif
1381 call ch_log('Test_close_and_exit_cb')
1382
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001383 let g:retdict = {'ret': {}}
1384 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001385 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1386 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001387 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001388 let self.ret['exit_cb'] = job_status(a:job)
1389 endfunc
1390
Bram Moolenaare2956092019-01-25 21:01:17 +01001391 let job = job_start([&shell, &shellcmdflag, 'echo'],
1392 \ {'close_cb': g:retdict.close_cb,
1393 \ 'exit_cb': g:retdict.exit_cb})
1394 " the job may be done quickly, also accept "dead"
1395 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001396 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001397 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001398 call assert_equal('dead', g:retdict.ret['exit_cb'])
1399 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001400endfunc
1401
Bram Moolenaard46ae142016-02-16 13:33:52 +01001402""""""""""
1403
Bram Moolenaar0b146882018-09-06 16:27:24 +02001404function ExitCbWipe(job, status)
1405 exe g:wipe_buf 'bw!'
1406endfunction
1407
1408" This caused a crash, because messages were handled while peeking for a
1409" character.
1410func Test_exit_cb_wipes_buf()
1411 if !has('timers')
1412 return
1413 endif
1414 set cursorline lazyredraw
1415 call test_override('redraw_flag', 1)
1416 new
1417 let g:wipe_buf = bufnr('')
1418
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001419 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001420 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001421 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1422 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1423 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1424 call timer_stop(timer)
1425
1426 set nocursorline nolazyredraw
1427 unlet g:wipe_buf
1428 call test_override('ALL', 0)
1429endfunc
1430
1431""""""""""
1432
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001433let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001434func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001435 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001436 unlet s:channelfd
1437endfunc
1438
1439" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001440func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001441 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001442 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001443 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001444endfunc
1445
1446func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001447 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001448 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001449endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001450
Bram Moolenaard46ae142016-02-16 13:33:52 +01001451""""""""""
1452
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001453let g:Ch_unletResponse = ''
1454func Ch_CloseHandler(handle, msg)
1455 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001456 call ch_close(s:channelfd)
1457endfunc
1458
1459" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001461 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001462 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001463 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001464endfunc
1465
1466func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001467 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001468 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001469endfunc
1470
1471""""""""""
1472
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001473func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001474 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001475 silent! let ch = ch_open("noserver")
1476 echo ch
1477 let d = ch
1478endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001479
1480""""""""""
1481
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001482func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001483 " Wait up to a second for the port to open.
1484 let s:chopt.waittime = 1000
1485 let channel = ch_open('localhost:' . a:port, s:chopt)
1486 unlet s:chopt.waittime
1487 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001488 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001489 return
1490 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001491 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001492 call ch_close(channel)
1493endfunc
1494
1495func Test_open_delay()
1496 call ch_log('Test_open_delay()')
1497 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001498 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001499endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001500
1501"""""""""
1502
1503function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001504 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001505endfunc
1506
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001507function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001508 let handle = ch_open('localhost:' . a:port, s:chopt)
1509 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001510 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001511 return
1512 endif
1513
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001514 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001515 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001516 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001517endfunc
1518
1519func Test_call()
1520 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001521 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001522endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001523
1524"""""""""
1525
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001526let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001527function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001528 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001529endfunc
1530
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001531function Ch_test_exit_callback(port)
1532 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1533 let g:Ch_exit_job = g:currentJob
1534 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001535endfunc
1536
1537func Test_exit_callback()
1538 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001539 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001540 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001541
Bram Moolenaar9730f742016-02-28 19:50:51 +01001542 " wait up to a second for the job to exit
1543 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001544 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001545 break
1546 endif
1547 sleep 10m
1548 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001549 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001550 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001551
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001552 call assert_equal('done', g:Ch_job_exit_ret)
1553 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1554 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001555 endif
1556endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001557
Bram Moolenaar97792de2016-10-15 18:36:49 +02001558function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001559 if job_info(a:job).process == g:exit_cb_val.process
1560 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1561 endif
1562 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001563endfunction
1564
1565func Test_exit_callback_interval()
1566 if !has('job')
1567 return
1568 endif
1569
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001570 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001571 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001572 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001573 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001574 let elapsed = reltimefloat(g:exit_cb_val.end)
1575 call assert_true(elapsed > 0.5)
1576 call assert_true(elapsed < 1.0)
1577
1578 " case: unreferenced job, using timer
1579 if !has('timers')
1580 return
1581 endif
1582
1583 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1584 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1585 let g:exit_cb_val.process = job_info(g:job).process
1586 unlet g:job
1587 call Standby(1000)
1588 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1589 let elapsed = reltimefloat(g:exit_cb_val.end)
1590 else
1591 let elapsed = 1.0
1592 endif
1593 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001594 call assert_true(elapsed < 1.0)
1595endfunc
1596
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001597"""""""""
1598
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001599let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001600function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001601 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001602endfunc
1603
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001604function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001605 let handle = ch_open('localhost:' . a:port, s:chopt)
1606 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001607 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001608 return
1609 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001610 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001611
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001612 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001613 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001614endfunc
1615
1616func Test_close_callback()
1617 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001618 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001619endfunc
1620
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001621function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001622 let handle = ch_open('localhost:' . a:port, s:chopt)
1623 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001624 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001625 return
1626 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001627 let g:Ch_d = {}
1628 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001629 let self.close_ret = 'closed'
1630 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001631 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001632
1633 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001634 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001635 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001636endfunc
1637
1638func Test_close_partial()
1639 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001640 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001641endfunc
1642
Bram Moolenaar80385682016-03-27 19:13:35 +02001643func Test_job_start_invalid()
1644 call assert_fails('call job_start($x)', 'E474:')
1645 call assert_fails('call job_start("")', 'E474:')
1646endfunc
1647
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001648func Test_job_stop_immediately()
1649 if !has('job')
1650 return
1651 endif
1652
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001653 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001654 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001655 call job_stop(g:job)
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001656 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001657 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001658 call job_stop(g:job, 'kill')
1659 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001660 endtry
1661endfunc
1662
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001663" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001664func Test_partial_in_channel_cycle()
1665 let d = {}
1666 let d.a = function('string', [d])
1667 try
1668 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1669 catch
1670 call assert_exception('E901:')
1671 endtry
1672 unlet d
1673endfunc
1674
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001675func Test_using_freed_memory()
1676 let g:a = job_start(['ls'])
1677 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001678 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001679endfunc
1680
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001681func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001682 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001683 return
1684 endif
1685 sp test_channel.vim
1686 let g:linecount = line('$')
1687 close
1688 split testout
1689 1,$delete
1690 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001691 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001692 bwipe!
1693endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001694
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001695func Test_write_to_deleted_buffer()
1696 if !executable('echo') || !has('job')
1697 return
1698 endif
1699 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001700 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001701 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001702 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1703 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001704
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001705 bdel test_buffer
1706 call assert_equal([], getbufline(bufnr, 1, '$'))
1707
1708 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001709 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001710 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1711 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1712
1713 bwipe! test_buffer
1714endfunc
1715
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001716func Test_cmd_parsing()
1717 if !has('unix')
1718 return
1719 endif
1720 call assert_false(filereadable("file with space"))
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")
1724
1725 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001726 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001727 call delete("file with space")
1728endfunc
1729
Bram Moolenaar82117982016-08-27 19:21:48 +02001730func Test_raw_passes_nul()
1731 if !executable('cat') || !has('job')
1732 return
1733 endif
1734
1735 " Test lines from the job containing NUL are stored correctly in a buffer.
1736 new
1737 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1738 w! Xtestread
1739 bwipe!
1740 split testout
1741 1,$delete
1742 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1743 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001744 call assert_equal("asdf\nasdf", getline(1))
1745 call assert_equal("xxx\n", getline(2))
1746 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001747
1748 call delete('Xtestread')
1749 bwipe!
1750
1751 " Test lines from a buffer with NUL bytes are written correctly to the job.
1752 new mybuffer
1753 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1754 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 +02001755 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001756 bwipe!
1757 split Xtestwrite
1758 call assert_equal("asdf\nasdf", getline(1))
1759 call assert_equal("xxx\n", getline(2))
1760 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001761 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001762
1763 call delete('Xtestwrite')
1764 bwipe!
1765endfunc
1766
Bram Moolenaarec68a992016-10-03 21:37:41 +02001767func MyLineCountCb(ch, msg)
1768 let g:linecount += 1
1769endfunc
1770
1771func Test_read_nonl_line()
1772 if !has('job')
1773 return
1774 endif
1775
1776 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001777 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001778 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001779 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaarec68a992016-10-03 21:37:41 +02001780endfunc
1781
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001782func Test_read_from_terminated_job()
1783 if !has('job')
1784 return
1785 endif
1786
1787 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001788 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001789 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001790 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001791endfunc
1792
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001793func Test_job_start_windows()
1794 if !has('job') || !has('win32')
1795 return
1796 endif
1797
1798 " Check that backslash in $COMSPEC is handled properly.
1799 let g:echostr = ''
1800 let cmd = $COMSPEC . ' /c echo 123'
1801 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1802 let info = job_info(job)
1803 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1804
1805 call WaitForAssert({-> assert_equal("123", g:echostr)})
1806 unlet g:echostr
1807endfunc
1808
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001809func Test_env()
1810 if !has('job')
1811 return
1812 endif
1813
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001814 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001815 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001816 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001817 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001818 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001819 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001820 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1821 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001822 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001823 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001824endfunc
1825
1826func Test_cwd()
1827 if !has('job')
1828 return
1829 endif
1830
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001831 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001832 if has('win32')
1833 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001834 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001835 else
1836 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001837 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001838 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001839 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001840 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001841 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001842 let expect = substitute(expect, '[/\\]$', '', '')
1843 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1844 if $CI != '' && stridx(g:envstr, '/private/') == 0
1845 let g:envstr = g:envstr[8:]
1846 endif
1847 call assert_equal(expect, g:envstr)
1848 finally
1849 call job_stop(job)
1850 unlet g:envstr
1851 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001852endfunc
1853
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001854function Ch_test_close_lambda(port)
1855 let handle = ch_open('localhost:' . a:port, s:chopt)
1856 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001857 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001858 return
1859 endif
1860 let g:Ch_close_ret = ''
1861 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1862
1863 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001864 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001865endfunc
1866
1867func Test_close_lambda()
1868 call ch_log('Test_close_lambda()')
1869 call s:run_server('Ch_test_close_lambda')
1870endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001871
1872func s:test_list_args(cmd, out, remove_lf)
1873 try
1874 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001875 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 +02001876 call WaitFor('"" != g:out')
1877 if has('win32')
1878 let g:out = substitute(g:out, '\r', '', 'g')
1879 endif
1880 if a:remove_lf
1881 let g:out = substitute(g:out, '\n$', '', 'g')
1882 endif
1883 call assert_equal(a:out, g:out)
1884 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001885 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001886 unlet g:out
1887 endtry
1888endfunc
1889
1890func Test_list_args()
1891 if !has('job')
1892 return
1893 endif
1894
1895 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1896 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1897 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1898 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1899 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1900 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1901 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1902 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1903 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1904 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1905 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1906 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1907 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1908
1909 " tests which not contain spaces in the argument
1910 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1911 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1912 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1913 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1914 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1915 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1916 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1917 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1918 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1919endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001920
1921" Do this last, it stops any channel log.
1922func Test_zz_ch_log()
1923 call ch_logfile('Xlog', 'w')
1924 call ch_log('hello there')
1925 call ch_log('%s%s')
1926 call ch_logfile('')
1927 let text = readfile('Xlog')
1928 call assert_match("hello there", text[1])
1929 call assert_match("%s%s", text[2])
1930 call delete('Xlog')
1931endfunc
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001932
1933func Test_keep_pty_open()
1934 if !has('unix')
1935 return
1936 endif
1937
Bram Moolenaare2956092019-01-25 21:01:17 +01001938 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1939 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001940 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1941 call assert_inrange(200, 1000, elapsed)
1942 call job_stop(job)
1943endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001944
1945func Test_job_start_in_timer()
1946 if !has('job') || !has('timers')
1947 return
1948 endif
1949
1950 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001951 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001952 endfunc
1953
1954 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001955 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001956 call Resume()
1957 endfunc
1958
1959 func TimerCb(timer)
1960 if has('win32')
1961 let cmd = ['cmd', '/c', 'echo.']
1962 else
1963 let cmd = ['echo']
1964 endif
1965 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1966 call substitute(repeat('a', 100000), '.', '', 'g')
1967 endfunc
1968
1969 " We should be interrupted before 'updatetime' elapsed.
1970 let g:val = 0
1971 call timer_start(1, 'TimerCb')
1972 let elapsed = Standby(&ut)
1973 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001974
1975 " Wait for both OutCb() and ExitCb() to have been called before deleting
1976 " them.
1977 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001978 call job_stop(g:job)
1979
1980 delfunc OutCb
1981 delfunc ExitCb
1982 delfunc TimerCb
1983 unlet! g:val
1984 unlet! g:job
1985endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001986
1987func Test_raw_large_data()
1988 try
1989 let g:out = ''
1990 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001991 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1992 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001993
Bram Moolenaare2956092019-01-25 21:01:17 +01001994 let outlen = 79999
1995 let want = repeat('X', outlen) . "\n"
Bram Moolenaar24058382019-01-24 23:11:49 +01001996 call ch_sendraw(job, want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001997 call WaitFor({-> len(g:out) >= outlen}, 10000)
1998 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001999 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2000 finally
2001 call job_stop(job)
2002 unlet g:out
2003 endtry
2004endfunc