blob: a7a609bd0e3fe49afcbd800feb38cfdb23fcb453 [file] [log] [blame]
Bram Moolenaarf386f082019-07-29 23:03:03 +02001" Test for channel and job functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaarf386f082019-07-29 23:03:03 +02003" When +channel is supported then +job is too, so we don't check for that.
Bram Moolenaar4641a122019-07-29 22:10:23 +02004source check.vim
5CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01006
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02008source screendump.vim
Bram Moolenaardfc33a62020-04-29 22:30:13 +02009source view_util.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +020010
11let s:python = PythonProg()
12if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010013 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020014 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010015endif
16
Bram Moolenaar37175402017-03-18 20:18:45 +010017" Uncomment the next line to see what happens. Output is in
18" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020019" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010020" call ch_logfile('channellog', 'w')
21
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +020022func SetUp()
23 if g:testfunc =~ '_ipv6()$'
24 let s:localhost = '[::1]:'
25 let s:testscript = 'test_channel_6.py'
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"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100527 " Oops, port does exists.
528 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"
541 " Oops, port does exists.
542 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 Moolenaarb69fccf2016-03-06 23:06:25 +0100717 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
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 call delete('Xinput')
730 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200731 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100732endfunc
733
Bram Moolenaare98d1212016-03-08 15:37:41 +0100734func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200735 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100736 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200737 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100738 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200739 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100740 call ch_sendraw(handle, "echo line one\n")
741 call ch_sendraw(handle, "echo line two\n")
742 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200743 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100744 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200745 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100746 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100747 call delete('Xoutput')
748 endtry
749endfunc
750
751func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200752 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100753 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200754 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100755 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200756 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100757 call ch_sendraw(handle, "echoerr line one\n")
758 call ch_sendraw(handle, "echoerr line two\n")
759 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200760 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100761 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200762 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100763 call delete('Xoutput')
764 endtry
765endfunc
766
767func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200768 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100769 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200770 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100771 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200772 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100773 call ch_sendraw(handle, "echoerr line one\n")
774 call ch_sendraw(handle, "echo line two\n")
775 call ch_sendraw(handle, "double this\n")
776 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200777 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100778 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200779 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100780 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100781 call delete('Xoutput')
782 endtry
783endfunc
784
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200785func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200786 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200787endfunc
788
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200789func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200790 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200791 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200792 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100793 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100794 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200795 if a:do_msg
796 let expected[0] = 'Reading from channel output...'
797 else
798 let options['out_msg'] = 0
799 call remove(expected, 0)
800 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100801 else
802 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100803 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100804 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200805 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100806 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200807 if a:nomod
808 let options['out_modifiable'] = 0
809 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100810 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100811 call assert_equal("run", job_status(job))
812 try
813 let handle = job_getchannel(job)
814 call ch_sendraw(handle, "echo line one\n")
815 call ch_sendraw(handle, "echo line two\n")
816 call ch_sendraw(handle, "double this\n")
817 call ch_sendraw(handle, "quit\n")
818 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100819 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200820 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200821 if a:nomod
822 call assert_equal(0, &modifiable)
823 else
824 call assert_equal(1, &modifiable)
825 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200826 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100827 bwipe!
828 finally
829 call job_stop(job)
830 endtry
831endfunc
832
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100833func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200834 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100835endfunc
836
837func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200838 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100839endfunc
840
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200841func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200842 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200843endfunc
844
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200845func Test_pipe_to_buffer_name_nomsg()
846 call Run_test_pipe_to_buffer(1, 0, 1)
847endfunc
848
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200849func Test_close_output_buffer()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100850 let g:test_is_flaky = 1
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200851 enew!
852 let test_lines = ['one', 'two']
853 call setline(1, test_lines)
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200854 let options = {'out_io': 'buffer'}
855 let options['out_name'] = 'buffer-output'
856 let options['out_msg'] = 0
857 split buffer-output
858 let job = job_start(s:python . " test_channel_write.py", options)
859 call assert_equal("run", job_status(job))
860 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200861 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200862 quit!
863 sleep 100m
864 " Make sure the write didn't happen to the wrong buffer.
865 call assert_equal(test_lines, getline(1, line('$')))
866 call assert_equal(-1, bufwinnr('buffer-output'))
867 sbuf buffer-output
868 call assert_notequal(-1, bufwinnr('buffer-output'))
869 sleep 100m
870 close " no more writes
871 bwipe!
872 finally
873 call job_stop(job)
874 endtry
875endfunc
876
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200877func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100878 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200879 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100880 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100881 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200882 if a:do_msg
883 let expected[0] = 'Reading from channel error...'
884 else
885 let options['err_msg'] = 0
886 call remove(expected, 0)
887 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100888 else
889 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100890 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100891 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200892 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100893 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200894 if a:nomod
895 let options['err_modifiable'] = 0
896 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100897 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100898 call assert_equal("run", job_status(job))
899 try
900 let handle = job_getchannel(job)
901 call ch_sendraw(handle, "echoerr line one\n")
902 call ch_sendraw(handle, "echoerr line two\n")
903 call ch_sendraw(handle, "doubleerr this\n")
904 call ch_sendraw(handle, "quit\n")
905 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200906 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200907 if a:nomod
908 call assert_equal(0, &modifiable)
909 else
910 call assert_equal(1, &modifiable)
911 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100912 bwipe!
913 finally
914 call job_stop(job)
915 endtry
916endfunc
917
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100918func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200919 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100920endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100921
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100922func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200923 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200924endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100925
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200926func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200927 call Run_test_pipe_err_to_buffer(1, 1, 1)
928endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100929
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200930func Test_pipe_err_to_buffer_name_nomsg()
931 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100932endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100933
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100934func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100935 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100936 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100937 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200938 let handle = job_getchannel(job)
939 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
940 call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100941 try
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100942 call ch_sendraw(handle, "echo line one\n")
943 call ch_sendraw(handle, "echoerr line two\n")
944 call ch_sendraw(handle, "double this\n")
945 call ch_sendraw(handle, "doubleerr that\n")
946 call ch_sendraw(handle, "quit\n")
947 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200948 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 +0100949 bwipe!
950 finally
951 call job_stop(job)
952 endtry
953endfunc
954
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100955func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100956 sp pipe-input
957 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200958 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100959 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100960 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100961 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100962 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100963 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100964
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100965 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100966 call assert_equal("run", job_status(job))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200967 if has('unix') && !a:use_name
968 call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
969 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100970 try
971 let handle = job_getchannel(job)
972 call assert_equal('one', ch_read(handle))
973 call assert_equal('two', ch_read(handle))
974 call assert_equal('three', ch_read(handle))
975 bwipe!
976 finally
977 call job_stop(job)
978 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100979endfunc
980
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100981func Test_pipe_from_buffer_name()
982 call Run_test_pipe_from_buffer(1)
983endfunc
984
985func Test_pipe_from_buffer_nr()
986 call Run_test_pipe_from_buffer(0)
987endfunc
988
Bram Moolenaar0874a832016-09-01 15:11:51 +0200989func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200990 CheckExecutable sort
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100991 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200992
Bram Moolenaar0874a832016-09-01 15:11:51 +0200993 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
994 if a:use_buffer
995 split sortin
996 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
997 let options.in_io = 'buffer'
998 let options.in_name = 'sortin'
999 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +02001000 if !a:all
1001 let options.in_top = 2
1002 let options.in_bot = 4
1003 endif
Bram Moolenaare2956092019-01-25 21:01:17 +01001004 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +02001005
1006 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +01001007 call assert_equal("run", job_status(job))
1008 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +02001009 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001010 endif
1011
Bram Moolenaare2956092019-01-25 21:01:17 +01001012 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +02001013
Bram Moolenaard8b55492016-09-01 14:35:22 +02001014 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +02001015 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +02001016 call assert_equal('Reading from channel output...', getline(1))
1017 if a:all
1018 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
1019 else
1020 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
1021 endif
1022
Bram Moolenaare2956092019-01-25 21:01:17 +01001023 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +02001024 if a:use_buffer
1025 bwipe! sortin
1026 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +02001027 bwipe! sortout
1028endfunc
1029
1030func Test_pipe_through_sort_all()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001031 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001032endfunc
1033
1034func Test_pipe_through_sort_some()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001035 call Run_pipe_through_sort(0, 1)
1036endfunc
1037
1038func Test_pipe_through_sort_feed()
Bram Moolenaar0874a832016-09-01 15:11:51 +02001039 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +02001040endfunc
1041
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001042func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001043 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001044 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001045 call assert_equal("run", job_status(job))
1046 try
1047 let handle = job_getchannel(job)
1048 call ch_sendraw(handle, "echo line one\n")
1049 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +02001050 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001051 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001052 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
1053 bwipe!
1054 finally
1055 call job_stop(job)
1056 endtry
1057endfunc
1058
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001059func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001060 CheckFunction reltimefloat
1061
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001062 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001063 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001064 call assert_equal("run", job_status(job))
1065 try
1066 let handle = job_getchannel(job)
1067 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1068 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1069 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001070 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001071 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1072 bwipe!
1073 finally
1074 call job_stop(job)
1075 endtry
1076endfunc
1077
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001078" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001079func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001080 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001081 if getline(line('$') - a:offset) == a:line
1082 break
1083 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001084 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001085 endfor
1086endfunc
1087
1088func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001089 " Create two buffers, one to read from and one to write to.
1090 split pipe-output
1091 set buftype=nofile
1092 split pipe-input
1093 set buftype=nofile
1094
1095 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001096 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001097 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1098 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001099 call assert_equal("run", job_status(job))
1100 try
1101 exe "normal Gaecho hello\<CR>"
1102 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001103 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001104 call assert_equal('hello', getline('$'))
1105
1106 exe bufwinnr('pipe-input') . "wincmd w"
1107 exe "normal Gadouble this\<CR>"
1108 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001109 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001110 call assert_equal('this', getline(line('$') - 1))
1111 call assert_equal('AND this', getline('$'))
1112
1113 bwipe!
1114 exe bufwinnr('pipe-input') . "wincmd w"
1115 bwipe!
1116 finally
1117 call job_stop(job)
1118 endtry
1119endfunc
1120
1121func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001122 " Create one buffer to read from and to write to.
1123 split pipe-io
1124 set buftype=nofile
1125
1126 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001127 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001128 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1129 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001130 call assert_equal("run", job_status(job))
1131 try
1132 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001133 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001134 call assert_equal('hello', getline(line('$') - 1))
1135
1136 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001137 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001138 call assert_equal('this', getline(line('$') - 2))
1139 call assert_equal('AND this', getline(line('$') - 1))
1140
1141 bwipe!
1142 finally
1143 call job_stop(job)
1144 endtry
1145endfunc
1146
Bram Moolenaar4641a122019-07-29 22:10:23 +02001147func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001148 CheckScreendump
1149
Bram Moolenaar4641a122019-07-29 22:10:23 +02001150 let lines =<< trim END
1151 new Xscrollbuffer
1152 call setline(1, range(1, 200))
1153 $
1154 redraw
1155 wincmd w
1156 call deletebufline('Xscrollbuffer', 1, '$')
1157 if has('win32')
1158 let cmd = ['cmd', '/c', 'echo sometext']
1159 else
1160 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1161 endif
1162 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1163 END
1164 call writefile(lines, 'XtestBufferScroll')
1165 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001166 call TermWait(buf, 50)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001167 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1168
1169 " clean up
1170 call StopVimInTerminal(buf)
1171 call delete('XtestBufferScroll')
1172endfunc
1173
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001174func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001175 " We cannot check that no I/O works, we only check that the job starts
1176 " properly.
1177 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001178 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001179 call assert_equal("run", job_status(job))
1180 try
1181 call assert_equal('something', ch_read(job))
1182 finally
1183 call job_stop(job)
1184 endtry
1185
1186 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001187 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001188 call assert_equal("run", job_status(job))
1189 try
1190 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1191 finally
1192 call job_stop(job)
1193 endtry
1194
1195 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001196 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001197 call assert_equal("run", job_status(job))
1198 try
1199 call assert_equal('something', ch_read(job))
1200 finally
1201 call job_stop(job)
1202 endtry
1203
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001204 " This causes spurious leak errors with valgrind.
1205 if !RunningWithValgrind()
1206 let job = job_start(s:python . " test_channel_pipe.py something",
1207 \ {'out_io': 'null', 'err_io': 'out'})
1208 call assert_equal("run", job_status(job))
1209 call job_stop(job)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001210
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001211 let job = job_start(s:python . " test_channel_pipe.py something",
1212 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
1213 call assert_equal("run", job_status(job))
1214 call assert_equal('channel fail', string(job_getchannel(job)))
1215 call assert_equal('fail', ch_status(job))
1216 call assert_equal('no process', string(test_null_job()))
1217 call assert_equal('channel fail', string(test_null_channel()))
1218 call job_stop(job)
1219 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001220endfunc
1221
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001222func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001223 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1224 split testout
1225 let job = job_start([s:python, '-c',
1226 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001227 " the job may be done quickly, also accept "dead"
1228 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001229 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001230 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001231 let totlen = 0
1232 for line in getline(1, '$')
1233 call assert_equal('', substitute(line, '^\.*', '', ''))
1234 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001235 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001236 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001237 finally
1238 call job_stop(job)
1239 bwipe!
1240 endtry
1241endfunc
1242
Bram Moolenaarde279892016-03-11 22:19:44 +01001243func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001244 let job = job_start(s:python . " test_channel_pipe.py")
1245 call assert_equal("run", job_status(job))
1246 let handle = job_getchannel(job)
1247 try
1248 call ch_sendraw(handle, "echo something\n")
1249 call assert_equal("something", ch_readraw(handle))
1250 finally
1251 call job_stop(job)
1252 endtry
1253
1254 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1255 call assert_equal("run", job_status(job))
1256 let handle = job_getchannel(job)
1257 try
1258 call ch_sendraw(handle, "echo again\n")
1259 call assert_equal("again", ch_readraw(handle))
1260 finally
1261 call job_stop(job)
1262 endtry
1263endfunc
1264
Bram Moolenaar75f72652016-03-20 22:16:56 +01001265func Test_out_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001266 let g:test_is_flaky = 1
Bram Moolenaar75f72652016-03-20 22:16:56 +01001267 let dict = {'thisis': 'dict: '}
1268 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001269 if type(a:msg) == v:t_string
1270 let g:Ch_outmsg = self.thisis . a:msg
1271 else
1272 let g:Ch_outobj = a:msg
1273 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001274 endfunc
1275 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001276 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001277 endfunc
1278 let job = job_start(s:python . " test_channel_pipe.py",
1279 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001280 \ 'out_mode': 'json',
1281 \ 'err_cb': dict.errHandler,
1282 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001283 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001284 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001285 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001286 let g:Ch_outmsg = ''
1287 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001288 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1289 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001290 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1291 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001292
1293 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001294 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001295 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001296 " For unknown reasons this can be very slow on Mac.
Bram Moolenaardeda6442021-12-17 11:44:33 +00001297 " Increase the timeout on every run.
1298 if g:run_nr == 1
1299 let timeout = 5000
1300 elseif g:run_nr == 2
1301 let timeout = 10000
1302 elseif g:run_nr == 3
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001303 let timeout = 20000
1304 else
Bram Moolenaardeda6442021-12-17 11:44:33 +00001305 let timeout = 40000
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001306 endif
1307 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001308 finally
1309 call job_stop(job)
1310 endtry
1311endfunc
1312
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001313func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001314 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001315 let g:Ch_msg1 = ''
1316 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001317 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001318 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001319 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001320 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001321 let s:counter += 1
1322 endfunc
1323 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001324 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001325 let s:counter += 1
1326 endfunc
1327 let job = job_start(s:python . " test_channel_pipe.py quit now",
1328 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001329 \ 'close_cb': 'CloseHandler'})
1330 " the job may be done quickly, also accept "dead"
1331 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001332 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001333 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1334 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001335 finally
1336 call job_stop(job)
1337 delfunc OutHandler
1338 delfunc CloseHandler
1339 endtry
1340endfunc
1341
Bram Moolenaar437905c2016-04-26 19:01:05 +02001342func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001343 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001344 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001345 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001346 endfunc
1347 let job = job_start(s:python . " test_channel_pipe.py quit now",
1348 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001349 " the job may be done quickly, also accept "dead"
1350 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001351 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001352 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001353 finally
1354 call job_stop(job)
1355 delfunc CloseHandler
1356 endtry
1357endfunc
1358
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001359" Use channel in NL mode but received text does not end in NL.
1360func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001361 let g:Ch_received = ''
1362 func! CloseHandler(chan)
1363 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1364 let g:Ch_received .= ch_read(a:chan)
1365 endwhile
1366 endfunc
1367 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1368 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001369 " the job may be done quickly, also accept "dead"
1370 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001371 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001372 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001373 finally
1374 call job_stop(job)
1375 delfunc CloseHandler
1376 endtry
1377endfunc
1378
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001379func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001380 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001381 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1382 \ 'out_mode': 'json',
1383 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1384 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001385 call assert_equal("run", job_status(job))
1386 try
1387 let g:Ch_outmsg = ''
1388 let g:Ch_errmsg = ''
1389 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1390 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001391 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1392 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001393 finally
1394 call job_stop(job)
1395 endtry
1396endfunc
1397
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001398func Test_close_and_exit_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001399 let g:test_is_flaky = 1
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001400 let g:retdict = {'ret': {}}
1401 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001402 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001403 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001404 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001405 let self.ret['exit_cb'] = job_status(a:job)
1406 endfunc
1407
Bram Moolenaare2956092019-01-25 21:01:17 +01001408 let job = job_start([&shell, &shellcmdflag, 'echo'],
1409 \ {'close_cb': g:retdict.close_cb,
1410 \ 'exit_cb': g:retdict.exit_cb})
1411 " the job may be done quickly, also accept "dead"
1412 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001413 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001414 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001415 call assert_equal('dead', g:retdict.ret['exit_cb'])
1416 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001417endfunc
1418
Bram Moolenaard46ae142016-02-16 13:33:52 +01001419""""""""""
1420
Bram Moolenaar0b146882018-09-06 16:27:24 +02001421function ExitCbWipe(job, status)
1422 exe g:wipe_buf 'bw!'
1423endfunction
1424
1425" This caused a crash, because messages were handled while peeking for a
1426" character.
1427func Test_exit_cb_wipes_buf()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001428 CheckFeature timers
Bram Moolenaar0b146882018-09-06 16:27:24 +02001429 set cursorline lazyredraw
1430 call test_override('redraw_flag', 1)
1431 new
1432 let g:wipe_buf = bufnr('')
1433
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001434 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001435 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001436 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1437 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1438 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1439 call timer_stop(timer)
1440
1441 set nocursorline nolazyredraw
1442 unlet g:wipe_buf
1443 call test_override('ALL', 0)
1444endfunc
1445
1446""""""""""
1447
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001448let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001449func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001450 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001451 unlet s:channelfd
1452endfunc
1453
1454" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001455func Ch_unlet_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001456 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001457 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001458 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001459endfunc
1460
1461func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001462 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001463endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001464
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001465func Test_unlet_handle_ipv6()
1466 CheckIPv6
1467 call Test_unlet_handle()
1468endfunc
1469
Bram Moolenaard46ae142016-02-16 13:33:52 +01001470""""""""""
1471
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001472let g:Ch_unletResponse = ''
1473func Ch_CloseHandler(handle, msg)
1474 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001475 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001476endfunc
1477
1478" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479func Ch_close_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001480 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001481 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001482 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001483endfunc
1484
1485func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001486 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001487endfunc
1488
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001489func Test_close_handle_ipv6()
1490 CheckIPv6
1491 call Test_close_handle()
1492endfunc
1493
1494""""""""""
1495
1496func Ch_open_ipv6(port)
LemonBoycc766a82022-04-04 15:46:58 +01001497 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001498 call assert_notequal('fail', ch_status(handle))
1499endfunc
1500
1501func Test_open_ipv6()
1502 CheckIPv6
1503 call s:run_server('Ch_open_ipv6')
1504endfunc
1505
Bram Moolenaard46ae142016-02-16 13:33:52 +01001506""""""""""
1507
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001508func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001509 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001510 echo ch
1511 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001512 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1513 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001514 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001515 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1516 \ 'E474:')
1517 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1518 \ 'E475:')
1519 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1520 \ 'E475:')
1521 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1522 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001523 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1524 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1525 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001526endfunc
1527
1528func Test_ch_info_fail()
1529 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001530endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001531
1532""""""""""
1533
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001534func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001535 " Wait up to a second for the port to open.
1536 let s:chopt.waittime = 1000
LemonBoycc766a82022-04-04 15:46:58 +01001537 let channel = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001538 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001539 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001540 return
1541 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001542 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001543 call ch_close(channel)
1544endfunc
1545
1546func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001547 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001548 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001549endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001550
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001551func Test_open_delay_ipv6()
1552 CheckIPv6
1553 call Test_open_delay()
1554endfunc
1555
Bram Moolenaarece61b02016-02-20 21:39:05 +01001556"""""""""
1557
1558function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001559 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001560endfunc
1561
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001562function Ch_test_call(port)
LemonBoycc766a82022-04-04 15:46:58 +01001563 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001564 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001565 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001566 return
1567 endif
1568
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001569 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001570 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001571 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001572
1573 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1574 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1575 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001576endfunc
1577
1578func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001579 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001580endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001581
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001582func Test_call_ipv6()
1583 CheckIPv6
1584 call Test_call()
1585endfunc
1586
LemonBoycc766a82022-04-04 15:46:58 +01001587func Test_call_unix()
1588 CheckUnix
1589 call Test_call()
1590 call delete('Xtestsocket')
1591endfunc
1592
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001593"""""""""
1594
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001595let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001596function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001597 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001598endfunc
1599
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001600function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001601 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001602 let g:Ch_exit_job = g:currentJob
1603 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001604endfunc
1605
1606func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001607 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001608
Bram Moolenaarf386f082019-07-29 23:03:03 +02001609 " wait up to a second for the job to exit
1610 for i in range(100)
1611 if g:Ch_job_exit_ret == 'done'
1612 break
1613 endif
1614 sleep 10m
1615 " calling job_status() triggers the callback
1616 call job_status(g:Ch_exit_job)
1617 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001618
Bram Moolenaarf386f082019-07-29 23:03:03 +02001619 call assert_equal('done', g:Ch_job_exit_ret)
1620 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1621 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001622endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001623
Bram Moolenaar97792de2016-10-15 18:36:49 +02001624function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001625 if job_info(a:job).process == g:exit_cb_val.process
1626 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1627 endif
1628 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001629endfunction
1630
1631func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001632 CheckFunction reltimefloat
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001633 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001634
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001635 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001636 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001637 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001638 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001639 let elapsed = reltimefloat(g:exit_cb_val.end)
zeertzjqcdc6a432022-06-19 11:45:46 +01001640 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001641
1642 " case: unreferenced job, using timer
1643 if !has('timers')
1644 return
1645 endif
1646
1647 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1648 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1649 let g:exit_cb_val.process = job_info(g:job).process
1650 unlet g:job
1651 call Standby(1000)
1652 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1653 let elapsed = reltimefloat(g:exit_cb_val.end)
1654 else
1655 let elapsed = 1.0
1656 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001657 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001658endfunc
1659
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001660"""""""""
1661
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001662let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001663function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001664 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001665endfunc
1666
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001667function Ch_test_close_callback(port)
LemonBoycc766a82022-04-04 15:46:58 +01001668 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001669 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001670 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001671 return
1672 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001673 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001674
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001675 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001676 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001677endfunc
1678
1679func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001680 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001681endfunc
1682
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001683func Test_close_callback_ipv6()
1684 CheckIPv6
1685 call Test_close_callback()
1686endfunc
1687
LemonBoycc766a82022-04-04 15:46:58 +01001688func Test_close_callback_unix()
1689 CheckUnix
1690 call Test_close_callback()
1691 call delete('Xtestsocket')
1692endfunc
1693
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001694function Ch_test_close_partial(port)
LemonBoycc766a82022-04-04 15:46:58 +01001695 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001696 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001697 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001698 return
1699 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001700 let g:Ch_d = {}
1701 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001702 let self.close_ret = 'closed'
1703 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001704 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001705
1706 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001707 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001708 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001709endfunc
1710
1711func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001712 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001713endfunc
1714
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001715func Test_close_partial_ipv6()
1716 CheckIPv6
1717 call Test_close_partial()
1718endfunc
1719
LemonBoycc766a82022-04-04 15:46:58 +01001720func Test_close_partial_unix()
1721 CheckUnix
1722 call Test_close_partial()
1723 call delete('Xtestsocket')
1724endfunc
1725
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001726func Test_job_start_fails()
1727 " this was leaking memory
1728 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001729 call assert_fails('call job_start($x)', 'E474:')
1730 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001731 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1732 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1733 call assert_fails('call job_start("ls", [])', 'E715:')
1734 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1735 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1736 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001737 call assert_fails("call job_start('ls', {'callback' : -1})", 'E921:')
1738 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E921:')
1739 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E921:')
1740 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E921:')
1741 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E921:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001742 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1743 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1744 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001745 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001746 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1747 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1748 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1749 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1750 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1751 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1752 call assert_fails("call job_start('ls',
1753 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1754 call assert_fails("call job_start('ls',
1755 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1756 call assert_fails("call job_start('ls',
1757 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1758
1759 call assert_fails("call job_start('ls',
1760 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1761 call assert_fails("call job_start('ls',
1762 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1763 call assert_fails("call job_start('ls',
1764 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1765
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001766 let cmd = has('win32') ? "cmd /c dir" : "ls"
1767
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001768 set nomodifiable
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001769 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001770 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001771 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001772 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1773 set modifiable
1774
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001775 call assert_fails("call job_start(cmd, {'in_io' : 'buffer'})", 'E915:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001776
1777 edit! XXX
1778 let bnum = bufnr()
1779 enew
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001780 call assert_fails("call job_start(cmd,
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001781 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1782
1783 " Empty job tests
1784 " This was crashing on MS-Windows.
1785 call assert_fails('let job = job_start([""])', 'E474:')
1786 call assert_fails('let job = job_start([" "])', 'E474:')
1787 call assert_fails('let job = job_start("")', 'E474:')
1788 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001789 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001790 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001791 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001792endfunc
1793
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001794func Test_job_stop_immediately()
Bram Moolenaar37bb3b12022-06-21 17:40:47 +01001795 " With valgrind this causes spurious leak reports
1796 if RunningWithValgrind()
1797 return
1798 endif
1799
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001800 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001801 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001802 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001803 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001804 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001805 call job_stop(g:job, 'kill')
1806 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001807 endtry
1808endfunc
1809
Bram Moolenaar271906b2021-08-28 12:30:12 +02001810func Test_null_job_eval()
1811 call assert_fails('eval test_null_job()->eval()', 'E121:')
1812endfunc
1813
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001814" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001815func Test_partial_in_channel_cycle()
1816 let d = {}
1817 let d.a = function('string', [d])
1818 try
1819 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001820 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001821 catch
1822 call assert_exception('E901:')
1823 endtry
1824 unlet d
1825endfunc
1826
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001827func Test_using_freed_memory()
1828 let g:a = job_start(['ls'])
1829 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001830 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001831endfunc
1832
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001833func Test_collapse_buffers()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001834 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001835 CheckExecutable cat
1836
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001837 sp test_channel.vim
1838 let g:linecount = line('$')
1839 close
1840 split testout
1841 1,$delete
1842 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001843 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001844 bwipe!
1845endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001846
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001847func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001848 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001849 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001850
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001851 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001852 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001853 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001854 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1855 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001856
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001857 bdel test_buffer
1858 call assert_equal([], getbufline(bufnr, 1, '$'))
1859
1860 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001861 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001862 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1863 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1864
1865 bwipe! test_buffer
1866endfunc
1867
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001868func Test_cmd_parsing()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02001869 CheckUnix
1870
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001871 call assert_false(filereadable("file with space"))
1872 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001873 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001874 call delete("file with space")
1875
1876 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001877 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001878 call delete("file with space")
1879endfunc
1880
Bram Moolenaar82117982016-08-27 19:21:48 +02001881func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001882 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001883
1884 " Test lines from the job containing NUL are stored correctly in a buffer.
1885 new
1886 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1887 w! Xtestread
1888 bwipe!
1889 split testout
1890 1,$delete
1891 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1892 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001893 call assert_equal("asdf\nasdf", getline(1))
1894 call assert_equal("xxx\n", getline(2))
1895 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001896
1897 call delete('Xtestread')
1898 bwipe!
1899
1900 " Test lines from a buffer with NUL bytes are written correctly to the job.
1901 new mybuffer
1902 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1903 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 +02001904 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001905 bwipe!
1906 split Xtestwrite
1907 call assert_equal("asdf\nasdf", getline(1))
1908 call assert_equal("xxx\n", getline(2))
1909 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001910 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001911
1912 call delete('Xtestwrite')
1913 bwipe!
1914endfunc
1915
Bram Moolenaarec68a992016-10-03 21:37:41 +02001916func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001917 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001918 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001919 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001920 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001921 unlet g:linecount
1922endfunc
1923
1924func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001925 func s:close_cb(ch)
1926 while ch_status(a:ch) == 'buffered'
1927 let g:out .= ch_read(a:ch)
1928 endwhile
1929 endfunc
1930
1931 let g:out = ''
1932 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1933 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001934 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001935 call WaitForAssert({-> assert_equal('123', g:out)})
1936 unlet g:out
1937 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001938endfunc
1939
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001940func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001941 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001942 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001943 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001944 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001945 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001946 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001947endfunc
1948
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001949func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001950 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001951
1952 " Check that backslash in $COMSPEC is handled properly.
1953 let g:echostr = ''
1954 let cmd = $COMSPEC . ' /c echo 123'
1955 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1956 let info = job_info(job)
1957 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1958
1959 call WaitForAssert({-> assert_equal("123", g:echostr)})
1960 unlet g:echostr
1961endfunc
1962
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001963func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001964 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001965 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001966 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001967 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001968 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001969 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001970 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1971 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001972 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001973 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001974endfunc
1975
1976func Test_cwd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001977 let g:test_is_flaky = 1
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001978 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001979 if has('win32')
1980 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001981 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001982 else
1983 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001984 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001985 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001986 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001987 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001988 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001989 let expect = substitute(expect, '[/\\]$', '', '')
1990 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1991 if $CI != '' && stridx(g:envstr, '/private/') == 0
1992 let g:envstr = g:envstr[8:]
1993 endif
1994 call assert_equal(expect, g:envstr)
1995 finally
1996 call job_stop(job)
1997 unlet g:envstr
1998 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001999endfunc
2000
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002001function Ch_test_close_lambda(port)
LemonBoycc766a82022-04-04 15:46:58 +01002002 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002003 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01002004 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002005 return
2006 endif
2007 let g:Ch_close_ret = ''
2008 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002009 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002010
2011 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002012 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002013endfunc
2014
2015func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002016 call s:run_server('Ch_test_close_lambda')
2017endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002018
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02002019func Test_close_lambda_ipv6()
2020 CheckIPv6
2021 call Test_close_lambda()
2022endfunc
2023
LemonBoycc766a82022-04-04 15:46:58 +01002024func Test_close_lambda_unix()
2025 CheckUnix
2026 call Test_close_lambda()
2027 call delete('Xtestsocket')
2028endfunc
2029
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002030func s:test_list_args(cmd, out, remove_lf)
2031 try
2032 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01002033 let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002034 call WaitFor('"" != g:out')
2035 if has('win32')
2036 let g:out = substitute(g:out, '\r', '', 'g')
2037 endif
2038 if a:remove_lf
2039 let g:out = substitute(g:out, '\n$', '', 'g')
2040 endif
2041 call assert_equal(a:out, g:out)
2042 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01002043 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002044 unlet g:out
2045 endtry
2046endfunc
2047
2048func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002049 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
2050 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
2051 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
2052 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
2053 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
2054 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
2055 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
2056 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
2057 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
2058 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
2059 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
2060 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
2061 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
2062
2063 " tests which not contain spaces in the argument
2064 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
2065 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
2066 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
2067 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
2068 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
2069 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
2070 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
2071 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
2072 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
2073endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02002074
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002075func Test_keep_pty_open()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002076 CheckUnix
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002077
Bram Moolenaare2956092019-01-25 21:01:17 +01002078 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
2079 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002080 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
2081 call assert_inrange(200, 1000, elapsed)
2082 call job_stop(job)
2083endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01002084
2085func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002086 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01002087 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01002088
2089 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002090 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002091 endfunc
2092
2093 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002094 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002095 call Resume()
2096 endfunc
2097
2098 func TimerCb(timer)
2099 if has('win32')
2100 let cmd = ['cmd', '/c', 'echo.']
2101 else
2102 let cmd = ['echo']
2103 endif
2104 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
2105 call substitute(repeat('a', 100000), '.', '', 'g')
2106 endfunc
2107
2108 " We should be interrupted before 'updatetime' elapsed.
2109 let g:val = 0
2110 call timer_start(1, 'TimerCb')
2111 let elapsed = Standby(&ut)
2112 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002113
2114 " Wait for both OutCb() and ExitCb() to have been called before deleting
2115 " them.
2116 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002117 call job_stop(g:job)
2118
2119 delfunc OutCb
2120 delfunc ExitCb
2121 delfunc TimerCb
2122 unlet! g:val
2123 unlet! g:job
2124endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002125
2126func Test_raw_large_data()
2127 try
2128 let g:out = ''
2129 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002130 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2131 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002132
Bram Moolenaare2956092019-01-25 21:01:17 +01002133 let outlen = 79999
2134 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002135 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002136 call WaitFor({-> len(g:out) >= outlen}, 10000)
2137 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002138 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2139 finally
2140 call job_stop(job)
2141 unlet g:out
2142 endtry
2143endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002144
Bram Moolenaar65240682019-02-10 22:23:26 +01002145func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002146 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002147
2148 try
2149 let job = job_start(s:python . " test_channel_pipe.py busy",
2150 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2151 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2152 finally
2153 call job_stop(job)
2154 endtry
2155endfunc
2156
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002157func Test_job_exitval_and_termsig()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002158 CheckUnix
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002159
2160 " Terminate job normally
2161 let cmd = ['echo']
2162 let job = job_start(cmd)
2163 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2164 let info = job_info(job)
2165 call assert_equal(0, info.exitval)
2166 call assert_equal("", info.termsig)
2167
2168 " Terminate job by signal
2169 let cmd = ['sleep', '10']
2170 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002171 " 10m usually works but 50m is needed when running Valgrind
2172 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002173 call job_stop(job)
2174 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2175 let info = job_info(job)
2176 call assert_equal(-1, info.exitval)
2177 call assert_equal("term", info.termsig)
2178endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002179
2180func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002181 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002182
2183 call writefile(['test'], 'Xtestin')
2184 let in_opts = [{},
2185 \ {'in_io': 'null'},
2186 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2187 let out_opts = [{},
2188 \ {'out_io': 'null'},
2189 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2190 let err_opts = [{},
2191 \ {'err_io': 'null'},
2192 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2193 \ {'err_io': 'out'}]
2194 let opts = []
2195
2196 for in_opt in in_opts
2197 let x = copy(in_opt)
2198 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002199 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002200 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002201 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002202 let opts += [extend({'pty': 1}, x)]
2203 endfor
2204 endfor
2205 endfor
2206
2207 for opt in opts
2208 let job = job_start('echo', opt)
2209 let info = job_info(job)
2210 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2211 \ get(opt, 'in_io', 'tty'),
2212 \ get(opt, 'out_io', 'tty'),
2213 \ get(opt, 'err_io', 'tty'))
2214
2215 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2216 call assert_notequal('', info.tty_in, msg)
2217 else
2218 call assert_equal('', info.tty_in, msg)
2219 endif
2220 call assert_equal(info.tty_in, info.tty_out, msg)
2221
2222 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2223 endfor
2224
2225 call delete('Xtestin')
2226 call delete('Xtestout')
2227 call delete('Xtesterr')
2228endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002229
2230" Do this last, it stops any channel log.
2231func Test_zz_nl_err_to_out_pipe()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002232
Bram Moolenaar570497a2019-08-22 22:55:13 +02002233 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002234 call ch_log('Test_zz_nl_err_to_out_pipe()')
2235 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2236 call assert_equal("run", job_status(job))
2237 try
2238 let handle = job_getchannel(job)
2239 call ch_sendraw(handle, "echo something\n")
2240 call assert_equal("something", ch_readraw(handle))
2241
2242 call ch_sendraw(handle, "echoerr wrong\n")
2243 call assert_equal("wrong", ch_readraw(handle))
2244 finally
2245 call job_stop(job)
2246 call ch_logfile('')
2247 let loglines = readfile('Xlog')
2248 call assert_true(len(loglines) > 10)
2249 let found_test = 0
2250 let found_send = 0
2251 let found_recv = 0
2252 let found_stop = 0
2253 for l in loglines
2254 if l =~ 'Test_zz_nl_err_to_out_pipe'
2255 let found_test = 1
2256 endif
2257 if l =~ 'SEND on.*echo something'
2258 let found_send = 1
2259 endif
2260 if l =~ 'RECV on.*something'
2261 let found_recv = 1
2262 endif
2263 if l =~ 'Stopping job with'
2264 let found_stop = 1
2265 endif
2266 endfor
2267 call assert_equal(1, found_test)
2268 call assert_equal(1, found_send)
2269 call assert_equal(1, found_recv)
2270 call assert_equal(1, found_stop)
2271 " On MS-Windows need to sleep for a moment to be able to delete the file.
2272 sleep 10m
2273 call delete('Xlog')
2274 endtry
2275endfunc
2276
2277" Do this last, it stops any channel log.
2278func Test_zz_ch_log()
2279 call ch_logfile('Xlog', 'w')
2280 call ch_log('hello there')
2281 call ch_log('%s%s')
2282 call ch_logfile('')
2283 let text = readfile('Xlog')
2284 call assert_match("hello there", text[1])
2285 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002286 call mkdir("Xdir1")
2287 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2288 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002289 call delete('Xlog')
2290endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002291
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002292func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002293 if has('win32')
2294 let cmd = 'cmd /c pause'
2295 else
2296 let cmd = 'grep foo'
2297 endif
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002298
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002299 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002300 sleep 50m " give the job time to start
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002301 call job_stop(g:job)
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002302 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2303
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002304 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002305 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002306 call job_stop(g:job, 'term')
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002307 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2308
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002309 let g:job = job_start(cmd, {})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002310 sleep 50m
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002311 call job_stop(g:job, 'kill')
2312 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002313endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002314
2315func Test_issue_5485()
2316 let $VAR1 = 'global'
2317 let g:Ch_reply = ""
2318 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2319 let g:Ch_job = l:job
2320 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2321 unlet $VAR1
2322endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002323
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002324func Test_job_trailing_space_unix()
2325 CheckUnix
2326 CheckExecutable cat
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002327
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002328 let job = job_start("cat ", #{in_io: 'null'})
2329 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2330 call assert_equal(0, job_info(job).exitval)
LemonBoycc766a82022-04-04 15:46:58 +01002331
2332 call delete('Xtestsocket')
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002333endfunc
2334
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002335func Test_ch_getbufnr()
2336 let ch = test_null_channel()
2337 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2338 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2339 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2340 call assert_equal(-1, ch_getbufnr(ch, ''))
2341endfunc
2342
2343" Test for unsupported options passed to ch_status()
2344func Test_invalid_job_chan_options()
2345 let ch = test_null_channel()
2346 let invalid_opts = [
2347 \ {'in_io' : 'null'},
2348 \ {'out_io' : 'null'},
2349 \ {'err_io' : 'null'},
2350 \ {'mode' : 'json'},
2351 \ {'out_mode' : 'json'},
2352 \ {'err_mode' : 'json'},
2353 \ {'noblock' : 1},
2354 \ {'in_name' : '/a/b'},
2355 \ {'pty' : 1},
2356 \ {'in_buf' : 1},
2357 \ {'out_buf' : 1},
2358 \ {'err_buf' : 1},
2359 \ {'out_modifiable' : 1},
2360 \ {'err_modifiable' : 1},
2361 \ {'out_msg' : 1},
2362 \ {'err_msg' : 1},
2363 \ {'in_top' : 1},
2364 \ {'in_bot' : 1},
2365 \ {'channel' : ch},
2366 \ {'callback' : ''},
2367 \ {'out_cb' : ''},
2368 \ {'err_cb' : ''},
2369 \ {'close_cb' : ''},
2370 \ {'exit_cb' : ''},
2371 \ {'term_opencmd' : ''},
2372 \ {'eof_chars' : ''},
2373 \ {'term_rows' : 10},
2374 \ {'term_cols' : 10},
2375 \ {'vertical' : 0},
2376 \ {'curwin' : 1},
2377 \ {'bufnr' : 1},
2378 \ {'hidden' : 0},
2379 \ {'norestore' : 0},
2380 \ {'term_kill' : 'kill'},
2381 \ {'tty_type' : ''},
2382 \ {'term_highlight' : ''},
2383 \ {'env' : {}},
2384 \ {'cwd' : ''},
2385 \ {'timeout' : 0},
2386 \ {'out_timeout' : 0},
2387 \ {'err_timeout' : 0},
2388 \ {'id' : 0},
2389 \ {'stoponexit' : ''},
2390 \ {'block_write' : 1}
2391 \ ]
2392 if has('gui')
2393 call add(invalid_opts, {'ansi_colors' : []})
2394 endif
2395
2396 for opt in invalid_opts
2397 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2398 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002399 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002400endfunc
2401
2402" Test for passing the command and the arguments as List on MS-Windows
2403func Test_job_with_list_args()
2404 CheckMSWindows
2405
2406 enew!
2407 let bnum = bufnr()
2408 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2409 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2410 call assert_equal('Hello World', getline(1))
2411 %bw!
2412endfunc
2413
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002414func ExitCb_cb_with_input(job, status)
2415 call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx')
2416 call assert_equal('default', Screenline(&lines))
2417 let g:wait_exit_cb = 0
2418endfunc
2419
2420func Test_cb_with_input()
2421 let g:wait_exit_cb = 1
2422
Bram Moolenaarf637bce2020-11-23 18:14:56 +01002423 if has('win32')
2424 let cmd = 'cmd /c echo "Vim''s test"'
2425 else
2426 let cmd = 'echo "Vim''s test"'
2427 endif
2428
2429 let job = job_start(cmd, {'out_cb': 'ExitCb_cb_with_input'})
2430 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002431 call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)})
2432
2433 unlet g:wait_exit_cb
2434endfunc
2435
Bram Moolenaar57942232021-08-04 20:54:55 +02002436function s:HandleBufEnter() abort
2437 let queue = []
2438 let job = job_start(['date'], {'callback': { j, d -> add(queue, d) }})
2439 while empty(queue)
2440 sleep! 10m
2441 endwhile
2442endfunction
2443
2444func Test_parse_messages_in_autocmd()
2445 CheckUnix
2446
2447 " Check that in the BufEnter autocommand events are being handled
2448 augroup bufenterjob
2449 autocmd!
2450 autocmd BufEnter Xbufenterjob call s:HandleBufEnter()
2451 augroup END
2452
2453 only
2454 split Xbufenterjob
2455 wincmd p
2456 redraw
2457
2458 close
2459 augroup bufenterjob
2460 autocmd!
2461 augroup END
2462endfunc
2463
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01002464func Test_job_start_with_invalid_argument()
2465 call assert_fails('call job_start([0zff])', 'E976:')
2466endfunc
2467
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002468" Test for the 'lsp' channel mode
2469func LspCb(chan, msg)
2470 call add(g:lspNotif, a:msg)
2471endfunc
2472
2473func LspOtCb(chan, msg)
2474 call add(g:lspOtMsgs, a:msg)
2475endfunc
2476
2477func LspTests(port)
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +01002478 " call ch_logfile('Xlspclient.log', 'w')
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002479 let ch = ch_open(s:localhost .. a:port, #{mode: 'lsp', callback: 'LspCb'})
2480 if ch_status(ch) == "fail"
2481 call assert_report("Can't open the lsp channel")
2482 return
2483 endif
2484
2485 " check for channel information
2486 let info = ch_info(ch)
2487 call assert_equal('LSP', info.sock_mode)
2488
2489 " Evaluate an expression
2490 let resp = ch_evalexpr(ch, #{method: 'simple-rpc', params: [10, 20]})
2491 call assert_false(empty(resp))
2492 call assert_equal(#{id: 1, jsonrpc: '2.0', result: 'simple-rpc'}, resp)
2493
2494 " Evaluate an expression. While waiting for the response, a notification
2495 " message is delivered.
2496 let g:lspNotif = []
2497 let resp = ch_evalexpr(ch, #{method: 'rpc-with-notif', params: {'v': 10}})
2498 call assert_false(empty(resp))
2499 call assert_equal(#{id: 2, jsonrpc: '2.0', result: 'rpc-with-notif-resp'},
2500 \ resp)
2501 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-with-notif-notif'}],
2502 \ g:lspNotif)
2503
2504 " Wrong payload notification test
2505 let g:lspNotif = []
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002506 let r = ch_sendexpr(ch, #{method: 'wrong-payload', params: {}})
2507 call assert_equal({}, r)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002508 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002509 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002510 call assert_equal([#{jsonrpc: '2.0', result: 'wrong-payload'}], g:lspNotif)
2511
2512 " Test for receiving a response with incorrect 'id' and additional
2513 " notification messages while evaluating an expression.
2514 let g:lspNotif = []
2515 let resp = ch_evalexpr(ch, #{method: 'rpc-resp-incorrect-id',
2516 \ params: {'a': [1, 2]}})
2517 call assert_false(empty(resp))
2518 call assert_equal(#{id: 4, jsonrpc: '2.0',
2519 \ result: 'rpc-resp-incorrect-id-4'}, resp)
2520 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-1'},
2521 \ #{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-2'},
2522 \ #{jsonrpc: '2.0', id: 1, result: 'rpc-resp-incorrect-id-3'}],
2523 \ g:lspNotif)
2524
2525 " simple notification test
2526 let g:lspNotif = []
2527 call ch_sendexpr(ch, #{method: 'simple-notif', params: [#{a: 10, b: []}]})
2528 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002529 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002530 call assert_equal([#{jsonrpc: '2.0', result: 'simple-notif'}], g:lspNotif)
2531
2532 " multiple notifications test
2533 let g:lspNotif = []
2534 call ch_sendexpr(ch, #{method: 'multi-notif', params: [#{a: {}, b: {}}]})
2535 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002536 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002537 call assert_equal([#{jsonrpc: '2.0', result: 'multi-notif1'},
2538 \ #{jsonrpc: '2.0', result: 'multi-notif2'}], g:lspNotif)
2539
2540 " Test for sending a message with an identifier.
2541 let g:lspNotif = []
2542 call ch_sendexpr(ch, #{method: 'msg-with-id', id: 93, params: #{s: 'str'}})
2543 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002544 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002545 call assert_equal([#{jsonrpc: '2.0', id: 93, result: 'msg-with-id'}],
2546 \ g:lspNotif)
2547
2548 " Test for setting the 'id' value in a request message
2549 let resp = ch_evalexpr(ch, #{method: 'ping', id: 1, params: {}})
2550 call assert_equal(#{id: 8, jsonrpc: '2.0', result: 'alive'}, resp)
2551
2552 " Test for using a one time callback function to process a response
2553 let g:lspOtMsgs = []
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002554 let r = ch_sendexpr(ch, #{method: 'msg-specifc-cb', params: {}},
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002555 \ #{callback: 'LspOtCb'})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002556 call assert_equal(9, r.id)
2557 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002558 call assert_equal([#{id: 9, jsonrpc: '2.0', result: 'msg-specifc-cb'}],
2559 \ g:lspOtMsgs)
2560
2561 " Test for generating a request message from the other end (server)
2562 let g:lspNotif = []
2563 call ch_sendexpr(ch, #{method: 'server-req', params: #{}})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002564 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002565 call assert_equal([{'id': 201, 'jsonrpc': '2.0',
2566 \ 'result': {'method': 'checkhealth', 'params': {'a': 20}}}],
2567 \ g:lspNotif)
2568
2569 " Test for sending a message without an id
2570 let g:lspNotif = []
2571 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'msg-without-id'}})
2572 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002573 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002574 call assert_equal([#{jsonrpc: '2.0', result:
2575 \ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'msg-without-id'}}}],
2576 \ g:lspNotif)
2577
2578 " Test for sending a notification message with an id
2579 let g:lspNotif = []
2580 call ch_sendexpr(ch, #{method: 'echo', id: 110, params: #{s: 'msg-with-id'}})
2581 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002582 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002583 call assert_equal([#{jsonrpc: '2.0', result:
2584 \ #{method: 'echo', jsonrpc: '2.0', id: 110,
2585 \ params: #{s: 'msg-with-id'}}}], g:lspNotif)
2586
2587 " Test for processing the extra fields in the HTTP header
2588 let resp = ch_evalexpr(ch, #{method: 'extra-hdr-fields', params: {}})
2589 call assert_equal({'id': 14, 'jsonrpc': '2.0', 'result': 'extra-hdr-fields'},
2590 \ resp)
2591
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +01002592 " Test for processing delayed payload
2593 let resp = ch_evalexpr(ch, #{method: 'delayed-payload', params: {}})
2594 call assert_equal({'id': 15, 'jsonrpc': '2.0', 'result': 'delayed-payload'},
2595 \ resp)
2596
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002597 " Test for processing a HTTP header without the Content-Length field
2598 let resp = ch_evalexpr(ch, #{method: 'hdr-without-len', params: {}},
2599 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002600 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002601 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002602 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002603
2604 " Test for processing a HTTP header with wrong length
2605 let resp = ch_evalexpr(ch, #{method: 'hdr-with-wrong-len', params: {}},
2606 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002607 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002608 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002609 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002610
2611 " Test for processing a HTTP header with negative length
2612 let resp = ch_evalexpr(ch, #{method: 'hdr-with-negative-len', params: {}},
2613 \ #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002614 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002615 " send a ping to make sure communication still works
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002616 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002617
2618 " Test for an empty header
2619 let resp = ch_evalexpr(ch, #{method: 'empty-header', 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 an empty payload
2626 let resp = ch_evalexpr(ch, #{method: 'empty-payload', 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
Yegappan Lakshmananbac9a9e2022-04-19 10:25:13 +01002632 " Test for a large payload
2633 let content = repeat('abcdef', 11000)
2634 let resp = ch_evalexpr(ch, #{method: 'large-payload',
2635 \ params: #{text: content}})
2636 call assert_equal(#{jsonrpc: '2.0', id: 26, result:
2637 \ #{method: 'large-payload', jsonrpc: '2.0', id: 26,
2638 \ params: #{text: content}}}, resp)
2639 " send a ping to make sure communication still works
2640 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
2641
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002642 " Test for invoking an unsupported method
2643 let resp = ch_evalexpr(ch, #{method: 'xyz', params: {}}, #{timeout: 200})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002644 call assert_equal({}, resp)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002645
2646 " Test for sending a message without a callback function. Notification
2647 " message should be dropped but RPC response should not be dropped.
2648 call ch_setoptions(ch, #{callback: ''})
2649 let g:lspNotif = []
2650 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
2651 " Send a ping to wait for all the notification messages to arrive
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002652 call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002653 call assert_equal([], g:lspNotif)
2654 " Restore the callback function
2655 call ch_setoptions(ch, #{callback: 'LspCb'})
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002656
2657 " " Test for sending a raw message
2658 " let g:lspNotif = []
2659 " let s = "Content-Length: 62\r\n"
2660 " let s ..= "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
2661 " let s ..= "\r\n"
2662 " let s ..= '{"method":"echo","jsonrpc":"2.0","params":{"m":"raw-message"}}'
2663 " call ch_sendraw(ch, s)
2664 " call ch_evalexpr(ch, #{method: 'ping'})
2665 " call assert_equal([{'jsonrpc': '2.0',
2666 " \ 'result': {'method': 'echo', 'jsonrpc': '2.0',
2667 " \ 'params': {'m': 'raw-message'}}}], g:lspNotif)
2668
2669 " Invalid arguments to ch_evalexpr() and ch_sendexpr()
2670 call assert_fails('call ch_sendexpr(ch, #{method: "cookie", id: "cookie"})',
2671 \ 'E475:')
2672 call assert_fails('call ch_evalexpr(ch, #{method: "ping", id: [{}]})', 'E475:')
2673 call assert_fails('call ch_evalexpr(ch, [1, 2, 3])', 'E1206:')
2674 call assert_fails('call ch_sendexpr(ch, "abc")', 'E1206:')
2675 call assert_fails('call ch_evalexpr(ch, #{method: "ping"}, #{callback: "LspOtCb"})', 'E917:')
2676 " call ch_logfile('', 'w')
2677endfunc
2678
2679func Test_channel_lsp_mode()
2680 call RunServer('test_channel_lsp.py', 'LspTests', [])
2681endfunc
Bram Moolenaar57942232021-08-04 20:54:55 +02002682
Bram Moolenaar8b633132020-03-20 18:20:51 +01002683" vim: shiftwidth=2 sts=2 expandtab