blob: a2dafb5600650defacba186d253d461dd3106b27 [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 Moolenaar321efdd2016-07-15 17:09:11 +02009
10let s:python = PythonProg()
11if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010012 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020013 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010014endif
15
Bram Moolenaar37175402017-03-18 20:18:45 +010016" Uncomment the next line to see what happens. Output is in
17" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020018" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010019" call ch_logfile('channellog', 'w')
20
Bram Moolenaare74e8e72016-02-16 22:01:30 +010021let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010022
Bram Moolenaar4b96df52020-01-26 22:00:26 +010023" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010024func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020025 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaar373a8762020-03-19 19:44:32 +010026
27 " communicating with a server can be flaky
28 let g:test_is_flaky = 1
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010029endfunc
30
Bram Moolenaar819524702018-02-27 19:10:00 +010031" Return a list of open files.
32" Can be used to make sure no resources leaked.
33" Returns an empty list on systems where this is not supported.
34func s:get_resources()
35 let pid = getpid()
36
Bram Moolenaar39536dd2019-01-29 22:58:21 +010037 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010038 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
39 elseif isdirectory('/proc/' . pid . '/fd/')
40 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
41 else
42 return []
43 endif
44endfunc
45
Bram Moolenaar321efdd2016-07-15 17:09:11 +020046let g:Ch_responseMsg = ''
47func Ch_requestHandler(handle, msg)
48 let g:Ch_responseHandle = a:handle
49 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010050endfunc
51
Bram Moolenaar321efdd2016-07-15 17:09:11 +020052func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010053 " Avoid dropping messages, since we don't use a callback here.
54 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020055 " Also add the noblock flag to try it out.
56 let s:chopt.noblock = 1
Bram Moolenaard6a8d482016-02-10 20:32:20 +010057 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010058 unlet s:chopt.drop
Bram Moolenaar0b146882018-09-06 16:27:24 +020059 unlet s:chopt.noblock
Bram Moolenaar77073442016-02-13 23:23:53 +010060 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010061 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010062 return
63 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020064
65 " check that getjob without a job is handled correctly
66 call assert_equal('no process', string(ch_getjob(handle)))
67
Bram Moolenaar570497a2019-08-22 22:55:13 +020068 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010069 call assert_true(dict.id != 0)
70 call assert_equal('open', dict.status)
71 call assert_equal(a:port, string(dict.port))
72 call assert_equal('open', dict.sock_status)
73 call assert_equal('socket', dict.sock_io)
74
Bram Moolenaard7ece102016-02-02 23:23:02 +010075 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010076 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010077
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010078 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010079 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
80 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
81 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
82
83 " split command should work
84 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020085 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010086 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010087
Bram Moolenaarf1f07922016-08-26 17:58:53 +020088 " string with ][ should work
89 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
90
Bram Moolenaar4b785f62016-11-29 21:54:44 +010091 " nothing to read now
92 call assert_equal(0, ch_canread(handle))
93
Bram Moolenaarf1f07922016-08-26 17:58:53 +020094 " sending three messages quickly then reading should work
95 for i in range(3)
96 call ch_sendexpr(handle, 'echo hello ' . i)
97 endfor
98 call assert_equal('hello 0', ch_read(handle)[1])
99 call assert_equal('hello 1', ch_read(handle)[1])
100 call assert_equal('hello 2', ch_read(handle)[1])
101
Bram Moolenaard7ece102016-02-02 23:23:02 +0100102 " Request that triggers sending two ex commands. These will usually be
103 " handled before getting the response, but it's not guaranteed, thus wait a
104 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100105 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200106 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100107 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100108
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100109 " Request command "foo bar", which fails silently.
110 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200111 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100112
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100113 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200114 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100115
Bram Moolenaara07fec92016-02-05 21:04:08 +0100116 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200117 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
118 call WaitFor('exists("g:Ch_responseHandle")')
119 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100120 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100121 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200122 call assert_equal(handle, g:Ch_responseHandle)
123 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100124 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200125 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100126
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200127 let g:Ch_responseMsg = ''
128 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
129 call WaitFor('exists("g:Ch_responseHandle")')
130 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100131 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100132 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200133 call assert_equal(handle, g:Ch_responseHandle)
134 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100135 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200136 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100137
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200138 " Using lambda.
139 let g:Ch_responseMsg = ''
140 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
141 call WaitFor('exists("g:Ch_responseHandle")')
142 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100143 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200144 else
145 call assert_equal(handle, g:Ch_responseHandle)
146 unlet g:Ch_responseHandle
147 endif
148 call assert_equal('got it', g:Ch_responseMsg)
149
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100150 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200151 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100152
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100153 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200154 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100155 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100156 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100157 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100158 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100159 call ch_setoptions(handle, {'drop': 'never'})
160 call ch_setoptions(handle, {'drop': 'auto'})
161 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100162
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100164 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100165 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100166 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100167
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100168 " Send an eval request with special characters.
169 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
170 sleep 10m
171 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
172
173 " Send an eval request to get a line with special characters.
174 call setline(3, "a\nb\<CR>c\x01d\x7fe")
175 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
176 sleep 10m
177 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
178
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100179 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100181 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100182 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100183
Bram Moolenaar55fab432016-02-07 16:53:13 +0100184 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100186 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100187 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100188
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100189 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100191 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100192 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100193
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100195 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200196 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100197 call assert_equal('one', getline(line('$') - 2))
198 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100199
200 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
202 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100203
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100204 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100205
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100206 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100207 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100208 if exists('*reltimefloat')
209 let start = reltime()
210 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
211 let elapsed = reltime(start)
212 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
213 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100214
215 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100216 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100217 let resp = ch_read(handle)
218 call assert_equal(type([]), type(resp))
219 call assert_equal(type(11), type(resp[0]))
220 call assert_equal('waited', resp[1])
221
Bram Moolenaard7ece102016-02-02 23:23:02 +0100222 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100223 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100224endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100225
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100227 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200228 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100229endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100230
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100231" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200232func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100233 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200234 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200235 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100236 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100237 return
238 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100240 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100241
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100242 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100243 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100244 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100245 return
246 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100247 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
248 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100249
250 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100251 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100252
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100253 call ch_close(newhandle)
254endfunc
255
256func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200257 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200258 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100259endfunc
260
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100261" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200262func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100263 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100264 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100265 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100266 return
267 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100268
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100269 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100270
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100271 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100272endfunc
273
274func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100275 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200276 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100277endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100278
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100279"""""""""
280
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200281func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100282 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200283 unlet g:Ch_reply
284 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100285endfunc
286
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200287func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100288 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100289 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100290 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100291 return
292 endif
293
294 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100295 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200296 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100297
298 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100299 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200300 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100301endfunc
302
303func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100304 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200305 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200306 let s:chopt.callback = 'Ch_handler'
307 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200308 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200309 let s:chopt.callback = function('Ch_handler')
310 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100311 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100312endfunc
313
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100314"""""""""
315
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200316let g:Ch_reply = ''
317func Ch_zeroHandler(chan, msg)
318 unlet g:Ch_reply
319 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320endfunc
321
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322let g:Ch_zero_reply = ''
323func Ch_oneHandler(chan, msg)
324 unlet g:Ch_zero_reply
325 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326endfunc
327
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200328func Ch_channel_zero(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200329 let handle = ('localhost:' .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100330 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100331 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100332 return
333 endif
334
335 " Check that eval works.
336 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
337
338 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100341 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200342 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100343 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100344 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200345 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100346 endif
347
348 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200349 let g:Ch_reply = ''
350 let g:Ch_zero_reply = ''
351 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200352 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100353 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200354 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100355 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200356 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100357 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100358endfunc
359
360func Test_zero_reply()
361 call ch_log('Test_zero_reply()')
362 " Run with channel handler
363 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200364 let s:chopt.callback = 'Ch_zeroHandler'
365 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100366 unlet s:chopt.callback
367
368 " Run without channel handler
369 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200370 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100371endfunc
372
373"""""""""
374
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200375let g:Ch_reply1 = ""
376func Ch_handleRaw1(chan, msg)
377 unlet g:Ch_reply1
378 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100379endfunc
380
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200381let g:Ch_reply2 = ""
382func Ch_handleRaw2(chan, msg)
383 unlet g:Ch_reply2
384 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100385endfunc
386
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200387let g:Ch_reply3 = ""
388func Ch_handleRaw3(chan, msg)
389 unlet g:Ch_reply3
390 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100391endfunc
392
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200393func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100394 let handle = ch_open('localhost:' . a:port, s:chopt)
395 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100396 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100397 return
398 endif
399 call ch_setoptions(handle, {'mode': 'raw'})
400
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100401 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200402 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200403 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200404 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
405 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200406 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100407 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200408 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100409endfunc
410
411func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100412 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200413 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100414endfunc
415
416"""""""""
417
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100418" Test that trying to connect to a non-existing port fails quickly.
419func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100420 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100421 " this is timing sensitive
422 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100423
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100424 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100425 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100426 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100427 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428 " Oops, port does exists.
429 call ch_close(handle)
430 else
431 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100432 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100433 endif
434
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100435 " We intend to use a socket that doesn't exist and wait for half a second
436 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100437 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100438 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100439 try
440 let handle = ch_open('localhost:9867', {'waittime': 500})
441 if ch_status(handle) != "fail"
442 " Oops, port does exists.
443 call ch_close(handle)
444 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100445 " Failed connection should wait about 500 msec. Can be longer if the
446 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100447 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100448 endif
449 catch
450 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100451 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100452 endif
453 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100454endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100455
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100456"""""""""
457
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100458func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100459 " Add a dummy close callback to avoid that messages are dropped when calling
460 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200461 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100462 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200463 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200464 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100465 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200466
467 call assert_equal("open", ch_status(job))
468 call assert_equal("open", ch_status(job), {"part": "out"})
469 call assert_equal("open", ch_status(job), {"part": "err"})
470 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
471 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
472
473 let dict = ch_info(job)
474 call assert_true(dict.id != 0)
475 call assert_equal('open', dict.status)
476 call assert_equal('open', dict.out_status)
477 call assert_equal('RAW', dict.out_mode)
478 call assert_equal('pipe', dict.out_io)
479 call assert_equal('open', dict.err_status)
480 call assert_equal('RAW', dict.err_mode)
481 call assert_equal('pipe', dict.err_io)
482
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100483 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100484 " For a change use the job where a channel is expected.
485 call ch_sendraw(job, "echo something\n")
486 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100487 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
488
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200490 let g:handle = job->job_getchannel()
491 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100492 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100493 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
495
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200496 let g:Ch_reply = ""
497 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200498 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200499
Bram Moolenaar570497a2019-08-22 22:55:13 +0200500 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100501 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
502 finally
503 call job_stop(job)
504 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100505
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200506 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200507 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200508 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100509 call assert_equal("dead", info.status)
510 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200511 call assert_equal(2, len(info.cmd))
512 call assert_equal("test_channel_pipe.py", info.cmd[1])
513
514 let found = 0
515 for j in job_info()
516 if j == job
517 let found += 1
518 endif
519 endfor
520 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100521
522 " Try to use the job and channel where a number is expected. This is not
523 " related to testing the raw pipe. This test is here just to reuse the
524 " already created job/channel.
525 let ch = job_getchannel(job)
526 call assert_fails('let i = job + 1', 'E910:')
527 call assert_fails('let j = ch + 1', 'E913:')
528 call assert_fails('echo 2.0 == job', 'E911:')
529 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100530endfunc
531
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100532func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100533 " Add a dummy close callback to avoid that messages are dropped when calling
534 " ch_canread().
535 " Also test the non-blocking option.
536 let job = job_start(s:python . " test_channel_pipe.py",
537 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
538 call assert_equal(v:t_job, type(job))
539 call assert_equal("run", job_status(job))
540
541 call assert_equal("open", ch_status(job))
542 call assert_equal("open", ch_status(job), {"part": "out"})
543
544 try
545 " Create a blob with the echo command and write it.
546 let blob = 0z00
547 let cmd = "echo something\n"
548 for i in range(0, len(cmd) - 1)
549 let blob[i] = char2nr(cmd[i])
550 endfor
551 call assert_equal(len(cmd), len(blob))
552 call ch_sendraw(job, blob)
553
554 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200555 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100556 let expected = 'something'
557 for i in range(0, len(expected) - 1)
558 call assert_equal(char2nr(expected[i]), msg[i])
559 endfor
560
561 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
562 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
563 finally
564 call job_stop(job)
565 endtry
566
567 let g:Ch_job = job
568 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
569 let info = job_info(job)
570 call assert_equal("dead", info.status)
571endfunc
572
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100573func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100574 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100575 call assert_equal("run", job_status(job))
576 try
577 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100578 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200579 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100580
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100581 call ch_sendraw(handle, "echoerr wrong\n")
582 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
583
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100584 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100585 call assert_equal("this", ch_readraw(handle))
586 call assert_equal("AND this", ch_readraw(handle))
587
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200588 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200589 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200590
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100591 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100592 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100593 finally
594 call job_stop(job)
595 endtry
596endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100597
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200598func Stop_g_job()
599 call job_stop(g:job)
600 if has('win32')
601 " On MS-Windows the server must close the file handle before we are able
602 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200603 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200604 sleep 10m
605 endif
606endfunc
607
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100608func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100609 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200610 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100611 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200612 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100613 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200614 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100615 call assert_equal("something", ch_readraw(handle))
616 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
617 call assert_equal("this", ch_readraw(handle))
618 call assert_equal("AND this", ch_readraw(handle))
619 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200620 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100621 call delete('Xinput')
622 endtry
623endfunc
624
Bram Moolenaare98d1212016-03-08 15:37:41 +0100625func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200626 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100627 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200628 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100629 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200630 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631 call ch_sendraw(handle, "echo line one\n")
632 call ch_sendraw(handle, "echo line two\n")
633 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200634 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200636 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100637 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100638 call delete('Xoutput')
639 endtry
640endfunc
641
642func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200643 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100644 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200645 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100646 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 call ch_sendraw(handle, "echoerr line one\n")
649 call ch_sendraw(handle, "echoerr line two\n")
650 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200651 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100652 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100654 call delete('Xoutput')
655 endtry
656endfunc
657
658func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100660 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200661 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100662 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200663 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100664 call ch_sendraw(handle, "echoerr line one\n")
665 call ch_sendraw(handle, "echo line two\n")
666 call ch_sendraw(handle, "double this\n")
667 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200668 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100669 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200670 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100671 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100672 call delete('Xoutput')
673 endtry
674endfunc
675
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200676func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200677 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200678endfunc
679
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200680func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200681 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200682 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200683 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100684 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100685 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200686 if a:do_msg
687 let expected[0] = 'Reading from channel output...'
688 else
689 let options['out_msg'] = 0
690 call remove(expected, 0)
691 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100692 else
693 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100694 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200696 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100697 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200698 if a:nomod
699 let options['out_modifiable'] = 0
700 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100701 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100702 call assert_equal("run", job_status(job))
703 try
704 let handle = job_getchannel(job)
705 call ch_sendraw(handle, "echo line one\n")
706 call ch_sendraw(handle, "echo line two\n")
707 call ch_sendraw(handle, "double this\n")
708 call ch_sendraw(handle, "quit\n")
709 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100710 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200711 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200712 if a:nomod
713 call assert_equal(0, &modifiable)
714 else
715 call assert_equal(1, &modifiable)
716 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200717 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100718 bwipe!
719 finally
720 call job_stop(job)
721 endtry
722endfunc
723
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100724func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200725 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726endfunc
727
728func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200729 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100730endfunc
731
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200732func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200733 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200734endfunc
735
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200736func Test_pipe_to_buffer_name_nomsg()
737 call Run_test_pipe_to_buffer(1, 0, 1)
738endfunc
739
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200740func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200741 enew!
742 let test_lines = ['one', 'two']
743 call setline(1, test_lines)
744 call ch_log('Test_close_output_buffer()')
745 let options = {'out_io': 'buffer'}
746 let options['out_name'] = 'buffer-output'
747 let options['out_msg'] = 0
748 split buffer-output
749 let job = job_start(s:python . " test_channel_write.py", options)
750 call assert_equal("run", job_status(job))
751 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200752 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200753 quit!
754 sleep 100m
755 " Make sure the write didn't happen to the wrong buffer.
756 call assert_equal(test_lines, getline(1, line('$')))
757 call assert_equal(-1, bufwinnr('buffer-output'))
758 sbuf buffer-output
759 call assert_notequal(-1, bufwinnr('buffer-output'))
760 sleep 100m
761 close " no more writes
762 bwipe!
763 finally
764 call job_stop(job)
765 endtry
766endfunc
767
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200768func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100769 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200770 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100771 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100772 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200773 if a:do_msg
774 let expected[0] = 'Reading from channel error...'
775 else
776 let options['err_msg'] = 0
777 call remove(expected, 0)
778 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100779 else
780 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100781 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100782 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200783 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100784 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200785 if a:nomod
786 let options['err_modifiable'] = 0
787 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100788 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100789 call assert_equal("run", job_status(job))
790 try
791 let handle = job_getchannel(job)
792 call ch_sendraw(handle, "echoerr line one\n")
793 call ch_sendraw(handle, "echoerr line two\n")
794 call ch_sendraw(handle, "doubleerr this\n")
795 call ch_sendraw(handle, "quit\n")
796 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200797 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200798 if a:nomod
799 call assert_equal(0, &modifiable)
800 else
801 call assert_equal(1, &modifiable)
802 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100803 bwipe!
804 finally
805 call job_stop(job)
806 endtry
807endfunc
808
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100809func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200810 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100811endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100812
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100813func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200814 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200815endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100816
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200817func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200818 call Run_test_pipe_err_to_buffer(1, 1, 1)
819endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100820
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200821func Test_pipe_err_to_buffer_name_nomsg()
822 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100823endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100824
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100825func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100826 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100827 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100828 call assert_equal("run", job_status(job))
829 try
830 let handle = job_getchannel(job)
831 call ch_sendraw(handle, "echo line one\n")
832 call ch_sendraw(handle, "echoerr line two\n")
833 call ch_sendraw(handle, "double this\n")
834 call ch_sendraw(handle, "doubleerr that\n")
835 call ch_sendraw(handle, "quit\n")
836 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200837 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 +0100838 bwipe!
839 finally
840 call job_stop(job)
841 endtry
842endfunc
843
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100844func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100845 sp pipe-input
846 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200847 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100848 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100849 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100850 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100851 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100852 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100853
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100854 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100855 call assert_equal("run", job_status(job))
856 try
857 let handle = job_getchannel(job)
858 call assert_equal('one', ch_read(handle))
859 call assert_equal('two', ch_read(handle))
860 call assert_equal('three', ch_read(handle))
861 bwipe!
862 finally
863 call job_stop(job)
864 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100865endfunc
866
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100867func Test_pipe_from_buffer_name()
868 call Run_test_pipe_from_buffer(1)
869endfunc
870
871func Test_pipe_from_buffer_nr()
872 call Run_test_pipe_from_buffer(0)
873endfunc
874
Bram Moolenaar0874a832016-09-01 15:11:51 +0200875func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200876 CheckExecutable sort
877
Bram Moolenaar0874a832016-09-01 15:11:51 +0200878 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
879 if a:use_buffer
880 split sortin
881 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
882 let options.in_io = 'buffer'
883 let options.in_name = 'sortin'
884 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200885 if !a:all
886 let options.in_top = 2
887 let options.in_bot = 4
888 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100889 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200890
891 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100892 call assert_equal("run", job_status(job))
893 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200894 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200895 endif
896
Bram Moolenaare2956092019-01-25 21:01:17 +0100897 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200898
Bram Moolenaard8b55492016-09-01 14:35:22 +0200899 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200900 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200901 call assert_equal('Reading from channel output...', getline(1))
902 if a:all
903 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
904 else
905 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
906 endif
907
Bram Moolenaare2956092019-01-25 21:01:17 +0100908 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909 if a:use_buffer
910 bwipe! sortin
911 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200912 bwipe! sortout
913endfunc
914
915func Test_pipe_through_sort_all()
916 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200917 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200918endfunc
919
920func Test_pipe_through_sort_some()
921 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200922 call Run_pipe_through_sort(0, 1)
923endfunc
924
925func Test_pipe_through_sort_feed()
926 call ch_log('Test_pipe_through_sort_feed()')
927 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200928endfunc
929
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100930func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100931 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100932 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100933 call assert_equal("run", job_status(job))
934 try
935 let handle = job_getchannel(job)
936 call ch_sendraw(handle, "echo line one\n")
937 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200938 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200939 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100940 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
941 bwipe!
942 finally
943 call job_stop(job)
944 endtry
945endfunc
946
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100947func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100948 CheckFunction reltimefloat
949
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100950 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100951 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100952 call assert_equal("run", job_status(job))
953 try
954 let handle = job_getchannel(job)
955 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
956 call ch_sendraw(handle, "echo [-2, 12.34]\n")
957 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200958 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100959 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
960 bwipe!
961 finally
962 call job_stop(job)
963 endtry
964endfunc
965
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100966" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100967func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100968 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100969 if getline(line('$') - a:offset) == a:line
970 break
971 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100972 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100973 endfor
974endfunc
975
976func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100977 " Create two buffers, one to read from and one to write to.
978 split pipe-output
979 set buftype=nofile
980 split pipe-input
981 set buftype=nofile
982
983 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100984 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200985 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
986 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100987 call assert_equal("run", job_status(job))
988 try
989 exe "normal Gaecho hello\<CR>"
990 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100991 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100992 call assert_equal('hello', getline('$'))
993
994 exe bufwinnr('pipe-input') . "wincmd w"
995 exe "normal Gadouble this\<CR>"
996 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100997 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100998 call assert_equal('this', getline(line('$') - 1))
999 call assert_equal('AND this', getline('$'))
1000
1001 bwipe!
1002 exe bufwinnr('pipe-input') . "wincmd w"
1003 bwipe!
1004 finally
1005 call job_stop(job)
1006 endtry
1007endfunc
1008
1009func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001010 " Create one buffer to read from and to write to.
1011 split pipe-io
1012 set buftype=nofile
1013
1014 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001015 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001016 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1017 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001018 call assert_equal("run", job_status(job))
1019 try
1020 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001021 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001022 call assert_equal('hello', getline(line('$') - 1))
1023
1024 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001025 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001026 call assert_equal('this', getline(line('$') - 2))
1027 call assert_equal('AND this', getline(line('$') - 1))
1028
1029 bwipe!
1030 finally
1031 call job_stop(job)
1032 endtry
1033endfunc
1034
Bram Moolenaar4641a122019-07-29 22:10:23 +02001035func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001036 CheckScreendump
1037
Bram Moolenaar4641a122019-07-29 22:10:23 +02001038 let lines =<< trim END
1039 new Xscrollbuffer
1040 call setline(1, range(1, 200))
1041 $
1042 redraw
1043 wincmd w
1044 call deletebufline('Xscrollbuffer', 1, '$')
1045 if has('win32')
1046 let cmd = ['cmd', '/c', 'echo sometext']
1047 else
1048 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1049 endif
1050 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1051 END
1052 call writefile(lines, 'XtestBufferScroll')
1053 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaarf386f082019-07-29 23:03:03 +02001054 call term_wait(buf, 100)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001055 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1056
1057 " clean up
1058 call StopVimInTerminal(buf)
1059 call delete('XtestBufferScroll')
1060endfunc
1061
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001062func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001063 " We cannot check that no I/O works, we only check that the job starts
1064 " properly.
1065 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001066 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001067 call assert_equal("run", job_status(job))
1068 try
1069 call assert_equal('something', ch_read(job))
1070 finally
1071 call job_stop(job)
1072 endtry
1073
1074 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001075 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001076 call assert_equal("run", job_status(job))
1077 try
1078 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1079 finally
1080 call job_stop(job)
1081 endtry
1082
1083 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001084 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001085 call assert_equal("run", job_status(job))
1086 try
1087 call assert_equal('something', ch_read(job))
1088 finally
1089 call job_stop(job)
1090 endtry
1091
1092 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001093 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001094 call assert_equal("run", job_status(job))
1095 call job_stop(job)
1096
1097 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001098 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001099 call assert_equal("run", job_status(job))
1100 call assert_equal('channel fail', string(job_getchannel(job)))
1101 call assert_equal('fail', ch_status(job))
1102 call job_stop(job)
1103endfunc
1104
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001105func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001106 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1107 split testout
1108 let job = job_start([s:python, '-c',
1109 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001110 " the job may be done quickly, also accept "dead"
1111 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001112 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001113 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001114 let totlen = 0
1115 for line in getline(1, '$')
1116 call assert_equal('', substitute(line, '^\.*', '', ''))
1117 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001118 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001119 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001120 finally
1121 call job_stop(job)
1122 bwipe!
1123 endtry
1124endfunc
1125
Bram Moolenaarde279892016-03-11 22:19:44 +01001126func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001127 let job = job_start(s:python . " test_channel_pipe.py")
1128 call assert_equal("run", job_status(job))
1129 let handle = job_getchannel(job)
1130 try
1131 call ch_sendraw(handle, "echo something\n")
1132 call assert_equal("something", ch_readraw(handle))
1133 finally
1134 call job_stop(job)
1135 endtry
1136
1137 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1138 call assert_equal("run", job_status(job))
1139 let handle = job_getchannel(job)
1140 try
1141 call ch_sendraw(handle, "echo again\n")
1142 call assert_equal("again", ch_readraw(handle))
1143 finally
1144 call job_stop(job)
1145 endtry
1146endfunc
1147
Bram Moolenaar75f72652016-03-20 22:16:56 +01001148func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001149 let dict = {'thisis': 'dict: '}
1150 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001151 if type(a:msg) == v:t_string
1152 let g:Ch_outmsg = self.thisis . a:msg
1153 else
1154 let g:Ch_outobj = a:msg
1155 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001156 endfunc
1157 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001158 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001159 endfunc
1160 let job = job_start(s:python . " test_channel_pipe.py",
1161 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001162 \ 'out_mode': 'json',
1163 \ 'err_cb': dict.errHandler,
1164 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001165 call assert_equal("run", job_status(job))
1166 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001167 let g:Ch_outmsg = ''
1168 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001169 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1170 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001171 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1172 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001173
1174 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001175 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001176 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001177 " For unknown reasons this can be very slow on Mac.
1178 if has('mac')
1179 let timeout = 20000
1180 else
1181 let timeout = 5000
1182 endif
1183 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001184 finally
1185 call job_stop(job)
1186 endtry
1187endfunc
1188
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001189func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001190 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001191 let g:Ch_msg1 = ''
1192 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001193 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001194 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001195 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001196 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001197 let s:counter += 1
1198 endfunc
1199 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001200 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001201 let s:counter += 1
1202 endfunc
1203 let job = job_start(s:python . " test_channel_pipe.py quit now",
1204 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001205 \ 'close_cb': 'CloseHandler'})
1206 " the job may be done quickly, also accept "dead"
1207 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001208 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001209 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1210 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001211 finally
1212 call job_stop(job)
1213 delfunc OutHandler
1214 delfunc CloseHandler
1215 endtry
1216endfunc
1217
Bram Moolenaar437905c2016-04-26 19:01:05 +02001218func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001219 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001220 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001221 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001222 endfunc
1223 let job = job_start(s:python . " test_channel_pipe.py quit now",
1224 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001225 " the job may be done quickly, also accept "dead"
1226 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001227 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001228 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001229 finally
1230 call job_stop(job)
1231 delfunc CloseHandler
1232 endtry
1233endfunc
1234
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001235" Use channel in NL mode but received text does not end in NL.
1236func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001237 let g:Ch_received = ''
1238 func! CloseHandler(chan)
1239 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1240 let g:Ch_received .= ch_read(a:chan)
1241 endwhile
1242 endfunc
1243 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1244 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001245 " the job may be done quickly, also accept "dead"
1246 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001247 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001248 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001249 finally
1250 call job_stop(job)
1251 delfunc CloseHandler
1252 endtry
1253endfunc
1254
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001255func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001256 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001257 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1258 \ 'out_mode': 'json',
1259 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1260 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001261 call assert_equal("run", job_status(job))
1262 try
1263 let g:Ch_outmsg = ''
1264 let g:Ch_errmsg = ''
1265 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1266 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001267 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1268 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001269 finally
1270 call job_stop(job)
1271 endtry
1272endfunc
1273
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001274func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001275 let g:retdict = {'ret': {}}
1276 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001277 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001278 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001279 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001280 let self.ret['exit_cb'] = job_status(a:job)
1281 endfunc
1282
Bram Moolenaare2956092019-01-25 21:01:17 +01001283 let job = job_start([&shell, &shellcmdflag, 'echo'],
1284 \ {'close_cb': g:retdict.close_cb,
1285 \ 'exit_cb': g:retdict.exit_cb})
1286 " the job may be done quickly, also accept "dead"
1287 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001288 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001289 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001290 call assert_equal('dead', g:retdict.ret['exit_cb'])
1291 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001292endfunc
1293
Bram Moolenaard46ae142016-02-16 13:33:52 +01001294""""""""""
1295
Bram Moolenaar0b146882018-09-06 16:27:24 +02001296function ExitCbWipe(job, status)
1297 exe g:wipe_buf 'bw!'
1298endfunction
1299
1300" This caused a crash, because messages were handled while peeking for a
1301" character.
1302func Test_exit_cb_wipes_buf()
1303 if !has('timers')
1304 return
1305 endif
1306 set cursorline lazyredraw
1307 call test_override('redraw_flag', 1)
1308 new
1309 let g:wipe_buf = bufnr('')
1310
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001311 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001312 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001313 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1314 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1315 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1316 call timer_stop(timer)
1317
1318 set nocursorline nolazyredraw
1319 unlet g:wipe_buf
1320 call test_override('ALL', 0)
1321endfunc
1322
1323""""""""""
1324
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001325let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001326func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001327 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001328 unlet s:channelfd
1329endfunc
1330
1331" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001332func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001333 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001334 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001335 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001336endfunc
1337
1338func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001339 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001341endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001342
Bram Moolenaard46ae142016-02-16 13:33:52 +01001343""""""""""
1344
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001345let g:Ch_unletResponse = ''
1346func Ch_CloseHandler(handle, msg)
1347 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001348 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001349endfunc
1350
1351" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001352func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001353 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001354 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001355 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001356endfunc
1357
1358func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001359 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001360endfunc
1361
1362""""""""""
1363
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001364func Test_open_fail()
1365 silent! let ch = ch_open("noserver")
1366 echo ch
1367 let d = ch
1368endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001369
1370""""""""""
1371
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001372func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001373 " Wait up to a second for the port to open.
1374 let s:chopt.waittime = 1000
1375 let channel = ch_open('localhost:' . a:port, s:chopt)
1376 unlet s:chopt.waittime
1377 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001378 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001379 return
1380 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001381 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001382 call ch_close(channel)
1383endfunc
1384
1385func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001386 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001387 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001388endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001389
1390"""""""""
1391
1392function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001393 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001394endfunc
1395
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001396function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001397 let handle = ch_open('localhost:' . a:port, s:chopt)
1398 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001399 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001400 return
1401 endif
1402
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001403 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001404 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001405 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001406endfunc
1407
1408func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001409 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001410endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001411
1412"""""""""
1413
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001414let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001415function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001416 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001417endfunc
1418
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001419function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001420 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001421 let g:Ch_exit_job = g:currentJob
1422 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001423endfunc
1424
1425func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001426 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001427
Bram Moolenaarf386f082019-07-29 23:03:03 +02001428 " wait up to a second for the job to exit
1429 for i in range(100)
1430 if g:Ch_job_exit_ret == 'done'
1431 break
1432 endif
1433 sleep 10m
1434 " calling job_status() triggers the callback
1435 call job_status(g:Ch_exit_job)
1436 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001437
Bram Moolenaarf386f082019-07-29 23:03:03 +02001438 call assert_equal('done', g:Ch_job_exit_ret)
1439 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1440 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001441endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001442
Bram Moolenaar97792de2016-10-15 18:36:49 +02001443function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001444 if job_info(a:job).process == g:exit_cb_val.process
1445 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1446 endif
1447 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001448endfunction
1449
1450func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001451 CheckFunction reltimefloat
1452
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001453 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001454 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001455 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001456 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001457 let elapsed = reltimefloat(g:exit_cb_val.end)
1458 call assert_true(elapsed > 0.5)
1459 call assert_true(elapsed < 1.0)
1460
1461 " case: unreferenced job, using timer
1462 if !has('timers')
1463 return
1464 endif
1465
1466 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1467 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1468 let g:exit_cb_val.process = job_info(g:job).process
1469 unlet g:job
1470 call Standby(1000)
1471 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1472 let elapsed = reltimefloat(g:exit_cb_val.end)
1473 else
1474 let elapsed = 1.0
1475 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001476 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001477endfunc
1478
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001479"""""""""
1480
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001481let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001482function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001483 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001484endfunc
1485
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001486function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001487 let handle = ch_open('localhost:' . a:port, s:chopt)
1488 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001489 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001490 return
1491 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001492 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001493
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001494 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001495 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001496endfunc
1497
1498func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001499 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001500endfunc
1501
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001502function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001503 let handle = ch_open('localhost:' . a:port, s:chopt)
1504 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001505 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001506 return
1507 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001508 let g:Ch_d = {}
1509 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001510 let self.close_ret = 'closed'
1511 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001512 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001513
1514 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001515 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001516 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001517endfunc
1518
1519func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001520 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001521endfunc
1522
Bram Moolenaar80385682016-03-27 19:13:35 +02001523func Test_job_start_invalid()
1524 call assert_fails('call job_start($x)', 'E474:')
1525 call assert_fails('call job_start("")', 'E474:')
1526endfunc
1527
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001528func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001529 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001530 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001531 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001532 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001533 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001534 call job_stop(g:job, 'kill')
1535 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001536 endtry
1537endfunc
1538
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001539" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001540func Test_partial_in_channel_cycle()
1541 let d = {}
1542 let d.a = function('string', [d])
1543 try
1544 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1545 catch
1546 call assert_exception('E901:')
1547 endtry
1548 unlet d
1549endfunc
1550
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001551func Test_using_freed_memory()
1552 let g:a = job_start(['ls'])
1553 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001554 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001555endfunc
1556
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001557func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001558 CheckExecutable cat
1559
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001560 sp test_channel.vim
1561 let g:linecount = line('$')
1562 close
1563 split testout
1564 1,$delete
1565 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001566 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001567 bwipe!
1568endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001569
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001570func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001571 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001572 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001573
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001574 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001575 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001576 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001577 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1578 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001579
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001580 bdel test_buffer
1581 call assert_equal([], getbufline(bufnr, 1, '$'))
1582
1583 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001584 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001585 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1586 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1587
1588 bwipe! test_buffer
1589endfunc
1590
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001591func Test_cmd_parsing()
1592 if !has('unix')
1593 return
1594 endif
1595 call assert_false(filereadable("file with space"))
1596 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001597 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001598 call delete("file with space")
1599
1600 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001601 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001602 call delete("file with space")
1603endfunc
1604
Bram Moolenaar82117982016-08-27 19:21:48 +02001605func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001606 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001607
1608 " Test lines from the job containing NUL are stored correctly in a buffer.
1609 new
1610 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1611 w! Xtestread
1612 bwipe!
1613 split testout
1614 1,$delete
1615 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1616 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001617 call assert_equal("asdf\nasdf", getline(1))
1618 call assert_equal("xxx\n", getline(2))
1619 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001620
1621 call delete('Xtestread')
1622 bwipe!
1623
1624 " Test lines from a buffer with NUL bytes are written correctly to the job.
1625 new mybuffer
1626 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1627 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 +02001628 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001629 bwipe!
1630 split Xtestwrite
1631 call assert_equal("asdf\nasdf", getline(1))
1632 call assert_equal("xxx\n", getline(2))
1633 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001634 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001635
1636 call delete('Xtestwrite')
1637 bwipe!
1638endfunc
1639
Bram Moolenaarec68a992016-10-03 21:37:41 +02001640func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001641 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001642 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001643 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001644 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001645 unlet g:linecount
1646endfunc
1647
1648func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001649 func s:close_cb(ch)
1650 while ch_status(a:ch) == 'buffered'
1651 let g:out .= ch_read(a:ch)
1652 endwhile
1653 endfunc
1654
1655 let g:out = ''
1656 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1657 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1658 call WaitForAssert({-> assert_equal('123', g:out)})
1659 unlet g:out
1660 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001661endfunc
1662
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001663func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001664 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001665 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001666 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001667 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001668 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001669endfunc
1670
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001671func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001672 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001673
1674 " Check that backslash in $COMSPEC is handled properly.
1675 let g:echostr = ''
1676 let cmd = $COMSPEC . ' /c echo 123'
1677 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1678 let info = job_info(job)
1679 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1680
1681 call WaitForAssert({-> assert_equal("123", g:echostr)})
1682 unlet g:echostr
1683endfunc
1684
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001685func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001686 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001687 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001688 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001689 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001690 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001691 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001692 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1693 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001694 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001695 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001696endfunc
1697
1698func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001699 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001700 if has('win32')
1701 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001702 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001703 else
1704 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001705 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001706 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001707 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001708 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001709 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001710 let expect = substitute(expect, '[/\\]$', '', '')
1711 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1712 if $CI != '' && stridx(g:envstr, '/private/') == 0
1713 let g:envstr = g:envstr[8:]
1714 endif
1715 call assert_equal(expect, g:envstr)
1716 finally
1717 call job_stop(job)
1718 unlet g:envstr
1719 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001720endfunc
1721
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001722function Ch_test_close_lambda(port)
1723 let handle = ch_open('localhost:' . a:port, s:chopt)
1724 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001725 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001726 return
1727 endif
1728 let g:Ch_close_ret = ''
1729 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1730
1731 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001732 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001733endfunc
1734
1735func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001736 call s:run_server('Ch_test_close_lambda')
1737endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001738
1739func s:test_list_args(cmd, out, remove_lf)
1740 try
1741 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001742 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 +02001743 call WaitFor('"" != g:out')
1744 if has('win32')
1745 let g:out = substitute(g:out, '\r', '', 'g')
1746 endif
1747 if a:remove_lf
1748 let g:out = substitute(g:out, '\n$', '', 'g')
1749 endif
1750 call assert_equal(a:out, g:out)
1751 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001752 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001753 unlet g:out
1754 endtry
1755endfunc
1756
1757func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001758 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1759 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1760 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1761 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1762 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1763 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1764 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1765 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1766 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1767 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1768 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1769 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1770 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1771
1772 " tests which not contain spaces in the argument
1773 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1774 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1775 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1776 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1777 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1778 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1779 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1780 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1781 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1782endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001783
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001784func Test_keep_pty_open()
1785 if !has('unix')
1786 return
1787 endif
1788
Bram Moolenaare2956092019-01-25 21:01:17 +01001789 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1790 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001791 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1792 call assert_inrange(200, 1000, elapsed)
1793 call job_stop(job)
1794endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001795
1796func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001797 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001798 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001799
1800 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001801 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001802 endfunc
1803
1804 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001805 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001806 call Resume()
1807 endfunc
1808
1809 func TimerCb(timer)
1810 if has('win32')
1811 let cmd = ['cmd', '/c', 'echo.']
1812 else
1813 let cmd = ['echo']
1814 endif
1815 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1816 call substitute(repeat('a', 100000), '.', '', 'g')
1817 endfunc
1818
1819 " We should be interrupted before 'updatetime' elapsed.
1820 let g:val = 0
1821 call timer_start(1, 'TimerCb')
1822 let elapsed = Standby(&ut)
1823 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001824
1825 " Wait for both OutCb() and ExitCb() to have been called before deleting
1826 " them.
1827 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001828 call job_stop(g:job)
1829
1830 delfunc OutCb
1831 delfunc ExitCb
1832 delfunc TimerCb
1833 unlet! g:val
1834 unlet! g:job
1835endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001836
1837func Test_raw_large_data()
1838 try
1839 let g:out = ''
1840 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001841 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1842 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001843
Bram Moolenaare2956092019-01-25 21:01:17 +01001844 let outlen = 79999
1845 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02001846 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001847 call WaitFor({-> len(g:out) >= outlen}, 10000)
1848 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001849 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1850 finally
1851 call job_stop(job)
1852 unlet g:out
1853 endtry
1854endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001855
Bram Moolenaar65240682019-02-10 22:23:26 +01001856func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001857 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01001858
1859 try
1860 let job = job_start(s:python . " test_channel_pipe.py busy",
1861 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
1862 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
1863 finally
1864 call job_stop(job)
1865 endtry
1866endfunc
1867
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001868func Test_job_exitval_and_termsig()
1869 if !has('unix')
1870 return
1871 endif
1872
1873 " Terminate job normally
1874 let cmd = ['echo']
1875 let job = job_start(cmd)
1876 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1877 let info = job_info(job)
1878 call assert_equal(0, info.exitval)
1879 call assert_equal("", info.termsig)
1880
1881 " Terminate job by signal
1882 let cmd = ['sleep', '10']
1883 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01001884 " 10m usually works but 50m is needed when running Valgrind
1885 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001886 call job_stop(job)
1887 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1888 let info = job_info(job)
1889 call assert_equal(-1, info.exitval)
1890 call assert_equal("term", info.termsig)
1891endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01001892
1893func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001894 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01001895
1896 call writefile(['test'], 'Xtestin')
1897 let in_opts = [{},
1898 \ {'in_io': 'null'},
1899 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
1900 let out_opts = [{},
1901 \ {'out_io': 'null'},
1902 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
1903 let err_opts = [{},
1904 \ {'err_io': 'null'},
1905 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
1906 \ {'err_io': 'out'}]
1907 let opts = []
1908
1909 for in_opt in in_opts
1910 let x = copy(in_opt)
1911 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001912 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001913 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001914 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001915 let opts += [extend({'pty': 1}, x)]
1916 endfor
1917 endfor
1918 endfor
1919
1920 for opt in opts
1921 let job = job_start('echo', opt)
1922 let info = job_info(job)
1923 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
1924 \ get(opt, 'in_io', 'tty'),
1925 \ get(opt, 'out_io', 'tty'),
1926 \ get(opt, 'err_io', 'tty'))
1927
1928 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
1929 call assert_notequal('', info.tty_in, msg)
1930 else
1931 call assert_equal('', info.tty_in, msg)
1932 endif
1933 call assert_equal(info.tty_in, info.tty_out, msg)
1934
1935 call WaitForAssert({-> assert_equal('dead', job_status(job))})
1936 endfor
1937
1938 call delete('Xtestin')
1939 call delete('Xtestout')
1940 call delete('Xtesterr')
1941endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02001942
1943" Do this last, it stops any channel log.
1944func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02001945 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001946 call ch_log('Test_zz_nl_err_to_out_pipe()')
1947 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
1948 call assert_equal("run", job_status(job))
1949 try
1950 let handle = job_getchannel(job)
1951 call ch_sendraw(handle, "echo something\n")
1952 call assert_equal("something", ch_readraw(handle))
1953
1954 call ch_sendraw(handle, "echoerr wrong\n")
1955 call assert_equal("wrong", ch_readraw(handle))
1956 finally
1957 call job_stop(job)
1958 call ch_logfile('')
1959 let loglines = readfile('Xlog')
1960 call assert_true(len(loglines) > 10)
1961 let found_test = 0
1962 let found_send = 0
1963 let found_recv = 0
1964 let found_stop = 0
1965 for l in loglines
1966 if l =~ 'Test_zz_nl_err_to_out_pipe'
1967 let found_test = 1
1968 endif
1969 if l =~ 'SEND on.*echo something'
1970 let found_send = 1
1971 endif
1972 if l =~ 'RECV on.*something'
1973 let found_recv = 1
1974 endif
1975 if l =~ 'Stopping job with'
1976 let found_stop = 1
1977 endif
1978 endfor
1979 call assert_equal(1, found_test)
1980 call assert_equal(1, found_send)
1981 call assert_equal(1, found_recv)
1982 call assert_equal(1, found_stop)
1983 " On MS-Windows need to sleep for a moment to be able to delete the file.
1984 sleep 10m
1985 call delete('Xlog')
1986 endtry
1987endfunc
1988
Bram Moolenaara27655e2019-12-21 22:22:01 +01001989func Test_empty_job()
1990 " This was crashing on MS-Windows.
Bram Moolenaarba0a7472019-12-22 16:09:06 +01001991 call assert_fails('let job = job_start([""])', 'E474:')
1992 call assert_fails('let job = job_start([" "])', 'E474:')
Bram Moolenaar7c2a2f82019-12-22 18:28:51 +01001993 call assert_fails('let job = job_start("")', 'E474:')
1994 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaara27655e2019-12-21 22:22:01 +01001995endfunc
1996
Bram Moolenaarf386f082019-07-29 23:03:03 +02001997" Do this last, it stops any channel log.
1998func Test_zz_ch_log()
1999 call ch_logfile('Xlog', 'w')
2000 call ch_log('hello there')
2001 call ch_log('%s%s')
2002 call ch_logfile('')
2003 let text = readfile('Xlog')
2004 call assert_match("hello there", text[1])
2005 call assert_match("%s%s", text[2])
2006 call delete('Xlog')
2007endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002008
2009func Test_job_start_fails()
2010 " this was leaking memory
2011 call assert_fails("call job_start([''])", "E474:")
2012endfunc
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002013
2014func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002015 if has('win32')
2016 let cmd = 'cmd /c pause'
2017 else
2018 let cmd = 'grep foo'
2019 endif
2020 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002021 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002022 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002023 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002024 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002025 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002026 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002027 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002028 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002029 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002030 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002031 call assert_equal(-1, job_info(g:job).exitval)
2032endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002033
2034func Test_issue_5485()
2035 let $VAR1 = 'global'
2036 let g:Ch_reply = ""
2037 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2038 let g:Ch_job = l:job
2039 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2040 unlet $VAR1
2041endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002042
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002043func Test_job_trailing_space_unix()
2044 CheckUnix
2045 CheckExecutable cat
2046 let job = job_start("cat ", #{in_io: 'null'})
2047 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2048 call assert_equal(0, job_info(job).exitval)
2049endfunc
2050
Bram Moolenaar8b633132020-03-20 18:20:51 +01002051" vim: shiftwidth=2 sts=2 expandtab