blob: edfd56e0bacc9697e45215b603c36c9a2f70f857 [file] [log] [blame]
Bram Moolenaarf386f082019-07-29 23:03:03 +02001" Test for channel and job functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaarf386f082019-07-29 23:03:03 +02003" When +channel is supported then +job is too, so we don't check for that.
Bram Moolenaar4641a122019-07-29 22:10:23 +02004source check.vim
5CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01006
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02008source screendump.vim
Bram Moolenaardfc33a62020-04-29 22:30:13 +02009source view_util.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +020010
11let s:python = PythonProg()
12if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010013 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020014 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010015endif
16
Bram Moolenaar37175402017-03-18 20:18:45 +010017" Uncomment the next line to see what happens. Output is in
18" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020019" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010020" call ch_logfile('channellog', 'w')
21
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020022func SetUp()
Bram Moolenaar94722c52023-01-28 19:19:03 +000023 if g:testfunc =~ '_ipv6()$'
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020024 let s:localhost = '[::1]:'
25 let s:testscript = 'test_channel_6.py'
LemonBoycc766a82022-04-04 15:46:58 +010026 elseif g:testfunc =~ '_unix()$'
27 let s:localhost = 'unix:Xtestsocket'
28 let s:testscript = 'test_channel_unix.py'
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020029 else
30 let s:localhost = 'localhost:'
31 let s:testscript = 'test_channel.py'
32 endif
33 let s:chopt = {}
34 call ch_log(g:testfunc)
Bram Moolenaarec9b0172020-06-19 19:10:59 +020035
36 " Most tests use job_start(), which can be flaky
37 let g:test_is_flaky = 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020038endfunc
Bram Moolenaar3b05b132016-02-03 23:25:07 +010039
Bram Moolenaar4b96df52020-01-26 22:00:26 +010040" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010041func s:run_server(testfunc, ...)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020042 call RunServer(s:testscript, a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010043endfunc
44
LemonBoycc766a82022-04-04 15:46:58 +010045" Returns the address of the test server.
46func s:address(port)
47 if s:localhost =~ '^unix:'
48 return s:localhost
49 else
50 return s:localhost . a:port
51 end
52endfunc
53
Bram Moolenaar819524702018-02-27 19:10:00 +010054" Return a list of open files.
55" Can be used to make sure no resources leaked.
56" Returns an empty list on systems where this is not supported.
57func s:get_resources()
58 let pid = getpid()
59
Bram Moolenaar39536dd2019-01-29 22:58:21 +010060 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010061 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
62 elseif isdirectory('/proc/' . pid . '/fd/')
63 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
64 else
65 return []
66 endif
67endfunc
68
Bram Moolenaar321efdd2016-07-15 17:09:11 +020069let g:Ch_responseMsg = ''
70func Ch_requestHandler(handle, msg)
71 let g:Ch_responseHandle = a:handle
72 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010073endfunc
74
Bram Moolenaar321efdd2016-07-15 17:09:11 +020075func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010076 " Avoid dropping messages, since we don't use a callback here.
77 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020078 " Also add the noblock flag to try it out.
79 let s:chopt.noblock = 1
LemonBoycc766a82022-04-04 15:46:58 +010080 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010081 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010082 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010083 return
84 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020085
86 " check that getjob without a job is handled correctly
87 call assert_equal('no process', string(ch_getjob(handle)))
88
Bram Moolenaar570497a2019-08-22 22:55:13 +020089 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010090 call assert_true(dict.id != 0)
91 call assert_equal('open', dict.status)
LemonBoycc766a82022-04-04 15:46:58 +010092 if has_key(dict, 'port')
93 " Channels using Unix sockets have no 'port' entry.
94 call assert_equal(a:port, string(dict.port))
95 end
Bram Moolenaar03602ec2016-03-20 20:57:45 +010096 call assert_equal('open', dict.sock_status)
97 call assert_equal('socket', dict.sock_io)
98
Bram Moolenaard7ece102016-02-02 23:23:02 +010099 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100100 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100101
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100102 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100103 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
104 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
105 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
106
107 " split command should work
108 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200109 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100110 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100111
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200112 " string with ][ should work
113 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
114
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100115 " nothing to read now
116 call assert_equal(0, ch_canread(handle))
117
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200118 " sending three messages quickly then reading should work
119 for i in range(3)
120 call ch_sendexpr(handle, 'echo hello ' . i)
121 endfor
122 call assert_equal('hello 0', ch_read(handle)[1])
123 call assert_equal('hello 1', ch_read(handle)[1])
124 call assert_equal('hello 2', ch_read(handle)[1])
125
Bram Moolenaard7ece102016-02-02 23:23:02 +0100126 " Request that triggers sending two ex commands. These will usually be
127 " handled before getting the response, but it's not guaranteed, thus wait a
128 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100129 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200130 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100131 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100132
Bram Moolenaarb836f632021-07-01 22:11:28 +0200133 " Request command "echoerr 'this is an error'".
134 " This will throw an exception, catch it here.
135 let caught = 'no'
136 try
137 call assert_equal('ok', ch_evalexpr(handle, 'echoerr'))
138 catch /this is an error/
139 let caught = 'yes'
140 endtry
141 if caught != 'yes'
142 call assert_report("Expected exception from error message")
143 endif
144
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100145 " Request command "foo bar", which fails silently.
146 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200147 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100148
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100149 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200150 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100151
Bram Moolenaara07fec92016-02-05 21:04:08 +0100152 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200153 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
154 call WaitFor('exists("g:Ch_responseHandle")')
155 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100156 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100157 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200158 call assert_equal(handle, g:Ch_responseHandle)
159 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100160 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200161 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100162
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200163 let g:Ch_responseMsg = ''
164 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
165 call WaitFor('exists("g:Ch_responseHandle")')
166 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100167 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100168 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200169 call assert_equal(handle, g:Ch_responseHandle)
170 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100171 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200172 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100173
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200174 " Using lambda.
175 let g:Ch_responseMsg = ''
176 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
177 call WaitFor('exists("g:Ch_responseHandle")')
178 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100179 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200180 else
181 call assert_equal(handle, g:Ch_responseHandle)
182 unlet g:Ch_responseHandle
183 endif
184 call assert_equal('got it', g:Ch_responseMsg)
185
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100186 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200187 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100188
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100189 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200190 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100191 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100192 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200193 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", 'E475:')
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100194 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100195 call ch_setoptions(handle, {'drop': 'never'})
196 call ch_setoptions(handle, {'drop': 'auto'})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200197 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", 'E475:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200198 call assert_equal(0, ch_setoptions(handle, test_null_dict()))
199 call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'}))
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100200
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100201 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100202 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100203 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100204 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100205
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100206 " Send an eval request with special characters.
207 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
208 sleep 10m
209 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
210
211 " Send an eval request to get a line with special characters.
212 call setline(3, "a\nb\<CR>c\x01d\x7fe")
213 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
214 sleep 10m
215 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
216
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100217 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100219 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100220 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100221
Bram Moolenaar55fab432016-02-07 16:53:13 +0100222 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100223 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100224 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100225 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100226
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100227 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100228 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100229 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100230 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100231
Bram Moolenaarf4160862016-02-05 23:09:12 +0100232 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100233 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200234 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100235 call assert_equal('one', getline(line('$') - 2))
236 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100237
238 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
240 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100241
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100242 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100243
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100244 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100245 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100246 if exists('*reltimefloat')
247 let start = reltime()
248 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
249 let elapsed = reltime(start)
250 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
251 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100252
253 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100254 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100255 let resp = ch_read(handle)
256 call assert_equal(type([]), type(resp))
257 call assert_equal(type(11), type(resp[0]))
258 call assert_equal('waited', resp[1])
259
Bram Moolenaard7ece102016-02-02 23:23:02 +0100260 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100261 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100262endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100263
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100264func Test_communicate()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200265 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100266endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100267
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200268func Test_communicate_ipv6()
269 CheckIPv6
270 call Test_communicate()
271endfunc
272
LemonBoycc766a82022-04-04 15:46:58 +0100273func Test_communicate_unix()
274 CheckUnix
275 call Test_communicate()
276 call delete('Xtestsocket')
277endfunc
278
279
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100280" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200281func Ch_two_channels(port)
LemonBoycc766a82022-04-04 15:46:58 +0100282 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200283 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200284 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100285 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100286 return
287 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100288
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100289 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100290
LemonBoycc766a82022-04-04 15:46:58 +0100291 let newhandle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100292 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100293 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100294 return
295 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100296 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
297 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100298
299 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100300 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100301
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100302 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200303 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100304endfunc
305
306func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200307 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200308 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100309endfunc
310
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200311func Test_two_channels_ipv6()
312 CheckIPv6
313 call Test_two_channels()
314endfunc
315
LemonBoycc766a82022-04-04 15:46:58 +0100316func Test_two_channels_unix()
317 CheckUnix
318 call Test_two_channels()
319 call delete('Xtestsocket')
320endfunc
321
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100322" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200323func Ch_server_crash(port)
LemonBoycc766a82022-04-04 15:46:58 +0100324 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100325 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100326 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100327 return
328 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100329
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100330 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100331
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100332 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100333endfunc
334
335func Test_server_crash()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200336 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100337endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100338
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200339func Test_server_crash_ipv6()
340 CheckIPv6
341 call Test_server_crash()
342endfunc
343
LemonBoycc766a82022-04-04 15:46:58 +0100344func Test_server_crash_unix()
345 CheckUnix
346 call Test_server_crash()
347 call delete('Xtestsocket')
348endfunc
349
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100350"""""""""
351
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200352func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100353 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200354 unlet g:Ch_reply
355 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100356endfunc
357
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200358func Ch_channel_handler(port)
LemonBoycc766a82022-04-04 15:46:58 +0100359 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100360 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100361 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100362 return
363 endif
364
365 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100366 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200367 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100368
369 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100370 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200371 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100372endfunc
373
374func Test_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 = 'Ch_handler'
377 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200378 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200379 let s:chopt.callback = function('Ch_handler')
380 call s:run_server('Ch_channel_handler')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200381endfunc
382
383func Test_channel_handler_ipv6()
384 CheckIPv6
385 call Test_channel_handler()
Bram Moolenaarf6157282016-02-10 21:07:14 +0100386endfunc
387
LemonBoycc766a82022-04-04 15:46:58 +0100388func Test_channel_handler_unix()
389 CheckUnix
390 call Test_channel_handler()
391 call delete('Xtestsocket')
392endfunc
393
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100394"""""""""
395
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200396let g:Ch_reply = ''
397func Ch_zeroHandler(chan, msg)
398 unlet g:Ch_reply
399 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100400endfunc
401
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200402let g:Ch_zero_reply = ''
403func Ch_oneHandler(chan, msg)
404 unlet g:Ch_zero_reply
405 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100406endfunc
407
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200408func Ch_channel_zero(port)
LemonBoycc766a82022-04-04 15:46:58 +0100409 let handle = (s:address(a:port))->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100410 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100411 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100412 return
413 endif
414
415 " Check that eval works.
416 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
417
418 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200419 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100420 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100421 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200422 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100423 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100424 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200425 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100426 endif
427
428 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200429 let g:Ch_reply = ''
430 let g:Ch_zero_reply = ''
431 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200432 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100433 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200434 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100435 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200436 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100437 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100438endfunc
439
440func Test_zero_reply()
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100441 " Run with channel handler
442 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200443 let s:chopt.callback = 'Ch_zeroHandler'
444 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100445 unlet s:chopt.callback
446
447 " Run without channel handler
448 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200449 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100450endfunc
451
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200452func Test_zero_reply_ipv6()
453 CheckIPv6
454 call Test_zero_reply()
455endfunc
456
LemonBoycc766a82022-04-04 15:46:58 +0100457func Test_zero_reply_unix()
458 CheckUnix
459 call Test_zero_reply()
460 call delete('Xtestsocket')
461endfunc
462
463
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100464"""""""""
465
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200466let g:Ch_reply1 = ""
467func Ch_handleRaw1(chan, msg)
468 unlet g:Ch_reply1
469 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100470endfunc
471
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200472let g:Ch_reply2 = ""
473func Ch_handleRaw2(chan, msg)
474 unlet g:Ch_reply2
475 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100476endfunc
477
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200478let g:Ch_reply3 = ""
479func Ch_handleRaw3(chan, msg)
480 unlet g:Ch_reply3
481 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100482endfunc
483
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200484func Ch_raw_one_time_callback(port)
LemonBoycc766a82022-04-04 15:46:58 +0100485 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100486 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100487 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100488 return
489 endif
490 call ch_setoptions(handle, {'mode': 'raw'})
491
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100492 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200493 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200494 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200495 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
496 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200497 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100498 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200499 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100500endfunc
501
502func Test_raw_one_time_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200503 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100504endfunc
505
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200506func Test_raw_one_time_callback_ipv6()
507 CheckIPv6
508 call Test_raw_one_time_callback()
509endfunc
510
LemonBoycc766a82022-04-04 15:46:58 +0100511func Test_raw_one_time_callback_unix()
512 CheckUnix
513 call Test_raw_one_time_callback()
514 call delete('Xtestsocket')
515endfunc
516
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100517"""""""""
518
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100519" Test that trying to connect to a non-existing port fails quickly.
520func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100521 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100522 " this is timing sensitive
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100523
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100524 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100525 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100526 if ch_status(handle) != "fail"
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200527 " Oops, port exists.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100528 call ch_close(handle)
529 else
530 let elapsed = reltime(start)
zeertzjqcdc6a432022-06-19 11:45:46 +0100531 call assert_inrange(0.0, 1.0, reltimefloat(elapsed))
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100532 endif
533
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100534 " We intend to use a socket that doesn't exist and wait for half a second
535 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100536 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100537 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100538 try
539 let handle = ch_open('localhost:9867', {'waittime': 500})
540 if ch_status(handle) != "fail"
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200541 " Oops, port exists.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100542 call ch_close(handle)
543 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100544 " Failed connection should wait about 500 msec. Can be longer if the
545 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100546 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100547 endif
548 catch
549 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100550 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100551 endif
552 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100553endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100554
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100555"""""""""
556
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100557func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100558 " Add a dummy close callback to avoid that messages are dropped when calling
559 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200560 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100561 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200562 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200563 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100564 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200565
566 call assert_equal("open", ch_status(job))
567 call assert_equal("open", ch_status(job), {"part": "out"})
568 call assert_equal("open", ch_status(job), {"part": "err"})
569 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
570 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
571
572 let dict = ch_info(job)
573 call assert_true(dict.id != 0)
574 call assert_equal('open', dict.status)
575 call assert_equal('open', dict.out_status)
576 call assert_equal('RAW', dict.out_mode)
577 call assert_equal('pipe', dict.out_io)
578 call assert_equal('open', dict.err_status)
579 call assert_equal('RAW', dict.err_mode)
580 call assert_equal('pipe', dict.err_io)
581
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100582 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100583 " For a change use the job where a channel is expected.
584 call ch_sendraw(job, "echo something\n")
585 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100586 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
587
Bram Moolenaar151f6562016-03-07 21:19:38 +0100588 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200589 let g:handle = job->job_getchannel()
590 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100591 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100592 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100593 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
594
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200595 let g:Ch_reply = ""
596 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200597 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200598
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200599 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
600 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
601 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
602 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
603
Bram Moolenaar570497a2019-08-22 22:55:13 +0200604 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100605 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
606 finally
607 call job_stop(job)
608 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100609
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200610 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200611 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200612 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100613 call assert_equal("dead", info.status)
614 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200615 call assert_equal(2, len(info.cmd))
616 call assert_equal("test_channel_pipe.py", info.cmd[1])
617
618 let found = 0
619 for j in job_info()
620 if j == job
621 let found += 1
622 endif
623 endfor
624 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100625
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200626 call assert_fails("call job_stop('abc')", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200627 call assert_fails("call job_stop(job, [])", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200628 call assert_fails("call job_stop(test_null_job())", 'E916:')
629
Bram Moolenaar8b633132020-03-20 18:20:51 +0100630 " Try to use the job and channel where a number is expected. This is not
631 " related to testing the raw pipe. This test is here just to reuse the
632 " already created job/channel.
633 let ch = job_getchannel(job)
634 call assert_fails('let i = job + 1', 'E910:')
635 call assert_fails('let j = ch + 1', 'E913:')
636 call assert_fails('echo 2.0 == job', 'E911:')
637 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100638endfunc
639
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100640func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100641 " Add a dummy close callback to avoid that messages are dropped when calling
642 " ch_canread().
643 " Also test the non-blocking option.
644 let job = job_start(s:python . " test_channel_pipe.py",
645 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
646 call assert_equal(v:t_job, type(job))
647 call assert_equal("run", job_status(job))
648
649 call assert_equal("open", ch_status(job))
650 call assert_equal("open", ch_status(job), {"part": "out"})
651
652 try
653 " Create a blob with the echo command and write it.
654 let blob = 0z00
655 let cmd = "echo something\n"
656 for i in range(0, len(cmd) - 1)
657 let blob[i] = char2nr(cmd[i])
658 endfor
659 call assert_equal(len(cmd), len(blob))
660 call ch_sendraw(job, blob)
661
662 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200663 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100664 let expected = 'something'
665 for i in range(0, len(expected) - 1)
666 call assert_equal(char2nr(expected[i]), msg[i])
667 endfor
668
669 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
670 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
671 finally
672 call job_stop(job)
673 endtry
674
675 let g:Ch_job = job
676 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
677 let info = job_info(job)
678 call assert_equal("dead", info.status)
679endfunc
680
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100681func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100682 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100683 call assert_equal("run", job_status(job))
684 try
685 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100686 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200687 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100688
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100689 call ch_sendraw(handle, "echoerr wrong\n")
690 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
691
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100692 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100693 call assert_equal("this", ch_readraw(handle))
694 call assert_equal("AND this", ch_readraw(handle))
695
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200696 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200697 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200698
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100699 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100700 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100701 finally
702 call job_stop(job)
703 endtry
704endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100705
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200706func Stop_g_job()
707 call job_stop(g:job)
708 if has('win32')
709 " On MS-Windows the server must close the file handle before we are able
710 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200711 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200712 sleep 10m
713 endif
714endfunc
715
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100716func Test_nl_read_file()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100717 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput', 'D')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200718 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100719 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200720 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100721 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200722 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100723 call assert_equal("something", ch_readraw(handle))
724 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
725 call assert_equal("this", ch_readraw(handle))
726 call assert_equal("AND this", ch_readraw(handle))
727 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200728 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100729 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200730 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100731endfunc
732
Bram Moolenaare98d1212016-03-08 15:37:41 +0100733func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200734 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100735 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200736 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100737 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200738 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100739 call ch_sendraw(handle, "echo line one\n")
740 call ch_sendraw(handle, "echo line two\n")
741 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200742 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100743 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200744 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100745 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100746 call delete('Xoutput')
747 endtry
748endfunc
749
750func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200751 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100752 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200753 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100754 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200755 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100756 call ch_sendraw(handle, "echoerr line one\n")
757 call ch_sendraw(handle, "echoerr line two\n")
758 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200759 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100760 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200761 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100762 call delete('Xoutput')
763 endtry
764endfunc
765
766func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200767 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100768 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200769 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100770 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200771 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100772 call ch_sendraw(handle, "echoerr line one\n")
773 call ch_sendraw(handle, "echo line two\n")
774 call ch_sendraw(handle, "double this\n")
775 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200776 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100777 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200778 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100779 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100780 call delete('Xoutput')
781 endtry
782endfunc
783
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200784func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200785 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200786endfunc
787
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200788func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200789 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200790 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200791 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100792 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100793 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200794 if a:do_msg
795 let expected[0] = 'Reading from channel output...'
796 else
797 let options['out_msg'] = 0
798 call remove(expected, 0)
799 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100800 else
801 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100802 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100803 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200804 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100805 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200806 if a:nomod
807 let options['out_modifiable'] = 0
808 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100809 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100810 call assert_equal("run", job_status(job))
811 try
812 let handle = job_getchannel(job)
813 call ch_sendraw(handle, "echo line one\n")
814 call ch_sendraw(handle, "echo line two\n")
815 call ch_sendraw(handle, "double this\n")
816 call ch_sendraw(handle, "quit\n")
817 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100818 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200819 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200820 if a:nomod
821 call assert_equal(0, &modifiable)
822 else
823 call assert_equal(1, &modifiable)
824 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200825 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100826 bwipe!
827 finally
828 call job_stop(job)
829 endtry
830endfunc
831
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100832func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200833 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834endfunc
835
836func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200837 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100838endfunc
839
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200840func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200841 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200842endfunc
843
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200844func Test_pipe_to_buffer_name_nomsg()
845 call Run_test_pipe_to_buffer(1, 0, 1)
846endfunc
847
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200848func Test_close_output_buffer()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100849 let g:test_is_flaky = 1
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200850 enew!
851 let test_lines = ['one', 'two']
852 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200853 let options = {'out_io': 'buffer'}
854 let options['out_name'] = 'buffer-output'
855 let options['out_msg'] = 0
856 split buffer-output
857 let job = job_start(s:python . " test_channel_write.py", options)
858 call assert_equal("run", job_status(job))
859 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200860 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200861 quit!
862 sleep 100m
863 " Make sure the write didn't happen to the wrong buffer.
864 call assert_equal(test_lines, getline(1, line('$')))
865 call assert_equal(-1, bufwinnr('buffer-output'))
866 sbuf buffer-output
867 call assert_notequal(-1, bufwinnr('buffer-output'))
868 sleep 100m
869 close " no more writes
870 bwipe!
871 finally
872 call job_stop(job)
873 endtry
874endfunc
875
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200876func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100877 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200878 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100879 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100880 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200881 if a:do_msg
882 let expected[0] = 'Reading from channel error...'
883 else
884 let options['err_msg'] = 0
885 call remove(expected, 0)
886 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100887 else
888 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100889 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100890 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200891 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100892 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200893 if a:nomod
894 let options['err_modifiable'] = 0
895 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100896 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100897 call assert_equal("run", job_status(job))
898 try
899 let handle = job_getchannel(job)
900 call ch_sendraw(handle, "echoerr line one\n")
901 call ch_sendraw(handle, "echoerr line two\n")
902 call ch_sendraw(handle, "doubleerr this\n")
903 call ch_sendraw(handle, "quit\n")
904 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200905 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200906 if a:nomod
907 call assert_equal(0, &modifiable)
908 else
909 call assert_equal(1, &modifiable)
910 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100911 bwipe!
912 finally
913 call job_stop(job)
914 endtry
915endfunc
916
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100917func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200918 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100919endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100920
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100921func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200922 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200923endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100924
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200925func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200926 call Run_test_pipe_err_to_buffer(1, 1, 1)
927endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100928
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200929func Test_pipe_err_to_buffer_name_nomsg()
930 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100931endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100932
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100933func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100934 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100935 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100936 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200937 let handle = job_getchannel(job)
938 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
939 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100940 try
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100941 call ch_sendraw(handle, "echo line one\n")
942 call ch_sendraw(handle, "echoerr line two\n")
943 call ch_sendraw(handle, "double this\n")
944 call ch_sendraw(handle, "doubleerr that\n")
945 call ch_sendraw(handle, "quit\n")
946 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200947 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 +0100948 bwipe!
949 finally
950 call job_stop(job)
951 endtry
952endfunc
953
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100954func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100955 sp pipe-input
956 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200957 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100958 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100959 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100960 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100961 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100962 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100963
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100964 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100965 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200966 if has('unix') && !a:use_name
967 call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
968 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100969 try
970 let handle = job_getchannel(job)
971 call assert_equal('one', ch_read(handle))
972 call assert_equal('two', ch_read(handle))
973 call assert_equal('three', ch_read(handle))
974 bwipe!
975 finally
976 call job_stop(job)
977 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100978endfunc
979
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100980func Test_pipe_from_buffer_name()
981 call Run_test_pipe_from_buffer(1)
982endfunc
983
984func Test_pipe_from_buffer_nr()
985 call Run_test_pipe_from_buffer(0)
986endfunc
987
Bram Moolenaar0874a832016-09-01 15:11:51 +0200988func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200989 CheckExecutable sort
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100990 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200991
Bram Moolenaar0874a832016-09-01 15:11:51 +0200992 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
993 if a:use_buffer
994 split sortin
995 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
996 let options.in_io = 'buffer'
997 let options.in_name = 'sortin'
998 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200999 if !a:all
1000 let options.in_top = 2
1001 let options.in_bot = 4
1002 endif
Bram Moolenaare2956092019-01-25 21:01:17 +01001003 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +02001004
1005 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +01001006 call assert_equal("run", job_status(job))
1007 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +02001008 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001009 endif
1010
Bram Moolenaare2956092019-01-25 21:01:17 +01001011 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +02001012
Bram Moolenaard8b55492016-09-01 14:35:22 +02001013 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +02001014 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +02001015 call assert_equal('Reading from channel output...', getline(1))
1016 if a:all
1017 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
1018 else
1019 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
1020 endif
1021
Bram Moolenaare2956092019-01-25 21:01:17 +01001022 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +02001023 if a:use_buffer
1024 bwipe! sortin
1025 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +02001026 bwipe! sortout
1027endfunc
1028
1029func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001030 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001031endfunc
1032
1033func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001034 call Run_pipe_through_sort(0, 1)
1035endfunc
1036
1037func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001038 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001039endfunc
1040
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001041func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001042 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001043 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001044 call assert_equal("run", job_status(job))
1045 try
1046 let handle = job_getchannel(job)
1047 call ch_sendraw(handle, "echo line one\n")
1048 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +02001049 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001050 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001051 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
1052 bwipe!
1053 finally
1054 call job_stop(job)
1055 endtry
1056endfunc
1057
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001058func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001059 CheckFunction reltimefloat
1060
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001061 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001062 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001063 call assert_equal("run", job_status(job))
1064 try
1065 let handle = job_getchannel(job)
1066 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1067 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1068 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001069 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001070 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1071 bwipe!
1072 finally
1073 call job_stop(job)
1074 endtry
1075endfunc
1076
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001077" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001078func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001079 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001080 if getline(line('$') - a:offset) == a:line
1081 break
1082 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001083 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001084 endfor
1085endfunc
1086
1087func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001088 " Create two buffers, one to read from and one to write to.
1089 split pipe-output
1090 set buftype=nofile
1091 split pipe-input
1092 set buftype=nofile
1093
1094 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001095 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001096 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1097 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001098 call assert_equal("run", job_status(job))
1099 try
1100 exe "normal Gaecho hello\<CR>"
1101 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001102 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001103 call assert_equal('hello', getline('$'))
1104
1105 exe bufwinnr('pipe-input') . "wincmd w"
1106 exe "normal Gadouble this\<CR>"
1107 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001108 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001109 call assert_equal('this', getline(line('$') - 1))
1110 call assert_equal('AND this', getline('$'))
1111
1112 bwipe!
1113 exe bufwinnr('pipe-input') . "wincmd w"
1114 bwipe!
1115 finally
1116 call job_stop(job)
1117 endtry
1118endfunc
1119
1120func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001121 " Create one buffer to read from and to write to.
1122 split pipe-io
1123 set buftype=nofile
1124
1125 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001126 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001127 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1128 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001129 call assert_equal("run", job_status(job))
1130 try
1131 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001132 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001133 call assert_equal('hello', getline(line('$') - 1))
1134
1135 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001136 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001137 call assert_equal('this', getline(line('$') - 2))
1138 call assert_equal('AND this', getline(line('$') - 1))
1139
1140 bwipe!
1141 finally
1142 call job_stop(job)
1143 endtry
1144endfunc
1145
Bram Moolenaar4641a122019-07-29 22:10:23 +02001146func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001147 CheckScreendump
1148
Bram Moolenaar4641a122019-07-29 22:10:23 +02001149 let lines =<< trim END
1150 new Xscrollbuffer
1151 call setline(1, range(1, 200))
1152 $
1153 redraw
1154 wincmd w
1155 call deletebufline('Xscrollbuffer', 1, '$')
1156 if has('win32')
1157 let cmd = ['cmd', '/c', 'echo sometext']
1158 else
1159 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1160 endif
1161 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1162 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001163 call writefile(lines, 'XtestBufferScroll', 'D')
Bram Moolenaar4641a122019-07-29 22:10:23 +02001164 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001165 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001166 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1167
1168 " clean up
1169 call StopVimInTerminal(buf)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001170endfunc
1171
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001172func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001173 " We cannot check that no I/O works, we only check that the job starts
1174 " properly.
1175 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001176 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001177 call assert_equal("run", job_status(job))
1178 try
1179 call assert_equal('something', ch_read(job))
1180 finally
1181 call job_stop(job)
1182 endtry
1183
1184 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001185 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001186 call assert_equal("run", job_status(job))
1187 try
1188 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1189 finally
1190 call job_stop(job)
1191 endtry
1192
1193 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001194 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001195 call assert_equal("run", job_status(job))
1196 try
1197 call assert_equal('something', ch_read(job))
1198 finally
1199 call job_stop(job)
1200 endtry
1201
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001202 " This causes spurious leak errors with valgrind.
1203 if !RunningWithValgrind()
1204 let job = job_start(s:python . " test_channel_pipe.py something",
1205 \ {'out_io': 'null', 'err_io': 'out'})
1206 call assert_equal("run", job_status(job))
1207 call job_stop(job)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001208
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001209 let job = job_start(s:python . " test_channel_pipe.py something",
1210 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
1211 call assert_equal("run", job_status(job))
1212 call assert_equal('channel fail', string(job_getchannel(job)))
1213 call assert_equal('fail', ch_status(job))
1214 call assert_equal('no process', string(test_null_job()))
1215 call assert_equal('channel fail', string(test_null_channel()))
1216 call job_stop(job)
1217 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001218endfunc
1219
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001220func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001221 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1222 split testout
Bram Moolenaar94722c52023-01-28 19:19:03 +00001223 let job = job_start([s:python, '-c',
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001224 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001225 " the job may be done quickly, also accept "dead"
1226 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001227 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001228 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001229 let totlen = 0
1230 for line in getline(1, '$')
1231 call assert_equal('', substitute(line, '^\.*', '', ''))
1232 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001233 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001234 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001235 finally
1236 call job_stop(job)
1237 bwipe!
1238 endtry
1239endfunc
1240
Bram Moolenaarde279892016-03-11 22:19:44 +01001241func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001242 let job = job_start(s:python . " test_channel_pipe.py")
1243 call assert_equal("run", job_status(job))
1244 let handle = job_getchannel(job)
1245 try
1246 call ch_sendraw(handle, "echo something\n")
1247 call assert_equal("something", ch_readraw(handle))
1248 finally
1249 call job_stop(job)
1250 endtry
1251
1252 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1253 call assert_equal("run", job_status(job))
1254 let handle = job_getchannel(job)
1255 try
1256 call ch_sendraw(handle, "echo again\n")
1257 call assert_equal("again", ch_readraw(handle))
1258 finally
1259 call job_stop(job)
1260 endtry
1261endfunc
1262
Bram Moolenaar75f72652016-03-20 22:16:56 +01001263func Test_out_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001264 let g:test_is_flaky = 1
Bram Moolenaar75f72652016-03-20 22:16:56 +01001265 let dict = {'thisis': 'dict: '}
1266 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001267 if type(a:msg) == v:t_string
1268 let g:Ch_outmsg = self.thisis . a:msg
1269 else
1270 let g:Ch_outobj = a:msg
1271 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001272 endfunc
1273 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001274 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001275 endfunc
1276 let job = job_start(s:python . " test_channel_pipe.py",
1277 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001278 \ 'out_mode': 'json',
1279 \ 'err_cb': dict.errHandler,
1280 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001281 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001282 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001283 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001284 let g:Ch_outmsg = ''
1285 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001286 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1287 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001288 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1289 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001290
1291 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001292 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001293 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001294 " For unknown reasons this can be very slow on Mac.
Bram Moolenaardeda6442021-12-17 11:44:33 +00001295 " Increase the timeout on every run.
1296 if g:run_nr == 1
1297 let timeout = 5000
1298 elseif g:run_nr == 2
1299 let timeout = 10000
1300 elseif g:run_nr == 3
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001301 let timeout = 20000
1302 else
Bram Moolenaardeda6442021-12-17 11:44:33 +00001303 let timeout = 40000
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001304 endif
1305 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001306 finally
1307 call job_stop(job)
1308 endtry
1309endfunc
1310
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001311func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001312 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001313 let g:Ch_msg1 = ''
1314 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001315 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001316 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001317 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001318 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001319 let s:counter += 1
1320 endfunc
1321 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001322 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001323 let s:counter += 1
1324 endfunc
1325 let job = job_start(s:python . " test_channel_pipe.py quit now",
1326 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001327 \ 'close_cb': 'CloseHandler'})
1328 " the job may be done quickly, also accept "dead"
1329 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001330 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001331 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1332 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001333 finally
1334 call job_stop(job)
1335 delfunc OutHandler
1336 delfunc CloseHandler
1337 endtry
1338endfunc
1339
Bram Moolenaar437905c2016-04-26 19:01:05 +02001340func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001341 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001342 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001343 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001344 endfunc
1345 let job = job_start(s:python . " test_channel_pipe.py quit now",
1346 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001347 " the job may be done quickly, also accept "dead"
1348 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001349 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001350 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001351 finally
1352 call job_stop(job)
1353 delfunc CloseHandler
1354 endtry
1355endfunc
1356
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001357" Use channel in NL mode but received text does not end in NL.
1358func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001359 let g:Ch_received = ''
1360 func! CloseHandler(chan)
1361 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1362 let g:Ch_received .= ch_read(a:chan)
1363 endwhile
1364 endfunc
1365 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1366 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001367 " the job may be done quickly, also accept "dead"
1368 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001369 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001370 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001371 finally
1372 call job_stop(job)
1373 delfunc CloseHandler
1374 endtry
1375endfunc
1376
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001377func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001378 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001379 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1380 \ 'out_mode': 'json',
1381 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1382 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001383 call assert_equal("run", job_status(job))
1384 try
1385 let g:Ch_outmsg = ''
1386 let g:Ch_errmsg = ''
1387 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1388 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001389 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1390 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001391 finally
1392 call job_stop(job)
1393 endtry
1394endfunc
1395
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001396func Test_close_and_exit_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001397 let g:test_is_flaky = 1
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001398 let g:retdict = {'ret': {}}
1399 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001400 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001401 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001402 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001403 let self.ret['exit_cb'] = job_status(a:job)
1404 endfunc
1405
Bram Moolenaare2956092019-01-25 21:01:17 +01001406 let job = job_start([&shell, &shellcmdflag, 'echo'],
1407 \ {'close_cb': g:retdict.close_cb,
1408 \ 'exit_cb': g:retdict.exit_cb})
1409 " the job may be done quickly, also accept "dead"
1410 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001411 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001412 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001413 call assert_equal('dead', g:retdict.ret['exit_cb'])
1414 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001415endfunc
1416
Bram Moolenaard46ae142016-02-16 13:33:52 +01001417""""""""""
1418
Bram Moolenaar0b146882018-09-06 16:27:24 +02001419function ExitCbWipe(job, status)
1420 exe g:wipe_buf 'bw!'
1421endfunction
1422
1423" This caused a crash, because messages were handled while peeking for a
1424" character.
1425func Test_exit_cb_wipes_buf()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001426 CheckFeature timers
Bram Moolenaar0b146882018-09-06 16:27:24 +02001427 set cursorline lazyredraw
1428 call test_override('redraw_flag', 1)
1429 new
1430 let g:wipe_buf = bufnr('')
1431
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001432 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001433 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001434 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1435 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1436 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1437 call timer_stop(timer)
1438
1439 set nocursorline nolazyredraw
1440 unlet g:wipe_buf
1441 call test_override('ALL', 0)
1442endfunc
1443
1444""""""""""
1445
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001446let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001447func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001448 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001449 unlet s:channelfd
1450endfunc
1451
1452" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001453func Ch_unlet_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001454 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001455 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001456 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001457endfunc
1458
1459func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001461endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001462
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001463func Test_unlet_handle_ipv6()
1464 CheckIPv6
1465 call Test_unlet_handle()
1466endfunc
1467
Bram Moolenaard46ae142016-02-16 13:33:52 +01001468""""""""""
1469
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001470let g:Ch_unletResponse = ''
1471func Ch_CloseHandler(handle, msg)
1472 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001473 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001474endfunc
1475
1476" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001477func Ch_close_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001478 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001480 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001481endfunc
1482
1483func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001484 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001485endfunc
1486
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001487func Test_close_handle_ipv6()
1488 CheckIPv6
1489 call Test_close_handle()
1490endfunc
1491
1492""""""""""
1493
1494func Ch_open_ipv6(port)
LemonBoycc766a82022-04-04 15:46:58 +01001495 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001496 call assert_notequal('fail', ch_status(handle))
1497endfunc
1498
1499func Test_open_ipv6()
1500 CheckIPv6
1501 call s:run_server('Ch_open_ipv6')
1502endfunc
1503
Bram Moolenaard46ae142016-02-16 13:33:52 +01001504""""""""""
1505
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001506func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001507 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001508 echo ch
1509 let d = ch
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001510 call assert_fails("let ch = ch_open('noserver', 10)", 'E1206:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001511 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001512 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001513 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1514 \ 'E474:')
1515 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1516 \ 'E475:')
1517 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1518 \ 'E475:')
1519 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1520 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001521 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1522 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1523 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001524endfunc
1525
1526func Test_ch_info_fail()
1527 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001528endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001529
1530""""""""""
1531
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001532func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001533 " Wait up to a second for the port to open.
1534 let s:chopt.waittime = 1000
LemonBoycc766a82022-04-04 15:46:58 +01001535 let channel = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001536 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001537 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001538 return
1539 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001540 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001541 call ch_close(channel)
1542endfunc
1543
1544func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001545 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001546 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001547endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001548
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001549func Test_open_delay_ipv6()
1550 CheckIPv6
1551 call Test_open_delay()
1552endfunc
1553
Bram Moolenaarece61b02016-02-20 21:39:05 +01001554"""""""""
1555
1556function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001557 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001558endfunc
1559
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001560function Ch_test_call(port)
LemonBoycc766a82022-04-04 15:46:58 +01001561 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001562 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001563 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001564 return
1565 endif
1566
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001567 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001568 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001569 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001570
1571 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1572 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1573 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001574endfunc
1575
1576func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001577 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001578endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001579
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001580func Test_call_ipv6()
1581 CheckIPv6
1582 call Test_call()
1583endfunc
1584
LemonBoycc766a82022-04-04 15:46:58 +01001585func Test_call_unix()
1586 CheckUnix
1587 call Test_call()
1588 call delete('Xtestsocket')
1589endfunc
1590
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001591"""""""""
1592
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001593let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001594function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001595 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001596endfunc
1597
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001598function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001599 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001600 let g:Ch_exit_job = g:currentJob
1601 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001602endfunc
1603
1604func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001605 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001606
Bram Moolenaarf386f082019-07-29 23:03:03 +02001607 " wait up to a second for the job to exit
1608 for i in range(100)
1609 if g:Ch_job_exit_ret == 'done'
1610 break
1611 endif
1612 sleep 10m
1613 " calling job_status() triggers the callback
1614 call job_status(g:Ch_exit_job)
1615 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001616
Bram Moolenaarf386f082019-07-29 23:03:03 +02001617 call assert_equal('done', g:Ch_job_exit_ret)
1618 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1619 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001620endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001621
Bram Moolenaar97792de2016-10-15 18:36:49 +02001622function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001623 if job_info(a:job).process == g:exit_cb_val.process
1624 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1625 endif
1626 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001627endfunction
1628
1629func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001630 CheckFunction reltimefloat
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001631 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001632
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001633 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001634 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001635 let g:exit_cb_val.process = job_info(job).process
K.Takatacd9fa252022-09-20 11:04:47 +01001636 try
1637 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1638 catch
1639 call add(v:errors, "Job status: " .. string(job->job_info()))
1640 throw v:exception
1641 endtry
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001642 let elapsed = reltimefloat(g:exit_cb_val.end)
zeertzjqcdc6a432022-06-19 11:45:46 +01001643 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001644
1645 " case: unreferenced job, using timer
1646 if !has('timers')
1647 return
1648 endif
1649
1650 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1651 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1652 let g:exit_cb_val.process = job_info(g:job).process
1653 unlet g:job
1654 call Standby(1000)
1655 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1656 let elapsed = reltimefloat(g:exit_cb_val.end)
1657 else
1658 let elapsed = 1.0
1659 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001660 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001661endfunc
1662
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001663"""""""""
1664
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001665let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001666function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001667 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001668endfunc
1669
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001670function Ch_test_close_callback(port)
LemonBoycc766a82022-04-04 15:46:58 +01001671 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001672 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001673 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001674 return
1675 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001676 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001677
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001678 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001679 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001680endfunc
1681
1682func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001683 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001684endfunc
1685
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001686func Test_close_callback_ipv6()
1687 CheckIPv6
1688 call Test_close_callback()
1689endfunc
1690
LemonBoycc766a82022-04-04 15:46:58 +01001691func Test_close_callback_unix()
1692 CheckUnix
1693 call Test_close_callback()
1694 call delete('Xtestsocket')
1695endfunc
1696
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001697function Ch_test_close_partial(port)
LemonBoycc766a82022-04-04 15:46:58 +01001698 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001699 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001700 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001701 return
1702 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001703 let g:Ch_d = {}
1704 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001705 let self.close_ret = 'closed'
1706 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001707 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001708
1709 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001710 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001711 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001712endfunc
1713
1714func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001715 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001716endfunc
1717
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001718func Test_close_partial_ipv6()
1719 CheckIPv6
1720 call Test_close_partial()
1721endfunc
1722
LemonBoycc766a82022-04-04 15:46:58 +01001723func Test_close_partial_unix()
1724 CheckUnix
1725 call Test_close_partial()
1726 call delete('Xtestsocket')
1727endfunc
1728
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001729func Test_job_start_fails()
1730 " this was leaking memory
1731 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001732 call assert_fails('call job_start($x)', 'E474:')
1733 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001734 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1735 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1736 call assert_fails('call job_start("ls", [])', 'E715:')
1737 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1738 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1739 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001740 call assert_fails("call job_start('ls', {'callback' : -1})", 'E921:')
1741 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E921:')
1742 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E921:')
1743 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E921:')
1744 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E921:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001745 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1746 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1747 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001748 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001749 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1750 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1751 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1752 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1753 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1754 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1755 call assert_fails("call job_start('ls',
1756 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1757 call assert_fails("call job_start('ls',
1758 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1759 call assert_fails("call job_start('ls',
1760 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1761
1762 call assert_fails("call job_start('ls',
1763 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1764 call assert_fails("call job_start('ls',
1765 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1766 call assert_fails("call job_start('ls',
1767 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1768
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001769 let cmd = has('win32') ? "cmd /c dir" : "ls"
1770
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001771 set nomodifiable
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001772 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001773 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001774 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001775 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1776 set modifiable
1777
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001778 call assert_fails("call job_start(cmd, {'in_io' : 'buffer'})", 'E915:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001779
1780 edit! XXX
1781 let bnum = bufnr()
1782 enew
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001783 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001784 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1785
1786 " Empty job tests
1787 " This was crashing on MS-Windows.
1788 call assert_fails('let job = job_start([""])', 'E474:')
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:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001792 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001793 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001794 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001795endfunc
1796
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001797func Test_job_stop_immediately()
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001798 " With valgrind this causes spurious leak reports
Bram Moolenaarcf801d42022-06-21 18:34:42 +01001799 CheckNotValgrind
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001800
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001801 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001802 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001803 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001804 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001805 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001806 call job_stop(g:job, 'kill')
1807 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001808 endtry
1809endfunc
1810
Bram Moolenaar271906b2021-08-28 12:30:12 +02001811func Test_null_job_eval()
1812 call assert_fails('eval test_null_job()->eval()', 'E121:')
1813endfunc
1814
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001815" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001816func Test_partial_in_channel_cycle()
1817 let d = {}
1818 let d.a = function('string', [d])
1819 try
1820 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001821 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001822 catch
1823 call assert_exception('E901:')
1824 endtry
1825 unlet d
1826endfunc
1827
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001828func Test_using_freed_memory()
1829 let g:a = job_start(['ls'])
1830 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001831 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001832endfunc
1833
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001834func Test_collapse_buffers()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001835 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001836 CheckExecutable cat
1837
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001838 sp test_channel.vim
1839 let g:linecount = line('$')
1840 close
1841 split testout
1842 1,$delete
1843 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001844 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001845 bwipe!
1846endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001847
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001848func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001849 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001850 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001851
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001852 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001853 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001854 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001855 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1856 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001857
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001858 bdel test_buffer
1859 call assert_equal([], getbufline(bufnr, 1, '$'))
1860
1861 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001862 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001863 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1864 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1865
1866 bwipe! test_buffer
1867endfunc
1868
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001869func Test_cmd_parsing()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02001870 CheckUnix
1871
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001872 call assert_false(filereadable("file with space"))
1873 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001874 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001875 call delete("file with space")
1876
1877 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001878 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001879 call delete("file with space")
1880endfunc
1881
Bram Moolenaar82117982016-08-27 19:21:48 +02001882func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001883 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001884
1885 " Test lines from the job containing NUL are stored correctly in a buffer.
1886 new
1887 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1888 w! Xtestread
1889 bwipe!
1890 split testout
1891 1,$delete
1892 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1893 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001894 call assert_equal("asdf\nasdf", getline(1))
1895 call assert_equal("xxx\n", getline(2))
1896 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001897
1898 call delete('Xtestread')
1899 bwipe!
1900
1901 " Test lines from a buffer with NUL bytes are written correctly to the job.
1902 new mybuffer
1903 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1904 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 +02001905 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001906 bwipe!
1907 split Xtestwrite
1908 call assert_equal("asdf\nasdf", getline(1))
1909 call assert_equal("xxx\n", getline(2))
1910 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001911 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001912
1913 call delete('Xtestwrite')
1914 bwipe!
1915endfunc
1916
Bram Moolenaarec68a992016-10-03 21:37:41 +02001917func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001918 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001919 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001920 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001921 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001922 unlet g:linecount
1923endfunc
1924
1925func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001926 func s:close_cb(ch)
1927 while ch_status(a:ch) == 'buffered'
1928 let g:out .= ch_read(a:ch)
1929 endwhile
1930 endfunc
1931
1932 let g:out = ''
1933 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1934 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001935 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001936 call WaitForAssert({-> assert_equal('123', g:out)})
1937 unlet g:out
1938 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001939endfunc
1940
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001941func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001942 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001943 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001944 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001945 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001946 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001947 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001948endfunc
1949
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001950func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001951 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001952
1953 " Check that backslash in $COMSPEC is handled properly.
1954 let g:echostr = ''
1955 let cmd = $COMSPEC . ' /c echo 123'
1956 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1957 let info = job_info(job)
1958 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1959
1960 call WaitForAssert({-> assert_equal("123", g:echostr)})
1961 unlet g:echostr
1962endfunc
1963
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001964func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001965 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001966 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001967 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001968 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001969 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001970 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001971 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
K.Takatacd9fa252022-09-20 11:04:47 +01001972 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
1973 if WaitForAssert({-> assert_equal("bar", g:envstr)}, 500) != 0
1974 call add(v:errors, "Job status: " .. string(job->job_info()))
1975 endif
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001976 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001977endfunc
1978
1979func Test_cwd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001980 let g:test_is_flaky = 1
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001981 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001982 if has('win32')
1983 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001984 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001985 else
1986 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001987 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001988 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001989 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001990 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001991 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaarec1238b2022-09-25 11:21:04 +01001992 " There may be a trailing slash or not, ignore it
Bram Moolenaar24820692017-12-02 16:38:12 +01001993 let expect = substitute(expect, '[/\\]$', '', '')
1994 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
Bram Moolenaarec1238b2022-09-25 11:21:04 +01001995 " on CI there can be /private prefix or not, ignore it
1996 if $CI != '' && stridx(expect, '/private/') == 0
1997 let expect = expect[8:]
1998 endif
Bram Moolenaar24820692017-12-02 16:38:12 +01001999 if $CI != '' && stridx(g:envstr, '/private/') == 0
2000 let g:envstr = g:envstr[8:]
2001 endif
2002 call assert_equal(expect, g:envstr)
2003 finally
2004 call job_stop(job)
2005 unlet g:envstr
2006 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02002007endfunc
2008
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002009function Ch_test_close_lambda(port)
LemonBoycc766a82022-04-04 15:46:58 +01002010 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002011 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01002012 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002013 return
2014 endif
2015 let g:Ch_close_ret = ''
2016 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002017 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002018
2019 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002020 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002021endfunc
2022
2023func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002024 call s:run_server('Ch_test_close_lambda')
2025endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002026
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02002027func Test_close_lambda_ipv6()
2028 CheckIPv6
2029 call Test_close_lambda()
2030endfunc
2031
LemonBoycc766a82022-04-04 15:46:58 +01002032func Test_close_lambda_unix()
2033 CheckUnix
2034 call Test_close_lambda()
2035 call delete('Xtestsocket')
2036endfunc
2037
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002038func s:test_list_args(cmd, out, remove_lf)
2039 try
2040 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01002041 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 +01002042 try
2043 call WaitFor('"" != g:out')
2044 catch
2045 call add(v:errors, "Job status: " .. string(job->job_info()))
2046 throw v:exception
2047 endtry
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002048 if has('win32')
2049 let g:out = substitute(g:out, '\r', '', 'g')
2050 endif
2051 if a:remove_lf
2052 let g:out = substitute(g:out, '\n$', '', 'g')
2053 endif
2054 call assert_equal(a:out, g:out)
2055 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01002056 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002057 unlet g:out
2058 endtry
2059endfunc
2060
2061func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002062 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
2063 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 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"world'')', "hello\"world", 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("h\"ello worl\"d")', 'h"ello worl"d', 0)
2072 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
2073 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
2074 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
2075
2076 " tests which not contain spaces in the argument
2077 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
2078 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
2079 call s:test_list_args('print(''hello"world'')', "hello\"world", 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\tworld")', "hello\tworld", 1)
2086endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02002087
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002088func Test_keep_pty_open()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002089 CheckUnix
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002090
Bram Moolenaare2956092019-01-25 21:01:17 +01002091 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
2092 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002093 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
2094 call assert_inrange(200, 1000, elapsed)
2095 call job_stop(job)
2096endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01002097
2098func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002099 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01002100 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01002101
2102 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002103 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002104 endfunc
2105
2106 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002107 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002108 call Resume()
2109 endfunc
2110
2111 func TimerCb(timer)
2112 if has('win32')
2113 let cmd = ['cmd', '/c', 'echo.']
2114 else
2115 let cmd = ['echo']
2116 endif
2117 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
2118 call substitute(repeat('a', 100000), '.', '', 'g')
2119 endfunc
2120
2121 " We should be interrupted before 'updatetime' elapsed.
2122 let g:val = 0
2123 call timer_start(1, 'TimerCb')
2124 let elapsed = Standby(&ut)
2125 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002126
2127 " Wait for both OutCb() and ExitCb() to have been called before deleting
2128 " them.
2129 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002130 call job_stop(g:job)
2131
2132 delfunc OutCb
2133 delfunc ExitCb
2134 delfunc TimerCb
2135 unlet! g:val
2136 unlet! g:job
2137endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002138
2139func Test_raw_large_data()
2140 try
2141 let g:out = ''
2142 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002143 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2144 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002145
Bram Moolenaare2956092019-01-25 21:01:17 +01002146 let outlen = 79999
2147 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002148 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002149 call WaitFor({-> len(g:out) >= outlen}, 10000)
2150 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002151 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2152 finally
2153 call job_stop(job)
2154 unlet g:out
2155 endtry
2156endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002157
Bram Moolenaar65240682019-02-10 22:23:26 +01002158func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002159 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002160
2161 try
2162 let job = job_start(s:python . " test_channel_pipe.py busy",
2163 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2164 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2165 finally
2166 call job_stop(job)
2167 endtry
2168endfunc
2169
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002170func Test_job_exitval_and_termsig()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002171 CheckUnix
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002172
2173 " Terminate job normally
2174 let cmd = ['echo']
2175 let job = job_start(cmd)
2176 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2177 let info = job_info(job)
2178 call assert_equal(0, info.exitval)
2179 call assert_equal("", info.termsig)
2180
2181 " Terminate job by signal
2182 let cmd = ['sleep', '10']
2183 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002184 " 10m usually works but 50m is needed when running Valgrind
2185 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002186 call job_stop(job)
2187 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2188 let info = job_info(job)
2189 call assert_equal(-1, info.exitval)
2190 call assert_equal("term", info.termsig)
2191endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002192
2193func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002194 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002195
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002196 call writefile(['test'], 'Xtestin', 'D')
Bram Moolenaar59386482019-02-10 22:43:46 +01002197 let in_opts = [{},
2198 \ {'in_io': 'null'},
2199 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2200 let out_opts = [{},
2201 \ {'out_io': 'null'},
2202 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2203 let err_opts = [{},
2204 \ {'err_io': 'null'},
2205 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2206 \ {'err_io': 'out'}]
2207 let opts = []
2208
2209 for in_opt in in_opts
2210 let x = copy(in_opt)
2211 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002212 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002213 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002214 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002215 let opts += [extend({'pty': 1}, x)]
2216 endfor
2217 endfor
2218 endfor
2219
2220 for opt in opts
2221 let job = job_start('echo', opt)
2222 let info = job_info(job)
2223 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2224 \ get(opt, 'in_io', 'tty'),
2225 \ get(opt, 'out_io', 'tty'),
2226 \ get(opt, 'err_io', 'tty'))
2227
2228 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2229 call assert_notequal('', info.tty_in, msg)
2230 else
2231 call assert_equal('', info.tty_in, msg)
2232 endif
2233 call assert_equal(info.tty_in, info.tty_out, msg)
2234
2235 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2236 endfor
2237
Bram Moolenaar59386482019-02-10 22:43:46 +01002238 call delete('Xtestout')
2239 call delete('Xtesterr')
2240endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002241
2242" Do this last, it stops any channel log.
2243func Test_zz_nl_err_to_out_pipe()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002244
Bram Moolenaar570497a2019-08-22 22:55:13 +02002245 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002246 call ch_log('Test_zz_nl_err_to_out_pipe()')
2247 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2248 call assert_equal("run", job_status(job))
2249 try
2250 let handle = job_getchannel(job)
2251 call ch_sendraw(handle, "echo something\n")
2252 call assert_equal("something", ch_readraw(handle))
2253
2254 call ch_sendraw(handle, "echoerr wrong\n")
2255 call assert_equal("wrong", ch_readraw(handle))
2256 finally
2257 call job_stop(job)
2258 call ch_logfile('')
2259 let loglines = readfile('Xlog')
2260 call assert_true(len(loglines) > 10)
2261 let found_test = 0
2262 let found_send = 0
2263 let found_recv = 0
2264 let found_stop = 0
2265 for l in loglines
2266 if l =~ 'Test_zz_nl_err_to_out_pipe'
2267 let found_test = 1
2268 endif
2269 if l =~ 'SEND on.*echo something'
2270 let found_send = 1
2271 endif
2272 if l =~ 'RECV on.*something'
2273 let found_recv = 1
2274 endif
2275 if l =~ 'Stopping job with'
2276 let found_stop = 1
2277 endif
2278 endfor
2279 call assert_equal(1, found_test)
2280 call assert_equal(1, found_send)
2281 call assert_equal(1, found_recv)
2282 call assert_equal(1, found_stop)
2283 " On MS-Windows need to sleep for a moment to be able to delete the file.
2284 sleep 10m
2285 call delete('Xlog')
2286 endtry
2287endfunc
2288
2289" Do this last, it stops any channel log.
2290func Test_zz_ch_log()
2291 call ch_logfile('Xlog', 'w')
2292 call ch_log('hello there')
2293 call ch_log('%s%s')
2294 call ch_logfile('')
2295 let text = readfile('Xlog')
Bram Moolenaar4f501172022-12-01 11:02:23 +00002296 call assert_match("start log session", text[0])
2297 call assert_match("ch_log(): hello there", text[1])
Bram Moolenaarf386f082019-07-29 23:03:03 +02002298 call assert_match("%s%s", text[2])
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002299 call mkdir("Xchlogdir1", 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002300 call assert_fails("call ch_logfile('Xchlogdir1')", 'E484:')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002301
Bram Moolenaarf386f082019-07-29 23:03:03 +02002302 call delete('Xlog')
2303endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002304
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002305func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002306 if has('win32')
2307 let cmd = 'cmd /c pause'
2308 else
2309 let cmd = 'grep foo'
2310 endif
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002311
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002312 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002313 sleep 50m " give the job time to start
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002314 call job_stop(g:job)
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002315 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2316
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002317 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002318 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002319 call job_stop(g:job, 'term')
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002320 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2321
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002322 let g:job = job_start(cmd, {})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002323 sleep 50m
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002324 call job_stop(g:job, 'kill')
2325 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002326endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002327
2328func Test_issue_5485()
2329 let $VAR1 = 'global'
2330 let g:Ch_reply = ""
2331 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2332 let g:Ch_job = l:job
2333 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2334 unlet $VAR1
2335endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002336
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002337func Test_job_trailing_space_unix()
2338 CheckUnix
2339 CheckExecutable cat
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002340
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002341 let job = job_start("cat ", #{in_io: 'null'})
2342 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2343 call assert_equal(0, job_info(job).exitval)
LemonBoycc766a82022-04-04 15:46:58 +01002344
2345 call delete('Xtestsocket')
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002346endfunc
2347
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002348func Test_ch_getbufnr()
2349 let ch = test_null_channel()
2350 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2351 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2352 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2353 call assert_equal(-1, ch_getbufnr(ch, ''))
2354endfunc
2355
2356" Test for unsupported options passed to ch_status()
2357func Test_invalid_job_chan_options()
2358 let ch = test_null_channel()
2359 let invalid_opts = [
2360 \ {'in_io' : 'null'},
2361 \ {'out_io' : 'null'},
2362 \ {'err_io' : 'null'},
2363 \ {'mode' : 'json'},
2364 \ {'out_mode' : 'json'},
2365 \ {'err_mode' : 'json'},
2366 \ {'noblock' : 1},
2367 \ {'in_name' : '/a/b'},
2368 \ {'pty' : 1},
2369 \ {'in_buf' : 1},
2370 \ {'out_buf' : 1},
2371 \ {'err_buf' : 1},
2372 \ {'out_modifiable' : 1},
2373 \ {'err_modifiable' : 1},
2374 \ {'out_msg' : 1},
2375 \ {'err_msg' : 1},
2376 \ {'in_top' : 1},
2377 \ {'in_bot' : 1},
2378 \ {'channel' : ch},
2379 \ {'callback' : ''},
2380 \ {'out_cb' : ''},
2381 \ {'err_cb' : ''},
2382 \ {'close_cb' : ''},
2383 \ {'exit_cb' : ''},
2384 \ {'term_opencmd' : ''},
2385 \ {'eof_chars' : ''},
2386 \ {'term_rows' : 10},
2387 \ {'term_cols' : 10},
2388 \ {'vertical' : 0},
2389 \ {'curwin' : 1},
2390 \ {'bufnr' : 1},
2391 \ {'hidden' : 0},
2392 \ {'norestore' : 0},
2393 \ {'term_kill' : 'kill'},
2394 \ {'tty_type' : ''},
2395 \ {'term_highlight' : ''},
2396 \ {'env' : {}},
2397 \ {'cwd' : ''},
2398 \ {'timeout' : 0},
2399 \ {'out_timeout' : 0},
2400 \ {'err_timeout' : 0},
2401 \ {'id' : 0},
2402 \ {'stoponexit' : ''},
2403 \ {'block_write' : 1}
2404 \ ]
2405 if has('gui')
2406 call add(invalid_opts, {'ansi_colors' : []})
2407 endif
2408
2409 for opt in invalid_opts
2410 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2411 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002412 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002413endfunc
2414
2415" Test for passing the command and the arguments as List on MS-Windows
2416func Test_job_with_list_args()
2417 CheckMSWindows
2418
2419 enew!
2420 let bnum = bufnr()
2421 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2422 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2423 call assert_equal('Hello World', getline(1))
2424 %bw!
2425endfunc
2426
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002427func ExitCb_cb_with_input(job, status)
2428 call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx')
2429 call assert_equal('default', Screenline(&lines))
2430 let g:wait_exit_cb = 0
2431endfunc
2432
2433func Test_cb_with_input()
2434 let g:wait_exit_cb = 1
2435
Bram Moolenaarf637bce2020-11-23 18:14:56 +01002436 if has('win32')
2437 let cmd = 'cmd /c echo "Vim''s test"'
2438 else
2439 let cmd = 'echo "Vim''s test"'
2440 endif
2441
2442 let job = job_start(cmd, {'out_cb': 'ExitCb_cb_with_input'})
2443 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002444 call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)})
2445
2446 unlet g:wait_exit_cb
2447endfunc
2448
Bram Moolenaar57942232021-08-04 20:54:55 +02002449function s:HandleBufEnter() abort
2450 let queue = []
2451 let job = job_start(['date'], {'callback': { j, d -> add(queue, d) }})
2452 while empty(queue)
2453 sleep! 10m
2454 endwhile
2455endfunction
2456
2457func Test_parse_messages_in_autocmd()
2458 CheckUnix
2459
2460 " Check that in the BufEnter autocommand events are being handled
2461 augroup bufenterjob
2462 autocmd!
2463 autocmd BufEnter Xbufenterjob call s:HandleBufEnter()
2464 augroup END
2465
2466 only
2467 split Xbufenterjob
2468 wincmd p
2469 redraw
2470
2471 close
2472 augroup bufenterjob
2473 autocmd!
2474 augroup END
2475endfunc
2476
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01002477func Test_job_start_with_invalid_argument()
2478 call assert_fails('call job_start([0zff])', 'E976:')
2479endfunc
2480
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002481" Test for the 'lsp' channel mode
2482func LspCb(chan, msg)
2483 call add(g:lspNotif, a:msg)
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002484 if a:msg->has_key('method')
2485 " Requests received from the LSP server
2486 if a:msg['method'] == 'server-req-in-middle'
2487 \ && a:msg['params']['text'] == 'server-req'
2488 call ch_sendexpr(a:chan, #{method: 'server-req-in-middle-resp',
2489 \ id: a:msg['id'], params: #{text: 'client-resp'}})
2490 endif
2491 endif
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002492endfunc
2493
2494func LspOtCb(chan, msg)
2495 call add(g:lspOtMsgs, a:msg)
2496endfunc
2497
2498func LspTests(port)
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +01002499 " call ch_logfile('Xlspclient.log', 'w')
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002500 let ch = ch_open(s:localhost .. a:port, #{mode: 'lsp', callback: 'LspCb'})
2501 if ch_status(ch) == "fail"
2502 call assert_report("Can't open the lsp channel")
2503 return
2504 endif
2505
2506 " check for channel information
2507 let info = ch_info(ch)
2508 call assert_equal('LSP', info.sock_mode)
2509
2510 " Evaluate an expression
2511 let resp = ch_evalexpr(ch, #{method: 'simple-rpc', params: [10, 20]})
2512 call assert_false(empty(resp))
2513 call assert_equal(#{id: 1, jsonrpc: '2.0', result: 'simple-rpc'}, resp)
2514
2515 " Evaluate an expression. While waiting for the response, a notification
2516 " message is delivered.
2517 let g:lspNotif = []
2518 let resp = ch_evalexpr(ch, #{method: 'rpc-with-notif', params: {'v': 10}})
2519 call assert_false(empty(resp))
2520 call assert_equal(#{id: 2, jsonrpc: '2.0', result: 'rpc-with-notif-resp'},
2521 \ resp)
2522 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-with-notif-notif'}],
2523 \ g:lspNotif)
2524
2525 " Wrong payload notification test
2526 let g:lspNotif = []
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002527 let r = ch_sendexpr(ch, #{method: 'wrong-payload', params: {}})
2528 call assert_equal({}, r)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002529 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002530 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002531 call assert_equal([#{jsonrpc: '2.0', result: 'wrong-payload'}], g:lspNotif)
2532
2533 " Test for receiving a response with incorrect 'id' and additional
2534 " notification messages while evaluating an expression.
2535 let g:lspNotif = []
2536 let resp = ch_evalexpr(ch, #{method: 'rpc-resp-incorrect-id',
2537 \ params: {'a': [1, 2]}})
2538 call assert_false(empty(resp))
2539 call assert_equal(#{id: 4, jsonrpc: '2.0',
2540 \ result: 'rpc-resp-incorrect-id-4'}, resp)
2541 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-1'},
2542 \ #{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-2'},
2543 \ #{jsonrpc: '2.0', id: 1, result: 'rpc-resp-incorrect-id-3'}],
2544 \ g:lspNotif)
2545
2546 " simple notification test
2547 let g:lspNotif = []
2548 call ch_sendexpr(ch, #{method: 'simple-notif', params: [#{a: 10, b: []}]})
2549 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002550 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002551 call assert_equal([#{jsonrpc: '2.0', result: 'simple-notif'}], g:lspNotif)
2552
2553 " multiple notifications test
2554 let g:lspNotif = []
2555 call ch_sendexpr(ch, #{method: 'multi-notif', params: [#{a: {}, b: {}}]})
2556 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002557 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002558 call assert_equal([#{jsonrpc: '2.0', result: 'multi-notif1'},
2559 \ #{jsonrpc: '2.0', result: 'multi-notif2'}], g:lspNotif)
2560
2561 " Test for sending a message with an identifier.
2562 let g:lspNotif = []
2563 call ch_sendexpr(ch, #{method: 'msg-with-id', id: 93, params: #{s: 'str'}})
2564 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002565 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002566 call assert_equal([#{jsonrpc: '2.0', id: 93, result: 'msg-with-id'}],
2567 \ g:lspNotif)
2568
2569 " Test for setting the 'id' value in a request message
2570 let resp = ch_evalexpr(ch, #{method: 'ping', id: 1, params: {}})
2571 call assert_equal(#{id: 8, jsonrpc: '2.0', result: 'alive'}, resp)
2572
2573 " Test for using a one time callback function to process a response
2574 let g:lspOtMsgs = []
dundargocc57b5bc2022-11-02 13:30:51 +00002575 let r = ch_sendexpr(ch, #{method: 'msg-specific-cb', params: {}},
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002576 \ #{callback: 'LspOtCb'})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002577 call assert_equal(9, r.id)
2578 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
dundargocc57b5bc2022-11-02 13:30:51 +00002579 call assert_equal([#{id: 9, jsonrpc: '2.0', result: 'msg-specific-cb'}],
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002580 \ g:lspOtMsgs)
2581
2582 " Test for generating a request message from the other end (server)
2583 let g:lspNotif = []
2584 call ch_sendexpr(ch, #{method: 'server-req', params: #{}})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002585 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002586 call assert_equal([{'id': 201, 'jsonrpc': '2.0',
2587 \ 'result': {'method': 'checkhealth', 'params': {'a': 20}}}],
2588 \ g:lspNotif)
2589
2590 " Test for sending a message without an id
2591 let g:lspNotif = []
2592 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'msg-without-id'}})
2593 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002594 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002595 call assert_equal([#{jsonrpc: '2.0', result:
2596 \ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'msg-without-id'}}}],
2597 \ g:lspNotif)
2598
2599 " Test for sending a notification message with an id
2600 let g:lspNotif = []
2601 call ch_sendexpr(ch, #{method: 'echo', id: 110, params: #{s: 'msg-with-id'}})
2602 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002603 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002604 call assert_equal([#{jsonrpc: '2.0', result:
2605 \ #{method: 'echo', jsonrpc: '2.0', id: 110,
2606 \ params: #{s: 'msg-with-id'}}}], g:lspNotif)
2607
2608 " Test for processing the extra fields in the HTTP header
2609 let resp = ch_evalexpr(ch, #{method: 'extra-hdr-fields', params: {}})
2610 call assert_equal({'id': 14, 'jsonrpc': '2.0', 'result': 'extra-hdr-fields'},
2611 \ resp)
2612
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +01002613 " Test for processing delayed payload
2614 let resp = ch_evalexpr(ch, #{method: 'delayed-payload', params: {}})
2615 call assert_equal({'id': 15, 'jsonrpc': '2.0', 'result': 'delayed-payload'},
2616 \ resp)
2617
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002618 " Test for processing a HTTP header without the Content-Length field
2619 let resp = ch_evalexpr(ch, #{method: 'hdr-without-len', params: {}},
2620 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002621 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002622 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002623 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002624
2625 " Test for processing a HTTP header with wrong length
2626 let resp = ch_evalexpr(ch, #{method: 'hdr-with-wrong-len', params: {}},
2627 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002628 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002629 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002630 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002631
2632 " Test for processing a HTTP header with negative length
2633 let resp = ch_evalexpr(ch, #{method: 'hdr-with-negative-len', params: {}},
2634 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002635 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002636 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002637 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002638
2639 " Test for an empty header
2640 let resp = ch_evalexpr(ch, #{method: 'empty-header', params: {}},
2641 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002642 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002643 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002644 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002645
2646 " Test for an empty payload
2647 let resp = ch_evalexpr(ch, #{method: 'empty-payload', params: {}},
2648 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002649 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002650 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002651 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002652
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +01002653 " Test for a large payload
2654 let content = repeat('abcdef', 11000)
2655 let resp = ch_evalexpr(ch, #{method: 'large-payload',
2656 \ params: #{text: content}})
2657 call assert_equal(#{jsonrpc: '2.0', id: 26, result:
2658 \ #{method: 'large-payload', jsonrpc: '2.0', id: 26,
2659 \ params: #{text: content}}}, resp)
2660 " send a ping to make sure communication still works
2661 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
2662
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002663 " Test for processing a request message from the server while the client
2664 " is waiting for a response with the same identifier.
2665 let g:lspNotif = []
2666 let resp = ch_evalexpr(ch, #{method: 'server-req-in-middle',
2667 \ params: #{text: 'client-req'}})
2668 call assert_equal(#{jsonrpc: '2.0', id: 28,
2669 \ result: #{text: 'server-resp'}}, resp)
2670 call assert_equal([
2671 \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle',
2672 \ params: #{text: 'server-notif'}},
2673 \ #{id: 28, jsonrpc: '2.0', method: 'server-req-in-middle',
2674 \ params: #{text: 'server-req'}}], g:lspNotif)
2675
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002676 " Test for invoking an unsupported method
2677 let resp = ch_evalexpr(ch, #{method: 'xyz', params: {}}, #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002678 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002679
2680 " Test for sending a message without a callback function. Notification
2681 " message should be dropped but RPC response should not be dropped.
2682 call ch_setoptions(ch, #{callback: ''})
2683 let g:lspNotif = []
2684 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
2685 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002686 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002687 call assert_equal([], g:lspNotif)
2688 " Restore the callback function
2689 call ch_setoptions(ch, #{callback: 'LspCb'})
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002690
2691 " " Test for sending a raw message
2692 " let g:lspNotif = []
2693 " let s = "Content-Length: 62\r\n"
Yegappan Lakshmananc3eddd22023-04-25 14:54:54 +01002694 " let s ..= "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002695 " let s ..= "\r\n"
2696 " let s ..= '{"method":"echo","jsonrpc":"2.0","params":{"m":"raw-message"}}'
2697 " call ch_sendraw(ch, s)
2698 " call ch_evalexpr(ch, #{method: 'ping'})
2699 " call assert_equal([{'jsonrpc': '2.0',
2700 " \ 'result': {'method': 'echo', 'jsonrpc': '2.0',
2701 " \ 'params': {'m': 'raw-message'}}}], g:lspNotif)
2702
2703 " Invalid arguments to ch_evalexpr() and ch_sendexpr()
2704 call assert_fails('call ch_sendexpr(ch, #{method: "cookie", id: "cookie"})',
2705 \ 'E475:')
2706 call assert_fails('call ch_evalexpr(ch, #{method: "ping", id: [{}]})', 'E475:')
2707 call assert_fails('call ch_evalexpr(ch, [1, 2, 3])', 'E1206:')
2708 call assert_fails('call ch_sendexpr(ch, "abc")', 'E1206:')
2709 call assert_fails('call ch_evalexpr(ch, #{method: "ping"}, #{callback: "LspOtCb"})', 'E917:')
2710 " call ch_logfile('', 'w')
2711endfunc
2712
2713func Test_channel_lsp_mode()
2714 call RunServer('test_channel_lsp.py', 'LspTests', [])
2715endfunc
Bram Moolenaar57942232021-08-04 20:54:55 +02002716
Bram Moolenaar8b633132020-03-20 18:20:51 +01002717" vim: shiftwidth=2 sts=2 expandtab