blob: 5634b98c9fb71edf44d8a4d9b4e18c0016d86fc7 [file] [log] [blame]
Bram Moolenaarf386f082019-07-29 23:03:03 +02001" Test for channel and job functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaarf386f082019-07-29 23:03:03 +02003" When +channel is supported then +job is too, so we don't check for that.
Bram Moolenaar4641a122019-07-29 22:10:23 +02004source check.vim
5CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01006
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02008source screendump.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +02009
10let s:python = PythonProg()
11if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010012 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020013 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010014endif
15
Bram Moolenaar37175402017-03-18 20:18:45 +010016" Uncomment the next line to see what happens. Output is in
17" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020018" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010019" call ch_logfile('channellog', 'w')
20
Bram Moolenaare74e8e72016-02-16 22:01:30 +010021let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010022
Bram Moolenaard6a8d482016-02-10 20:32:20 +010023" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010024func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020025 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010026endfunc
27
Bram Moolenaar819524702018-02-27 19:10:00 +010028" Return a list of open files.
29" Can be used to make sure no resources leaked.
30" Returns an empty list on systems where this is not supported.
31func s:get_resources()
32 let pid = getpid()
33
Bram Moolenaar39536dd2019-01-29 22:58:21 +010034 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010035 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
36 elseif isdirectory('/proc/' . pid . '/fd/')
37 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
38 else
39 return []
40 endif
41endfunc
42
Bram Moolenaar321efdd2016-07-15 17:09:11 +020043let g:Ch_responseMsg = ''
44func Ch_requestHandler(handle, msg)
45 let g:Ch_responseHandle = a:handle
46 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010047endfunc
48
Bram Moolenaar321efdd2016-07-15 17:09:11 +020049func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010050 " Avoid dropping messages, since we don't use a callback here.
51 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020052 " Also add the noblock flag to try it out.
53 let s:chopt.noblock = 1
Bram Moolenaard6a8d482016-02-10 20:32:20 +010054 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010055 unlet s:chopt.drop
Bram Moolenaar0b146882018-09-06 16:27:24 +020056 unlet s:chopt.noblock
Bram Moolenaar77073442016-02-13 23:23:53 +010057 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010058 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010059 return
60 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020061
62 " check that getjob without a job is handled correctly
63 call assert_equal('no process', string(ch_getjob(handle)))
64
Bram Moolenaar570497a2019-08-22 22:55:13 +020065 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010066 call assert_true(dict.id != 0)
67 call assert_equal('open', dict.status)
68 call assert_equal(a:port, string(dict.port))
69 call assert_equal('open', dict.sock_status)
70 call assert_equal('socket', dict.sock_io)
71
Bram Moolenaard7ece102016-02-02 23:23:02 +010072 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010073 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010074
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010075 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010076 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
77 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
78 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
79
80 " split command should work
81 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020082 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010083 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010084
Bram Moolenaarf1f07922016-08-26 17:58:53 +020085 " string with ][ should work
86 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
87
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088 " nothing to read now
89 call assert_equal(0, ch_canread(handle))
90
Bram Moolenaarf1f07922016-08-26 17:58:53 +020091 " sending three messages quickly then reading should work
92 for i in range(3)
93 call ch_sendexpr(handle, 'echo hello ' . i)
94 endfor
95 call assert_equal('hello 0', ch_read(handle)[1])
96 call assert_equal('hello 1', ch_read(handle)[1])
97 call assert_equal('hello 2', ch_read(handle)[1])
98
Bram Moolenaard7ece102016-02-02 23:23:02 +010099 " Request that triggers sending two ex commands. These will usually be
100 " handled before getting the response, but it's not guaranteed, thus wait a
101 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100102 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200103 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100104 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100105
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100106 " Request command "foo bar", which fails silently.
107 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200108 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100109
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100110 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200111 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100112
Bram Moolenaara07fec92016-02-05 21:04:08 +0100113 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200114 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
115 call WaitFor('exists("g:Ch_responseHandle")')
116 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100117 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100118 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200119 call assert_equal(handle, g:Ch_responseHandle)
120 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100121 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200122 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100123
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200124 let g:Ch_responseMsg = ''
125 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
126 call WaitFor('exists("g:Ch_responseHandle")')
127 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100128 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100129 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200130 call assert_equal(handle, g:Ch_responseHandle)
131 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100132 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200133 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100134
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200135 " Using lambda.
136 let g:Ch_responseMsg = ''
137 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
138 call WaitFor('exists("g:Ch_responseHandle")')
139 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100140 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200141 else
142 call assert_equal(handle, g:Ch_responseHandle)
143 unlet g:Ch_responseHandle
144 endif
145 call assert_equal('got it', g:Ch_responseMsg)
146
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100147 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200148 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100149
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100150 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200151 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100152 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100153 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100154 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100155 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100156 call ch_setoptions(handle, {'drop': 'never'})
157 call ch_setoptions(handle, {'drop': 'auto'})
158 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100159
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100160 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100162 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100163 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100164
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100165 " Send an eval request with special characters.
166 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
167 sleep 10m
168 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
169
170 " Send an eval request to get a line with special characters.
171 call setline(3, "a\nb\<CR>c\x01d\x7fe")
172 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
173 sleep 10m
174 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
175
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100176 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100178 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100179 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100180
Bram Moolenaar55fab432016-02-07 16:53:13 +0100181 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100183 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100184 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100185
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100186 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100188 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100189 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100190
Bram Moolenaarf4160862016-02-05 23:09:12 +0100191 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100192 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200193 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194 call assert_equal('one', getline(line('$') - 2))
195 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100196
197 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
199 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100200
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100202
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100203 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100204 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
205 let start = reltime()
206 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
207 let elapsed = reltime(start)
Bram Moolenaar772153f2019-03-04 12:09:49 +0100208 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100209
210 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100211 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100212 let resp = ch_read(handle)
213 call assert_equal(type([]), type(resp))
214 call assert_equal(type(11), type(resp[0]))
215 call assert_equal('waited', resp[1])
216
Bram Moolenaard7ece102016-02-02 23:23:02 +0100217 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100220
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100222 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200223 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100224endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100225
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100226" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200227func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100228 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200229 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200230 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100231 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100232 return
233 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100234
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100235 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100236
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100237 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100238 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100239 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100240 return
241 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100242 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
243 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100244
245 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100246 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100247
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248 call ch_close(newhandle)
249endfunc
250
251func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200252 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200253 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100254endfunc
255
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100256" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200257func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100258 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100259 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100260 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100261 return
262 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100263
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100264 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100265
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100266 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100267endfunc
268
269func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100270 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200271 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100272endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100273
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100274"""""""""
275
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200276func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100277 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200278 unlet g:Ch_reply
279 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280endfunc
281
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200282func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100283 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100284 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100285 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100286 return
287 endif
288
289 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100290 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200291 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292
293 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100294 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200295 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296endfunc
297
298func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100299 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200300 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200301 let s:chopt.callback = 'Ch_handler'
302 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200303 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200304 let s:chopt.callback = function('Ch_handler')
305 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100306 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100307endfunc
308
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100309"""""""""
310
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200311let g:Ch_reply = ''
312func Ch_zeroHandler(chan, msg)
313 unlet g:Ch_reply
314 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100315endfunc
316
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200317let g:Ch_zero_reply = ''
318func Ch_oneHandler(chan, msg)
319 unlet g:Ch_zero_reply
320 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100321endfunc
322
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200323func Ch_channel_zero(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200324 let handle = ('localhost:' .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100325 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100326 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100327 return
328 endif
329
330 " Check that eval works.
331 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
332
333 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200334 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200337 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100338 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100339 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200340 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100341 endif
342
343 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200344 let g:Ch_reply = ''
345 let g:Ch_zero_reply = ''
346 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200347 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100348 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200349 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100352 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100353endfunc
354
355func Test_zero_reply()
356 call ch_log('Test_zero_reply()')
357 " Run with channel handler
358 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200359 let s:chopt.callback = 'Ch_zeroHandler'
360 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100361 unlet s:chopt.callback
362
363 " Run without channel handler
364 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200365 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100366endfunc
367
368"""""""""
369
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200370let g:Ch_reply1 = ""
371func Ch_handleRaw1(chan, msg)
372 unlet g:Ch_reply1
373 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100374endfunc
375
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376let g:Ch_reply2 = ""
377func Ch_handleRaw2(chan, msg)
378 unlet g:Ch_reply2
379 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100380endfunc
381
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200382let g:Ch_reply3 = ""
383func Ch_handleRaw3(chan, msg)
384 unlet g:Ch_reply3
385 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100386endfunc
387
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200388func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100389 let handle = ch_open('localhost:' . a:port, s:chopt)
390 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100391 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100392 return
393 endif
394 call ch_setoptions(handle, {'mode': 'raw'})
395
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100396 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200397 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200398 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200399 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
400 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200401 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100402 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200403 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100404endfunc
405
406func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100407 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200408 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100409endfunc
410
411"""""""""
412
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100413" Test that trying to connect to a non-existing port fails quickly.
414func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100415 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100416 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100417 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100418 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100419 " Oops, port does exists.
420 call ch_close(handle)
421 else
422 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100423 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100424 endif
425
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100426 " We intend to use a socket that doesn't exist and wait for half a second
427 " before giving up. If the socket does exist it can fail in various ways.
428 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100429 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100430 try
431 let handle = ch_open('localhost:9867', {'waittime': 500})
432 if ch_status(handle) != "fail"
433 " Oops, port does exists.
434 call ch_close(handle)
435 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100436 " Failed connection should wait about 500 msec. Can be longer if the
437 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100438 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100439 endif
440 catch
441 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100442 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100443 endif
444 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100445endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100446
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100447"""""""""
448
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100449func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100450 " Add a dummy close callback to avoid that messages are dropped when calling
451 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200452 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100453 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200454 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200455 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100456 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200457
458 call assert_equal("open", ch_status(job))
459 call assert_equal("open", ch_status(job), {"part": "out"})
460 call assert_equal("open", ch_status(job), {"part": "err"})
461 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
462 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
463
464 let dict = ch_info(job)
465 call assert_true(dict.id != 0)
466 call assert_equal('open', dict.status)
467 call assert_equal('open', dict.out_status)
468 call assert_equal('RAW', dict.out_mode)
469 call assert_equal('pipe', dict.out_io)
470 call assert_equal('open', dict.err_status)
471 call assert_equal('RAW', dict.err_mode)
472 call assert_equal('pipe', dict.err_io)
473
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100474 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100475 " For a change use the job where a channel is expected.
476 call ch_sendraw(job, "echo something\n")
477 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100478 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
479
Bram Moolenaar151f6562016-03-07 21:19:38 +0100480 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200481 let g:handle = job->job_getchannel()
482 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100483 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100484 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100485 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
486
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200487 let g:Ch_reply = ""
488 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200489 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200490
Bram Moolenaar570497a2019-08-22 22:55:13 +0200491 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100492 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
493 finally
494 call job_stop(job)
495 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100496
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200497 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200498 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200499 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100500 call assert_equal("dead", info.status)
501 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200502 call assert_equal(2, len(info.cmd))
503 call assert_equal("test_channel_pipe.py", info.cmd[1])
504
505 let found = 0
506 for j in job_info()
507 if j == job
508 let found += 1
509 endif
510 endfor
511 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100512endfunc
513
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100514func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100515 " Add a dummy close callback to avoid that messages are dropped when calling
516 " ch_canread().
517 " Also test the non-blocking option.
518 let job = job_start(s:python . " test_channel_pipe.py",
519 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
520 call assert_equal(v:t_job, type(job))
521 call assert_equal("run", job_status(job))
522
523 call assert_equal("open", ch_status(job))
524 call assert_equal("open", ch_status(job), {"part": "out"})
525
526 try
527 " Create a blob with the echo command and write it.
528 let blob = 0z00
529 let cmd = "echo something\n"
530 for i in range(0, len(cmd) - 1)
531 let blob[i] = char2nr(cmd[i])
532 endfor
533 call assert_equal(len(cmd), len(blob))
534 call ch_sendraw(job, blob)
535
536 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200537 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100538 let expected = 'something'
539 for i in range(0, len(expected) - 1)
540 call assert_equal(char2nr(expected[i]), msg[i])
541 endfor
542
543 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
544 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
545 finally
546 call job_stop(job)
547 endtry
548
549 let g:Ch_job = job
550 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
551 let info = job_info(job)
552 call assert_equal("dead", info.status)
553endfunc
554
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100555func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100556 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100557 call assert_equal("run", job_status(job))
558 try
559 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100560 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200561 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100562
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100563 call ch_sendraw(handle, "echoerr wrong\n")
564 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
565
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100566 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100567 call assert_equal("this", ch_readraw(handle))
568 call assert_equal("AND this", ch_readraw(handle))
569
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200570 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200571 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200572
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100573 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100574 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100575 finally
576 call job_stop(job)
577 endtry
578endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100579
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200580func Stop_g_job()
581 call job_stop(g:job)
582 if has('win32')
583 " On MS-Windows the server must close the file handle before we are able
584 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200585 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200586 sleep 10m
587 endif
588endfunc
589
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100590func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100591 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200592 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100593 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200594 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100595 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200596 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100597 call assert_equal("something", ch_readraw(handle))
598 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
599 call assert_equal("this", ch_readraw(handle))
600 call assert_equal("AND this", ch_readraw(handle))
601 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200602 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100603 call delete('Xinput')
604 endtry
605endfunc
606
Bram Moolenaare98d1212016-03-08 15:37:41 +0100607func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200608 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100609 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200610 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100611 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200612 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100613 call ch_sendraw(handle, "echo line one\n")
614 call ch_sendraw(handle, "echo line two\n")
615 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200616 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100617 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200618 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100619 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100620 call delete('Xoutput')
621 endtry
622endfunc
623
624func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200625 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100626 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200627 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100628 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200629 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100630 call ch_sendraw(handle, "echoerr line one\n")
631 call ch_sendraw(handle, "echoerr line two\n")
632 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200633 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100634 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200635 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100636 call delete('Xoutput')
637 endtry
638endfunc
639
640func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200641 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100642 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200643 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100644 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200645 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100646 call ch_sendraw(handle, "echoerr line one\n")
647 call ch_sendraw(handle, "echo line two\n")
648 call ch_sendraw(handle, "double this\n")
649 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200650 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100651 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200652 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100653 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100654 call delete('Xoutput')
655 endtry
656endfunc
657
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200658func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200659 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200660endfunc
661
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200662func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200663 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200664 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200665 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100666 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100667 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200668 if a:do_msg
669 let expected[0] = 'Reading from channel output...'
670 else
671 let options['out_msg'] = 0
672 call remove(expected, 0)
673 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100674 else
675 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100676 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100677 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200678 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100679 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200680 if a:nomod
681 let options['out_modifiable'] = 0
682 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100684 call assert_equal("run", job_status(job))
685 try
686 let handle = job_getchannel(job)
687 call ch_sendraw(handle, "echo line one\n")
688 call ch_sendraw(handle, "echo line two\n")
689 call ch_sendraw(handle, "double this\n")
690 call ch_sendraw(handle, "quit\n")
691 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100692 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200693 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200694 if a:nomod
695 call assert_equal(0, &modifiable)
696 else
697 call assert_equal(1, &modifiable)
698 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200699 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100700 bwipe!
701 finally
702 call job_stop(job)
703 endtry
704endfunc
705
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100706func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200707 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100708endfunc
709
710func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200711 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100712endfunc
713
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200714func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200715 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200716endfunc
717
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200718func Test_pipe_to_buffer_name_nomsg()
719 call Run_test_pipe_to_buffer(1, 0, 1)
720endfunc
721
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200722func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200723 enew!
724 let test_lines = ['one', 'two']
725 call setline(1, test_lines)
726 call ch_log('Test_close_output_buffer()')
727 let options = {'out_io': 'buffer'}
728 let options['out_name'] = 'buffer-output'
729 let options['out_msg'] = 0
730 split buffer-output
731 let job = job_start(s:python . " test_channel_write.py", options)
732 call assert_equal("run", job_status(job))
733 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200734 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200735 quit!
736 sleep 100m
737 " Make sure the write didn't happen to the wrong buffer.
738 call assert_equal(test_lines, getline(1, line('$')))
739 call assert_equal(-1, bufwinnr('buffer-output'))
740 sbuf buffer-output
741 call assert_notequal(-1, bufwinnr('buffer-output'))
742 sleep 100m
743 close " no more writes
744 bwipe!
745 finally
746 call job_stop(job)
747 endtry
748endfunc
749
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200750func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100751 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200752 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100753 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100754 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200755 if a:do_msg
756 let expected[0] = 'Reading from channel error...'
757 else
758 let options['err_msg'] = 0
759 call remove(expected, 0)
760 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100761 else
762 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100763 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100764 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200765 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100766 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200767 if a:nomod
768 let options['err_modifiable'] = 0
769 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100771 call assert_equal("run", job_status(job))
772 try
773 let handle = job_getchannel(job)
774 call ch_sendraw(handle, "echoerr line one\n")
775 call ch_sendraw(handle, "echoerr line two\n")
776 call ch_sendraw(handle, "doubleerr this\n")
777 call ch_sendraw(handle, "quit\n")
778 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200779 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200780 if a:nomod
781 call assert_equal(0, &modifiable)
782 else
783 call assert_equal(1, &modifiable)
784 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100785 bwipe!
786 finally
787 call job_stop(job)
788 endtry
789endfunc
790
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100791func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200792 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100793endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100794
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100795func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200796 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200797endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100798
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200799func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200800 call Run_test_pipe_err_to_buffer(1, 1, 1)
801endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100802
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200803func Test_pipe_err_to_buffer_name_nomsg()
804 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100805endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100806
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100807func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100808 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100809 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100810 call assert_equal("run", job_status(job))
811 try
812 let handle = job_getchannel(job)
813 call ch_sendraw(handle, "echo line one\n")
814 call ch_sendraw(handle, "echoerr line two\n")
815 call ch_sendraw(handle, "double this\n")
816 call ch_sendraw(handle, "doubleerr that\n")
817 call ch_sendraw(handle, "quit\n")
818 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200819 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 +0100820 bwipe!
821 finally
822 call job_stop(job)
823 endtry
824endfunc
825
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100826func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100827 sp pipe-input
828 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200829 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100830 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100831 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100832 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100833 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100835
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100836 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100837 call assert_equal("run", job_status(job))
838 try
839 let handle = job_getchannel(job)
840 call assert_equal('one', ch_read(handle))
841 call assert_equal('two', ch_read(handle))
842 call assert_equal('three', ch_read(handle))
843 bwipe!
844 finally
845 call job_stop(job)
846 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100847endfunc
848
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100849func Test_pipe_from_buffer_name()
850 call Run_test_pipe_from_buffer(1)
851endfunc
852
853func Test_pipe_from_buffer_nr()
854 call Run_test_pipe_from_buffer(0)
855endfunc
856
Bram Moolenaar0874a832016-09-01 15:11:51 +0200857func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200858 CheckExecutable sort
859
Bram Moolenaar0874a832016-09-01 15:11:51 +0200860 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
861 if a:use_buffer
862 split sortin
863 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
864 let options.in_io = 'buffer'
865 let options.in_name = 'sortin'
866 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200867 if !a:all
868 let options.in_top = 2
869 let options.in_bot = 4
870 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100871 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200872
873 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100874 call assert_equal("run", job_status(job))
875 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200876 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200877 endif
878
Bram Moolenaare2956092019-01-25 21:01:17 +0100879 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200880
Bram Moolenaard8b55492016-09-01 14:35:22 +0200881 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200882 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200883 call assert_equal('Reading from channel output...', getline(1))
884 if a:all
885 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
886 else
887 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
888 endif
889
Bram Moolenaare2956092019-01-25 21:01:17 +0100890 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200891 if a:use_buffer
892 bwipe! sortin
893 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200894 bwipe! sortout
895endfunc
896
897func Test_pipe_through_sort_all()
898 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200899 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200900endfunc
901
902func Test_pipe_through_sort_some()
903 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200904 call Run_pipe_through_sort(0, 1)
905endfunc
906
907func Test_pipe_through_sort_feed()
908 call ch_log('Test_pipe_through_sort_feed()')
909 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200910endfunc
911
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100912func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100913 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100914 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100915 call assert_equal("run", job_status(job))
916 try
917 let handle = job_getchannel(job)
918 call ch_sendraw(handle, "echo line one\n")
919 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200920 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200921 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100922 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
923 bwipe!
924 finally
925 call job_stop(job)
926 endtry
927endfunc
928
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100929func Test_pipe_to_buffer_json()
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100930 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100931 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100932 call assert_equal("run", job_status(job))
933 try
934 let handle = job_getchannel(job)
935 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
936 call ch_sendraw(handle, "echo [-2, 12.34]\n")
937 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200938 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100939 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
940 bwipe!
941 finally
942 call job_stop(job)
943 endtry
944endfunc
945
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100946" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100947func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100948 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100949 if getline(line('$') - a:offset) == a:line
950 break
951 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100952 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100953 endfor
954endfunc
955
956func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100957 " Create two buffers, one to read from and one to write to.
958 split pipe-output
959 set buftype=nofile
960 split pipe-input
961 set buftype=nofile
962
963 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100964 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200965 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
966 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100967 call assert_equal("run", job_status(job))
968 try
969 exe "normal Gaecho hello\<CR>"
970 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100971 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100972 call assert_equal('hello', getline('$'))
973
974 exe bufwinnr('pipe-input') . "wincmd w"
975 exe "normal Gadouble this\<CR>"
976 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100977 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100978 call assert_equal('this', getline(line('$') - 1))
979 call assert_equal('AND this', getline('$'))
980
981 bwipe!
982 exe bufwinnr('pipe-input') . "wincmd w"
983 bwipe!
984 finally
985 call job_stop(job)
986 endtry
987endfunc
988
989func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100990 " Create one buffer to read from and to write to.
991 split pipe-io
992 set buftype=nofile
993
994 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100995 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200996 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
997 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100998 call assert_equal("run", job_status(job))
999 try
1000 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001001 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001002 call assert_equal('hello', getline(line('$') - 1))
1003
1004 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001005 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001006 call assert_equal('this', getline(line('$') - 2))
1007 call assert_equal('AND this', getline(line('$') - 1))
1008
1009 bwipe!
1010 finally
1011 call job_stop(job)
1012 endtry
1013endfunc
1014
Bram Moolenaar4641a122019-07-29 22:10:23 +02001015func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001016 CheckScreendump
1017
Bram Moolenaar4641a122019-07-29 22:10:23 +02001018 let lines =<< trim END
1019 new Xscrollbuffer
1020 call setline(1, range(1, 200))
1021 $
1022 redraw
1023 wincmd w
1024 call deletebufline('Xscrollbuffer', 1, '$')
1025 if has('win32')
1026 let cmd = ['cmd', '/c', 'echo sometext']
1027 else
1028 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1029 endif
1030 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1031 END
1032 call writefile(lines, 'XtestBufferScroll')
1033 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaarf386f082019-07-29 23:03:03 +02001034 call term_wait(buf, 100)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001035 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1036
1037 " clean up
1038 call StopVimInTerminal(buf)
1039 call delete('XtestBufferScroll')
1040endfunc
1041
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001042func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001043 " We cannot check that no I/O works, we only check that the job starts
1044 " properly.
1045 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001046 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001047 call assert_equal("run", job_status(job))
1048 try
1049 call assert_equal('something', ch_read(job))
1050 finally
1051 call job_stop(job)
1052 endtry
1053
1054 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001055 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001056 call assert_equal("run", job_status(job))
1057 try
1058 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1059 finally
1060 call job_stop(job)
1061 endtry
1062
1063 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001064 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001065 call assert_equal("run", job_status(job))
1066 try
1067 call assert_equal('something', ch_read(job))
1068 finally
1069 call job_stop(job)
1070 endtry
1071
1072 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001073 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001074 call assert_equal("run", job_status(job))
1075 call job_stop(job)
1076
1077 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001078 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001079 call assert_equal("run", job_status(job))
1080 call assert_equal('channel fail', string(job_getchannel(job)))
1081 call assert_equal('fail', ch_status(job))
1082 call job_stop(job)
1083endfunc
1084
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001085func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001086 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1087 split testout
1088 let job = job_start([s:python, '-c',
1089 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001090 " the job may be done quickly, also accept "dead"
1091 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001092 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001093 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001094 let totlen = 0
1095 for line in getline(1, '$')
1096 call assert_equal('', substitute(line, '^\.*', '', ''))
1097 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001098 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001099 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001100 finally
1101 call job_stop(job)
1102 bwipe!
1103 endtry
1104endfunc
1105
Bram Moolenaarde279892016-03-11 22:19:44 +01001106func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001107 let job = job_start(s:python . " test_channel_pipe.py")
1108 call assert_equal("run", job_status(job))
1109 let handle = job_getchannel(job)
1110 try
1111 call ch_sendraw(handle, "echo something\n")
1112 call assert_equal("something", ch_readraw(handle))
1113 finally
1114 call job_stop(job)
1115 endtry
1116
1117 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1118 call assert_equal("run", job_status(job))
1119 let handle = job_getchannel(job)
1120 try
1121 call ch_sendraw(handle, "echo again\n")
1122 call assert_equal("again", ch_readraw(handle))
1123 finally
1124 call job_stop(job)
1125 endtry
1126endfunc
1127
Bram Moolenaar75f72652016-03-20 22:16:56 +01001128func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001129 let dict = {'thisis': 'dict: '}
1130 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001131 if type(a:msg) == v:t_string
1132 let g:Ch_outmsg = self.thisis . a:msg
1133 else
1134 let g:Ch_outobj = a:msg
1135 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001136 endfunc
1137 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001138 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001139 endfunc
1140 let job = job_start(s:python . " test_channel_pipe.py",
1141 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001142 \ 'out_mode': 'json',
1143 \ 'err_cb': dict.errHandler,
1144 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001145 call assert_equal("run", job_status(job))
1146 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001147 let g:Ch_outmsg = ''
1148 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001149 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1150 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001151 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1152 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001153
1154 " Receive a json object split in pieces
1155 unlet! g:Ch_outobj
1156 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001157 let g:Ch_outobj = ''
1158 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001159 finally
1160 call job_stop(job)
1161 endtry
1162endfunc
1163
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001164func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001165 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001166 let g:Ch_msg1 = ''
1167 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001168 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001169 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001170 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001171 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001172 let s:counter += 1
1173 endfunc
1174 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001175 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001176 let s:counter += 1
1177 endfunc
1178 let job = job_start(s:python . " test_channel_pipe.py quit now",
1179 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001180 \ 'close_cb': 'CloseHandler'})
1181 " the job may be done quickly, also accept "dead"
1182 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001183 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001184 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1185 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001186 finally
1187 call job_stop(job)
1188 delfunc OutHandler
1189 delfunc CloseHandler
1190 endtry
1191endfunc
1192
Bram Moolenaar437905c2016-04-26 19:01:05 +02001193func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001194 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001195 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001196 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001197 endfunc
1198 let job = job_start(s:python . " test_channel_pipe.py quit now",
1199 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001200 " the job may be done quickly, also accept "dead"
1201 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001202 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001203 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001204 finally
1205 call job_stop(job)
1206 delfunc CloseHandler
1207 endtry
1208endfunc
1209
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001210" Use channel in NL mode but received text does not end in NL.
1211func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001212 let g:Ch_received = ''
1213 func! CloseHandler(chan)
1214 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1215 let g:Ch_received .= ch_read(a:chan)
1216 endwhile
1217 endfunc
1218 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1219 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001220 " the job may be done quickly, also accept "dead"
1221 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001222 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001223 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001224 finally
1225 call job_stop(job)
1226 delfunc CloseHandler
1227 endtry
1228endfunc
1229
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001230func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001231 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001232 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1233 \ 'out_mode': 'json',
1234 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1235 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001236 call assert_equal("run", job_status(job))
1237 try
1238 let g:Ch_outmsg = ''
1239 let g:Ch_errmsg = ''
1240 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1241 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001242 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1243 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001244 finally
1245 call job_stop(job)
1246 endtry
1247endfunc
1248
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001249func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001250 let g:retdict = {'ret': {}}
1251 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001252 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001253 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001254 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001255 let self.ret['exit_cb'] = job_status(a:job)
1256 endfunc
1257
Bram Moolenaare2956092019-01-25 21:01:17 +01001258 let job = job_start([&shell, &shellcmdflag, 'echo'],
1259 \ {'close_cb': g:retdict.close_cb,
1260 \ 'exit_cb': g:retdict.exit_cb})
1261 " the job may be done quickly, also accept "dead"
1262 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001263 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001264 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001265 call assert_equal('dead', g:retdict.ret['exit_cb'])
1266 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001267endfunc
1268
Bram Moolenaard46ae142016-02-16 13:33:52 +01001269""""""""""
1270
Bram Moolenaar0b146882018-09-06 16:27:24 +02001271function ExitCbWipe(job, status)
1272 exe g:wipe_buf 'bw!'
1273endfunction
1274
1275" This caused a crash, because messages were handled while peeking for a
1276" character.
1277func Test_exit_cb_wipes_buf()
1278 if !has('timers')
1279 return
1280 endif
1281 set cursorline lazyredraw
1282 call test_override('redraw_flag', 1)
1283 new
1284 let g:wipe_buf = bufnr('')
1285
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001286 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001287 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001288 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1289 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1290 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1291 call timer_stop(timer)
1292
1293 set nocursorline nolazyredraw
1294 unlet g:wipe_buf
1295 call test_override('ALL', 0)
1296endfunc
1297
1298""""""""""
1299
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001300let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001301func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001302 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001303 unlet s:channelfd
1304endfunc
1305
1306" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001307func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001308 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001309 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001310 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001311endfunc
1312
1313func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001314 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001315 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001316endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001317
Bram Moolenaard46ae142016-02-16 13:33:52 +01001318""""""""""
1319
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001320let g:Ch_unletResponse = ''
1321func Ch_CloseHandler(handle, msg)
1322 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001323 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001324endfunc
1325
1326" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001327func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001328 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001329 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001330 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001331endfunc
1332
1333func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001334 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001335 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001336endfunc
1337
1338""""""""""
1339
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001340func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001341 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001342 silent! let ch = ch_open("noserver")
1343 echo ch
1344 let d = ch
1345endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001346
1347""""""""""
1348
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001349func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001350 " Wait up to a second for the port to open.
1351 let s:chopt.waittime = 1000
1352 let channel = ch_open('localhost:' . a:port, s:chopt)
1353 unlet s:chopt.waittime
1354 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001355 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001356 return
1357 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001358 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001359 call ch_close(channel)
1360endfunc
1361
1362func Test_open_delay()
1363 call ch_log('Test_open_delay()')
1364 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001365 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001366endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001367
1368"""""""""
1369
1370function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001371 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001372endfunc
1373
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001374function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001375 let handle = ch_open('localhost:' . a:port, s:chopt)
1376 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001377 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001378 return
1379 endif
1380
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001381 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001382 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001383 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001384endfunc
1385
1386func Test_call()
1387 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001388 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001389endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001390
1391"""""""""
1392
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001393let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001394function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001395 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001396endfunc
1397
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001398function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001399 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001400 let g:Ch_exit_job = g:currentJob
1401 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001402endfunc
1403
1404func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001405 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001406
Bram Moolenaarf386f082019-07-29 23:03:03 +02001407 " wait up to a second for the job to exit
1408 for i in range(100)
1409 if g:Ch_job_exit_ret == 'done'
1410 break
1411 endif
1412 sleep 10m
1413 " calling job_status() triggers the callback
1414 call job_status(g:Ch_exit_job)
1415 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001416
Bram Moolenaarf386f082019-07-29 23:03:03 +02001417 call assert_equal('done', g:Ch_job_exit_ret)
1418 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1419 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001420endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001421
Bram Moolenaar97792de2016-10-15 18:36:49 +02001422function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001423 if job_info(a:job).process == g:exit_cb_val.process
1424 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1425 endif
1426 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001427endfunction
1428
1429func Test_exit_callback_interval()
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001430 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001431 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001432 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001433 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001434 let elapsed = reltimefloat(g:exit_cb_val.end)
1435 call assert_true(elapsed > 0.5)
1436 call assert_true(elapsed < 1.0)
1437
1438 " case: unreferenced job, using timer
1439 if !has('timers')
1440 return
1441 endif
1442
1443 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1444 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1445 let g:exit_cb_val.process = job_info(g:job).process
1446 unlet g:job
1447 call Standby(1000)
1448 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1449 let elapsed = reltimefloat(g:exit_cb_val.end)
1450 else
1451 let elapsed = 1.0
1452 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001453 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001454endfunc
1455
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001456"""""""""
1457
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001458let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001459function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001461endfunc
1462
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001463function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001464 let handle = ch_open('localhost:' . a:port, s:chopt)
1465 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001466 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001467 return
1468 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001469 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001470
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001471 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001472 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001473endfunc
1474
1475func Test_close_callback()
1476 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001477 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001478endfunc
1479
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001480function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001481 let handle = ch_open('localhost:' . a:port, s:chopt)
1482 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001483 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001484 return
1485 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001486 let g:Ch_d = {}
1487 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001488 let self.close_ret = 'closed'
1489 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001490 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001491
1492 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001493 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001495endfunc
1496
1497func Test_close_partial()
1498 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001499 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001500endfunc
1501
Bram Moolenaar80385682016-03-27 19:13:35 +02001502func Test_job_start_invalid()
1503 call assert_fails('call job_start($x)', 'E474:')
1504 call assert_fails('call job_start("")', 'E474:')
1505endfunc
1506
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001507func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001508 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001509 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001510 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001511 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001512 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001513 call job_stop(g:job, 'kill')
1514 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001515 endtry
1516endfunc
1517
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001518" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001519func Test_partial_in_channel_cycle()
1520 let d = {}
1521 let d.a = function('string', [d])
1522 try
1523 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1524 catch
1525 call assert_exception('E901:')
1526 endtry
1527 unlet d
1528endfunc
1529
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001530func Test_using_freed_memory()
1531 let g:a = job_start(['ls'])
1532 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001533 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001534endfunc
1535
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001536func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001537 CheckExecutable cat
1538
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001539 sp test_channel.vim
1540 let g:linecount = line('$')
1541 close
1542 split testout
1543 1,$delete
1544 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001545 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001546 bwipe!
1547endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001548
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001549func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001550 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001551 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001552
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001553 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001554 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001555 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001556 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1557 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001558
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001559 bdel test_buffer
1560 call assert_equal([], getbufline(bufnr, 1, '$'))
1561
1562 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001563 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001564 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1565 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1566
1567 bwipe! test_buffer
1568endfunc
1569
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001570func Test_cmd_parsing()
1571 if !has('unix')
1572 return
1573 endif
1574 call assert_false(filereadable("file with space"))
1575 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001576 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001577 call delete("file with space")
1578
1579 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001580 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001581 call delete("file with space")
1582endfunc
1583
Bram Moolenaar82117982016-08-27 19:21:48 +02001584func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001585 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001586
1587 " Test lines from the job containing NUL are stored correctly in a buffer.
1588 new
1589 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1590 w! Xtestread
1591 bwipe!
1592 split testout
1593 1,$delete
1594 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1595 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001596 call assert_equal("asdf\nasdf", getline(1))
1597 call assert_equal("xxx\n", getline(2))
1598 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001599
1600 call delete('Xtestread')
1601 bwipe!
1602
1603 " Test lines from a buffer with NUL bytes are written correctly to the job.
1604 new mybuffer
1605 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1606 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 +02001607 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001608 bwipe!
1609 split Xtestwrite
1610 call assert_equal("asdf\nasdf", getline(1))
1611 call assert_equal("xxx\n", getline(2))
1612 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001613 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001614
1615 call delete('Xtestwrite')
1616 bwipe!
1617endfunc
1618
Bram Moolenaarec68a992016-10-03 21:37:41 +02001619func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001620 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001621 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001622 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001623 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001624 unlet g:linecount
1625endfunc
1626
1627func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001628 func s:close_cb(ch)
1629 while ch_status(a:ch) == 'buffered'
1630 let g:out .= ch_read(a:ch)
1631 endwhile
1632 endfunc
1633
1634 let g:out = ''
1635 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1636 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1637 call WaitForAssert({-> assert_equal('123', g:out)})
1638 unlet g:out
1639 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001640endfunc
1641
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001642func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001643 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001644 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001645 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001646 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001647 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001648endfunc
1649
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001650func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001651 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001652
1653 " Check that backslash in $COMSPEC is handled properly.
1654 let g:echostr = ''
1655 let cmd = $COMSPEC . ' /c echo 123'
1656 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1657 let info = job_info(job)
1658 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1659
1660 call WaitForAssert({-> assert_equal("123", g:echostr)})
1661 unlet g:echostr
1662endfunc
1663
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001664func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001665 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001666 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001667 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001668 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001669 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001670 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001671 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1672 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001673 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001674 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001675endfunc
1676
1677func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001678 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001679 if has('win32')
1680 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001681 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001682 else
1683 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001684 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001685 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001686 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001687 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001688 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001689 let expect = substitute(expect, '[/\\]$', '', '')
1690 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1691 if $CI != '' && stridx(g:envstr, '/private/') == 0
1692 let g:envstr = g:envstr[8:]
1693 endif
1694 call assert_equal(expect, g:envstr)
1695 finally
1696 call job_stop(job)
1697 unlet g:envstr
1698 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001699endfunc
1700
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001701function Ch_test_close_lambda(port)
1702 let handle = ch_open('localhost:' . a:port, s:chopt)
1703 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001704 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001705 return
1706 endif
1707 let g:Ch_close_ret = ''
1708 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1709
1710 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001711 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001712endfunc
1713
1714func Test_close_lambda()
1715 call ch_log('Test_close_lambda()')
1716 call s:run_server('Ch_test_close_lambda')
1717endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001718
1719func s:test_list_args(cmd, out, remove_lf)
1720 try
1721 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001722 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 +02001723 call WaitFor('"" != g:out')
1724 if has('win32')
1725 let g:out = substitute(g:out, '\r', '', 'g')
1726 endif
1727 if a:remove_lf
1728 let g:out = substitute(g:out, '\n$', '', 'g')
1729 endif
1730 call assert_equal(a:out, g:out)
1731 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001732 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001733 unlet g:out
1734 endtry
1735endfunc
1736
1737func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001738 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1739 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1740 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1741 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1742 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1743 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1744 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1745 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1746 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1747 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1748 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1749 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1750 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1751
1752 " tests which not contain spaces in the argument
1753 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1754 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1755 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1756 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1757 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1758 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1759 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1760 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1761 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1762endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001763
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001764func Test_keep_pty_open()
1765 if !has('unix')
1766 return
1767 endif
1768
Bram Moolenaare2956092019-01-25 21:01:17 +01001769 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1770 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001771 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1772 call assert_inrange(200, 1000, elapsed)
1773 call job_stop(job)
1774endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001775
1776func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001777 CheckFeature timers
Bram Moolenaarc46af532019-01-09 22:24:49 +01001778
1779 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001780 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001781 endfunc
1782
1783 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001784 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001785 call Resume()
1786 endfunc
1787
1788 func TimerCb(timer)
1789 if has('win32')
1790 let cmd = ['cmd', '/c', 'echo.']
1791 else
1792 let cmd = ['echo']
1793 endif
1794 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1795 call substitute(repeat('a', 100000), '.', '', 'g')
1796 endfunc
1797
1798 " We should be interrupted before 'updatetime' elapsed.
1799 let g:val = 0
1800 call timer_start(1, 'TimerCb')
1801 let elapsed = Standby(&ut)
1802 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001803
1804 " Wait for both OutCb() and ExitCb() to have been called before deleting
1805 " them.
1806 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001807 call job_stop(g:job)
1808
1809 delfunc OutCb
1810 delfunc ExitCb
1811 delfunc TimerCb
1812 unlet! g:val
1813 unlet! g:job
1814endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001815
1816func Test_raw_large_data()
1817 try
1818 let g:out = ''
1819 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001820 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1821 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001822
Bram Moolenaare2956092019-01-25 21:01:17 +01001823 let outlen = 79999
1824 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02001825 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001826 call WaitFor({-> len(g:out) >= outlen}, 10000)
1827 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001828 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1829 finally
1830 call job_stop(job)
1831 unlet g:out
1832 endtry
1833endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001834
Bram Moolenaar65240682019-02-10 22:23:26 +01001835func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001836 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01001837
1838 try
1839 let job = job_start(s:python . " test_channel_pipe.py busy",
1840 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
1841 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
1842 finally
1843 call job_stop(job)
1844 endtry
1845endfunc
1846
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001847func Test_job_exitval_and_termsig()
1848 if !has('unix')
1849 return
1850 endif
1851
1852 " Terminate job normally
1853 let cmd = ['echo']
1854 let job = job_start(cmd)
1855 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1856 let info = job_info(job)
1857 call assert_equal(0, info.exitval)
1858 call assert_equal("", info.termsig)
1859
1860 " Terminate job by signal
1861 let cmd = ['sleep', '10']
1862 let job = job_start(cmd)
1863 sleep 10m
1864 call job_stop(job)
1865 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1866 let info = job_info(job)
1867 call assert_equal(-1, info.exitval)
1868 call assert_equal("term", info.termsig)
1869endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01001870
1871func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001872 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01001873
1874 call writefile(['test'], 'Xtestin')
1875 let in_opts = [{},
1876 \ {'in_io': 'null'},
1877 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
1878 let out_opts = [{},
1879 \ {'out_io': 'null'},
1880 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
1881 let err_opts = [{},
1882 \ {'err_io': 'null'},
1883 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
1884 \ {'err_io': 'out'}]
1885 let opts = []
1886
1887 for in_opt in in_opts
1888 let x = copy(in_opt)
1889 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001890 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001891 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001892 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001893 let opts += [extend({'pty': 1}, x)]
1894 endfor
1895 endfor
1896 endfor
1897
1898 for opt in opts
1899 let job = job_start('echo', opt)
1900 let info = job_info(job)
1901 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
1902 \ get(opt, 'in_io', 'tty'),
1903 \ get(opt, 'out_io', 'tty'),
1904 \ get(opt, 'err_io', 'tty'))
1905
1906 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
1907 call assert_notequal('', info.tty_in, msg)
1908 else
1909 call assert_equal('', info.tty_in, msg)
1910 endif
1911 call assert_equal(info.tty_in, info.tty_out, msg)
1912
1913 call WaitForAssert({-> assert_equal('dead', job_status(job))})
1914 endfor
1915
1916 call delete('Xtestin')
1917 call delete('Xtestout')
1918 call delete('Xtesterr')
1919endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02001920
1921" Do this last, it stops any channel log.
1922func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02001923 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001924 call ch_log('Test_zz_nl_err_to_out_pipe()')
1925 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
1926 call assert_equal("run", job_status(job))
1927 try
1928 let handle = job_getchannel(job)
1929 call ch_sendraw(handle, "echo something\n")
1930 call assert_equal("something", ch_readraw(handle))
1931
1932 call ch_sendraw(handle, "echoerr wrong\n")
1933 call assert_equal("wrong", ch_readraw(handle))
1934 finally
1935 call job_stop(job)
1936 call ch_logfile('')
1937 let loglines = readfile('Xlog')
1938 call assert_true(len(loglines) > 10)
1939 let found_test = 0
1940 let found_send = 0
1941 let found_recv = 0
1942 let found_stop = 0
1943 for l in loglines
1944 if l =~ 'Test_zz_nl_err_to_out_pipe'
1945 let found_test = 1
1946 endif
1947 if l =~ 'SEND on.*echo something'
1948 let found_send = 1
1949 endif
1950 if l =~ 'RECV on.*something'
1951 let found_recv = 1
1952 endif
1953 if l =~ 'Stopping job with'
1954 let found_stop = 1
1955 endif
1956 endfor
1957 call assert_equal(1, found_test)
1958 call assert_equal(1, found_send)
1959 call assert_equal(1, found_recv)
1960 call assert_equal(1, found_stop)
1961 " On MS-Windows need to sleep for a moment to be able to delete the file.
1962 sleep 10m
1963 call delete('Xlog')
1964 endtry
1965endfunc
1966
1967" Do this last, it stops any channel log.
1968func Test_zz_ch_log()
1969 call ch_logfile('Xlog', 'w')
1970 call ch_log('hello there')
1971 call ch_log('%s%s')
1972 call ch_logfile('')
1973 let text = readfile('Xlog')
1974 call assert_match("hello there", text[1])
1975 call assert_match("%s%s", text[2])
1976 call delete('Xlog')
1977endfunc