blob: dcd05266905de62c55a8f8b165d982d8d3468b12 [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 +02004CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01005
Christian Brabandteb380b92025-07-07 20:53:55 +02006source util/screendump.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007
8let s:python = PythonProg()
9if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010010 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020011 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010012endif
13
Bram Moolenaar37175402017-03-18 20:18:45 +010014" Uncomment the next line to see what happens. Output is in
15" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020016" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010017" call ch_logfile('channellog', 'w')
18
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020019func SetUp()
Bram Moolenaar94722c52023-01-28 19:19:03 +000020 if g:testfunc =~ '_ipv6()$'
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020021 let s:localhost = '[::1]:'
22 let s:testscript = 'test_channel_6.py'
LemonBoycc766a82022-04-04 15:46:58 +010023 elseif g:testfunc =~ '_unix()$'
24 let s:localhost = 'unix:Xtestsocket'
25 let s:testscript = 'test_channel_unix.py'
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020026 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
LemonBoycc766a82022-04-04 15:46:58 +010042" Returns the address of the test server.
43func s:address(port)
44 if s:localhost =~ '^unix:'
45 return s:localhost
46 else
47 return s:localhost . a:port
48 end
49endfunc
50
Bram Moolenaar819524702018-02-27 19:10:00 +010051" Return a list of open files.
52" Can be used to make sure no resources leaked.
53" Returns an empty list on systems where this is not supported.
54func s:get_resources()
55 let pid = getpid()
56
Bram Moolenaar39536dd2019-01-29 22:58:21 +010057 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010058 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
59 elseif isdirectory('/proc/' . pid . '/fd/')
60 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
61 else
62 return []
63 endif
64endfunc
65
Bram Moolenaar321efdd2016-07-15 17:09:11 +020066let g:Ch_responseMsg = ''
67func Ch_requestHandler(handle, msg)
68 let g:Ch_responseHandle = a:handle
69 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010070endfunc
71
Bram Moolenaar321efdd2016-07-15 17:09:11 +020072func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010073 " Avoid dropping messages, since we don't use a callback here.
74 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020075 " Also add the noblock flag to try it out.
76 let s:chopt.noblock = 1
LemonBoycc766a82022-04-04 15:46:58 +010077 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010078 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010079 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010080 return
81 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020082
83 " check that getjob without a job is handled correctly
84 call assert_equal('no process', string(ch_getjob(handle)))
85
Bram Moolenaar570497a2019-08-22 22:55:13 +020086 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010087 call assert_true(dict.id != 0)
88 call assert_equal('open', dict.status)
LemonBoycc766a82022-04-04 15:46:58 +010089 if has_key(dict, 'port')
90 " Channels using Unix sockets have no 'port' entry.
91 call assert_equal(a:port, string(dict.port))
92 end
Bram Moolenaar03602ec2016-03-20 20:57:45 +010093 call assert_equal('open', dict.sock_status)
94 call assert_equal('socket', dict.sock_io)
95
Bram Moolenaard7ece102016-02-02 23:23:02 +010096 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010097 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010098
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010099 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100100 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
101 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
102 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
103
104 " split command should work
105 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200106 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100107 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100108
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200109 " string with ][ should work
110 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
111
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100112 " nothing to read now
113 call assert_equal(0, ch_canread(handle))
114
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200115 " sending three messages quickly then reading should work
116 for i in range(3)
117 call ch_sendexpr(handle, 'echo hello ' . i)
118 endfor
119 call assert_equal('hello 0', ch_read(handle)[1])
120 call assert_equal('hello 1', ch_read(handle)[1])
121 call assert_equal('hello 2', ch_read(handle)[1])
122
Bram Moolenaard7ece102016-02-02 23:23:02 +0100123 " Request that triggers sending two ex commands. These will usually be
124 " handled before getting the response, but it's not guaranteed, thus wait a
125 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100126 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200127 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100128 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100129
Bram Moolenaarb836f632021-07-01 22:11:28 +0200130 " Request command "echoerr 'this is an error'".
131 " This will throw an exception, catch it here.
132 let caught = 'no'
133 try
134 call assert_equal('ok', ch_evalexpr(handle, 'echoerr'))
135 catch /this is an error/
136 let caught = 'yes'
137 endtry
138 if caught != 'yes'
139 call assert_report("Expected exception from error message")
140 endif
141
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100142 " Request command "foo bar", which fails silently.
143 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200144 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100145
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100146 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200147 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100148
Bram Moolenaara07fec92016-02-05 21:04:08 +0100149 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200150 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
151 call WaitFor('exists("g:Ch_responseHandle")')
152 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100153 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100154 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200155 call assert_equal(handle, g:Ch_responseHandle)
156 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100157 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200158 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100159
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200160 let g:Ch_responseMsg = ''
161 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
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 Moolenaar77073442016-02-13 23:23:53 +0100165 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200166 call assert_equal(handle, g:Ch_responseHandle)
167 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100168 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200169 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100170
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200171 " Using lambda.
172 let g:Ch_responseMsg = ''
173 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
174 call WaitFor('exists("g:Ch_responseHandle")')
175 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100176 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200177 else
178 call assert_equal(handle, g:Ch_responseHandle)
179 unlet g:Ch_responseHandle
180 endif
181 call assert_equal('got it', g:Ch_responseMsg)
182
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100183 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200184 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100185
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100186 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200187 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100188 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100189 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200190 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", 'E475:')
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100191 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100192 call ch_setoptions(handle, {'drop': 'never'})
193 call ch_setoptions(handle, {'drop': 'auto'})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200194 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", 'E475:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200195 call assert_equal(0, ch_setoptions(handle, test_null_dict()))
196 call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'}))
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100197
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100198 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100199 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100200 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100202
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100203 " Send an eval request with special characters.
204 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
205 sleep 10m
206 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
207
208 " Send an eval request to get a line with special characters.
209 call setline(3, "a\nb\<CR>c\x01d\x7fe")
210 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
211 sleep 10m
212 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
213
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100214 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100215 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100216 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100217 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100218
Bram Moolenaar55fab432016-02-07 16:53:13 +0100219 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100221 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100222 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100223
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100224 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100225 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100226 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100227 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100228
Bram Moolenaarf4160862016-02-05 23:09:12 +0100229 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100230 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200231 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100232 call assert_equal('one', getline(line('$') - 2))
233 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100234
235 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100236 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
237 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100238
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100240
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100241 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100242 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100243 if exists('*reltimefloat')
244 let start = reltime()
245 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
246 let elapsed = reltime(start)
247 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
248 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100249
250 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100251 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100252 let resp = ch_read(handle)
253 call assert_equal(type([]), type(resp))
254 call assert_equal(type(11), type(resp[0]))
255 call assert_equal('waited', resp[1])
256
Bram Moolenaard7ece102016-02-02 23:23:02 +0100257 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100258 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100259endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100260
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100261func Test_communicate()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200262 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100263endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100264
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200265func Test_communicate_ipv6()
266 CheckIPv6
267 call Test_communicate()
268endfunc
269
LemonBoycc766a82022-04-04 15:46:58 +0100270func Test_communicate_unix()
271 CheckUnix
272 call Test_communicate()
273 call delete('Xtestsocket')
274endfunc
275
276
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100277" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200278func Ch_two_channels(port)
LemonBoycc766a82022-04-04 15:46:58 +0100279 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200280 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200281 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100282 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100283 return
284 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100285
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100286 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100287
LemonBoycc766a82022-04-04 15:46:58 +0100288 let newhandle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100289 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100290 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100291 return
292 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100293 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
294 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100295
296 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100297 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100298
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100299 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200300 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100301endfunc
302
303func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200304 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200305 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100306endfunc
307
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200308func Test_two_channels_ipv6()
309 CheckIPv6
310 call Test_two_channels()
311endfunc
312
LemonBoycc766a82022-04-04 15:46:58 +0100313func Test_two_channels_unix()
314 CheckUnix
315 call Test_two_channels()
316 call delete('Xtestsocket')
317endfunc
318
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100319" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200320func Ch_server_crash(port)
LemonBoycc766a82022-04-04 15:46:58 +0100321 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100322 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100323 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100324 return
325 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100326
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100327 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100328
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100329 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100330endfunc
331
332func Test_server_crash()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100334endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100335
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200336func Test_server_crash_ipv6()
337 CheckIPv6
338 call Test_server_crash()
339endfunc
340
LemonBoycc766a82022-04-04 15:46:58 +0100341func Test_server_crash_unix()
342 CheckUnix
343 call Test_server_crash()
344 call delete('Xtestsocket')
345endfunc
346
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100347"""""""""
348
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200349func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100350 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351 unlet g:Ch_reply
352 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100353endfunc
354
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200355func Ch_channel_handler(port)
LemonBoycc766a82022-04-04 15:46:58 +0100356 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100357 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100358 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100359 return
360 endif
361
362 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100363 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200364 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100365
366 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100367 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200368 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100369endfunc
370
371func Test_channel_handler()
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200372 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200373 let s:chopt.callback = 'Ch_handler'
374 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200375 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376 let s:chopt.callback = function('Ch_handler')
377 call s:run_server('Ch_channel_handler')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200378endfunc
379
380func Test_channel_handler_ipv6()
381 CheckIPv6
382 call Test_channel_handler()
Bram Moolenaarf6157282016-02-10 21:07:14 +0100383endfunc
384
LemonBoycc766a82022-04-04 15:46:58 +0100385func Test_channel_handler_unix()
386 CheckUnix
387 call Test_channel_handler()
388 call delete('Xtestsocket')
389endfunc
390
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100391"""""""""
392
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200393let g:Ch_reply = ''
394func Ch_zeroHandler(chan, msg)
395 unlet g:Ch_reply
396 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100397endfunc
398
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200399let g:Ch_zero_reply = ''
400func Ch_oneHandler(chan, msg)
401 unlet g:Ch_zero_reply
402 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100403endfunc
404
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200405func Ch_channel_zero(port)
LemonBoycc766a82022-04-04 15:46:58 +0100406 let handle = (s:address(a:port))->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100407 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100408 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100409 return
410 endif
411
412 " Check that eval works.
413 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
414
415 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200416 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100417 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100418 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200419 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100420 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100421 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200422 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100423 endif
424
425 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200426 let g:Ch_reply = ''
427 let g:Ch_zero_reply = ''
428 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200429 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100430 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200431 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100432 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200433 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100434 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100435endfunc
436
437func Test_zero_reply()
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100438 " Run with channel handler
439 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200440 let s:chopt.callback = 'Ch_zeroHandler'
441 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100442 unlet s:chopt.callback
443
444 " Run without channel handler
445 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200446 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100447endfunc
448
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200449func Test_zero_reply_ipv6()
450 CheckIPv6
451 call Test_zero_reply()
452endfunc
453
LemonBoycc766a82022-04-04 15:46:58 +0100454func Test_zero_reply_unix()
455 CheckUnix
456 call Test_zero_reply()
457 call delete('Xtestsocket')
458endfunc
459
460
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100461"""""""""
462
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200463let g:Ch_reply1 = ""
464func Ch_handleRaw1(chan, msg)
465 unlet g:Ch_reply1
466 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100467endfunc
468
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200469let g:Ch_reply2 = ""
470func Ch_handleRaw2(chan, msg)
471 unlet g:Ch_reply2
472 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100473endfunc
474
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200475let g:Ch_reply3 = ""
476func Ch_handleRaw3(chan, msg)
477 unlet g:Ch_reply3
478 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100479endfunc
480
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200481func Ch_raw_one_time_callback(port)
LemonBoycc766a82022-04-04 15:46:58 +0100482 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100483 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100484 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100485 return
486 endif
487 call ch_setoptions(handle, {'mode': 'raw'})
488
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100489 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200490 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200491 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200492 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
493 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200494 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100495 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200496 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100497endfunc
498
499func Test_raw_one_time_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200500 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100501endfunc
502
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200503func Test_raw_one_time_callback_ipv6()
504 CheckIPv6
505 call Test_raw_one_time_callback()
506endfunc
507
LemonBoycc766a82022-04-04 15:46:58 +0100508func Test_raw_one_time_callback_unix()
509 CheckUnix
510 call Test_raw_one_time_callback()
511 call delete('Xtestsocket')
512endfunc
513
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100514"""""""""
515
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100516" Test that trying to connect to a non-existing port fails quickly.
517func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100518 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100519 " this is timing sensitive
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100520
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100521 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100522 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100523 if ch_status(handle) != "fail"
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200524 " Oops, port exists.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100525 call ch_close(handle)
526 else
527 let elapsed = reltime(start)
zeertzjqcdc6a432022-06-19 11:45:46 +0100528 call assert_inrange(0.0, 1.0, reltimefloat(elapsed))
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100529 endif
530
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100531 " We intend to use a socket that doesn't exist and wait for half a second
532 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100533 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100534 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100535 try
536 let handle = ch_open('localhost:9867', {'waittime': 500})
537 if ch_status(handle) != "fail"
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200538 " Oops, port exists.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100539 call ch_close(handle)
540 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100541 " Failed connection should wait about 500 msec. Can be longer if the
542 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100543 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100544 endif
545 catch
546 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100547 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100548 endif
549 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100550endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100551
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100552"""""""""
553
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100554func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100555 " Add a dummy close callback to avoid that messages are dropped when calling
556 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200557 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100558 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200559 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200560 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100561 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200562
563 call assert_equal("open", ch_status(job))
564 call assert_equal("open", ch_status(job), {"part": "out"})
565 call assert_equal("open", ch_status(job), {"part": "err"})
566 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
567 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
568
569 let dict = ch_info(job)
570 call assert_true(dict.id != 0)
571 call assert_equal('open', dict.status)
572 call assert_equal('open', dict.out_status)
573 call assert_equal('RAW', dict.out_mode)
574 call assert_equal('pipe', dict.out_io)
575 call assert_equal('open', dict.err_status)
576 call assert_equal('RAW', dict.err_mode)
577 call assert_equal('pipe', dict.err_io)
578
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100579 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100580 " For a change use the job where a channel is expected.
581 call ch_sendraw(job, "echo something\n")
582 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100583 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
584
Bram Moolenaar151f6562016-03-07 21:19:38 +0100585 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200586 let g:handle = job->job_getchannel()
587 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100588 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100589 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100590 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
591
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200592 let g:Ch_reply = ""
593 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200594 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200595
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200596 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
597 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
598 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
599 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
600
Bram Moolenaar570497a2019-08-22 22:55:13 +0200601 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100602 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
603 finally
604 call job_stop(job)
605 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100606
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200607 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200608 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200609 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100610 call assert_equal("dead", info.status)
611 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200612 call assert_equal(2, len(info.cmd))
613 call assert_equal("test_channel_pipe.py", info.cmd[1])
614
615 let found = 0
616 for j in job_info()
617 if j == job
618 let found += 1
619 endif
620 endfor
621 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100622
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200623 call assert_fails("call job_stop('abc')", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200624 call assert_fails("call job_stop(job, [])", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200625 call assert_fails("call job_stop(test_null_job())", 'E916:')
626
Bram Moolenaar8b633132020-03-20 18:20:51 +0100627 " Try to use the job and channel where a number is expected. This is not
628 " related to testing the raw pipe. This test is here just to reuse the
629 " already created job/channel.
630 let ch = job_getchannel(job)
631 call assert_fails('let i = job + 1', 'E910:')
632 call assert_fails('let j = ch + 1', 'E913:')
633 call assert_fails('echo 2.0 == job', 'E911:')
634 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100635endfunc
636
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100637func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100638 " Add a dummy close callback to avoid that messages are dropped when calling
639 " ch_canread().
640 " Also test the non-blocking option.
641 let job = job_start(s:python . " test_channel_pipe.py",
642 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
643 call assert_equal(v:t_job, type(job))
644 call assert_equal("run", job_status(job))
645
646 call assert_equal("open", ch_status(job))
647 call assert_equal("open", ch_status(job), {"part": "out"})
648
649 try
650 " Create a blob with the echo command and write it.
651 let blob = 0z00
652 let cmd = "echo something\n"
653 for i in range(0, len(cmd) - 1)
654 let blob[i] = char2nr(cmd[i])
655 endfor
656 call assert_equal(len(cmd), len(blob))
657 call ch_sendraw(job, blob)
658
659 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200660 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100661 let expected = 'something'
662 for i in range(0, len(expected) - 1)
663 call assert_equal(char2nr(expected[i]), msg[i])
664 endfor
665
666 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
667 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
668 finally
669 call job_stop(job)
670 endtry
671
672 let g:Ch_job = job
673 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
674 let info = job_info(job)
675 call assert_equal("dead", info.status)
676endfunc
677
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100678func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100679 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100680 call assert_equal("run", job_status(job))
681 try
682 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100683 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200684 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100685
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100686 call ch_sendraw(handle, "echoerr wrong\n")
687 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
688
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100689 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100690 call assert_equal("this", ch_readraw(handle))
691 call assert_equal("AND this", ch_readraw(handle))
692
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200693 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200694 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200695
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100696 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100697 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100698 finally
699 call job_stop(job)
700 endtry
701endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100702
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200703func Stop_g_job()
704 call job_stop(g:job)
705 if has('win32')
706 " On MS-Windows the server must close the file handle before we are able
707 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200708 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200709 sleep 10m
710 endif
711endfunc
712
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100713func Test_nl_read_file()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100714 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput', 'D')
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 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200717 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100718 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200719 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100720 call assert_equal("something", ch_readraw(handle))
721 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
722 call assert_equal("this", ch_readraw(handle))
723 call assert_equal("AND this", ch_readraw(handle))
724 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200725 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100726 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200727 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100728endfunc
729
Bram Moolenaare98d1212016-03-08 15:37:41 +0100730func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200731 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100732 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200733 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100734 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200735 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100736 call ch_sendraw(handle, "echo line one\n")
737 call ch_sendraw(handle, "echo line two\n")
738 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200739 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100740 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200741 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100742 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100743 call delete('Xoutput')
744 endtry
745endfunc
746
747func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200748 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100749 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200750 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100751 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200752 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100753 call ch_sendraw(handle, "echoerr line one\n")
754 call ch_sendraw(handle, "echoerr line two\n")
755 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200756 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100757 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200758 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100759 call delete('Xoutput')
760 endtry
761endfunc
762
763func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200764 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100765 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200766 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100767 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200768 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100769 call ch_sendraw(handle, "echoerr line one\n")
770 call ch_sendraw(handle, "echo line two\n")
771 call ch_sendraw(handle, "double this\n")
772 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200773 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100774 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200775 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100776 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100777 call delete('Xoutput')
778 endtry
779endfunc
780
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200781func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200782 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200783endfunc
784
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200785func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200786 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200787 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200788 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100789 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100790 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200791 if a:do_msg
792 let expected[0] = 'Reading from channel output...'
793 else
794 let options['out_msg'] = 0
795 call remove(expected, 0)
796 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100797 else
798 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100799 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100800 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200801 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100802 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200803 if a:nomod
804 let options['out_modifiable'] = 0
805 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100806 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100807 call assert_equal("run", job_status(job))
808 try
809 let handle = job_getchannel(job)
810 call ch_sendraw(handle, "echo line one\n")
811 call ch_sendraw(handle, "echo line two\n")
812 call ch_sendraw(handle, "double this\n")
813 call ch_sendraw(handle, "quit\n")
814 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100815 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200816 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200817 if a:nomod
818 call assert_equal(0, &modifiable)
819 else
820 call assert_equal(1, &modifiable)
821 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200822 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100823 bwipe!
824 finally
825 call job_stop(job)
826 endtry
827endfunc
828
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100829func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200830 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100831endfunc
832
833func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200834 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100835endfunc
836
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200837func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200838 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200839endfunc
840
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200841func Test_pipe_to_buffer_name_nomsg()
842 call Run_test_pipe_to_buffer(1, 0, 1)
843endfunc
844
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200845func Test_close_output_buffer()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100846 let g:test_is_flaky = 1
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200847 enew!
848 let test_lines = ['one', 'two']
849 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200850 let options = {'out_io': 'buffer'}
851 let options['out_name'] = 'buffer-output'
852 let options['out_msg'] = 0
853 split buffer-output
854 let job = job_start(s:python . " test_channel_write.py", options)
855 call assert_equal("run", job_status(job))
856 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200857 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200858 quit!
859 sleep 100m
860 " Make sure the write didn't happen to the wrong buffer.
861 call assert_equal(test_lines, getline(1, line('$')))
862 call assert_equal(-1, bufwinnr('buffer-output'))
863 sbuf buffer-output
864 call assert_notequal(-1, bufwinnr('buffer-output'))
865 sleep 100m
866 close " no more writes
867 bwipe!
868 finally
869 call job_stop(job)
870 endtry
871endfunc
872
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200873func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100874 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200875 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100876 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100877 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200878 if a:do_msg
879 let expected[0] = 'Reading from channel error...'
880 else
881 let options['err_msg'] = 0
882 call remove(expected, 0)
883 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100884 else
885 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100886 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100887 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200888 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100889 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200890 if a:nomod
891 let options['err_modifiable'] = 0
892 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100893 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100894 call assert_equal("run", job_status(job))
895 try
896 let handle = job_getchannel(job)
897 call ch_sendraw(handle, "echoerr line one\n")
898 call ch_sendraw(handle, "echoerr line two\n")
899 call ch_sendraw(handle, "doubleerr this\n")
900 call ch_sendraw(handle, "quit\n")
901 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200902 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200903 if a:nomod
904 call assert_equal(0, &modifiable)
905 else
906 call assert_equal(1, &modifiable)
907 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100908 bwipe!
909 finally
910 call job_stop(job)
911 endtry
912endfunc
913
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100914func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200915 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100916endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100917
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100918func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200919 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200920endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100921
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200922func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200923 call Run_test_pipe_err_to_buffer(1, 1, 1)
924endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100925
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200926func Test_pipe_err_to_buffer_name_nomsg()
927 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100928endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100929
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100930func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100931 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100932 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100933 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200934 let handle = job_getchannel(job)
935 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
936 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100937 try
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100938 call ch_sendraw(handle, "echo line one\n")
939 call ch_sendraw(handle, "echoerr line two\n")
940 call ch_sendraw(handle, "double this\n")
941 call ch_sendraw(handle, "doubleerr that\n")
942 call ch_sendraw(handle, "quit\n")
943 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200944 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 +0100945 bwipe!
946 finally
947 call job_stop(job)
948 endtry
949endfunc
950
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100951func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100952 sp pipe-input
953 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200954 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100955 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100956 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100957 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100958 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100959 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100960
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100961 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100962 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200963 if has('unix') && !a:use_name
964 call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
965 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100966 try
967 let handle = job_getchannel(job)
968 call assert_equal('one', ch_read(handle))
969 call assert_equal('two', ch_read(handle))
970 call assert_equal('three', ch_read(handle))
971 bwipe!
972 finally
973 call job_stop(job)
974 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100975endfunc
976
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100977func Test_pipe_from_buffer_name()
978 call Run_test_pipe_from_buffer(1)
979endfunc
980
981func Test_pipe_from_buffer_nr()
982 call Run_test_pipe_from_buffer(0)
983endfunc
984
Bram Moolenaar0874a832016-09-01 15:11:51 +0200985func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200986 CheckExecutable sort
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100987 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200988
Bram Moolenaar0874a832016-09-01 15:11:51 +0200989 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
990 if a:use_buffer
991 split sortin
992 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
993 let options.in_io = 'buffer'
994 let options.in_name = 'sortin'
995 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200996 if !a:all
997 let options.in_top = 2
998 let options.in_bot = 4
999 endif
Bram Moolenaare2956092019-01-25 21:01:17 +01001000 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +02001001
1002 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +01001003 call assert_equal("run", job_status(job))
1004 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +02001005 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001006 endif
1007
Bram Moolenaare2956092019-01-25 21:01:17 +01001008 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +02001009
Bram Moolenaard8b55492016-09-01 14:35:22 +02001010 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +02001011 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +02001012 call assert_equal('Reading from channel output...', getline(1))
1013 if a:all
1014 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
1015 else
1016 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
1017 endif
1018
Bram Moolenaare2956092019-01-25 21:01:17 +01001019 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +02001020 if a:use_buffer
1021 bwipe! sortin
1022 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +02001023 bwipe! sortout
1024endfunc
1025
1026func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001027 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001028endfunc
1029
1030func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001031 call Run_pipe_through_sort(0, 1)
1032endfunc
1033
1034func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001035 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001036endfunc
1037
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001038func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001039 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001040 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001041 call assert_equal("run", job_status(job))
1042 try
1043 let handle = job_getchannel(job)
1044 call ch_sendraw(handle, "echo line one\n")
1045 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +02001046 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001047 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001048 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
1049 bwipe!
1050 finally
1051 call job_stop(job)
1052 endtry
1053endfunc
1054
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001055func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001056 CheckFunction reltimefloat
1057
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001058 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001059 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001060 call assert_equal("run", job_status(job))
1061 try
1062 let handle = job_getchannel(job)
1063 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1064 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1065 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001066 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001067 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1068 bwipe!
1069 finally
1070 call job_stop(job)
1071 endtry
1072endfunc
1073
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001074" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001075func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001076 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001077 if getline(line('$') - a:offset) == a:line
1078 break
1079 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001080 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001081 endfor
1082endfunc
1083
1084func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001085 " Create two buffers, one to read from and one to write to.
1086 split pipe-output
1087 set buftype=nofile
1088 split pipe-input
1089 set buftype=nofile
1090
1091 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001092 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001093 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1094 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001095 call assert_equal("run", job_status(job))
1096 try
1097 exe "normal Gaecho hello\<CR>"
1098 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001099 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001100 call assert_equal('hello', getline('$'))
1101
1102 exe bufwinnr('pipe-input') . "wincmd w"
1103 exe "normal Gadouble this\<CR>"
1104 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001105 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001106 call assert_equal('this', getline(line('$') - 1))
1107 call assert_equal('AND this', getline('$'))
1108
1109 bwipe!
1110 exe bufwinnr('pipe-input') . "wincmd w"
1111 bwipe!
1112 finally
1113 call job_stop(job)
1114 endtry
1115endfunc
1116
1117func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001118 " Create one buffer to read from and to write to.
1119 split pipe-io
1120 set buftype=nofile
1121
1122 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001123 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001124 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1125 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001126 call assert_equal("run", job_status(job))
1127 try
1128 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001129 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001130 call assert_equal('hello', getline(line('$') - 1))
1131
1132 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001133 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001134 call assert_equal('this', getline(line('$') - 2))
1135 call assert_equal('AND this', getline(line('$') - 1))
1136
1137 bwipe!
1138 finally
1139 call job_stop(job)
1140 endtry
1141endfunc
1142
Bram Moolenaar4641a122019-07-29 22:10:23 +02001143func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001144 CheckScreendump
1145
Bram Moolenaar4641a122019-07-29 22:10:23 +02001146 let lines =<< trim END
1147 new Xscrollbuffer
1148 call setline(1, range(1, 200))
1149 $
1150 redraw
1151 wincmd w
1152 call deletebufline('Xscrollbuffer', 1, '$')
1153 if has('win32')
1154 let cmd = ['cmd', '/c', 'echo sometext']
1155 else
1156 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1157 endif
1158 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1159 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001160 call writefile(lines, 'XtestBufferScroll', 'D')
Bram Moolenaar4641a122019-07-29 22:10:23 +02001161 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001162 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001163 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1164
1165 " clean up
1166 call StopVimInTerminal(buf)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001167endfunc
1168
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001169func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001170 " We cannot check that no I/O works, we only check that the job starts
1171 " properly.
1172 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001173 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001174 call assert_equal("run", job_status(job))
1175 try
1176 call assert_equal('something', ch_read(job))
1177 finally
1178 call job_stop(job)
1179 endtry
1180
1181 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001182 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001183 call assert_equal("run", job_status(job))
1184 try
1185 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1186 finally
1187 call job_stop(job)
1188 endtry
1189
1190 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001191 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001192 call assert_equal("run", job_status(job))
1193 try
1194 call assert_equal('something', ch_read(job))
1195 finally
1196 call job_stop(job)
1197 endtry
1198
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001199 " This causes spurious leak errors with valgrind.
1200 if !RunningWithValgrind()
1201 let job = job_start(s:python . " test_channel_pipe.py something",
1202 \ {'out_io': 'null', 'err_io': 'out'})
1203 call assert_equal("run", job_status(job))
1204 call job_stop(job)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001205
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001206 let job = job_start(s:python . " test_channel_pipe.py something",
1207 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
1208 call assert_equal("run", job_status(job))
1209 call assert_equal('channel fail', string(job_getchannel(job)))
1210 call assert_equal('fail', ch_status(job))
1211 call assert_equal('no process', string(test_null_job()))
1212 call assert_equal('channel fail', string(test_null_channel()))
1213 call job_stop(job)
1214 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001215endfunc
1216
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001217func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001218 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1219 split testout
Bram Moolenaar94722c52023-01-28 19:19:03 +00001220 let job = job_start([s:python, '-c',
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001221 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001222 " the job may be done quickly, also accept "dead"
1223 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001224 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001225 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001226 let totlen = 0
1227 for line in getline(1, '$')
1228 call assert_equal('', substitute(line, '^\.*', '', ''))
1229 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001230 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001231 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001232 finally
1233 call job_stop(job)
1234 bwipe!
1235 endtry
1236endfunc
1237
Bram Moolenaarde279892016-03-11 22:19:44 +01001238func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001239 let job = job_start(s:python . " test_channel_pipe.py")
1240 call assert_equal("run", job_status(job))
1241 let handle = job_getchannel(job)
1242 try
1243 call ch_sendraw(handle, "echo something\n")
1244 call assert_equal("something", ch_readraw(handle))
1245 finally
1246 call job_stop(job)
1247 endtry
1248
1249 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1250 call assert_equal("run", job_status(job))
1251 let handle = job_getchannel(job)
1252 try
1253 call ch_sendraw(handle, "echo again\n")
1254 call assert_equal("again", ch_readraw(handle))
1255 finally
1256 call job_stop(job)
1257 endtry
1258endfunc
1259
Bram Moolenaar75f72652016-03-20 22:16:56 +01001260func Test_out_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001261 let g:test_is_flaky = 1
Bram Moolenaar75f72652016-03-20 22:16:56 +01001262 let dict = {'thisis': 'dict: '}
1263 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001264 if type(a:msg) == v:t_string
1265 let g:Ch_outmsg = self.thisis . a:msg
1266 else
1267 let g:Ch_outobj = a:msg
1268 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001269 endfunc
1270 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001271 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001272 endfunc
1273 let job = job_start(s:python . " test_channel_pipe.py",
1274 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001275 \ 'out_mode': 'json',
1276 \ 'err_cb': dict.errHandler,
1277 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001278 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001279 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001280 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001281 let g:Ch_outmsg = ''
1282 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001283 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1284 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001285 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1286 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001287
1288 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001289 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001290 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001291 " For unknown reasons this can be very slow on Mac.
Bram Moolenaardeda6442021-12-17 11:44:33 +00001292 " Increase the timeout on every run.
1293 if g:run_nr == 1
1294 let timeout = 5000
1295 elseif g:run_nr == 2
1296 let timeout = 10000
1297 elseif g:run_nr == 3
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001298 let timeout = 20000
1299 else
Bram Moolenaardeda6442021-12-17 11:44:33 +00001300 let timeout = 40000
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001301 endif
1302 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001303 finally
1304 call job_stop(job)
1305 endtry
1306endfunc
1307
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001308func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001309 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001310 let g:Ch_msg1 = ''
1311 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001312 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001313 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001314 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001315 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001316 let s:counter += 1
1317 endfunc
1318 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001319 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001320 let s:counter += 1
1321 endfunc
1322 let job = job_start(s:python . " test_channel_pipe.py quit now",
1323 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001324 \ 'close_cb': 'CloseHandler'})
1325 " the job may be done quickly, also accept "dead"
1326 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001327 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001328 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1329 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001330 finally
1331 call job_stop(job)
1332 delfunc OutHandler
1333 delfunc CloseHandler
1334 endtry
1335endfunc
1336
Bram Moolenaar437905c2016-04-26 19:01:05 +02001337func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001338 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001339 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001341 endfunc
1342 let job = job_start(s:python . " test_channel_pipe.py quit now",
1343 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001344 " the job may be done quickly, also accept "dead"
1345 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001346 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001347 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001348 finally
1349 call job_stop(job)
1350 delfunc CloseHandler
1351 endtry
1352endfunc
1353
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001354" Use channel in NL mode but received text does not end in NL.
1355func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001356 let g:Ch_received = ''
1357 func! CloseHandler(chan)
1358 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1359 let g:Ch_received .= ch_read(a:chan)
1360 endwhile
1361 endfunc
1362 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1363 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001364 " the job may be done quickly, also accept "dead"
1365 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001366 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001367 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001368 finally
1369 call job_stop(job)
1370 delfunc CloseHandler
1371 endtry
1372endfunc
1373
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001374func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001375 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001376 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1377 \ 'out_mode': 'json',
1378 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1379 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001380 call assert_equal("run", job_status(job))
1381 try
1382 let g:Ch_outmsg = ''
1383 let g:Ch_errmsg = ''
1384 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1385 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001386 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1387 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001388 finally
1389 call job_stop(job)
1390 endtry
1391endfunc
1392
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001393func Test_close_and_exit_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001394 let g:test_is_flaky = 1
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001395 let g:retdict = {'ret': {}}
1396 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001397 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001398 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001399 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001400 let self.ret['exit_cb'] = job_status(a:job)
1401 endfunc
1402
Bram Moolenaare2956092019-01-25 21:01:17 +01001403 let job = job_start([&shell, &shellcmdflag, 'echo'],
1404 \ {'close_cb': g:retdict.close_cb,
1405 \ 'exit_cb': g:retdict.exit_cb})
1406 " the job may be done quickly, also accept "dead"
1407 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001408 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001409 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001410 call assert_equal('dead', g:retdict.ret['exit_cb'])
1411 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001412endfunc
1413
Bram Moolenaard46ae142016-02-16 13:33:52 +01001414""""""""""
1415
Bram Moolenaar0b146882018-09-06 16:27:24 +02001416function ExitCbWipe(job, status)
1417 exe g:wipe_buf 'bw!'
1418endfunction
1419
1420" This caused a crash, because messages were handled while peeking for a
1421" character.
1422func Test_exit_cb_wipes_buf()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001423 CheckFeature timers
Bram Moolenaar0b146882018-09-06 16:27:24 +02001424 set cursorline lazyredraw
1425 call test_override('redraw_flag', 1)
1426 new
1427 let g:wipe_buf = bufnr('')
1428
Milly4f5681d2024-10-20 11:06:00 +02001429 let job = job_start(has('win32') ? 'cmd /D /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001430 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001431 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1432 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1433 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1434 call timer_stop(timer)
1435
1436 set nocursorline nolazyredraw
1437 unlet g:wipe_buf
1438 call test_override('ALL', 0)
1439endfunc
1440
1441""""""""""
1442
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001443let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001444func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001445 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001446 unlet s:channelfd
1447endfunc
1448
1449" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001450func Ch_unlet_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001451 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001452 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001453 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001454endfunc
1455
1456func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001457 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001458endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001459
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001460func Test_unlet_handle_ipv6()
1461 CheckIPv6
1462 call Test_unlet_handle()
1463endfunc
1464
Bram Moolenaard46ae142016-02-16 13:33:52 +01001465""""""""""
1466
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001467let g:Ch_unletResponse = ''
1468func Ch_CloseHandler(handle, msg)
1469 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001470 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001471endfunc
1472
1473" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474func Ch_close_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001475 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001476 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001477 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001478endfunc
1479
1480func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001481 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001482endfunc
1483
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001484func Test_close_handle_ipv6()
1485 CheckIPv6
1486 call Test_close_handle()
1487endfunc
1488
1489""""""""""
1490
1491func Ch_open_ipv6(port)
LemonBoycc766a82022-04-04 15:46:58 +01001492 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001493 call assert_notequal('fail', ch_status(handle))
1494endfunc
1495
1496func Test_open_ipv6()
1497 CheckIPv6
1498 call s:run_server('Ch_open_ipv6')
1499endfunc
1500
Bram Moolenaard46ae142016-02-16 13:33:52 +01001501""""""""""
1502
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001503func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001504 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001505 echo ch
1506 let d = ch
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001507 call assert_fails("let ch = ch_open('noserver', 10)", 'E1206:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001508 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001509 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001510 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1511 \ 'E474:')
1512 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1513 \ 'E475:')
1514 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1515 \ 'E475:')
1516 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1517 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001518 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1519 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1520 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001521endfunc
1522
1523func Test_ch_info_fail()
1524 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001525endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001526
1527""""""""""
1528
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001529func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001530 " Wait up to a second for the port to open.
1531 let s:chopt.waittime = 1000
LemonBoycc766a82022-04-04 15:46:58 +01001532 let channel = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001533 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001534 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001535 return
1536 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001537 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001538 call ch_close(channel)
1539endfunc
1540
1541func Test_open_delay()
Christian Brabandt85ff0c12023-10-04 21:58:24 +02001542 " This fails on BSD (e.g. Cirrus-CI), why?
1543 CheckNotBSD
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001544 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001545 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001546endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001547
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001548func Test_open_delay_ipv6()
1549 CheckIPv6
Christian Brabandt85ff0c12023-10-04 21:58:24 +02001550 " This fails on BSD (e.g. Cirrus-CI), why?
1551 CheckNotBSD
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001552 call Test_open_delay()
1553endfunc
1554
Bram Moolenaarece61b02016-02-20 21:39:05 +01001555"""""""""
1556
1557function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001558 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001559endfunc
1560
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001561function Ch_test_call(port)
LemonBoycc766a82022-04-04 15:46:58 +01001562 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001563 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001564 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001565 return
1566 endif
1567
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001568 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001569 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001570 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001571
1572 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1573 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1574 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001575endfunc
1576
1577func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001578 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001579endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001580
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001581func Test_call_ipv6()
1582 CheckIPv6
1583 call Test_call()
1584endfunc
1585
LemonBoycc766a82022-04-04 15:46:58 +01001586func Test_call_unix()
1587 CheckUnix
1588 call Test_call()
1589 call delete('Xtestsocket')
1590endfunc
1591
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001592"""""""""
1593
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001594let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001595function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001596 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001597endfunc
1598
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001599function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001600 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001601 let g:Ch_exit_job = g:currentJob
1602 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001603endfunc
1604
1605func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001606 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001607
Bram Moolenaarf386f082019-07-29 23:03:03 +02001608 " wait up to a second for the job to exit
1609 for i in range(100)
1610 if g:Ch_job_exit_ret == 'done'
1611 break
1612 endif
1613 sleep 10m
1614 " calling job_status() triggers the callback
1615 call job_status(g:Ch_exit_job)
1616 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001617
Bram Moolenaarf386f082019-07-29 23:03:03 +02001618 call assert_equal('done', g:Ch_job_exit_ret)
1619 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1620 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001621endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001622
Bram Moolenaar97792de2016-10-15 18:36:49 +02001623function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001624 if job_info(a:job).process == g:exit_cb_val.process
1625 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1626 endif
1627 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001628endfunction
1629
1630func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001631 CheckFunction reltimefloat
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001632 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001633
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001634 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001635 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001636 let g:exit_cb_val.process = job_info(job).process
K.Takatacd9fa252022-09-20 11:04:47 +01001637 try
1638 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1639 catch
1640 call add(v:errors, "Job status: " .. string(job->job_info()))
1641 throw v:exception
1642 endtry
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001643 let elapsed = reltimefloat(g:exit_cb_val.end)
zeertzjqcdc6a432022-06-19 11:45:46 +01001644 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001645
1646 " case: unreferenced job, using timer
1647 if !has('timers')
1648 return
1649 endif
1650
1651 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1652 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1653 let g:exit_cb_val.process = job_info(g:job).process
1654 unlet g:job
1655 call Standby(1000)
1656 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1657 let elapsed = reltimefloat(g:exit_cb_val.end)
1658 else
1659 let elapsed = 1.0
1660 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001661 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001662endfunc
1663
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001664"""""""""
1665
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001666let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001667function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001668 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001669endfunc
1670
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001671function Ch_test_close_callback(port)
LemonBoycc766a82022-04-04 15:46:58 +01001672 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001673 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001674 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001675 return
1676 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001677 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001678
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001679 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001680 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001681endfunc
1682
1683func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001684 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001685endfunc
1686
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001687func Test_close_callback_ipv6()
1688 CheckIPv6
1689 call Test_close_callback()
1690endfunc
1691
LemonBoycc766a82022-04-04 15:46:58 +01001692func Test_close_callback_unix()
1693 CheckUnix
1694 call Test_close_callback()
1695 call delete('Xtestsocket')
1696endfunc
1697
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001698function Ch_test_close_partial(port)
LemonBoycc766a82022-04-04 15:46:58 +01001699 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001700 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001701 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001702 return
1703 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001704 let g:Ch_d = {}
1705 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001706 let self.close_ret = 'closed'
1707 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001708 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001709
1710 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001711 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001712 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001713endfunc
1714
1715func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001716 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001717endfunc
1718
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001719func Test_close_partial_ipv6()
1720 CheckIPv6
1721 call Test_close_partial()
1722endfunc
1723
LemonBoycc766a82022-04-04 15:46:58 +01001724func Test_close_partial_unix()
1725 CheckUnix
1726 call Test_close_partial()
1727 call delete('Xtestsocket')
1728endfunc
1729
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001730func Test_job_start_fails()
1731 " this was leaking memory
1732 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001733 call assert_fails('call job_start($x)', 'E474:')
1734 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001735 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1736 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1737 call assert_fails('call job_start("ls", [])', 'E715:')
1738 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1739 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1740 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001741 call assert_fails("call job_start('ls', {'callback' : -1})", 'E921:')
1742 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E921:')
1743 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E921:')
1744 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E921:')
1745 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E921:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001746 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1747 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1748 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001749 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001750 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1751 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1752 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1753 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1754 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1755 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1756 call assert_fails("call job_start('ls',
1757 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1758 call assert_fails("call job_start('ls',
1759 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1760 call assert_fails("call job_start('ls',
1761 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1762
1763 call assert_fails("call job_start('ls',
1764 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1765 call assert_fails("call job_start('ls',
1766 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1767 call assert_fails("call job_start('ls',
1768 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1769
Milly4f5681d2024-10-20 11:06:00 +02001770 let cmd = has('win32') ? "cmd /D /c dir" : "ls"
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001771
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001772 set nomodifiable
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001773 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001774 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001775 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001776 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1777 set modifiable
1778
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001779 call assert_fails("call job_start(cmd, {'in_io' : 'buffer'})", 'E915:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001780
1781 edit! XXX
1782 let bnum = bufnr()
1783 enew
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001784 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001785 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1786
1787 " Empty job tests
1788 " This was crashing on MS-Windows.
1789 call assert_fails('let job = job_start([""])', 'E474:')
1790 call assert_fails('let job = job_start([" "])', 'E474:')
1791 call assert_fails('let job = job_start("")', 'E474:')
1792 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001793 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001794 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001795 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001796endfunc
1797
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001798func Test_job_stop_immediately()
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001799 " With valgrind this causes spurious leak reports
Bram Moolenaarcf801d42022-06-21 18:34:42 +01001800 CheckNotValgrind
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001801
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001802 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001803 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001804 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001805 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001806 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001807 call job_stop(g:job, 'kill')
1808 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001809 endtry
1810endfunc
1811
Bram Moolenaar271906b2021-08-28 12:30:12 +02001812func Test_null_job_eval()
1813 call assert_fails('eval test_null_job()->eval()', 'E121:')
1814endfunc
1815
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001816" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001817func Test_partial_in_channel_cycle()
1818 let d = {}
1819 let d.a = function('string', [d])
1820 try
1821 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001822 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001823 catch
1824 call assert_exception('E901:')
1825 endtry
1826 unlet d
1827endfunc
1828
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001829func Test_using_freed_memory()
1830 let g:a = job_start(['ls'])
1831 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001832 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001833endfunc
1834
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001835func Test_collapse_buffers()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001836 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001837 CheckExecutable cat
1838
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001839 sp test_channel.vim
1840 let g:linecount = line('$')
1841 close
1842 split testout
1843 1,$delete
1844 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001845 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001846 bwipe!
1847endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001848
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001849func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001850 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001851 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001852
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001853 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001854 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001855 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001856 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1857 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001858
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001859 bdel test_buffer
1860 call assert_equal([], getbufline(bufnr, 1, '$'))
1861
1862 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001863 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001864 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1865 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1866
1867 bwipe! test_buffer
1868endfunc
1869
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001870func Test_cmd_parsing()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02001871 CheckUnix
1872
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001873 call assert_false(filereadable("file with space"))
1874 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001875 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001876 call delete("file with space")
1877
1878 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001879 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001880 call delete("file with space")
1881endfunc
1882
Bram Moolenaar82117982016-08-27 19:21:48 +02001883func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001884 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001885
1886 " Test lines from the job containing NUL are stored correctly in a buffer.
1887 new
1888 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1889 w! Xtestread
1890 bwipe!
1891 split testout
1892 1,$delete
1893 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1894 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001895 call assert_equal("asdf\nasdf", getline(1))
1896 call assert_equal("xxx\n", getline(2))
1897 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001898
1899 call delete('Xtestread')
1900 bwipe!
1901
1902 " Test lines from a buffer with NUL bytes are written correctly to the job.
1903 new mybuffer
1904 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1905 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 +02001906 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001907 bwipe!
1908 split Xtestwrite
1909 call assert_equal("asdf\nasdf", getline(1))
1910 call assert_equal("xxx\n", getline(2))
1911 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001912 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001913
1914 call delete('Xtestwrite')
1915 bwipe!
1916endfunc
1917
Bram Moolenaarec68a992016-10-03 21:37:41 +02001918func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001919 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001920 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001921 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001922 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001923 unlet g:linecount
1924endfunc
1925
1926func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001927 func s:close_cb(ch)
1928 while ch_status(a:ch) == 'buffered'
1929 let g:out .= ch_read(a:ch)
1930 endwhile
1931 endfunc
1932
1933 let g:out = ''
1934 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1935 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001936 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001937 call WaitForAssert({-> assert_equal('123', g:out)})
1938 unlet g:out
1939 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001940endfunc
1941
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001942func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001943 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001944 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001945 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001946 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001947 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001948 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001949endfunc
1950
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001951func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001952 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001953
1954 " Check that backslash in $COMSPEC is handled properly.
1955 let g:echostr = ''
1956 let cmd = $COMSPEC . ' /c echo 123'
1957 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1958 let info = job_info(job)
1959 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1960
1961 call WaitForAssert({-> assert_equal("123", g:echostr)})
1962 unlet g:echostr
1963endfunc
1964
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001965func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001966 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001967 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001968 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001969 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001970 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001971 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001972 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
K.Takatacd9fa252022-09-20 11:04:47 +01001973 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
1974 if WaitForAssert({-> assert_equal("bar", g:envstr)}, 500) != 0
1975 call add(v:errors, "Job status: " .. string(job->job_info()))
1976 endif
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001977 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001978endfunc
1979
1980func Test_cwd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001981 let g:test_is_flaky = 1
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001982 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001983 if has('win32')
1984 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001985 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001986 else
1987 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001988 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001989 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001990 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001991 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001992 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaarec1238b2022-09-25 11:21:04 +01001993 " There may be a trailing slash or not, ignore it
Bram Moolenaar24820692017-12-02 16:38:12 +01001994 let expect = substitute(expect, '[/\\]$', '', '')
1995 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
Bram Moolenaarec1238b2022-09-25 11:21:04 +01001996 " on CI there can be /private prefix or not, ignore it
1997 if $CI != '' && stridx(expect, '/private/') == 0
1998 let expect = expect[8:]
1999 endif
Bram Moolenaar24820692017-12-02 16:38:12 +01002000 if $CI != '' && stridx(g:envstr, '/private/') == 0
2001 let g:envstr = g:envstr[8:]
2002 endif
2003 call assert_equal(expect, g:envstr)
2004 finally
2005 call job_stop(job)
2006 unlet g:envstr
2007 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02002008endfunc
2009
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002010function Ch_test_close_lambda(port)
LemonBoycc766a82022-04-04 15:46:58 +01002011 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002012 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01002013 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002014 return
2015 endif
2016 let g:Ch_close_ret = ''
2017 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002018 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002019
2020 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002021 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002022endfunc
2023
2024func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002025 call s:run_server('Ch_test_close_lambda')
2026endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002027
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02002028func Test_close_lambda_ipv6()
2029 CheckIPv6
2030 call Test_close_lambda()
2031endfunc
2032
LemonBoycc766a82022-04-04 15:46:58 +01002033func Test_close_lambda_unix()
2034 CheckUnix
2035 call Test_close_lambda()
2036 call delete('Xtestsocket')
2037endfunc
2038
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002039func s:test_list_args(cmd, out, remove_lf)
2040 try
2041 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01002042 let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
K.Takatacd9fa252022-09-20 11:04:47 +01002043 try
2044 call WaitFor('"" != g:out')
2045 catch
2046 call add(v:errors, "Job status: " .. string(job->job_info()))
2047 throw v:exception
2048 endtry
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002049 if has('win32')
2050 let g:out = substitute(g:out, '\r', '', 'g')
2051 endif
2052 if a:remove_lf
2053 let g:out = substitute(g:out, '\n$', '', 'g')
2054 endif
2055 call assert_equal(a:out, g:out)
2056 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01002057 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002058 unlet g:out
2059 endtry
2060endfunc
2061
2062func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002063 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
2064 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
2065 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
2066 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
2067 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
2068 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
2069 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
2070 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
2071 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
2072 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
2073 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
2074 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
2075 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
2076
2077 " tests which not contain spaces in the argument
2078 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
2079 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
2080 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
2081 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
2082 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
2083 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
2084 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
2085 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
2086 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
2087endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02002088
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002089func Test_keep_pty_open()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002090 CheckUnix
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002091
Bram Moolenaare2956092019-01-25 21:01:17 +01002092 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
2093 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002094 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
2095 call assert_inrange(200, 1000, elapsed)
2096 call job_stop(job)
2097endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01002098
2099func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002100 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01002101 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01002102
2103 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002104 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002105 endfunc
2106
2107 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002108 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002109 call Resume()
2110 endfunc
2111
2112 func TimerCb(timer)
2113 if has('win32')
2114 let cmd = ['cmd', '/c', 'echo.']
2115 else
2116 let cmd = ['echo']
2117 endif
2118 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
2119 call substitute(repeat('a', 100000), '.', '', 'g')
2120 endfunc
2121
2122 " We should be interrupted before 'updatetime' elapsed.
2123 let g:val = 0
2124 call timer_start(1, 'TimerCb')
2125 let elapsed = Standby(&ut)
2126 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002127
2128 " Wait for both OutCb() and ExitCb() to have been called before deleting
2129 " them.
2130 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002131 call job_stop(g:job)
2132
2133 delfunc OutCb
2134 delfunc ExitCb
2135 delfunc TimerCb
2136 unlet! g:val
2137 unlet! g:job
2138endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002139
2140func Test_raw_large_data()
2141 try
2142 let g:out = ''
2143 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002144 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2145 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002146
Bram Moolenaare2956092019-01-25 21:01:17 +01002147 let outlen = 79999
2148 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002149 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002150 call WaitFor({-> len(g:out) >= outlen}, 10000)
2151 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002152 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2153 finally
2154 call job_stop(job)
2155 unlet g:out
2156 endtry
2157endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002158
Bram Moolenaar65240682019-02-10 22:23:26 +01002159func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002160 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002161
2162 try
2163 let job = job_start(s:python . " test_channel_pipe.py busy",
2164 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2165 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2166 finally
2167 call job_stop(job)
2168 endtry
2169endfunc
2170
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002171func Test_job_exitval_and_termsig()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002172 CheckUnix
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002173
2174 " Terminate job normally
2175 let cmd = ['echo']
2176 let job = job_start(cmd)
2177 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2178 let info = job_info(job)
2179 call assert_equal(0, info.exitval)
2180 call assert_equal("", info.termsig)
2181
2182 " Terminate job by signal
2183 let cmd = ['sleep', '10']
2184 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002185 " 10m usually works but 50m is needed when running Valgrind
2186 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002187 call job_stop(job)
2188 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2189 let info = job_info(job)
2190 call assert_equal(-1, info.exitval)
2191 call assert_equal("term", info.termsig)
2192endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002193
2194func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002195 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002196
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002197 call writefile(['test'], 'Xtestin', 'D')
Bram Moolenaar59386482019-02-10 22:43:46 +01002198 let in_opts = [{},
2199 \ {'in_io': 'null'},
2200 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2201 let out_opts = [{},
2202 \ {'out_io': 'null'},
2203 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2204 let err_opts = [{},
2205 \ {'err_io': 'null'},
2206 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2207 \ {'err_io': 'out'}]
2208 let opts = []
2209
2210 for in_opt in in_opts
2211 let x = copy(in_opt)
2212 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002213 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002214 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002215 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002216 let opts += [extend({'pty': 1}, x)]
2217 endfor
2218 endfor
2219 endfor
2220
2221 for opt in opts
2222 let job = job_start('echo', opt)
2223 let info = job_info(job)
2224 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2225 \ get(opt, 'in_io', 'tty'),
2226 \ get(opt, 'out_io', 'tty'),
2227 \ get(opt, 'err_io', 'tty'))
2228
2229 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2230 call assert_notequal('', info.tty_in, msg)
2231 else
2232 call assert_equal('', info.tty_in, msg)
2233 endif
2234 call assert_equal(info.tty_in, info.tty_out, msg)
2235
2236 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2237 endfor
2238
Bram Moolenaar59386482019-02-10 22:43:46 +01002239 call delete('Xtestout')
2240 call delete('Xtesterr')
2241endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002242
2243" Do this last, it stops any channel log.
2244func Test_zz_nl_err_to_out_pipe()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002245
Bram Moolenaar570497a2019-08-22 22:55:13 +02002246 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002247 call ch_log('Test_zz_nl_err_to_out_pipe()')
2248 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2249 call assert_equal("run", job_status(job))
2250 try
2251 let handle = job_getchannel(job)
2252 call ch_sendraw(handle, "echo something\n")
2253 call assert_equal("something", ch_readraw(handle))
2254
2255 call ch_sendraw(handle, "echoerr wrong\n")
2256 call assert_equal("wrong", ch_readraw(handle))
2257 finally
2258 call job_stop(job)
2259 call ch_logfile('')
2260 let loglines = readfile('Xlog')
2261 call assert_true(len(loglines) > 10)
2262 let found_test = 0
2263 let found_send = 0
2264 let found_recv = 0
2265 let found_stop = 0
2266 for l in loglines
2267 if l =~ 'Test_zz_nl_err_to_out_pipe'
2268 let found_test = 1
2269 endif
2270 if l =~ 'SEND on.*echo something'
2271 let found_send = 1
2272 endif
2273 if l =~ 'RECV on.*something'
2274 let found_recv = 1
2275 endif
2276 if l =~ 'Stopping job with'
2277 let found_stop = 1
2278 endif
2279 endfor
2280 call assert_equal(1, found_test)
2281 call assert_equal(1, found_send)
2282 call assert_equal(1, found_recv)
2283 call assert_equal(1, found_stop)
2284 " On MS-Windows need to sleep for a moment to be able to delete the file.
2285 sleep 10m
2286 call delete('Xlog')
2287 endtry
2288endfunc
2289
2290" Do this last, it stops any channel log.
2291func Test_zz_ch_log()
2292 call ch_logfile('Xlog', 'w')
2293 call ch_log('hello there')
2294 call ch_log('%s%s')
2295 call ch_logfile('')
2296 let text = readfile('Xlog')
Bram Moolenaar4f501172022-12-01 11:02:23 +00002297 call assert_match("start log session", text[0])
2298 call assert_match("ch_log(): hello there", text[1])
Bram Moolenaarf386f082019-07-29 23:03:03 +02002299 call assert_match("%s%s", text[2])
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002300 call mkdir("Xchlogdir1", 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002301 call assert_fails("call ch_logfile('Xchlogdir1')", 'E484:')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002302
Bram Moolenaarf386f082019-07-29 23:03:03 +02002303 call delete('Xlog')
2304endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002305
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002306func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002307 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +02002308 let cmd = 'cmd /D /c pause'
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002309 else
2310 let cmd = 'grep foo'
2311 endif
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002312
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002313 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002314 sleep 50m " give the job time to start
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002315 call job_stop(g:job)
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002316 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2317
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002318 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002319 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002320 call job_stop(g:job, 'term')
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002321 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2322
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002323 let g:job = job_start(cmd, {})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002324 sleep 50m
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002325 call job_stop(g:job, 'kill')
2326 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002327endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002328
2329func Test_issue_5485()
2330 let $VAR1 = 'global'
2331 let g:Ch_reply = ""
2332 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2333 let g:Ch_job = l:job
2334 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2335 unlet $VAR1
2336endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002337
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002338func Test_job_trailing_space_unix()
2339 CheckUnix
2340 CheckExecutable cat
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002341
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002342 let job = job_start("cat ", #{in_io: 'null'})
2343 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2344 call assert_equal(0, job_info(job).exitval)
LemonBoycc766a82022-04-04 15:46:58 +01002345
2346 call delete('Xtestsocket')
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002347endfunc
2348
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002349func Test_ch_getbufnr()
2350 let ch = test_null_channel()
2351 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2352 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2353 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2354 call assert_equal(-1, ch_getbufnr(ch, ''))
2355endfunc
2356
2357" Test for unsupported options passed to ch_status()
2358func Test_invalid_job_chan_options()
2359 let ch = test_null_channel()
2360 let invalid_opts = [
2361 \ {'in_io' : 'null'},
2362 \ {'out_io' : 'null'},
2363 \ {'err_io' : 'null'},
2364 \ {'mode' : 'json'},
2365 \ {'out_mode' : 'json'},
2366 \ {'err_mode' : 'json'},
2367 \ {'noblock' : 1},
2368 \ {'in_name' : '/a/b'},
2369 \ {'pty' : 1},
2370 \ {'in_buf' : 1},
2371 \ {'out_buf' : 1},
2372 \ {'err_buf' : 1},
2373 \ {'out_modifiable' : 1},
2374 \ {'err_modifiable' : 1},
2375 \ {'out_msg' : 1},
2376 \ {'err_msg' : 1},
2377 \ {'in_top' : 1},
2378 \ {'in_bot' : 1},
2379 \ {'channel' : ch},
2380 \ {'callback' : ''},
2381 \ {'out_cb' : ''},
2382 \ {'err_cb' : ''},
2383 \ {'close_cb' : ''},
2384 \ {'exit_cb' : ''},
2385 \ {'term_opencmd' : ''},
2386 \ {'eof_chars' : ''},
2387 \ {'term_rows' : 10},
2388 \ {'term_cols' : 10},
2389 \ {'vertical' : 0},
2390 \ {'curwin' : 1},
2391 \ {'bufnr' : 1},
2392 \ {'hidden' : 0},
2393 \ {'norestore' : 0},
2394 \ {'term_kill' : 'kill'},
2395 \ {'tty_type' : ''},
2396 \ {'term_highlight' : ''},
2397 \ {'env' : {}},
2398 \ {'cwd' : ''},
2399 \ {'timeout' : 0},
2400 \ {'out_timeout' : 0},
2401 \ {'err_timeout' : 0},
2402 \ {'id' : 0},
2403 \ {'stoponexit' : ''},
2404 \ {'block_write' : 1}
2405 \ ]
2406 if has('gui')
2407 call add(invalid_opts, {'ansi_colors' : []})
2408 endif
2409
2410 for opt in invalid_opts
2411 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2412 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002413 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002414endfunc
2415
2416" Test for passing the command and the arguments as List on MS-Windows
2417func Test_job_with_list_args()
2418 CheckMSWindows
2419
2420 enew!
2421 let bnum = bufnr()
2422 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2423 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2424 call assert_equal('Hello World', getline(1))
2425 %bw!
2426endfunc
2427
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002428func ExitCb_cb_with_input(job, status)
2429 call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx')
2430 call assert_equal('default', Screenline(&lines))
2431 let g:wait_exit_cb = 0
2432endfunc
2433
2434func Test_cb_with_input()
2435 let g:wait_exit_cb = 1
2436
Bram Moolenaarf637bce2020-11-23 18:14:56 +01002437 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +02002438 let cmd = 'cmd /D /c echo "Vim''s test"'
Bram Moolenaarf637bce2020-11-23 18:14:56 +01002439 else
2440 let cmd = 'echo "Vim''s test"'
2441 endif
2442
2443 let job = job_start(cmd, {'out_cb': 'ExitCb_cb_with_input'})
2444 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002445 call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)})
2446
2447 unlet g:wait_exit_cb
2448endfunc
2449
Bram Moolenaar57942232021-08-04 20:54:55 +02002450function s:HandleBufEnter() abort
2451 let queue = []
2452 let job = job_start(['date'], {'callback': { j, d -> add(queue, d) }})
2453 while empty(queue)
2454 sleep! 10m
2455 endwhile
2456endfunction
2457
2458func Test_parse_messages_in_autocmd()
2459 CheckUnix
2460
2461 " Check that in the BufEnter autocommand events are being handled
2462 augroup bufenterjob
2463 autocmd!
2464 autocmd BufEnter Xbufenterjob call s:HandleBufEnter()
2465 augroup END
2466
2467 only
2468 split Xbufenterjob
2469 wincmd p
2470 redraw
2471
2472 close
2473 augroup bufenterjob
2474 autocmd!
2475 augroup END
2476endfunc
2477
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01002478func Test_job_start_with_invalid_argument()
2479 call assert_fails('call job_start([0zff])', 'E976:')
2480endfunc
2481
Yegappan Lakshmananb80ae6c2023-09-24 23:38:46 +02002482" Process requests received from the LSP server
2483func LspProcessServerRequests(chan, msg)
2484 if a:msg['method'] == 'server-req-in-middle'
2485 \ && a:msg['params']['text'] == 'server-req'
2486 call ch_sendexpr(a:chan, #{method: 'server-req-in-middle-resp',
2487 \ id: a:msg['id'], params: #{text: 'client-resp'}})
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002488 endif
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002489endfunc
2490
Yegappan Lakshmananb80ae6c2023-09-24 23:38:46 +02002491" LSP channel message callback function
2492func LspCb(chan, msg)
2493 call add(g:lspNotif, a:msg)
2494 if a:msg->has_key('method')
2495 call LspProcessServerRequests(a:chan, a:msg)
2496 endif
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002497endfunc
2498
Yegappan Lakshmananb80ae6c2023-09-24 23:38:46 +02002499" LSP one-time message callback function (used for ch_sendexpr())
2500func LspOtCb(chan, msg)
2501 call add(g:lspOtMsgs, a:msg)
2502 if a:msg->has_key('method')
2503 call LspProcessServerRequests(a:chan, a:msg)
2504 endif
2505endfunc
2506
2507" Test for the 'lsp' channel mode
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002508func LspTests(port)
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +01002509 " call ch_logfile('Xlspclient.log', 'w')
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002510 let ch = ch_open(s:localhost .. a:port, #{mode: 'lsp', callback: 'LspCb'})
2511 if ch_status(ch) == "fail"
2512 call assert_report("Can't open the lsp channel")
2513 return
2514 endif
2515
2516 " check for channel information
2517 let info = ch_info(ch)
2518 call assert_equal('LSP', info.sock_mode)
2519
2520 " Evaluate an expression
2521 let resp = ch_evalexpr(ch, #{method: 'simple-rpc', params: [10, 20]})
2522 call assert_false(empty(resp))
2523 call assert_equal(#{id: 1, jsonrpc: '2.0', result: 'simple-rpc'}, resp)
2524
2525 " Evaluate an expression. While waiting for the response, a notification
2526 " message is delivered.
2527 let g:lspNotif = []
2528 let resp = ch_evalexpr(ch, #{method: 'rpc-with-notif', params: {'v': 10}})
2529 call assert_false(empty(resp))
2530 call assert_equal(#{id: 2, jsonrpc: '2.0', result: 'rpc-with-notif-resp'},
2531 \ resp)
2532 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-with-notif-notif'}],
2533 \ g:lspNotif)
2534
2535 " Wrong payload notification test
2536 let g:lspNotif = []
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002537 let r = ch_sendexpr(ch, #{method: 'wrong-payload', params: {}})
2538 call assert_equal({}, r)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002539 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002540 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002541 call assert_equal([#{jsonrpc: '2.0', result: 'wrong-payload'}], g:lspNotif)
2542
2543 " Test for receiving a response with incorrect 'id' and additional
2544 " notification messages while evaluating an expression.
2545 let g:lspNotif = []
2546 let resp = ch_evalexpr(ch, #{method: 'rpc-resp-incorrect-id',
2547 \ params: {'a': [1, 2]}})
2548 call assert_false(empty(resp))
2549 call assert_equal(#{id: 4, jsonrpc: '2.0',
2550 \ result: 'rpc-resp-incorrect-id-4'}, resp)
2551 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-1'},
2552 \ #{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-2'},
2553 \ #{jsonrpc: '2.0', id: 1, result: 'rpc-resp-incorrect-id-3'}],
2554 \ g:lspNotif)
2555
2556 " simple notification test
2557 let g:lspNotif = []
2558 call ch_sendexpr(ch, #{method: 'simple-notif', params: [#{a: 10, b: []}]})
2559 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002560 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002561 call assert_equal([#{jsonrpc: '2.0', result: 'simple-notif'}], g:lspNotif)
2562
2563 " multiple notifications test
2564 let g:lspNotif = []
2565 call ch_sendexpr(ch, #{method: 'multi-notif', params: [#{a: {}, b: {}}]})
2566 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002567 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002568 call assert_equal([#{jsonrpc: '2.0', result: 'multi-notif1'},
2569 \ #{jsonrpc: '2.0', result: 'multi-notif2'}], g:lspNotif)
2570
2571 " Test for sending a message with an identifier.
2572 let g:lspNotif = []
2573 call ch_sendexpr(ch, #{method: 'msg-with-id', id: 93, params: #{s: 'str'}})
2574 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002575 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002576 call assert_equal([#{jsonrpc: '2.0', id: 93, result: 'msg-with-id'}],
2577 \ g:lspNotif)
2578
2579 " Test for setting the 'id' value in a request message
2580 let resp = ch_evalexpr(ch, #{method: 'ping', id: 1, params: {}})
2581 call assert_equal(#{id: 8, jsonrpc: '2.0', result: 'alive'}, resp)
2582
2583 " Test for using a one time callback function to process a response
2584 let g:lspOtMsgs = []
dundargocc57b5bc2022-11-02 13:30:51 +00002585 let r = ch_sendexpr(ch, #{method: 'msg-specific-cb', params: {}},
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002586 \ #{callback: 'LspOtCb'})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002587 call assert_equal(9, r.id)
2588 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
dundargocc57b5bc2022-11-02 13:30:51 +00002589 call assert_equal([#{id: 9, jsonrpc: '2.0', result: 'msg-specific-cb'}],
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002590 \ g:lspOtMsgs)
2591
2592 " Test for generating a request message from the other end (server)
2593 let g:lspNotif = []
2594 call ch_sendexpr(ch, #{method: 'server-req', params: #{}})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002595 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002596 call assert_equal([{'id': 201, 'jsonrpc': '2.0',
2597 \ 'result': {'method': 'checkhealth', 'params': {'a': 20}}}],
2598 \ g:lspNotif)
2599
2600 " Test for sending a message without an id
2601 let g:lspNotif = []
2602 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'msg-without-id'}})
2603 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002604 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002605 call assert_equal([#{jsonrpc: '2.0', result:
2606 \ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'msg-without-id'}}}],
2607 \ g:lspNotif)
2608
2609 " Test for sending a notification message with an id
2610 let g:lspNotif = []
2611 call ch_sendexpr(ch, #{method: 'echo', id: 110, params: #{s: 'msg-with-id'}})
2612 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002613 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002614 call assert_equal([#{jsonrpc: '2.0', result:
2615 \ #{method: 'echo', jsonrpc: '2.0', id: 110,
2616 \ params: #{s: 'msg-with-id'}}}], g:lspNotif)
2617
2618 " Test for processing the extra fields in the HTTP header
2619 let resp = ch_evalexpr(ch, #{method: 'extra-hdr-fields', params: {}})
2620 call assert_equal({'id': 14, 'jsonrpc': '2.0', 'result': 'extra-hdr-fields'},
2621 \ resp)
2622
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +01002623 " Test for processing delayed payload
2624 let resp = ch_evalexpr(ch, #{method: 'delayed-payload', params: {}})
2625 call assert_equal({'id': 15, 'jsonrpc': '2.0', 'result': 'delayed-payload'},
2626 \ resp)
2627
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002628 " Test for processing a HTTP header without the Content-Length field
2629 let resp = ch_evalexpr(ch, #{method: 'hdr-without-len', params: {}},
2630 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002631 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002632 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002633 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002634
2635 " Test for processing a HTTP header with wrong length
2636 let resp = ch_evalexpr(ch, #{method: 'hdr-with-wrong-len', params: {}},
2637 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002638 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002639 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002640 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002641
2642 " Test for processing a HTTP header with negative length
2643 let resp = ch_evalexpr(ch, #{method: 'hdr-with-negative-len', params: {}},
2644 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002645 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002646 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002647 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002648
2649 " Test for an empty header
2650 let resp = ch_evalexpr(ch, #{method: 'empty-header', params: {}},
2651 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002652 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002653 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002654 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002655
2656 " Test for an empty payload
2657 let resp = ch_evalexpr(ch, #{method: 'empty-payload', params: {}},
2658 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002659 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002660 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002661 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002662
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +01002663 " Test for a large payload
2664 let content = repeat('abcdef', 11000)
2665 let resp = ch_evalexpr(ch, #{method: 'large-payload',
2666 \ params: #{text: content}})
2667 call assert_equal(#{jsonrpc: '2.0', id: 26, result:
2668 \ #{method: 'large-payload', jsonrpc: '2.0', id: 26,
2669 \ params: #{text: content}}}, resp)
2670 " send a ping to make sure communication still works
2671 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
2672
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002673 " Test for processing a request message from the server while the client
Yegappan Lakshmananb80ae6c2023-09-24 23:38:46 +02002674 " is waiting for a response with the same identifier (sync-rpc)
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002675 let g:lspNotif = []
2676 let resp = ch_evalexpr(ch, #{method: 'server-req-in-middle',
2677 \ params: #{text: 'client-req'}})
2678 call assert_equal(#{jsonrpc: '2.0', id: 28,
2679 \ result: #{text: 'server-resp'}}, resp)
2680 call assert_equal([
2681 \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle',
2682 \ params: #{text: 'server-notif'}},
2683 \ #{id: 28, jsonrpc: '2.0', method: 'server-req-in-middle',
2684 \ params: #{text: 'server-req'}}], g:lspNotif)
2685
Yegappan Lakshmananb80ae6c2023-09-24 23:38:46 +02002686 " Test for processing a request message from the server while the client
2687 " is waiting for a response with the same identifier (async-rpc using the
2688 " channel callback function)
2689 let g:lspNotif = []
2690 call ch_sendexpr(ch, #{method: 'server-req-in-middle', id: 500,
2691 \ params: #{text: 'client-req'}})
2692 " Send three pings to wait for all the notification messages to arrive
2693 for i in range(3)
2694 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
2695 endfor
2696 call assert_equal([
2697 \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle',
2698 \ params: #{text: 'server-notif'}},
2699 \ #{id: 500, jsonrpc: '2.0', method: 'server-req-in-middle',
2700 \ params: #{text: 'server-req'}},
2701 \ #{id: 500, jsonrpc: '2.0', result: #{text: 'server-resp'}}
2702 \ ], g:lspNotif)
2703
2704 " Test for processing a request message from the server while the client
2705 " is waiting for a response with the same identifier (async-rpc using a
2706 " one-time callback function)
2707 let g:lspNotif = []
2708 let g:lspOtMsgs = []
2709 call ch_sendexpr(ch, #{method: 'server-req-in-middle',
2710 \ params: #{text: 'client-req'}}, #{callback: 'LspOtCb'})
2711 " Send a ping to wait for all the notification messages to arrive
2712 for i in range(3)
2713 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
2714 endfor
2715 call assert_equal([
2716 \ #{id: 32, jsonrpc: '2.0', result: #{text: 'server-resp'}}],
2717 \ g:lspOtMsgs)
2718 call assert_equal([
2719 \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle',
2720 \ params: #{text: 'server-notif'}},
2721 \ #{id: 32, jsonrpc: '2.0', method: 'server-req-in-middle',
2722 \ params: {'text': 'server-req'}}], g:lspNotif)
2723
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002724 " Test for invoking an unsupported method
2725 let resp = ch_evalexpr(ch, #{method: 'xyz', params: {}}, #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002726 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002727
2728 " Test for sending a message without a callback function. Notification
2729 " message should be dropped but RPC response should not be dropped.
2730 call ch_setoptions(ch, #{callback: ''})
2731 let g:lspNotif = []
2732 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
2733 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002734 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002735 call assert_equal([], g:lspNotif)
2736 " Restore the callback function
2737 call ch_setoptions(ch, #{callback: 'LspCb'})
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002738
2739 " " Test for sending a raw message
2740 " let g:lspNotif = []
2741 " let s = "Content-Length: 62\r\n"
Yegappan Lakshmananc3eddd22023-04-25 14:54:54 +01002742 " let s ..= "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002743 " let s ..= "\r\n"
2744 " let s ..= '{"method":"echo","jsonrpc":"2.0","params":{"m":"raw-message"}}'
2745 " call ch_sendraw(ch, s)
2746 " call ch_evalexpr(ch, #{method: 'ping'})
2747 " call assert_equal([{'jsonrpc': '2.0',
2748 " \ 'result': {'method': 'echo', 'jsonrpc': '2.0',
2749 " \ 'params': {'m': 'raw-message'}}}], g:lspNotif)
2750
2751 " Invalid arguments to ch_evalexpr() and ch_sendexpr()
2752 call assert_fails('call ch_sendexpr(ch, #{method: "cookie", id: "cookie"})',
2753 \ 'E475:')
2754 call assert_fails('call ch_evalexpr(ch, #{method: "ping", id: [{}]})', 'E475:')
2755 call assert_fails('call ch_evalexpr(ch, [1, 2, 3])', 'E1206:')
2756 call assert_fails('call ch_sendexpr(ch, "abc")', 'E1206:')
2757 call assert_fails('call ch_evalexpr(ch, #{method: "ping"}, #{callback: "LspOtCb"})', 'E917:')
2758 " call ch_logfile('', 'w')
2759endfunc
2760
2761func Test_channel_lsp_mode()
Millybaab7c02024-10-28 21:56:14 +01002762 " The channel lsp mode test is flaky and gives the same error.
2763 let g:giveup_same_error = 0
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002764 call RunServer('test_channel_lsp.py', 'LspTests', [])
2765endfunc
Bram Moolenaar57942232021-08-04 20:54:55 +02002766
Christian Brabandtb50bc9a2024-09-30 21:29:43 +02002767func Test_error_callback_terminal()
2768 CheckUnix
2769 CheckFeature terminal
2770 let g:out = ''
2771 let g:error = ''
2772
2773 func! s:Out(channel, msg)
2774 let g:out .= string(a:msg)
2775 endfunc
2776
2777 func! s:Err(channel, msg)
2778 let g:error .= string(a:msg)
2779 endfunc
2780
2781 let buf = term_start(['sh'], #{term_finish: 'close', out_cb: 's:Out', err_cb: 's:Err', err_io: 'pipe'})
2782 let job = term_getjob(buf)
2783 let dict = job_info(job).channel->ch_info()
2784
2785 call assert_true(dict.id != 0)
2786 call assert_equal('open', dict.status)
2787 call assert_equal('open', dict.out_status)
2788 call assert_equal('RAW', dict.out_mode)
2789 call assert_equal('buffer', dict.out_io)
2790 call assert_equal('open', dict.err_status)
2791 call assert_equal('RAW', dict.err_mode)
2792 call assert_equal('pipe', dict.err_io)
2793 call term_sendkeys(buf, "XXXX\<cr>")
2794 call term_wait(buf)
2795 call term_sendkeys(buf, "exit\<cr>")
2796 call term_wait(buf)
2797 call assert_match('XXX.*exit', g:out)
2798 call assert_match('sh:.*XXXX:.*not found', g:error)
2799
2800 delfunc s:Out
2801 delfunc s:Err
2802 unlet! g:out g:error
2803endfunc
2804
Bram Moolenaar8b633132020-03-20 18:20:51 +01002805" vim: shiftwidth=2 sts=2 expandtab