blob: 37dfd258619c79e76351aae1b731ba069a54587a [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 Moolenaardfc33a62020-04-29 22:30:13 +02009source view_util.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +020010
11let s:python = PythonProg()
12if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010013 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020014 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010015endif
16
Bram Moolenaar37175402017-03-18 20:18:45 +010017" Uncomment the next line to see what happens. Output is in
18" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020019" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010020" call ch_logfile('channellog', 'w')
21
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020022func SetUp()
23 if g:testfunc =~ '_ipv6()$'
24 let s:localhost = '[::1]:'
25 let s:testscript = 'test_channel_6.py'
26 else
27 let s:localhost = 'localhost:'
28 let s:testscript = 'test_channel.py'
29 endif
30 let s:chopt = {}
31 call ch_log(g:testfunc)
Bram Moolenaarec9b0172020-06-19 19:10:59 +020032
33 " Most tests use job_start(), which can be flaky
34 let g:test_is_flaky = 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020035endfunc
Bram Moolenaar3b05b132016-02-03 23:25:07 +010036
Bram Moolenaar4b96df52020-01-26 22:00:26 +010037" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010038func s:run_server(testfunc, ...)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020039 call RunServer(s:testscript, a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010040endfunc
41
Bram Moolenaar819524702018-02-27 19:10:00 +010042" Return a list of open files.
43" Can be used to make sure no resources leaked.
44" Returns an empty list on systems where this is not supported.
45func s:get_resources()
46 let pid = getpid()
47
Bram Moolenaar39536dd2019-01-29 22:58:21 +010048 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010049 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
50 elseif isdirectory('/proc/' . pid . '/fd/')
51 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
52 else
53 return []
54 endif
55endfunc
56
Bram Moolenaar321efdd2016-07-15 17:09:11 +020057let g:Ch_responseMsg = ''
58func Ch_requestHandler(handle, msg)
59 let g:Ch_responseHandle = a:handle
60 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010061endfunc
62
Bram Moolenaar321efdd2016-07-15 17:09:11 +020063func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010064 " Avoid dropping messages, since we don't use a callback here.
65 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020066 " Also add the noblock flag to try it out.
67 let s:chopt.noblock = 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020068 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010069 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010070 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010071 return
72 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020073
74 " check that getjob without a job is handled correctly
75 call assert_equal('no process', string(ch_getjob(handle)))
76
Bram Moolenaar570497a2019-08-22 22:55:13 +020077 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010078 call assert_true(dict.id != 0)
79 call assert_equal('open', dict.status)
80 call assert_equal(a:port, string(dict.port))
81 call assert_equal('open', dict.sock_status)
82 call assert_equal('socket', dict.sock_io)
83
Bram Moolenaard7ece102016-02-02 23:23:02 +010084 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010085 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010086
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010087 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010088 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
89 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
90 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
91
92 " split command should work
93 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020094 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010095 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010096
Bram Moolenaarf1f07922016-08-26 17:58:53 +020097 " string with ][ should work
98 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
99
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100100 " nothing to read now
101 call assert_equal(0, ch_canread(handle))
102
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200103 " sending three messages quickly then reading should work
104 for i in range(3)
105 call ch_sendexpr(handle, 'echo hello ' . i)
106 endfor
107 call assert_equal('hello 0', ch_read(handle)[1])
108 call assert_equal('hello 1', ch_read(handle)[1])
109 call assert_equal('hello 2', ch_read(handle)[1])
110
Bram Moolenaard7ece102016-02-02 23:23:02 +0100111 " Request that triggers sending two ex commands. These will usually be
112 " handled before getting the response, but it's not guaranteed, thus wait a
113 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100114 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200115 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100116 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100117
Bram Moolenaarb836f632021-07-01 22:11:28 +0200118 " Request command "echoerr 'this is an error'".
119 " This will throw an exception, catch it here.
120 let caught = 'no'
121 try
122 call assert_equal('ok', ch_evalexpr(handle, 'echoerr'))
123 catch /this is an error/
124 let caught = 'yes'
125 endtry
126 if caught != 'yes'
127 call assert_report("Expected exception from error message")
128 endif
129
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100130 " Request command "foo bar", which fails silently.
131 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200132 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100133
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100134 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200135 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100136
Bram Moolenaara07fec92016-02-05 21:04:08 +0100137 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200138 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
139 call WaitFor('exists("g:Ch_responseHandle")')
140 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100141 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100142 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200143 call assert_equal(handle, g:Ch_responseHandle)
144 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100145 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200146 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100147
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200148 let g:Ch_responseMsg = ''
149 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
150 call WaitFor('exists("g:Ch_responseHandle")')
151 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100152 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100153 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200154 call assert_equal(handle, g:Ch_responseHandle)
155 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100156 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200157 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100158
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200159 " Using lambda.
160 let g:Ch_responseMsg = ''
161 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
162 call WaitFor('exists("g:Ch_responseHandle")')
163 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100164 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200165 else
166 call assert_equal(handle, g:Ch_responseHandle)
167 unlet g:Ch_responseHandle
168 endif
169 call assert_equal('got it', g:Ch_responseMsg)
170
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100171 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200172 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100173
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100174 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200175 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100176 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100177 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200178 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", 'E475:')
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100179 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100180 call ch_setoptions(handle, {'drop': 'never'})
181 call ch_setoptions(handle, {'drop': 'auto'})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200182 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", 'E475:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200183 call assert_equal(0, ch_setoptions(handle, test_null_dict()))
184 call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'}))
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100185
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100186 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100188 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100189 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100190
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100191 " Send an eval request with special characters.
192 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
193 sleep 10m
194 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
195
196 " Send an eval request to get a line with special characters.
197 call setline(3, "a\nb\<CR>c\x01d\x7fe")
198 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
199 sleep 10m
200 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
201
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100202 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100204 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100205 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100206
Bram Moolenaar55fab432016-02-07 16:53:13 +0100207 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100208 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100209 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100210 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100211
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100212 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100213 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100214 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100215 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100216
Bram Moolenaarf4160862016-02-05 23:09:12 +0100217 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200219 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100220 call assert_equal('one', getline(line('$') - 2))
221 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100222
223 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100224 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
225 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100226
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100228
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100229 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100230 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100231 if exists('*reltimefloat')
232 let start = reltime()
233 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
234 let elapsed = reltime(start)
235 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
236 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100237
238 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100240 let resp = ch_read(handle)
241 call assert_equal(type([]), type(resp))
242 call assert_equal(type(11), type(resp[0]))
243 call assert_equal('waited', resp[1])
244
Bram Moolenaard7ece102016-02-02 23:23:02 +0100245 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100246 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100247endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100248
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100249func Test_communicate()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200250 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100251endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100252
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200253func Test_communicate_ipv6()
254 CheckIPv6
255 call Test_communicate()
256endfunc
257
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100258" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200259func Ch_two_channels(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200260 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200261 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200262 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100263 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100264 return
265 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100266
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100267 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100268
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200269 let newhandle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100270 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100271 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100272 return
273 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100274 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
275 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100276
277 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100278 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100279
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100280 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200281 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100282endfunc
283
284func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200285 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200286 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100287endfunc
288
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200289func Test_two_channels_ipv6()
290 CheckIPv6
291 call Test_two_channels()
292endfunc
293
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100294" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200295func Ch_server_crash(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200296 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100297 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100298 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100299 return
300 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100301
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100302 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100303
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100304 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100305endfunc
306
307func Test_server_crash()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200308 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100309endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100310
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200311func Test_server_crash_ipv6()
312 CheckIPv6
313 call Test_server_crash()
314endfunc
315
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100316"""""""""
317
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200318func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100319 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200320 unlet g:Ch_reply
321 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100322endfunc
323
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200324func Ch_channel_handler(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200325 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100326 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100327 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100328 return
329 endif
330
331 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100332 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200333 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100334
335 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100336 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200337 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100338endfunc
339
340func Test_channel_handler()
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200341 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200342 let s:chopt.callback = 'Ch_handler'
343 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200344 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200345 let s:chopt.callback = function('Ch_handler')
346 call s:run_server('Ch_channel_handler')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200347endfunc
348
349func Test_channel_handler_ipv6()
350 CheckIPv6
351 call Test_channel_handler()
Bram Moolenaarf6157282016-02-10 21:07:14 +0100352endfunc
353
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100354"""""""""
355
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200356let g:Ch_reply = ''
357func Ch_zeroHandler(chan, msg)
358 unlet g:Ch_reply
359 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100360endfunc
361
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200362let g:Ch_zero_reply = ''
363func Ch_oneHandler(chan, msg)
364 unlet g:Ch_zero_reply
365 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100366endfunc
367
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200368func Ch_channel_zero(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200369 let handle = (s:localhost .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100370 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100371 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100372 return
373 endif
374
375 " Check that eval works.
376 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
377
378 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200379 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100380 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100381 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200382 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100383 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100384 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200385 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100386 endif
387
388 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200389 let g:Ch_reply = ''
390 let g:Ch_zero_reply = ''
391 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200392 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100393 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200394 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100395 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200396 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100397 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100398endfunc
399
400func Test_zero_reply()
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100401 " Run with channel handler
402 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200403 let s:chopt.callback = 'Ch_zeroHandler'
404 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100405 unlet s:chopt.callback
406
407 " Run without channel handler
408 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200409 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100410endfunc
411
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200412func Test_zero_reply_ipv6()
413 CheckIPv6
414 call Test_zero_reply()
415endfunc
416
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100417"""""""""
418
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200419let g:Ch_reply1 = ""
420func Ch_handleRaw1(chan, msg)
421 unlet g:Ch_reply1
422 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100423endfunc
424
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200425let g:Ch_reply2 = ""
426func Ch_handleRaw2(chan, msg)
427 unlet g:Ch_reply2
428 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100429endfunc
430
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200431let g:Ch_reply3 = ""
432func Ch_handleRaw3(chan, msg)
433 unlet g:Ch_reply3
434 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100435endfunc
436
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200437func Ch_raw_one_time_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200438 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100439 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100440 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100441 return
442 endif
443 call ch_setoptions(handle, {'mode': 'raw'})
444
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100445 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200446 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200447 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200448 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
449 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200450 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100451 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200452 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100453endfunc
454
455func Test_raw_one_time_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200456 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100457endfunc
458
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200459func Test_raw_one_time_callback_ipv6()
460 CheckIPv6
461 call Test_raw_one_time_callback()
462endfunc
463
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100464"""""""""
465
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100466" Test that trying to connect to a non-existing port fails quickly.
467func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100468 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100469 " this is timing sensitive
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100470
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100471 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100472 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100473 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100474 " Oops, port does exists.
475 call ch_close(handle)
476 else
477 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100478 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100479 endif
480
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100481 " We intend to use a socket that doesn't exist and wait for half a second
482 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100483 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100484 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100485 try
486 let handle = ch_open('localhost:9867', {'waittime': 500})
487 if ch_status(handle) != "fail"
488 " Oops, port does exists.
489 call ch_close(handle)
490 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100491 " Failed connection should wait about 500 msec. Can be longer if the
492 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100493 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100494 endif
495 catch
496 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100497 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100498 endif
499 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100500endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100501
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100502"""""""""
503
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100504func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100505 " Add a dummy close callback to avoid that messages are dropped when calling
506 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200507 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100508 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200509 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200510 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100511 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200512
513 call assert_equal("open", ch_status(job))
514 call assert_equal("open", ch_status(job), {"part": "out"})
515 call assert_equal("open", ch_status(job), {"part": "err"})
516 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
517 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
518
519 let dict = ch_info(job)
520 call assert_true(dict.id != 0)
521 call assert_equal('open', dict.status)
522 call assert_equal('open', dict.out_status)
523 call assert_equal('RAW', dict.out_mode)
524 call assert_equal('pipe', dict.out_io)
525 call assert_equal('open', dict.err_status)
526 call assert_equal('RAW', dict.err_mode)
527 call assert_equal('pipe', dict.err_io)
528
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100529 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100530 " For a change use the job where a channel is expected.
531 call ch_sendraw(job, "echo something\n")
532 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100533 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
534
Bram Moolenaar151f6562016-03-07 21:19:38 +0100535 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200536 let g:handle = job->job_getchannel()
537 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100538 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100539 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100540 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
541
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200542 let g:Ch_reply = ""
543 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200544 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200545
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200546 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
547 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
548 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
549 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
550
Bram Moolenaar570497a2019-08-22 22:55:13 +0200551 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100552 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
553 finally
554 call job_stop(job)
555 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100556
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200557 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200558 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200559 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100560 call assert_equal("dead", info.status)
561 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200562 call assert_equal(2, len(info.cmd))
563 call assert_equal("test_channel_pipe.py", info.cmd[1])
564
565 let found = 0
566 for j in job_info()
567 if j == job
568 let found += 1
569 endif
570 endfor
571 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100572
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200573 call assert_fails("call job_stop('abc')", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200574 call assert_fails("call job_stop(job, [])", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200575 call assert_fails("call job_stop(test_null_job())", 'E916:')
576
Bram Moolenaar8b633132020-03-20 18:20:51 +0100577 " Try to use the job and channel where a number is expected. This is not
578 " related to testing the raw pipe. This test is here just to reuse the
579 " already created job/channel.
580 let ch = job_getchannel(job)
581 call assert_fails('let i = job + 1', 'E910:')
582 call assert_fails('let j = ch + 1', 'E913:')
583 call assert_fails('echo 2.0 == job', 'E911:')
584 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100585endfunc
586
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100587func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100588 " Add a dummy close callback to avoid that messages are dropped when calling
589 " ch_canread().
590 " Also test the non-blocking option.
591 let job = job_start(s:python . " test_channel_pipe.py",
592 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
593 call assert_equal(v:t_job, type(job))
594 call assert_equal("run", job_status(job))
595
596 call assert_equal("open", ch_status(job))
597 call assert_equal("open", ch_status(job), {"part": "out"})
598
599 try
600 " Create a blob with the echo command and write it.
601 let blob = 0z00
602 let cmd = "echo something\n"
603 for i in range(0, len(cmd) - 1)
604 let blob[i] = char2nr(cmd[i])
605 endfor
606 call assert_equal(len(cmd), len(blob))
607 call ch_sendraw(job, blob)
608
609 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200610 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100611 let expected = 'something'
612 for i in range(0, len(expected) - 1)
613 call assert_equal(char2nr(expected[i]), msg[i])
614 endfor
615
616 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
617 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
618 finally
619 call job_stop(job)
620 endtry
621
622 let g:Ch_job = job
623 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
624 let info = job_info(job)
625 call assert_equal("dead", info.status)
626endfunc
627
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100628func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100629 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100630 call assert_equal("run", job_status(job))
631 try
632 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100633 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200634 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100635
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100636 call ch_sendraw(handle, "echoerr wrong\n")
637 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
638
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100639 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100640 call assert_equal("this", ch_readraw(handle))
641 call assert_equal("AND this", ch_readraw(handle))
642
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200643 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200644 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200645
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100646 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100647 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100648 finally
649 call job_stop(job)
650 endtry
651endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100652
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653func Stop_g_job()
654 call job_stop(g:job)
655 if has('win32')
656 " On MS-Windows the server must close the file handle before we are able
657 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200658 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 sleep 10m
660 endif
661endfunc
662
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100663func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100664 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200665 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100666 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200667 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100668 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200669 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100670 call assert_equal("something", ch_readraw(handle))
671 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
672 call assert_equal("this", ch_readraw(handle))
673 call assert_equal("AND this", ch_readraw(handle))
674 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200675 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100676 call delete('Xinput')
677 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200678 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100679endfunc
680
Bram Moolenaare98d1212016-03-08 15:37:41 +0100681func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200682 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100683 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200684 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100685 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200686 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100687 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")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200690 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100691 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200692 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100693 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100694 call delete('Xoutput')
695 endtry
696endfunc
697
698func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200699 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100700 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200701 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100702 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200703 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100704 call ch_sendraw(handle, "echoerr line one\n")
705 call ch_sendraw(handle, "echoerr line two\n")
706 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200707 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100708 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200709 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100710 call delete('Xoutput')
711 endtry
712endfunc
713
714func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200715 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100716 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200717 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100718 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200719 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100720 call ch_sendraw(handle, "echoerr line one\n")
721 call ch_sendraw(handle, "echo line two\n")
722 call ch_sendraw(handle, "double this\n")
723 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200724 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100725 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200726 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100727 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100728 call delete('Xoutput')
729 endtry
730endfunc
731
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200732func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200733 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200734endfunc
735
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200736func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200737 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200738 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200739 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100740 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100741 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200742 if a:do_msg
743 let expected[0] = 'Reading from channel output...'
744 else
745 let options['out_msg'] = 0
746 call remove(expected, 0)
747 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100748 else
749 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100750 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100751 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200752 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100753 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200754 if a:nomod
755 let options['out_modifiable'] = 0
756 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100758 call assert_equal("run", job_status(job))
759 try
760 let handle = job_getchannel(job)
761 call ch_sendraw(handle, "echo line one\n")
762 call ch_sendraw(handle, "echo line two\n")
763 call ch_sendraw(handle, "double this\n")
764 call ch_sendraw(handle, "quit\n")
765 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100766 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200767 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200768 if a:nomod
769 call assert_equal(0, &modifiable)
770 else
771 call assert_equal(1, &modifiable)
772 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200773 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100774 bwipe!
775 finally
776 call job_stop(job)
777 endtry
778endfunc
779
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100780func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200781 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100782endfunc
783
784func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200785 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100786endfunc
787
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200788func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200789 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200790endfunc
791
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200792func Test_pipe_to_buffer_name_nomsg()
793 call Run_test_pipe_to_buffer(1, 0, 1)
794endfunc
795
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200796func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200797 enew!
798 let test_lines = ['one', 'two']
799 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200800 let options = {'out_io': 'buffer'}
801 let options['out_name'] = 'buffer-output'
802 let options['out_msg'] = 0
803 split buffer-output
804 let job = job_start(s:python . " test_channel_write.py", options)
805 call assert_equal("run", job_status(job))
806 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200807 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200808 quit!
809 sleep 100m
810 " Make sure the write didn't happen to the wrong buffer.
811 call assert_equal(test_lines, getline(1, line('$')))
812 call assert_equal(-1, bufwinnr('buffer-output'))
813 sbuf buffer-output
814 call assert_notequal(-1, bufwinnr('buffer-output'))
815 sleep 100m
816 close " no more writes
817 bwipe!
818 finally
819 call job_stop(job)
820 endtry
821endfunc
822
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200823func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100824 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200825 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100826 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100827 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200828 if a:do_msg
829 let expected[0] = 'Reading from channel error...'
830 else
831 let options['err_msg'] = 0
832 call remove(expected, 0)
833 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834 else
835 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100836 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100837 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200838 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100839 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200840 if a:nomod
841 let options['err_modifiable'] = 0
842 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100843 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100844 call assert_equal("run", job_status(job))
845 try
846 let handle = job_getchannel(job)
847 call ch_sendraw(handle, "echoerr line one\n")
848 call ch_sendraw(handle, "echoerr line two\n")
849 call ch_sendraw(handle, "doubleerr this\n")
850 call ch_sendraw(handle, "quit\n")
851 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200852 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200853 if a:nomod
854 call assert_equal(0, &modifiable)
855 else
856 call assert_equal(1, &modifiable)
857 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100858 bwipe!
859 finally
860 call job_stop(job)
861 endtry
862endfunc
863
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100864func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200865 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100866endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100867
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100868func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200869 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200870endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100871
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200872func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200873 call Run_test_pipe_err_to_buffer(1, 1, 1)
874endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100875
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200876func Test_pipe_err_to_buffer_name_nomsg()
877 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100878endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100879
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100880func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100881 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100882 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100883 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200884 let handle = job_getchannel(job)
885 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
886 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100887 try
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100888 call ch_sendraw(handle, "echo line one\n")
889 call ch_sendraw(handle, "echoerr line two\n")
890 call ch_sendraw(handle, "double this\n")
891 call ch_sendraw(handle, "doubleerr that\n")
892 call ch_sendraw(handle, "quit\n")
893 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200894 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 +0100895 bwipe!
896 finally
897 call job_stop(job)
898 endtry
899endfunc
900
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100901func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100902 sp pipe-input
903 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200904 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100905 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100906 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100907 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100908 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100909 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100910
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100911 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100912 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200913 if has('unix') && !a:use_name
914 call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
915 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100916 try
917 let handle = job_getchannel(job)
918 call assert_equal('one', ch_read(handle))
919 call assert_equal('two', ch_read(handle))
920 call assert_equal('three', ch_read(handle))
921 bwipe!
922 finally
923 call job_stop(job)
924 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100925endfunc
926
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100927func Test_pipe_from_buffer_name()
928 call Run_test_pipe_from_buffer(1)
929endfunc
930
931func Test_pipe_from_buffer_nr()
932 call Run_test_pipe_from_buffer(0)
933endfunc
934
Bram Moolenaar0874a832016-09-01 15:11:51 +0200935func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200936 CheckExecutable sort
937
Bram Moolenaar0874a832016-09-01 15:11:51 +0200938 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
939 if a:use_buffer
940 split sortin
941 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
942 let options.in_io = 'buffer'
943 let options.in_name = 'sortin'
944 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200945 if !a:all
946 let options.in_top = 2
947 let options.in_bot = 4
948 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100949 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200950
951 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100952 call assert_equal("run", job_status(job))
953 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200954 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200955 endif
956
Bram Moolenaare2956092019-01-25 21:01:17 +0100957 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200958
Bram Moolenaard8b55492016-09-01 14:35:22 +0200959 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200960 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200961 call assert_equal('Reading from channel output...', getline(1))
962 if a:all
963 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
964 else
965 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
966 endif
967
Bram Moolenaare2956092019-01-25 21:01:17 +0100968 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200969 if a:use_buffer
970 bwipe! sortin
971 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200972 bwipe! sortout
973endfunc
974
975func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200976 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200977endfunc
978
979func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200980 call Run_pipe_through_sort(0, 1)
981endfunc
982
983func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200984 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200985endfunc
986
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100987func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100988 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100989 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100990 call assert_equal("run", job_status(job))
991 try
992 let handle = job_getchannel(job)
993 call ch_sendraw(handle, "echo line one\n")
994 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200995 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200996 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100997 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
998 bwipe!
999 finally
1000 call job_stop(job)
1001 endtry
1002endfunc
1003
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001004func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001005 CheckFunction reltimefloat
1006
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001007 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001008 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001009 call assert_equal("run", job_status(job))
1010 try
1011 let handle = job_getchannel(job)
1012 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1013 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1014 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001015 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001016 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1017 bwipe!
1018 finally
1019 call job_stop(job)
1020 endtry
1021endfunc
1022
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001023" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001024func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001025 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001026 if getline(line('$') - a:offset) == a:line
1027 break
1028 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001029 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001030 endfor
1031endfunc
1032
1033func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001034 " Create two buffers, one to read from and one to write to.
1035 split pipe-output
1036 set buftype=nofile
1037 split pipe-input
1038 set buftype=nofile
1039
1040 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001041 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001042 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1043 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001044 call assert_equal("run", job_status(job))
1045 try
1046 exe "normal Gaecho hello\<CR>"
1047 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001048 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001049 call assert_equal('hello', getline('$'))
1050
1051 exe bufwinnr('pipe-input') . "wincmd w"
1052 exe "normal Gadouble this\<CR>"
1053 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001054 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001055 call assert_equal('this', getline(line('$') - 1))
1056 call assert_equal('AND this', getline('$'))
1057
1058 bwipe!
1059 exe bufwinnr('pipe-input') . "wincmd w"
1060 bwipe!
1061 finally
1062 call job_stop(job)
1063 endtry
1064endfunc
1065
1066func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001067 " Create one buffer to read from and to write to.
1068 split pipe-io
1069 set buftype=nofile
1070
1071 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001072 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001073 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1074 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001075 call assert_equal("run", job_status(job))
1076 try
1077 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001078 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001079 call assert_equal('hello', getline(line('$') - 1))
1080
1081 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001082 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001083 call assert_equal('this', getline(line('$') - 2))
1084 call assert_equal('AND this', getline(line('$') - 1))
1085
1086 bwipe!
1087 finally
1088 call job_stop(job)
1089 endtry
1090endfunc
1091
Bram Moolenaar4641a122019-07-29 22:10:23 +02001092func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001093 CheckScreendump
1094
Bram Moolenaar4641a122019-07-29 22:10:23 +02001095 let lines =<< trim END
1096 new Xscrollbuffer
1097 call setline(1, range(1, 200))
1098 $
1099 redraw
1100 wincmd w
1101 call deletebufline('Xscrollbuffer', 1, '$')
1102 if has('win32')
1103 let cmd = ['cmd', '/c', 'echo sometext']
1104 else
1105 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1106 endif
1107 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1108 END
1109 call writefile(lines, 'XtestBufferScroll')
1110 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001111 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001112 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1113
1114 " clean up
1115 call StopVimInTerminal(buf)
1116 call delete('XtestBufferScroll')
1117endfunc
1118
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001119func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001120 " We cannot check that no I/O works, we only check that the job starts
1121 " properly.
1122 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001123 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001124 call assert_equal("run", job_status(job))
1125 try
1126 call assert_equal('something', ch_read(job))
1127 finally
1128 call job_stop(job)
1129 endtry
1130
1131 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001132 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001133 call assert_equal("run", job_status(job))
1134 try
1135 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1136 finally
1137 call job_stop(job)
1138 endtry
1139
1140 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001141 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001142 call assert_equal("run", job_status(job))
1143 try
1144 call assert_equal('something', ch_read(job))
1145 finally
1146 call job_stop(job)
1147 endtry
1148
1149 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001150 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001151 call assert_equal("run", job_status(job))
1152 call job_stop(job)
1153
1154 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001155 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001156 call assert_equal("run", job_status(job))
1157 call assert_equal('channel fail', string(job_getchannel(job)))
1158 call assert_equal('fail', ch_status(job))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001159 call assert_equal('no process', string(test_null_job()))
1160 call assert_equal('channel fail', string(test_null_channel()))
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001161 call job_stop(job)
1162endfunc
1163
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001164func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001165 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1166 split testout
1167 let job = job_start([s:python, '-c',
1168 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001169 " the job may be done quickly, also accept "dead"
1170 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001171 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001172 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001173 let totlen = 0
1174 for line in getline(1, '$')
1175 call assert_equal('', substitute(line, '^\.*', '', ''))
1176 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001177 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001178 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001179 finally
1180 call job_stop(job)
1181 bwipe!
1182 endtry
1183endfunc
1184
Bram Moolenaarde279892016-03-11 22:19:44 +01001185func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001186 let job = job_start(s:python . " test_channel_pipe.py")
1187 call assert_equal("run", job_status(job))
1188 let handle = job_getchannel(job)
1189 try
1190 call ch_sendraw(handle, "echo something\n")
1191 call assert_equal("something", ch_readraw(handle))
1192 finally
1193 call job_stop(job)
1194 endtry
1195
1196 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1197 call assert_equal("run", job_status(job))
1198 let handle = job_getchannel(job)
1199 try
1200 call ch_sendraw(handle, "echo again\n")
1201 call assert_equal("again", ch_readraw(handle))
1202 finally
1203 call job_stop(job)
1204 endtry
1205endfunc
1206
Bram Moolenaar75f72652016-03-20 22:16:56 +01001207func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001208 let dict = {'thisis': 'dict: '}
1209 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001210 if type(a:msg) == v:t_string
1211 let g:Ch_outmsg = self.thisis . a:msg
1212 else
1213 let g:Ch_outobj = a:msg
1214 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001215 endfunc
1216 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001217 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001218 endfunc
1219 let job = job_start(s:python . " test_channel_pipe.py",
1220 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001221 \ 'out_mode': 'json',
1222 \ 'err_cb': dict.errHandler,
1223 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001224 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001225 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001226 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001227 let g:Ch_outmsg = ''
1228 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001229 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1230 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001231 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1232 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001233
1234 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001235 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001236 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001237 " For unknown reasons this can be very slow on Mac.
1238 if has('mac')
1239 let timeout = 20000
1240 else
1241 let timeout = 5000
1242 endif
1243 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001244 finally
1245 call job_stop(job)
1246 endtry
1247endfunc
1248
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001249func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001250 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001251 let g:Ch_msg1 = ''
1252 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001253 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001254 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001255 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001256 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001257 let s:counter += 1
1258 endfunc
1259 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001260 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001261 let s:counter += 1
1262 endfunc
1263 let job = job_start(s:python . " test_channel_pipe.py quit now",
1264 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001265 \ 'close_cb': 'CloseHandler'})
1266 " the job may be done quickly, also accept "dead"
1267 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001268 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001269 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1270 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001271 finally
1272 call job_stop(job)
1273 delfunc OutHandler
1274 delfunc CloseHandler
1275 endtry
1276endfunc
1277
Bram Moolenaar437905c2016-04-26 19:01:05 +02001278func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001279 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001280 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001281 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001282 endfunc
1283 let job = job_start(s:python . " test_channel_pipe.py quit now",
1284 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001285 " the job may be done quickly, also accept "dead"
1286 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001287 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001288 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001289 finally
1290 call job_stop(job)
1291 delfunc CloseHandler
1292 endtry
1293endfunc
1294
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001295" Use channel in NL mode but received text does not end in NL.
1296func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001297 let g:Ch_received = ''
1298 func! CloseHandler(chan)
1299 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1300 let g:Ch_received .= ch_read(a:chan)
1301 endwhile
1302 endfunc
1303 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1304 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001305 " the job may be done quickly, also accept "dead"
1306 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001307 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001308 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001309 finally
1310 call job_stop(job)
1311 delfunc CloseHandler
1312 endtry
1313endfunc
1314
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001315func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001316 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001317 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1318 \ 'out_mode': 'json',
1319 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1320 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001321 call assert_equal("run", job_status(job))
1322 try
1323 let g:Ch_outmsg = ''
1324 let g:Ch_errmsg = ''
1325 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1326 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001327 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1328 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001329 finally
1330 call job_stop(job)
1331 endtry
1332endfunc
1333
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001334func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001335 let g:retdict = {'ret': {}}
1336 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001337 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001338 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001339 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001340 let self.ret['exit_cb'] = job_status(a:job)
1341 endfunc
1342
Bram Moolenaare2956092019-01-25 21:01:17 +01001343 let job = job_start([&shell, &shellcmdflag, 'echo'],
1344 \ {'close_cb': g:retdict.close_cb,
1345 \ 'exit_cb': g:retdict.exit_cb})
1346 " the job may be done quickly, also accept "dead"
1347 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001348 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001349 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001350 call assert_equal('dead', g:retdict.ret['exit_cb'])
1351 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001352endfunc
1353
Bram Moolenaard46ae142016-02-16 13:33:52 +01001354""""""""""
1355
Bram Moolenaar0b146882018-09-06 16:27:24 +02001356function ExitCbWipe(job, status)
1357 exe g:wipe_buf 'bw!'
1358endfunction
1359
1360" This caused a crash, because messages were handled while peeking for a
1361" character.
1362func Test_exit_cb_wipes_buf()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001363 CheckFeature timers
Bram Moolenaar0b146882018-09-06 16:27:24 +02001364 set cursorline lazyredraw
1365 call test_override('redraw_flag', 1)
1366 new
1367 let g:wipe_buf = bufnr('')
1368
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001369 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001370 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001371 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1372 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1373 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1374 call timer_stop(timer)
1375
1376 set nocursorline nolazyredraw
1377 unlet g:wipe_buf
1378 call test_override('ALL', 0)
1379endfunc
1380
1381""""""""""
1382
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001383let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001384func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001385 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001386 unlet s:channelfd
1387endfunc
1388
1389" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001390func Ch_unlet_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001391 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001392 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001393 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001394endfunc
1395
1396func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001397 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001398endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001399
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001400func Test_unlet_handle_ipv6()
1401 CheckIPv6
1402 call Test_unlet_handle()
1403endfunc
1404
Bram Moolenaard46ae142016-02-16 13:33:52 +01001405""""""""""
1406
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001407let g:Ch_unletResponse = ''
1408func Ch_CloseHandler(handle, msg)
1409 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001410 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001411endfunc
1412
1413" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001414func Ch_close_handle(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001415 let s:channelfd = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001416 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001417 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001418endfunc
1419
1420func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001421 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001422endfunc
1423
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001424func Test_close_handle_ipv6()
1425 CheckIPv6
1426 call Test_close_handle()
1427endfunc
1428
1429""""""""""
1430
1431func Ch_open_ipv6(port)
1432 let handle = ch_open('[::1]:' .. a:port, s:chopt)
1433 call assert_notequal('fail', ch_status(handle))
1434endfunc
1435
1436func Test_open_ipv6()
1437 CheckIPv6
1438 call s:run_server('Ch_open_ipv6')
1439endfunc
1440
Bram Moolenaard46ae142016-02-16 13:33:52 +01001441""""""""""
1442
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001443func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001444 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001445 echo ch
1446 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001447 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1448 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001449 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001450 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1451 \ 'E474:')
1452 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1453 \ 'E475:')
1454 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1455 \ 'E475:')
1456 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1457 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001458 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1459 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1460 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001461endfunc
1462
1463func Test_ch_info_fail()
1464 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001465endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001466
1467""""""""""
1468
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001469func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001470 " Wait up to a second for the port to open.
1471 let s:chopt.waittime = 1000
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001472 let channel = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001473 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001474 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001475 return
1476 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001477 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001478 call ch_close(channel)
1479endfunc
1480
1481func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001482 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001483 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001484endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001485
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001486func Test_open_delay_ipv6()
1487 CheckIPv6
1488 call Test_open_delay()
1489endfunc
1490
Bram Moolenaarece61b02016-02-20 21:39:05 +01001491"""""""""
1492
1493function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001495endfunc
1496
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001497function Ch_test_call(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001498 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001499 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001500 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001501 return
1502 endif
1503
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001504 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001505 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001506 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001507
1508 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1509 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1510 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001511endfunc
1512
1513func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001514 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001515endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001516
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001517func Test_call_ipv6()
1518 CheckIPv6
1519 call Test_call()
1520endfunc
1521
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001522"""""""""
1523
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001524let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001525function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001526 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001527endfunc
1528
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001529function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001530 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001531 let g:Ch_exit_job = g:currentJob
1532 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001533endfunc
1534
1535func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001536 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001537
Bram Moolenaarf386f082019-07-29 23:03:03 +02001538 " wait up to a second for the job to exit
1539 for i in range(100)
1540 if g:Ch_job_exit_ret == 'done'
1541 break
1542 endif
1543 sleep 10m
1544 " calling job_status() triggers the callback
1545 call job_status(g:Ch_exit_job)
1546 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001547
Bram Moolenaarf386f082019-07-29 23:03:03 +02001548 call assert_equal('done', g:Ch_job_exit_ret)
1549 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1550 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001551endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001552
Bram Moolenaar97792de2016-10-15 18:36:49 +02001553function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001554 if job_info(a:job).process == g:exit_cb_val.process
1555 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1556 endif
1557 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001558endfunction
1559
1560func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001561 CheckFunction reltimefloat
1562
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001563 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001564 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001565 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001566 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001567 let elapsed = reltimefloat(g:exit_cb_val.end)
1568 call assert_true(elapsed > 0.5)
1569 call assert_true(elapsed < 1.0)
1570
1571 " case: unreferenced job, using timer
1572 if !has('timers')
1573 return
1574 endif
1575
1576 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1577 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1578 let g:exit_cb_val.process = job_info(g:job).process
1579 unlet g:job
1580 call Standby(1000)
1581 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1582 let elapsed = reltimefloat(g:exit_cb_val.end)
1583 else
1584 let elapsed = 1.0
1585 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001586 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001587endfunc
1588
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001589"""""""""
1590
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001591let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001592function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001593 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001594endfunc
1595
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001596function Ch_test_close_callback(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001597 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001598 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001599 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001600 return
1601 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001602 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001603
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001604 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001605 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001606endfunc
1607
1608func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001609 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001610endfunc
1611
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001612func Test_close_callback_ipv6()
1613 CheckIPv6
1614 call Test_close_callback()
1615endfunc
1616
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001617function Ch_test_close_partial(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001618 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001619 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001620 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001621 return
1622 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001623 let g:Ch_d = {}
1624 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001625 let self.close_ret = 'closed'
1626 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001627 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001628
1629 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001630 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001631 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001632endfunc
1633
1634func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001635 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001636endfunc
1637
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001638func Test_close_partial_ipv6()
1639 CheckIPv6
1640 call Test_close_partial()
1641endfunc
1642
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001643func Test_job_start_fails()
1644 " this was leaking memory
1645 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001646 call assert_fails('call job_start($x)', 'E474:')
1647 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001648 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1649 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1650 call assert_fails('call job_start("ls", [])', 'E715:')
1651 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1652 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1653 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001654 call assert_fails("call job_start('ls', {'callback' : -1})", 'E921:')
1655 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E921:')
1656 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E921:')
1657 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E921:')
1658 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E921:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001659 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1660 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1661 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001662 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001663 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1664 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1665 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1666 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1667 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1668 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1669 call assert_fails("call job_start('ls',
1670 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1671 call assert_fails("call job_start('ls',
1672 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1673 call assert_fails("call job_start('ls',
1674 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1675
1676 call assert_fails("call job_start('ls',
1677 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1678 call assert_fails("call job_start('ls',
1679 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1680 call assert_fails("call job_start('ls',
1681 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1682
1683 set nomodifiable
1684 call assert_fails("call job_start('cmd /c dir',
1685 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
1686 call assert_fails("call job_start('cmd /c dir',
1687 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1688 set modifiable
1689
1690 call assert_fails("call job_start('ls', {'in_io' : 'buffer'})", 'E915:')
1691
1692 edit! XXX
1693 let bnum = bufnr()
1694 enew
1695 call assert_fails("call job_start('ls',
1696 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1697
1698 " Empty job tests
1699 " This was crashing on MS-Windows.
1700 call assert_fails('let job = job_start([""])', 'E474:')
1701 call assert_fails('let job = job_start([" "])', 'E474:')
1702 call assert_fails('let job = job_start("")', 'E474:')
1703 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001704 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001705 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001706 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001707endfunc
1708
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001709func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001710 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001711 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001712 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001713 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001714 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001715 call job_stop(g:job, 'kill')
1716 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001717 endtry
1718endfunc
1719
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001720" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001721func Test_partial_in_channel_cycle()
1722 let d = {}
1723 let d.a = function('string', [d])
1724 try
1725 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001726 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001727 catch
1728 call assert_exception('E901:')
1729 endtry
1730 unlet d
1731endfunc
1732
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001733func Test_using_freed_memory()
1734 let g:a = job_start(['ls'])
1735 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001736 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001737endfunc
1738
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001739func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001740 CheckExecutable cat
1741
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001742 sp test_channel.vim
1743 let g:linecount = line('$')
1744 close
1745 split testout
1746 1,$delete
1747 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001748 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001749 bwipe!
1750endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001751
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001752func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001753 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001754 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001755
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001756 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001757 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001758 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001759 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1760 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001761
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001762 bdel test_buffer
1763 call assert_equal([], getbufline(bufnr, 1, '$'))
1764
1765 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001766 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001767 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1768 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1769
1770 bwipe! test_buffer
1771endfunc
1772
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001773func Test_cmd_parsing()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02001774 CheckUnix
1775
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001776 call assert_false(filereadable("file with space"))
1777 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001778 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001779 call delete("file with space")
1780
1781 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001782 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001783 call delete("file with space")
1784endfunc
1785
Bram Moolenaar82117982016-08-27 19:21:48 +02001786func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001787 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001788
1789 " Test lines from the job containing NUL are stored correctly in a buffer.
1790 new
1791 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1792 w! Xtestread
1793 bwipe!
1794 split testout
1795 1,$delete
1796 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1797 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001798 call assert_equal("asdf\nasdf", getline(1))
1799 call assert_equal("xxx\n", getline(2))
1800 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001801
1802 call delete('Xtestread')
1803 bwipe!
1804
1805 " Test lines from a buffer with NUL bytes are written correctly to the job.
1806 new mybuffer
1807 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1808 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 +02001809 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001810 bwipe!
1811 split Xtestwrite
1812 call assert_equal("asdf\nasdf", getline(1))
1813 call assert_equal("xxx\n", getline(2))
1814 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001815 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001816
1817 call delete('Xtestwrite')
1818 bwipe!
1819endfunc
1820
Bram Moolenaarec68a992016-10-03 21:37:41 +02001821func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001822 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001823 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001824 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001825 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001826 unlet g:linecount
1827endfunc
1828
1829func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001830 func s:close_cb(ch)
1831 while ch_status(a:ch) == 'buffered'
1832 let g:out .= ch_read(a:ch)
1833 endwhile
1834 endfunc
1835
1836 let g:out = ''
1837 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1838 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001839 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001840 call WaitForAssert({-> assert_equal('123', g:out)})
1841 unlet g:out
1842 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001843endfunc
1844
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001845func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001846 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001847 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001848 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001849 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001850 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001851 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001852endfunc
1853
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001854func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001855 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001856
1857 " Check that backslash in $COMSPEC is handled properly.
1858 let g:echostr = ''
1859 let cmd = $COMSPEC . ' /c echo 123'
1860 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1861 let info = job_info(job)
1862 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1863
1864 call WaitForAssert({-> assert_equal("123", g:echostr)})
1865 unlet g:echostr
1866endfunc
1867
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001868func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001869 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001870 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001871 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001872 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001873 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001874 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001875 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1876 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001877 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001878 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001879endfunc
1880
1881func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001882 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001883 if has('win32')
1884 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001885 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001886 else
1887 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001888 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001889 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001890 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001891 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001892 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001893 let expect = substitute(expect, '[/\\]$', '', '')
1894 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1895 if $CI != '' && stridx(g:envstr, '/private/') == 0
1896 let g:envstr = g:envstr[8:]
1897 endif
1898 call assert_equal(expect, g:envstr)
1899 finally
1900 call job_stop(job)
1901 unlet g:envstr
1902 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001903endfunc
1904
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001905function Ch_test_close_lambda(port)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001906 let handle = ch_open(s:localhost . a:port, s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001907 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001908 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001909 return
1910 endif
1911 let g:Ch_close_ret = ''
1912 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001913 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001914
1915 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001916 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001917endfunc
1918
1919func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001920 call s:run_server('Ch_test_close_lambda')
1921endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001922
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001923func Test_close_lambda_ipv6()
1924 CheckIPv6
1925 call Test_close_lambda()
1926endfunc
1927
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001928func s:test_list_args(cmd, out, remove_lf)
1929 try
1930 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001931 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 +02001932 call WaitFor('"" != g:out')
1933 if has('win32')
1934 let g:out = substitute(g:out, '\r', '', 'g')
1935 endif
1936 if a:remove_lf
1937 let g:out = substitute(g:out, '\n$', '', 'g')
1938 endif
1939 call assert_equal(a:out, g:out)
1940 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001941 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001942 unlet g:out
1943 endtry
1944endfunc
1945
1946func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001947 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1948 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1949 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1950 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1951 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1952 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1953 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1954 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1955 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1956 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1957 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1958 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1959 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1960
1961 " tests which not contain spaces in the argument
1962 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1963 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1964 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1965 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1966 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1967 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1968 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1969 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1970 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1971endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001972
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001973func Test_keep_pty_open()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02001974 CheckUnix
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001975
Bram Moolenaare2956092019-01-25 21:01:17 +01001976 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1977 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001978 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1979 call assert_inrange(200, 1000, elapsed)
1980 call job_stop(job)
1981endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001982
1983func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001984 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001985 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001986
1987 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001988 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001989 endfunc
1990
1991 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001992 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001993 call Resume()
1994 endfunc
1995
1996 func TimerCb(timer)
1997 if has('win32')
1998 let cmd = ['cmd', '/c', 'echo.']
1999 else
2000 let cmd = ['echo']
2001 endif
2002 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
2003 call substitute(repeat('a', 100000), '.', '', 'g')
2004 endfunc
2005
2006 " We should be interrupted before 'updatetime' elapsed.
2007 let g:val = 0
2008 call timer_start(1, 'TimerCb')
2009 let elapsed = Standby(&ut)
2010 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002011
2012 " Wait for both OutCb() and ExitCb() to have been called before deleting
2013 " them.
2014 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002015 call job_stop(g:job)
2016
2017 delfunc OutCb
2018 delfunc ExitCb
2019 delfunc TimerCb
2020 unlet! g:val
2021 unlet! g:job
2022endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002023
2024func Test_raw_large_data()
2025 try
2026 let g:out = ''
2027 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002028 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2029 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002030
Bram Moolenaare2956092019-01-25 21:01:17 +01002031 let outlen = 79999
2032 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002033 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002034 call WaitFor({-> len(g:out) >= outlen}, 10000)
2035 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002036 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2037 finally
2038 call job_stop(job)
2039 unlet g:out
2040 endtry
2041endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002042
Bram Moolenaar65240682019-02-10 22:23:26 +01002043func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002044 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002045
2046 try
2047 let job = job_start(s:python . " test_channel_pipe.py busy",
2048 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2049 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2050 finally
2051 call job_stop(job)
2052 endtry
2053endfunc
2054
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002055func Test_job_exitval_and_termsig()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002056 CheckUnix
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002057
2058 " Terminate job normally
2059 let cmd = ['echo']
2060 let job = job_start(cmd)
2061 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2062 let info = job_info(job)
2063 call assert_equal(0, info.exitval)
2064 call assert_equal("", info.termsig)
2065
2066 " Terminate job by signal
2067 let cmd = ['sleep', '10']
2068 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002069 " 10m usually works but 50m is needed when running Valgrind
2070 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002071 call job_stop(job)
2072 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2073 let info = job_info(job)
2074 call assert_equal(-1, info.exitval)
2075 call assert_equal("term", info.termsig)
2076endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002077
2078func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002079 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002080
2081 call writefile(['test'], 'Xtestin')
2082 let in_opts = [{},
2083 \ {'in_io': 'null'},
2084 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2085 let out_opts = [{},
2086 \ {'out_io': 'null'},
2087 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2088 let err_opts = [{},
2089 \ {'err_io': 'null'},
2090 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2091 \ {'err_io': 'out'}]
2092 let opts = []
2093
2094 for in_opt in in_opts
2095 let x = copy(in_opt)
2096 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002097 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002098 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002099 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002100 let opts += [extend({'pty': 1}, x)]
2101 endfor
2102 endfor
2103 endfor
2104
2105 for opt in opts
2106 let job = job_start('echo', opt)
2107 let info = job_info(job)
2108 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2109 \ get(opt, 'in_io', 'tty'),
2110 \ get(opt, 'out_io', 'tty'),
2111 \ get(opt, 'err_io', 'tty'))
2112
2113 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2114 call assert_notequal('', info.tty_in, msg)
2115 else
2116 call assert_equal('', info.tty_in, msg)
2117 endif
2118 call assert_equal(info.tty_in, info.tty_out, msg)
2119
2120 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2121 endfor
2122
2123 call delete('Xtestin')
2124 call delete('Xtestout')
2125 call delete('Xtesterr')
2126endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002127
2128" Do this last, it stops any channel log.
2129func Test_zz_nl_err_to_out_pipe()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002130
Bram Moolenaar570497a2019-08-22 22:55:13 +02002131 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002132 call ch_log('Test_zz_nl_err_to_out_pipe()')
2133 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2134 call assert_equal("run", job_status(job))
2135 try
2136 let handle = job_getchannel(job)
2137 call ch_sendraw(handle, "echo something\n")
2138 call assert_equal("something", ch_readraw(handle))
2139
2140 call ch_sendraw(handle, "echoerr wrong\n")
2141 call assert_equal("wrong", ch_readraw(handle))
2142 finally
2143 call job_stop(job)
2144 call ch_logfile('')
2145 let loglines = readfile('Xlog')
2146 call assert_true(len(loglines) > 10)
2147 let found_test = 0
2148 let found_send = 0
2149 let found_recv = 0
2150 let found_stop = 0
2151 for l in loglines
2152 if l =~ 'Test_zz_nl_err_to_out_pipe'
2153 let found_test = 1
2154 endif
2155 if l =~ 'SEND on.*echo something'
2156 let found_send = 1
2157 endif
2158 if l =~ 'RECV on.*something'
2159 let found_recv = 1
2160 endif
2161 if l =~ 'Stopping job with'
2162 let found_stop = 1
2163 endif
2164 endfor
2165 call assert_equal(1, found_test)
2166 call assert_equal(1, found_send)
2167 call assert_equal(1, found_recv)
2168 call assert_equal(1, found_stop)
2169 " On MS-Windows need to sleep for a moment to be able to delete the file.
2170 sleep 10m
2171 call delete('Xlog')
2172 endtry
2173endfunc
2174
2175" Do this last, it stops any channel log.
2176func Test_zz_ch_log()
2177 call ch_logfile('Xlog', 'w')
2178 call ch_log('hello there')
2179 call ch_log('%s%s')
2180 call ch_logfile('')
2181 let text = readfile('Xlog')
2182 call assert_match("hello there", text[1])
2183 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002184 call mkdir("Xdir1")
2185 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2186 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002187 call delete('Xlog')
2188endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002189
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002190func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002191 if has('win32')
2192 let cmd = 'cmd /c pause'
2193 else
2194 let cmd = 'grep foo'
2195 endif
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002196
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002197 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002198 sleep 50m " give the job time to start
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002199 call job_stop(g:job)
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002200 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2201
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002202 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002203 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002204 call job_stop(g:job, 'term')
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002205 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2206
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002207 let g:job = job_start(cmd, {})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002208 sleep 50m
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002209 call job_stop(g:job, 'kill')
2210 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002211endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002212
2213func Test_issue_5485()
2214 let $VAR1 = 'global'
2215 let g:Ch_reply = ""
2216 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2217 let g:Ch_job = l:job
2218 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2219 unlet $VAR1
2220endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002221
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002222func Test_job_trailing_space_unix()
2223 CheckUnix
2224 CheckExecutable cat
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002225
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002226 let job = job_start("cat ", #{in_io: 'null'})
2227 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2228 call assert_equal(0, job_info(job).exitval)
2229endfunc
2230
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002231func Test_ch_getbufnr()
2232 let ch = test_null_channel()
2233 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2234 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2235 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2236 call assert_equal(-1, ch_getbufnr(ch, ''))
2237endfunc
2238
2239" Test for unsupported options passed to ch_status()
2240func Test_invalid_job_chan_options()
2241 let ch = test_null_channel()
2242 let invalid_opts = [
2243 \ {'in_io' : 'null'},
2244 \ {'out_io' : 'null'},
2245 \ {'err_io' : 'null'},
2246 \ {'mode' : 'json'},
2247 \ {'out_mode' : 'json'},
2248 \ {'err_mode' : 'json'},
2249 \ {'noblock' : 1},
2250 \ {'in_name' : '/a/b'},
2251 \ {'pty' : 1},
2252 \ {'in_buf' : 1},
2253 \ {'out_buf' : 1},
2254 \ {'err_buf' : 1},
2255 \ {'out_modifiable' : 1},
2256 \ {'err_modifiable' : 1},
2257 \ {'out_msg' : 1},
2258 \ {'err_msg' : 1},
2259 \ {'in_top' : 1},
2260 \ {'in_bot' : 1},
2261 \ {'channel' : ch},
2262 \ {'callback' : ''},
2263 \ {'out_cb' : ''},
2264 \ {'err_cb' : ''},
2265 \ {'close_cb' : ''},
2266 \ {'exit_cb' : ''},
2267 \ {'term_opencmd' : ''},
2268 \ {'eof_chars' : ''},
2269 \ {'term_rows' : 10},
2270 \ {'term_cols' : 10},
2271 \ {'vertical' : 0},
2272 \ {'curwin' : 1},
2273 \ {'bufnr' : 1},
2274 \ {'hidden' : 0},
2275 \ {'norestore' : 0},
2276 \ {'term_kill' : 'kill'},
2277 \ {'tty_type' : ''},
2278 \ {'term_highlight' : ''},
2279 \ {'env' : {}},
2280 \ {'cwd' : ''},
2281 \ {'timeout' : 0},
2282 \ {'out_timeout' : 0},
2283 \ {'err_timeout' : 0},
2284 \ {'id' : 0},
2285 \ {'stoponexit' : ''},
2286 \ {'block_write' : 1}
2287 \ ]
2288 if has('gui')
2289 call add(invalid_opts, {'ansi_colors' : []})
2290 endif
2291
2292 for opt in invalid_opts
2293 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2294 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002295 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002296endfunc
2297
2298" Test for passing the command and the arguments as List on MS-Windows
2299func Test_job_with_list_args()
2300 CheckMSWindows
2301
2302 enew!
2303 let bnum = bufnr()
2304 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2305 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2306 call assert_equal('Hello World', getline(1))
2307 %bw!
2308endfunc
2309
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002310func ExitCb_cb_with_input(job, status)
2311 call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx')
2312 call assert_equal('default', Screenline(&lines))
2313 let g:wait_exit_cb = 0
2314endfunc
2315
2316func Test_cb_with_input()
2317 let g:wait_exit_cb = 1
2318
Bram Moolenaarf637bce2020-11-23 18:14:56 +01002319 if has('win32')
2320 let cmd = 'cmd /c echo "Vim''s test"'
2321 else
2322 let cmd = 'echo "Vim''s test"'
2323 endif
2324
2325 let job = job_start(cmd, {'out_cb': 'ExitCb_cb_with_input'})
2326 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002327 call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)})
2328
2329 unlet g:wait_exit_cb
2330endfunc
2331
Bram Moolenaar8b633132020-03-20 18:20:51 +01002332" vim: shiftwidth=2 sts=2 expandtab