blob: 6ad9dc0e162b26f6d5bd2e1128b9c011e7220ce7 [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)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100531 call assert_true(reltimefloat(elapsed) < 1.0)
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
1204 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001205 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001206 call assert_equal("run", job_status(job))
1207 call job_stop(job)
1208
1209 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001210 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001211 call assert_equal("run", job_status(job))
1212 call assert_equal('channel fail', string(job_getchannel(job)))
1213 call assert_equal('fail', ch_status(job))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001214 call assert_equal('no process', string(test_null_job()))
1215 call assert_equal('channel fail', string(test_null_channel()))
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001216 call job_stop(job)
1217endfunc
1218
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001219func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001220 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1221 split testout
1222 let job = job_start([s:python, '-c',
1223 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001224 " the job may be done quickly, also accept "dead"
1225 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001226 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001227 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001228 let totlen = 0
1229 for line in getline(1, '$')
1230 call assert_equal('', substitute(line, '^\.*', '', ''))
1231 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001232 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001233 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001234 finally
1235 call job_stop(job)
1236 bwipe!
1237 endtry
1238endfunc
1239
Bram Moolenaarde279892016-03-11 22:19:44 +01001240func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001241 let job = job_start(s:python . " test_channel_pipe.py")
1242 call assert_equal("run", job_status(job))
1243 let handle = job_getchannel(job)
1244 try
1245 call ch_sendraw(handle, "echo something\n")
1246 call assert_equal("something", ch_readraw(handle))
1247 finally
1248 call job_stop(job)
1249 endtry
1250
1251 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1252 call assert_equal("run", job_status(job))
1253 let handle = job_getchannel(job)
1254 try
1255 call ch_sendraw(handle, "echo again\n")
1256 call assert_equal("again", ch_readraw(handle))
1257 finally
1258 call job_stop(job)
1259 endtry
1260endfunc
1261
Bram Moolenaar75f72652016-03-20 22:16:56 +01001262func Test_out_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001263 let g:test_is_flaky = 1
Bram Moolenaar75f72652016-03-20 22:16:56 +01001264 let dict = {'thisis': 'dict: '}
1265 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001266 if type(a:msg) == v:t_string
1267 let g:Ch_outmsg = self.thisis . a:msg
1268 else
1269 let g:Ch_outobj = a:msg
1270 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001271 endfunc
1272 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001273 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001274 endfunc
1275 let job = job_start(s:python . " test_channel_pipe.py",
1276 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001277 \ 'out_mode': 'json',
1278 \ 'err_cb': dict.errHandler,
1279 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001280 call assert_equal("run", job_status(job))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001281 call test_garbagecollect_now()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001282 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001283 let g:Ch_outmsg = ''
1284 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001285 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1286 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001287 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1288 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001289
1290 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001291 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001292 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001293 " For unknown reasons this can be very slow on Mac.
Bram Moolenaardeda6442021-12-17 11:44:33 +00001294 " Increase the timeout on every run.
1295 if g:run_nr == 1
1296 let timeout = 5000
1297 elseif g:run_nr == 2
1298 let timeout = 10000
1299 elseif g:run_nr == 3
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001300 let timeout = 20000
1301 else
Bram Moolenaardeda6442021-12-17 11:44:33 +00001302 let timeout = 40000
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001303 endif
1304 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001305 finally
1306 call job_stop(job)
1307 endtry
1308endfunc
1309
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001310func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001311 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001312 let g:Ch_msg1 = ''
1313 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001314 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001315 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001316 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001317 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001318 let s:counter += 1
1319 endfunc
1320 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001321 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001322 let s:counter += 1
1323 endfunc
1324 let job = job_start(s:python . " test_channel_pipe.py quit now",
1325 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001326 \ 'close_cb': 'CloseHandler'})
1327 " the job may be done quickly, also accept "dead"
1328 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001329 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001330 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1331 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001332 finally
1333 call job_stop(job)
1334 delfunc OutHandler
1335 delfunc CloseHandler
1336 endtry
1337endfunc
1338
Bram Moolenaar437905c2016-04-26 19:01:05 +02001339func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001341 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001342 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001343 endfunc
1344 let job = job_start(s:python . " test_channel_pipe.py quit now",
1345 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001346 " the job may be done quickly, also accept "dead"
1347 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001348 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001349 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001350 finally
1351 call job_stop(job)
1352 delfunc CloseHandler
1353 endtry
1354endfunc
1355
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001356" Use channel in NL mode but received text does not end in NL.
1357func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001358 let g:Ch_received = ''
1359 func! CloseHandler(chan)
1360 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1361 let g:Ch_received .= ch_read(a:chan)
1362 endwhile
1363 endfunc
1364 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1365 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001366 " the job may be done quickly, also accept "dead"
1367 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001368 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001369 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001370 finally
1371 call job_stop(job)
1372 delfunc CloseHandler
1373 endtry
1374endfunc
1375
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001376func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001377 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001378 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1379 \ 'out_mode': 'json',
1380 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1381 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001382 call assert_equal("run", job_status(job))
1383 try
1384 let g:Ch_outmsg = ''
1385 let g:Ch_errmsg = ''
1386 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1387 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001388 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1389 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001390 finally
1391 call job_stop(job)
1392 endtry
1393endfunc
1394
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001395func Test_close_and_exit_cb()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001396 let g:test_is_flaky = 1
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001397 let g:retdict = {'ret': {}}
1398 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001399 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001400 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001401 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001402 let self.ret['exit_cb'] = job_status(a:job)
1403 endfunc
1404
Bram Moolenaare2956092019-01-25 21:01:17 +01001405 let job = job_start([&shell, &shellcmdflag, 'echo'],
1406 \ {'close_cb': g:retdict.close_cb,
1407 \ 'exit_cb': g:retdict.exit_cb})
1408 " the job may be done quickly, also accept "dead"
1409 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001410 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001411 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001412 call assert_equal('dead', g:retdict.ret['exit_cb'])
1413 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001414endfunc
1415
Bram Moolenaard46ae142016-02-16 13:33:52 +01001416""""""""""
1417
Bram Moolenaar0b146882018-09-06 16:27:24 +02001418function ExitCbWipe(job, status)
1419 exe g:wipe_buf 'bw!'
1420endfunction
1421
1422" This caused a crash, because messages were handled while peeking for a
1423" character.
1424func Test_exit_cb_wipes_buf()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001425 CheckFeature timers
Bram Moolenaar0b146882018-09-06 16:27:24 +02001426 set cursorline lazyredraw
1427 call test_override('redraw_flag', 1)
1428 new
1429 let g:wipe_buf = bufnr('')
1430
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001431 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001432 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001433 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1434 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1435 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1436 call timer_stop(timer)
1437
1438 set nocursorline nolazyredraw
1439 unlet g:wipe_buf
1440 call test_override('ALL', 0)
1441endfunc
1442
1443""""""""""
1444
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001445let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001446func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001447 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001448 unlet s:channelfd
1449endfunc
1450
1451" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001452func Ch_unlet_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001453 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001454 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001455 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001456endfunc
1457
1458func Test_unlet_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001459 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001460endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001461
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001462func Test_unlet_handle_ipv6()
1463 CheckIPv6
1464 call Test_unlet_handle()
1465endfunc
1466
Bram Moolenaard46ae142016-02-16 13:33:52 +01001467""""""""""
1468
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001469let g:Ch_unletResponse = ''
1470func Ch_CloseHandler(handle, msg)
1471 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001472 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001473endfunc
1474
1475" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001476func Ch_close_handle(port)
LemonBoycc766a82022-04-04 15:46:58 +01001477 let s:channelfd = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001478 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001479 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001480endfunc
1481
1482func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001483 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001484endfunc
1485
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001486func Test_close_handle_ipv6()
1487 CheckIPv6
1488 call Test_close_handle()
1489endfunc
1490
1491""""""""""
1492
1493func Ch_open_ipv6(port)
LemonBoycc766a82022-04-04 15:46:58 +01001494 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001495 call assert_notequal('fail', ch_status(handle))
1496endfunc
1497
1498func Test_open_ipv6()
1499 CheckIPv6
1500 call s:run_server('Ch_open_ipv6')
1501endfunc
1502
Bram Moolenaard46ae142016-02-16 13:33:52 +01001503""""""""""
1504
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001505func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001506 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001507 echo ch
1508 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001509 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1510 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001511 call assert_fails("let ch = ch_open('localhost:65537')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001512 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1513 \ 'E474:')
1514 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1515 \ 'E475:')
1516 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1517 \ 'E475:')
1518 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1519 \ 'E475:')
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001520 call assert_fails("let ch = ch_open('[::]')", 'E475:')
1521 call assert_fails("let ch = ch_open('[::.80')", 'E475:')
1522 call assert_fails("let ch = ch_open('[::]8080')", 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001523endfunc
1524
1525func Test_ch_info_fail()
1526 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001527endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001528
1529""""""""""
1530
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001531func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001532 " Wait up to a second for the port to open.
1533 let s:chopt.waittime = 1000
LemonBoycc766a82022-04-04 15:46:58 +01001534 let channel = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001535 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001536 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001537 return
1538 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001539 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001540 call ch_close(channel)
1541endfunc
1542
1543func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001544 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001545 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001546endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001547
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001548func Test_open_delay_ipv6()
1549 CheckIPv6
1550 call Test_open_delay()
1551endfunc
1552
Bram Moolenaarece61b02016-02-20 21:39:05 +01001553"""""""""
1554
1555function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001556 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001557endfunc
1558
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001559function Ch_test_call(port)
LemonBoycc766a82022-04-04 15:46:58 +01001560 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001561 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001562 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001563 return
1564 endif
1565
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001566 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001567 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001568 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001569
1570 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1571 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1572 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001573endfunc
1574
1575func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001576 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001577endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001578
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001579func Test_call_ipv6()
1580 CheckIPv6
1581 call Test_call()
1582endfunc
1583
LemonBoycc766a82022-04-04 15:46:58 +01001584func Test_call_unix()
1585 CheckUnix
1586 call Test_call()
1587 call delete('Xtestsocket')
1588endfunc
1589
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001590"""""""""
1591
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001592let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001593function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001594 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001595endfunc
1596
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001597function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001598 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001599 let g:Ch_exit_job = g:currentJob
1600 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001601endfunc
1602
1603func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001604 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001605
Bram Moolenaarf386f082019-07-29 23:03:03 +02001606 " wait up to a second for the job to exit
1607 for i in range(100)
1608 if g:Ch_job_exit_ret == 'done'
1609 break
1610 endif
1611 sleep 10m
1612 " calling job_status() triggers the callback
1613 call job_status(g:Ch_exit_job)
1614 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001615
Bram Moolenaarf386f082019-07-29 23:03:03 +02001616 call assert_equal('done', g:Ch_job_exit_ret)
1617 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1618 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001619endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001620
Bram Moolenaar97792de2016-10-15 18:36:49 +02001621function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001622 if job_info(a:job).process == g:exit_cb_val.process
1623 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1624 endif
1625 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001626endfunction
1627
1628func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001629 CheckFunction reltimefloat
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001630 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001631
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001632 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001633 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001634 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001635 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001636 let elapsed = reltimefloat(g:exit_cb_val.end)
1637 call assert_true(elapsed > 0.5)
1638 call assert_true(elapsed < 1.0)
1639
1640 " case: unreferenced job, using timer
1641 if !has('timers')
1642 return
1643 endif
1644
1645 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1646 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1647 let g:exit_cb_val.process = job_info(g:job).process
1648 unlet g:job
1649 call Standby(1000)
1650 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1651 let elapsed = reltimefloat(g:exit_cb_val.end)
1652 else
1653 let elapsed = 1.0
1654 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001655 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001656endfunc
1657
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001658"""""""""
1659
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001660let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001661function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001662 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001663endfunc
1664
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001665function Ch_test_close_callback(port)
LemonBoycc766a82022-04-04 15:46:58 +01001666 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001667 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001668 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001669 return
1670 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001671 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001672
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001673 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001674 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001675endfunc
1676
1677func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001678 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001679endfunc
1680
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001681func Test_close_callback_ipv6()
1682 CheckIPv6
1683 call Test_close_callback()
1684endfunc
1685
LemonBoycc766a82022-04-04 15:46:58 +01001686func Test_close_callback_unix()
1687 CheckUnix
1688 call Test_close_callback()
1689 call delete('Xtestsocket')
1690endfunc
1691
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001692function Ch_test_close_partial(port)
LemonBoycc766a82022-04-04 15:46:58 +01001693 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001694 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001695 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001696 return
1697 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001698 let g:Ch_d = {}
1699 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001700 let self.close_ret = 'closed'
1701 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001702 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001703
1704 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001705 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001706 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001707endfunc
1708
1709func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001710 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001711endfunc
1712
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02001713func Test_close_partial_ipv6()
1714 CheckIPv6
1715 call Test_close_partial()
1716endfunc
1717
LemonBoycc766a82022-04-04 15:46:58 +01001718func Test_close_partial_unix()
1719 CheckUnix
1720 call Test_close_partial()
1721 call delete('Xtestsocket')
1722endfunc
1723
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001724func Test_job_start_fails()
1725 " this was leaking memory
1726 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001727 call assert_fails('call job_start($x)', 'E474:')
1728 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001729 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1730 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1731 call assert_fails('call job_start("ls", [])', 'E715:')
1732 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1733 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1734 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001735 call assert_fails("call job_start('ls', {'callback' : -1})", 'E921:')
1736 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E921:')
1737 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E921:')
1738 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E921:')
1739 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E921:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001740 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1741 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1742 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001743 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001744 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1745 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1746 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1747 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1748 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1749 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1750 call assert_fails("call job_start('ls',
1751 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1752 call assert_fails("call job_start('ls',
1753 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1754 call assert_fails("call job_start('ls',
1755 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1756
1757 call assert_fails("call job_start('ls',
1758 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1759 call assert_fails("call job_start('ls',
1760 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1761 call assert_fails("call job_start('ls',
1762 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1763
1764 set nomodifiable
1765 call assert_fails("call job_start('cmd /c dir',
1766 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
1767 call assert_fails("call job_start('cmd /c dir',
1768 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1769 set modifiable
1770
1771 call assert_fails("call job_start('ls', {'in_io' : 'buffer'})", 'E915:')
1772
1773 edit! XXX
1774 let bnum = bufnr()
1775 enew
1776 call assert_fails("call job_start('ls',
1777 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1778
1779 " Empty job tests
1780 " This was crashing on MS-Windows.
1781 call assert_fails('let job = job_start([""])', 'E474:')
1782 call assert_fails('let job = job_start([" "])', 'E474:')
1783 call assert_fails('let job = job_start("")', 'E474:')
1784 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaar00157952020-04-13 17:44:47 +02001785 call assert_fails('let job = job_start(["ls", []])', 'E730:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001786 call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001787 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001788endfunc
1789
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001790func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001791 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001792 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001793 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001794 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001795 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001796 call job_stop(g:job, 'kill')
1797 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001798 endtry
1799endfunc
1800
Bram Moolenaar271906b2021-08-28 12:30:12 +02001801func Test_null_job_eval()
1802 call assert_fails('eval test_null_job()->eval()', 'E121:')
1803endfunc
1804
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001805" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001806func Test_partial_in_channel_cycle()
1807 let d = {}
1808 let d.a = function('string', [d])
1809 try
1810 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001811 call test_garbagecollect_now()
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001812 catch
1813 call assert_exception('E901:')
1814 endtry
1815 unlet d
1816endfunc
1817
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001818func Test_using_freed_memory()
1819 let g:a = job_start(['ls'])
1820 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001821 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001822endfunc
1823
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001824func Test_collapse_buffers()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001825 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001826 CheckExecutable cat
1827
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001828 sp test_channel.vim
1829 let g:linecount = line('$')
1830 close
1831 split testout
1832 1,$delete
1833 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001834 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001835 bwipe!
1836endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001837
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001838func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001839 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001840 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001841
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001842 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001843 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001844 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001845 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1846 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001847
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001848 bdel test_buffer
1849 call assert_equal([], getbufline(bufnr, 1, '$'))
1850
1851 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001852 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001853 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1854 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1855
1856 bwipe! test_buffer
1857endfunc
1858
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001859func Test_cmd_parsing()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02001860 CheckUnix
1861
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001862 call assert_false(filereadable("file with space"))
1863 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001864 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001865 call delete("file with space")
1866
1867 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001868 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001869 call delete("file with space")
1870endfunc
1871
Bram Moolenaar82117982016-08-27 19:21:48 +02001872func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001873 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001874
1875 " Test lines from the job containing NUL are stored correctly in a buffer.
1876 new
1877 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1878 w! Xtestread
1879 bwipe!
1880 split testout
1881 1,$delete
1882 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1883 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001884 call assert_equal("asdf\nasdf", getline(1))
1885 call assert_equal("xxx\n", getline(2))
1886 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001887
1888 call delete('Xtestread')
1889 bwipe!
1890
1891 " Test lines from a buffer with NUL bytes are written correctly to the job.
1892 new mybuffer
1893 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1894 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 +02001895 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001896 bwipe!
1897 split Xtestwrite
1898 call assert_equal("asdf\nasdf", getline(1))
1899 call assert_equal("xxx\n", getline(2))
1900 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001901 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001902
1903 call delete('Xtestwrite')
1904 bwipe!
1905endfunc
1906
Bram Moolenaarec68a992016-10-03 21:37:41 +02001907func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001908 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001909 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001910 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001911 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001912 unlet g:linecount
1913endfunc
1914
1915func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001916 func s:close_cb(ch)
1917 while ch_status(a:ch) == 'buffered'
1918 let g:out .= ch_read(a:ch)
1919 endwhile
1920 endfunc
1921
1922 let g:out = ''
1923 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1924 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001925 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001926 call WaitForAssert({-> assert_equal('123', g:out)})
1927 unlet g:out
1928 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001929endfunc
1930
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001931func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001932 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001933 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001934 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001935 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001936 call test_garbagecollect_now()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001937 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001938endfunc
1939
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001940func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001941 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001942
1943 " Check that backslash in $COMSPEC is handled properly.
1944 let g:echostr = ''
1945 let cmd = $COMSPEC . ' /c echo 123'
1946 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1947 let info = job_info(job)
1948 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1949
1950 call WaitForAssert({-> assert_equal("123", g:echostr)})
1951 unlet g:echostr
1952endfunc
1953
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001954func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001955 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001956 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001957 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001958 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001959 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001960 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001961 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1962 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001963 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001964 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001965endfunc
1966
1967func Test_cwd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001968 let g:test_is_flaky = 1
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001969 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001970 if has('win32')
1971 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001972 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001973 else
1974 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001975 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001976 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001977 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001978 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001979 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001980 let expect = substitute(expect, '[/\\]$', '', '')
1981 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1982 if $CI != '' && stridx(g:envstr, '/private/') == 0
1983 let g:envstr = g:envstr[8:]
1984 endif
1985 call assert_equal(expect, g:envstr)
1986 finally
1987 call job_stop(job)
1988 unlet g:envstr
1989 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001990endfunc
1991
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001992function Ch_test_close_lambda(port)
LemonBoycc766a82022-04-04 15:46:58 +01001993 let handle = ch_open(s:address(a:port), s:chopt)
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001994 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001995 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001996 return
1997 endif
1998 let g:Ch_close_ret = ''
1999 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002000 call test_garbagecollect_now()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002001
2002 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002003 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002004endfunc
2005
2006func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002007 call s:run_server('Ch_test_close_lambda')
2008endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002009
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02002010func Test_close_lambda_ipv6()
2011 CheckIPv6
2012 call Test_close_lambda()
2013endfunc
2014
LemonBoycc766a82022-04-04 15:46:58 +01002015func Test_close_lambda_unix()
2016 CheckUnix
2017 call Test_close_lambda()
2018 call delete('Xtestsocket')
2019endfunc
2020
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002021func s:test_list_args(cmd, out, remove_lf)
2022 try
2023 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01002024 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 +02002025 call WaitFor('"" != g:out')
2026 if has('win32')
2027 let g:out = substitute(g:out, '\r', '', 'g')
2028 endif
2029 if a:remove_lf
2030 let g:out = substitute(g:out, '\n$', '', 'g')
2031 endif
2032 call assert_equal(a:out, g:out)
2033 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01002034 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002035 unlet g:out
2036 endtry
2037endfunc
2038
2039func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002040 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
2041 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
2042 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
2043 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
2044 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
2045 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
2046 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
2047 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
2048 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
2049 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
2050 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
2051 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
2052 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
2053
2054 " tests which not contain spaces in the argument
2055 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
2056 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
2057 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
2058 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
2059 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
2060 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
2061 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
2062 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
2063 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
2064endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02002065
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002066func Test_keep_pty_open()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002067 CheckUnix
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002068
Bram Moolenaare2956092019-01-25 21:01:17 +01002069 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
2070 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02002071 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
2072 call assert_inrange(200, 1000, elapsed)
2073 call job_stop(job)
2074endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01002075
2076func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002077 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01002078 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01002079
2080 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002081 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002082 endfunc
2083
2084 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002085 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01002086 call Resume()
2087 endfunc
2088
2089 func TimerCb(timer)
2090 if has('win32')
2091 let cmd = ['cmd', '/c', 'echo.']
2092 else
2093 let cmd = ['echo']
2094 endif
2095 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
2096 call substitute(repeat('a', 100000), '.', '', 'g')
2097 endfunc
2098
2099 " We should be interrupted before 'updatetime' elapsed.
2100 let g:val = 0
2101 call timer_start(1, 'TimerCb')
2102 let elapsed = Standby(&ut)
2103 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01002104
2105 " Wait for both OutCb() and ExitCb() to have been called before deleting
2106 " them.
2107 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01002108 call job_stop(g:job)
2109
2110 delfunc OutCb
2111 delfunc ExitCb
2112 delfunc TimerCb
2113 unlet! g:val
2114 unlet! g:job
2115endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01002116
2117func Test_raw_large_data()
2118 try
2119 let g:out = ''
2120 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01002121 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
2122 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01002123
Bram Moolenaare2956092019-01-25 21:01:17 +01002124 let outlen = 79999
2125 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02002126 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01002127 call WaitFor({-> len(g:out) >= outlen}, 10000)
2128 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01002129 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
2130 finally
2131 call job_stop(job)
2132 unlet g:out
2133 endtry
2134endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002135
Bram Moolenaar65240682019-02-10 22:23:26 +01002136func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002137 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01002138
2139 try
2140 let job = job_start(s:python . " test_channel_pipe.py busy",
2141 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
2142 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
2143 finally
2144 call job_stop(job)
2145 endtry
2146endfunc
2147
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002148func Test_job_exitval_and_termsig()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002149 CheckUnix
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002150
2151 " Terminate job normally
2152 let cmd = ['echo']
2153 let job = job_start(cmd)
2154 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2155 let info = job_info(job)
2156 call assert_equal(0, info.exitval)
2157 call assert_equal("", info.termsig)
2158
2159 " Terminate job by signal
2160 let cmd = ['sleep', '10']
2161 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01002162 " 10m usually works but 50m is needed when running Valgrind
2163 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01002164 call job_stop(job)
2165 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2166 let info = job_info(job)
2167 call assert_equal(-1, info.exitval)
2168 call assert_equal("term", info.termsig)
2169endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01002170
2171func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02002172 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01002173
2174 call writefile(['test'], 'Xtestin')
2175 let in_opts = [{},
2176 \ {'in_io': 'null'},
2177 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
2178 let out_opts = [{},
2179 \ {'out_io': 'null'},
2180 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
2181 let err_opts = [{},
2182 \ {'err_io': 'null'},
2183 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
2184 \ {'err_io': 'out'}]
2185 let opts = []
2186
2187 for in_opt in in_opts
2188 let x = copy(in_opt)
2189 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002190 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002191 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002192 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002193 let opts += [extend({'pty': 1}, x)]
2194 endfor
2195 endfor
2196 endfor
2197
2198 for opt in opts
2199 let job = job_start('echo', opt)
2200 let info = job_info(job)
2201 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2202 \ get(opt, 'in_io', 'tty'),
2203 \ get(opt, 'out_io', 'tty'),
2204 \ get(opt, 'err_io', 'tty'))
2205
2206 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2207 call assert_notequal('', info.tty_in, msg)
2208 else
2209 call assert_equal('', info.tty_in, msg)
2210 endif
2211 call assert_equal(info.tty_in, info.tty_out, msg)
2212
2213 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2214 endfor
2215
2216 call delete('Xtestin')
2217 call delete('Xtestout')
2218 call delete('Xtesterr')
2219endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002220
2221" Do this last, it stops any channel log.
2222func Test_zz_nl_err_to_out_pipe()
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002223
Bram Moolenaar570497a2019-08-22 22:55:13 +02002224 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002225 call ch_log('Test_zz_nl_err_to_out_pipe()')
2226 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2227 call assert_equal("run", job_status(job))
2228 try
2229 let handle = job_getchannel(job)
2230 call ch_sendraw(handle, "echo something\n")
2231 call assert_equal("something", ch_readraw(handle))
2232
2233 call ch_sendraw(handle, "echoerr wrong\n")
2234 call assert_equal("wrong", ch_readraw(handle))
2235 finally
2236 call job_stop(job)
2237 call ch_logfile('')
2238 let loglines = readfile('Xlog')
2239 call assert_true(len(loglines) > 10)
2240 let found_test = 0
2241 let found_send = 0
2242 let found_recv = 0
2243 let found_stop = 0
2244 for l in loglines
2245 if l =~ 'Test_zz_nl_err_to_out_pipe'
2246 let found_test = 1
2247 endif
2248 if l =~ 'SEND on.*echo something'
2249 let found_send = 1
2250 endif
2251 if l =~ 'RECV on.*something'
2252 let found_recv = 1
2253 endif
2254 if l =~ 'Stopping job with'
2255 let found_stop = 1
2256 endif
2257 endfor
2258 call assert_equal(1, found_test)
2259 call assert_equal(1, found_send)
2260 call assert_equal(1, found_recv)
2261 call assert_equal(1, found_stop)
2262 " On MS-Windows need to sleep for a moment to be able to delete the file.
2263 sleep 10m
2264 call delete('Xlog')
2265 endtry
2266endfunc
2267
2268" Do this last, it stops any channel log.
2269func Test_zz_ch_log()
2270 call ch_logfile('Xlog', 'w')
2271 call ch_log('hello there')
2272 call ch_log('%s%s')
2273 call ch_logfile('')
2274 let text = readfile('Xlog')
2275 call assert_match("hello there", text[1])
2276 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002277 call mkdir("Xdir1")
2278 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2279 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002280 call delete('Xlog')
2281endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002282
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002283func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002284 if has('win32')
2285 let cmd = 'cmd /c pause'
2286 else
2287 let cmd = 'grep foo'
2288 endif
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002289
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002290 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002291 sleep 50m " give the job time to start
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002292 call job_stop(g:job)
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002293 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2294
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002295 let g:job = job_start(cmd, {})
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002296 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002297 call job_stop(g:job, 'term')
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002298 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
2299
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002300 let g:job = job_start(cmd, {})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002301 sleep 50m
Bram Moolenaar18dc3552020-11-22 14:24:00 +01002302 call job_stop(g:job, 'kill')
2303 call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002304endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002305
2306func Test_issue_5485()
2307 let $VAR1 = 'global'
2308 let g:Ch_reply = ""
2309 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2310 let g:Ch_job = l:job
2311 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2312 unlet $VAR1
2313endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002314
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002315func Test_job_trailing_space_unix()
2316 CheckUnix
2317 CheckExecutable cat
Bram Moolenaarec9b0172020-06-19 19:10:59 +02002318
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002319 let job = job_start("cat ", #{in_io: 'null'})
2320 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2321 call assert_equal(0, job_info(job).exitval)
LemonBoycc766a82022-04-04 15:46:58 +01002322
2323 call delete('Xtestsocket')
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002324endfunc
2325
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002326func Test_ch_getbufnr()
2327 let ch = test_null_channel()
2328 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2329 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2330 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2331 call assert_equal(-1, ch_getbufnr(ch, ''))
2332endfunc
2333
2334" Test for unsupported options passed to ch_status()
2335func Test_invalid_job_chan_options()
2336 let ch = test_null_channel()
2337 let invalid_opts = [
2338 \ {'in_io' : 'null'},
2339 \ {'out_io' : 'null'},
2340 \ {'err_io' : 'null'},
2341 \ {'mode' : 'json'},
2342 \ {'out_mode' : 'json'},
2343 \ {'err_mode' : 'json'},
2344 \ {'noblock' : 1},
2345 \ {'in_name' : '/a/b'},
2346 \ {'pty' : 1},
2347 \ {'in_buf' : 1},
2348 \ {'out_buf' : 1},
2349 \ {'err_buf' : 1},
2350 \ {'out_modifiable' : 1},
2351 \ {'err_modifiable' : 1},
2352 \ {'out_msg' : 1},
2353 \ {'err_msg' : 1},
2354 \ {'in_top' : 1},
2355 \ {'in_bot' : 1},
2356 \ {'channel' : ch},
2357 \ {'callback' : ''},
2358 \ {'out_cb' : ''},
2359 \ {'err_cb' : ''},
2360 \ {'close_cb' : ''},
2361 \ {'exit_cb' : ''},
2362 \ {'term_opencmd' : ''},
2363 \ {'eof_chars' : ''},
2364 \ {'term_rows' : 10},
2365 \ {'term_cols' : 10},
2366 \ {'vertical' : 0},
2367 \ {'curwin' : 1},
2368 \ {'bufnr' : 1},
2369 \ {'hidden' : 0},
2370 \ {'norestore' : 0},
2371 \ {'term_kill' : 'kill'},
2372 \ {'tty_type' : ''},
2373 \ {'term_highlight' : ''},
2374 \ {'env' : {}},
2375 \ {'cwd' : ''},
2376 \ {'timeout' : 0},
2377 \ {'out_timeout' : 0},
2378 \ {'err_timeout' : 0},
2379 \ {'id' : 0},
2380 \ {'stoponexit' : ''},
2381 \ {'block_write' : 1}
2382 \ ]
2383 if has('gui')
2384 call add(invalid_opts, {'ansi_colors' : []})
2385 endif
2386
2387 for opt in invalid_opts
2388 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2389 endfor
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002390 call assert_equal('fail', ch_status(ch, test_null_dict()))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002391endfunc
2392
2393" Test for passing the command and the arguments as List on MS-Windows
2394func Test_job_with_list_args()
2395 CheckMSWindows
2396
2397 enew!
2398 let bnum = bufnr()
2399 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2400 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2401 call assert_equal('Hello World', getline(1))
2402 %bw!
2403endfunc
2404
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002405func ExitCb_cb_with_input(job, status)
2406 call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx')
2407 call assert_equal('default', Screenline(&lines))
2408 let g:wait_exit_cb = 0
2409endfunc
2410
2411func Test_cb_with_input()
2412 let g:wait_exit_cb = 1
2413
Bram Moolenaarf637bce2020-11-23 18:14:56 +01002414 if has('win32')
2415 let cmd = 'cmd /c echo "Vim''s test"'
2416 else
2417 let cmd = 'echo "Vim''s test"'
2418 endif
2419
2420 let job = job_start(cmd, {'out_cb': 'ExitCb_cb_with_input'})
2421 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002422 call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)})
2423
2424 unlet g:wait_exit_cb
2425endfunc
2426
Bram Moolenaar57942232021-08-04 20:54:55 +02002427function s:HandleBufEnter() abort
2428 let queue = []
2429 let job = job_start(['date'], {'callback': { j, d -> add(queue, d) }})
2430 while empty(queue)
2431 sleep! 10m
2432 endwhile
2433endfunction
2434
2435func Test_parse_messages_in_autocmd()
2436 CheckUnix
2437
2438 " Check that in the BufEnter autocommand events are being handled
2439 augroup bufenterjob
2440 autocmd!
2441 autocmd BufEnter Xbufenterjob call s:HandleBufEnter()
2442 augroup END
2443
2444 only
2445 split Xbufenterjob
2446 wincmd p
2447 redraw
2448
2449 close
2450 augroup bufenterjob
2451 autocmd!
2452 augroup END
2453endfunc
2454
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01002455func Test_job_start_with_invalid_argument()
2456 call assert_fails('call job_start([0zff])', 'E976:')
2457endfunc
2458
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01002459" Test for the 'lsp' channel mode
2460func LspCb(chan, msg)
2461 call add(g:lspNotif, a:msg)
2462endfunc
2463
2464func LspOtCb(chan, msg)
2465 call add(g:lspOtMsgs, a:msg)
2466endfunc
2467
2468func LspTests(port)
2469 " call ch_logfile('Xlsprpc.log', 'w')
2470 let ch = ch_open(s:localhost .. a:port, #{mode: 'lsp', callback: 'LspCb'})
2471 if ch_status(ch) == "fail"
2472 call assert_report("Can't open the lsp channel")
2473 return
2474 endif
2475
2476 " check for channel information
2477 let info = ch_info(ch)
2478 call assert_equal('LSP', info.sock_mode)
2479
2480 " Evaluate an expression
2481 let resp = ch_evalexpr(ch, #{method: 'simple-rpc', params: [10, 20]})
2482 call assert_false(empty(resp))
2483 call assert_equal(#{id: 1, jsonrpc: '2.0', result: 'simple-rpc'}, resp)
2484
2485 " Evaluate an expression. While waiting for the response, a notification
2486 " message is delivered.
2487 let g:lspNotif = []
2488 let resp = ch_evalexpr(ch, #{method: 'rpc-with-notif', params: {'v': 10}})
2489 call assert_false(empty(resp))
2490 call assert_equal(#{id: 2, jsonrpc: '2.0', result: 'rpc-with-notif-resp'},
2491 \ resp)
2492 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-with-notif-notif'}],
2493 \ g:lspNotif)
2494
2495 " Wrong payload notification test
2496 let g:lspNotif = []
2497 call ch_sendexpr(ch, #{method: 'wrong-payload', params: {}})
2498 " Send a ping to wait for all the notification messages to arrive
2499 call ch_evalexpr(ch, #{method: 'ping'})
2500 call assert_equal([#{jsonrpc: '2.0', result: 'wrong-payload'}], g:lspNotif)
2501
2502 " Test for receiving a response with incorrect 'id' and additional
2503 " notification messages while evaluating an expression.
2504 let g:lspNotif = []
2505 let resp = ch_evalexpr(ch, #{method: 'rpc-resp-incorrect-id',
2506 \ params: {'a': [1, 2]}})
2507 call assert_false(empty(resp))
2508 call assert_equal(#{id: 4, jsonrpc: '2.0',
2509 \ result: 'rpc-resp-incorrect-id-4'}, resp)
2510 call assert_equal([#{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-1'},
2511 \ #{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-2'},
2512 \ #{jsonrpc: '2.0', id: 1, result: 'rpc-resp-incorrect-id-3'}],
2513 \ g:lspNotif)
2514
2515 " simple notification test
2516 let g:lspNotif = []
2517 call ch_sendexpr(ch, #{method: 'simple-notif', params: [#{a: 10, b: []}]})
2518 " Send a ping to wait for all the notification messages to arrive
2519 call ch_evalexpr(ch, #{method: 'ping'})
2520 call assert_equal([#{jsonrpc: '2.0', result: 'simple-notif'}], g:lspNotif)
2521
2522 " multiple notifications test
2523 let g:lspNotif = []
2524 call ch_sendexpr(ch, #{method: 'multi-notif', params: [#{a: {}, b: {}}]})
2525 " Send a ping to wait for all the notification messages to arrive
2526 call ch_evalexpr(ch, #{method: 'ping'})
2527 call assert_equal([#{jsonrpc: '2.0', result: 'multi-notif1'},
2528 \ #{jsonrpc: '2.0', result: 'multi-notif2'}], g:lspNotif)
2529
2530 " Test for sending a message with an identifier.
2531 let g:lspNotif = []
2532 call ch_sendexpr(ch, #{method: 'msg-with-id', id: 93, params: #{s: 'str'}})
2533 " Send a ping to wait for all the notification messages to arrive
2534 call ch_evalexpr(ch, #{method: 'ping'})
2535 call assert_equal([#{jsonrpc: '2.0', id: 93, result: 'msg-with-id'}],
2536 \ g:lspNotif)
2537
2538 " Test for setting the 'id' value in a request message
2539 let resp = ch_evalexpr(ch, #{method: 'ping', id: 1, params: {}})
2540 call assert_equal(#{id: 8, jsonrpc: '2.0', result: 'alive'}, resp)
2541
2542 " Test for using a one time callback function to process a response
2543 let g:lspOtMsgs = []
2544 call ch_sendexpr(ch, #{method: 'msg-specifc-cb', params: {}},
2545 \ #{callback: 'LspOtCb'})
2546 call ch_evalexpr(ch, #{method: 'ping'})
2547 call assert_equal([#{id: 9, jsonrpc: '2.0', result: 'msg-specifc-cb'}],
2548 \ g:lspOtMsgs)
2549
2550 " Test for generating a request message from the other end (server)
2551 let g:lspNotif = []
2552 call ch_sendexpr(ch, #{method: 'server-req', params: #{}})
2553 call ch_evalexpr(ch, #{method: 'ping'})
2554 call assert_equal([{'id': 201, 'jsonrpc': '2.0',
2555 \ 'result': {'method': 'checkhealth', 'params': {'a': 20}}}],
2556 \ g:lspNotif)
2557
2558 " Test for sending a message without an id
2559 let g:lspNotif = []
2560 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'msg-without-id'}})
2561 " Send a ping to wait for all the notification messages to arrive
2562 call ch_evalexpr(ch, #{method: 'ping'})
2563 call assert_equal([#{jsonrpc: '2.0', result:
2564 \ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'msg-without-id'}}}],
2565 \ g:lspNotif)
2566
2567 " Test for sending a notification message with an id
2568 let g:lspNotif = []
2569 call ch_sendexpr(ch, #{method: 'echo', id: 110, params: #{s: 'msg-with-id'}})
2570 " Send a ping to wait for all the notification messages to arrive
2571 call ch_evalexpr(ch, #{method: 'ping'})
2572 call assert_equal([#{jsonrpc: '2.0', result:
2573 \ #{method: 'echo', jsonrpc: '2.0', id: 110,
2574 \ params: #{s: 'msg-with-id'}}}], g:lspNotif)
2575
2576 " Test for processing the extra fields in the HTTP header
2577 let resp = ch_evalexpr(ch, #{method: 'extra-hdr-fields', params: {}})
2578 call assert_equal({'id': 14, 'jsonrpc': '2.0', 'result': 'extra-hdr-fields'},
2579 \ resp)
2580
2581 " Test for processing a HTTP header without the Content-Length field
2582 let resp = ch_evalexpr(ch, #{method: 'hdr-without-len', params: {}},
2583 \ #{timeout: 200})
2584 call assert_equal('', resp)
2585 " send a ping to make sure communication still works
2586 let resp = ch_evalexpr(ch, #{method: 'ping'})
2587 call assert_equal({'id': 16, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
2588
2589 " Test for processing a HTTP header with wrong length
2590 let resp = ch_evalexpr(ch, #{method: 'hdr-with-wrong-len', params: {}},
2591 \ #{timeout: 200})
2592 call assert_equal('', resp)
2593 " send a ping to make sure communication still works
2594 let resp = ch_evalexpr(ch, #{method: 'ping'})
2595 call assert_equal({'id': 18, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
2596
2597 " Test for processing a HTTP header with negative length
2598 let resp = ch_evalexpr(ch, #{method: 'hdr-with-negative-len', params: {}},
2599 \ #{timeout: 200})
2600 call assert_equal('', resp)
2601 " send a ping to make sure communication still works
2602 let resp = ch_evalexpr(ch, #{method: 'ping'})
2603 call assert_equal({'id': 20, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
2604
2605 " Test for an empty header
2606 let resp = ch_evalexpr(ch, #{method: 'empty-header', params: {}},
2607 \ #{timeout: 200})
2608 call assert_equal('', resp)
2609 " send a ping to make sure communication still works
2610 let resp = ch_evalexpr(ch, #{method: 'ping'})
2611 call assert_equal({'id': 22, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
2612
2613 " Test for an empty payload
2614 let resp = ch_evalexpr(ch, #{method: 'empty-payload', params: {}},
2615 \ #{timeout: 200})
2616 call assert_equal('', resp)
2617 " send a ping to make sure communication still works
2618 let resp = ch_evalexpr(ch, #{method: 'ping'})
2619 call assert_equal({'id': 24, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
2620
2621 " Test for invoking an unsupported method
2622 let resp = ch_evalexpr(ch, #{method: 'xyz', params: {}}, #{timeout: 200})
2623 call assert_equal('', resp)
2624
2625 " Test for sending a message without a callback function. Notification
2626 " message should be dropped but RPC response should not be dropped.
2627 call ch_setoptions(ch, #{callback: ''})
2628 let g:lspNotif = []
2629 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
2630 " Send a ping to wait for all the notification messages to arrive
2631 call ch_evalexpr(ch, #{method: 'ping'})
2632 call assert_equal([], g:lspNotif)
2633 " Restore the callback function
2634 call ch_setoptions(ch, #{callback: 'LspCb'})
2635 let g:lspNotif = []
2636 call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
2637 " Send a ping to wait for all the notification messages to arrive
2638 call ch_evalexpr(ch, #{method: 'ping'})
2639 call assert_equal([#{jsonrpc: '2.0', result:
2640 \ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'no-callback'}}}],
2641 \ g:lspNotif)
2642
2643 " " Test for sending a raw message
2644 " let g:lspNotif = []
2645 " let s = "Content-Length: 62\r\n"
2646 " let s ..= "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
2647 " let s ..= "\r\n"
2648 " let s ..= '{"method":"echo","jsonrpc":"2.0","params":{"m":"raw-message"}}'
2649 " call ch_sendraw(ch, s)
2650 " call ch_evalexpr(ch, #{method: 'ping'})
2651 " call assert_equal([{'jsonrpc': '2.0',
2652 " \ 'result': {'method': 'echo', 'jsonrpc': '2.0',
2653 " \ 'params': {'m': 'raw-message'}}}], g:lspNotif)
2654
2655 " Invalid arguments to ch_evalexpr() and ch_sendexpr()
2656 call assert_fails('call ch_sendexpr(ch, #{method: "cookie", id: "cookie"})',
2657 \ 'E475:')
2658 call assert_fails('call ch_evalexpr(ch, #{method: "ping", id: [{}]})', 'E475:')
2659 call assert_fails('call ch_evalexpr(ch, [1, 2, 3])', 'E1206:')
2660 call assert_fails('call ch_sendexpr(ch, "abc")', 'E1206:')
2661 call assert_fails('call ch_evalexpr(ch, #{method: "ping"}, #{callback: "LspOtCb"})', 'E917:')
2662 " call ch_logfile('', 'w')
2663endfunc
2664
2665func Test_channel_lsp_mode()
2666 call RunServer('test_channel_lsp.py', 'LspTests', [])
2667endfunc
Bram Moolenaar57942232021-08-04 20:54:55 +02002668
Bram Moolenaar8b633132020-03-20 18:20:51 +01002669" vim: shiftwidth=2 sts=2 expandtab