blob: fc8b3729ab3db0e63cc1bc7e625069fff00f320d [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 Moolenaar36968392020-03-19 16:22:44 +01001177 " For unknown reason this can be very slow on Mac.
1178 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, 10000)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001179 finally
1180 call job_stop(job)
1181 endtry
1182endfunc
1183
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001184func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001185 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001186 let g:Ch_msg1 = ''
1187 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001188 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001189 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001190 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001191 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001192 let s:counter += 1
1193 endfunc
1194 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001195 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001196 let s:counter += 1
1197 endfunc
1198 let job = job_start(s:python . " test_channel_pipe.py quit now",
1199 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001200 \ 'close_cb': 'CloseHandler'})
1201 " the job may be done quickly, also accept "dead"
1202 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001203 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001204 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1205 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001206 finally
1207 call job_stop(job)
1208 delfunc OutHandler
1209 delfunc CloseHandler
1210 endtry
1211endfunc
1212
Bram Moolenaar437905c2016-04-26 19:01:05 +02001213func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001214 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001215 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001216 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001217 endfunc
1218 let job = job_start(s:python . " test_channel_pipe.py quit now",
1219 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001220 " the job may be done quickly, also accept "dead"
1221 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001222 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001223 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001224 finally
1225 call job_stop(job)
1226 delfunc CloseHandler
1227 endtry
1228endfunc
1229
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001230" Use channel in NL mode but received text does not end in NL.
1231func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001232 let g:Ch_received = ''
1233 func! CloseHandler(chan)
1234 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1235 let g:Ch_received .= ch_read(a:chan)
1236 endwhile
1237 endfunc
1238 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1239 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001240 " the job may be done quickly, also accept "dead"
1241 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001242 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001243 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001244 finally
1245 call job_stop(job)
1246 delfunc CloseHandler
1247 endtry
1248endfunc
1249
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001250func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001251 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001252 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1253 \ 'out_mode': 'json',
1254 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1255 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001256 call assert_equal("run", job_status(job))
1257 try
1258 let g:Ch_outmsg = ''
1259 let g:Ch_errmsg = ''
1260 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1261 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001262 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1263 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001264 finally
1265 call job_stop(job)
1266 endtry
1267endfunc
1268
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001269func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001270 let g:retdict = {'ret': {}}
1271 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001272 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001273 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001274 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001275 let self.ret['exit_cb'] = job_status(a:job)
1276 endfunc
1277
Bram Moolenaare2956092019-01-25 21:01:17 +01001278 let job = job_start([&shell, &shellcmdflag, 'echo'],
1279 \ {'close_cb': g:retdict.close_cb,
1280 \ 'exit_cb': g:retdict.exit_cb})
1281 " the job may be done quickly, also accept "dead"
1282 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001283 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001284 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001285 call assert_equal('dead', g:retdict.ret['exit_cb'])
1286 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001287endfunc
1288
Bram Moolenaard46ae142016-02-16 13:33:52 +01001289""""""""""
1290
Bram Moolenaar0b146882018-09-06 16:27:24 +02001291function ExitCbWipe(job, status)
1292 exe g:wipe_buf 'bw!'
1293endfunction
1294
1295" This caused a crash, because messages were handled while peeking for a
1296" character.
1297func Test_exit_cb_wipes_buf()
1298 if !has('timers')
1299 return
1300 endif
1301 set cursorline lazyredraw
1302 call test_override('redraw_flag', 1)
1303 new
1304 let g:wipe_buf = bufnr('')
1305
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001306 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001307 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001308 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1309 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1310 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1311 call timer_stop(timer)
1312
1313 set nocursorline nolazyredraw
1314 unlet g:wipe_buf
1315 call test_override('ALL', 0)
1316endfunc
1317
1318""""""""""
1319
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001320let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001321func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001322 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001323 unlet s:channelfd
1324endfunc
1325
1326" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001327func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001328 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001329 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001330 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001331endfunc
1332
1333func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001334 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001335 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001336endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001337
Bram Moolenaard46ae142016-02-16 13:33:52 +01001338""""""""""
1339
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340let g:Ch_unletResponse = ''
1341func Ch_CloseHandler(handle, msg)
1342 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001343 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001344endfunc
1345
1346" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001347func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001348 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001349 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001350 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001351endfunc
1352
1353func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001354 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001355endfunc
1356
1357""""""""""
1358
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001359func Test_open_fail()
1360 silent! let ch = ch_open("noserver")
1361 echo ch
1362 let d = ch
1363endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001364
1365""""""""""
1366
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001367func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001368 " Wait up to a second for the port to open.
1369 let s:chopt.waittime = 1000
1370 let channel = ch_open('localhost:' . a:port, s:chopt)
1371 unlet s:chopt.waittime
1372 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001373 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001374 return
1375 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001376 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001377 call ch_close(channel)
1378endfunc
1379
1380func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001381 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001382 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001383endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001384
1385"""""""""
1386
1387function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001388 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001389endfunc
1390
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001391function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001392 let handle = ch_open('localhost:' . a:port, s:chopt)
1393 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001394 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001395 return
1396 endif
1397
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001398 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001399 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001400 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001401endfunc
1402
1403func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001404 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001405endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001406
1407"""""""""
1408
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001409let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001410function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001411 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001412endfunc
1413
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001414function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001415 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001416 let g:Ch_exit_job = g:currentJob
1417 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001418endfunc
1419
1420func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001421 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001422
Bram Moolenaarf386f082019-07-29 23:03:03 +02001423 " wait up to a second for the job to exit
1424 for i in range(100)
1425 if g:Ch_job_exit_ret == 'done'
1426 break
1427 endif
1428 sleep 10m
1429 " calling job_status() triggers the callback
1430 call job_status(g:Ch_exit_job)
1431 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001432
Bram Moolenaarf386f082019-07-29 23:03:03 +02001433 call assert_equal('done', g:Ch_job_exit_ret)
1434 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1435 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001436endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001437
Bram Moolenaar97792de2016-10-15 18:36:49 +02001438function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001439 if job_info(a:job).process == g:exit_cb_val.process
1440 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1441 endif
1442 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001443endfunction
1444
1445func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001446 CheckFunction reltimefloat
1447
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001448 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001449 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001450 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001451 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001452 let elapsed = reltimefloat(g:exit_cb_val.end)
1453 call assert_true(elapsed > 0.5)
1454 call assert_true(elapsed < 1.0)
1455
1456 " case: unreferenced job, using timer
1457 if !has('timers')
1458 return
1459 endif
1460
1461 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1462 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1463 let g:exit_cb_val.process = job_info(g:job).process
1464 unlet g:job
1465 call Standby(1000)
1466 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1467 let elapsed = reltimefloat(g:exit_cb_val.end)
1468 else
1469 let elapsed = 1.0
1470 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001471 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001472endfunc
1473
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001474"""""""""
1475
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001476let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001477function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001478 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001479endfunc
1480
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001481function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001482 let handle = ch_open('localhost:' . a:port, s:chopt)
1483 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001484 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001485 return
1486 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001487 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001488
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001489 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001490 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001491endfunc
1492
1493func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001495endfunc
1496
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001497function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001498 let handle = ch_open('localhost:' . a:port, s:chopt)
1499 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001500 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001501 return
1502 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001503 let g:Ch_d = {}
1504 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001505 let self.close_ret = 'closed'
1506 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001507 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001508
1509 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001510 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001511 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001512endfunc
1513
1514func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001515 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001516endfunc
1517
Bram Moolenaar80385682016-03-27 19:13:35 +02001518func Test_job_start_invalid()
1519 call assert_fails('call job_start($x)', 'E474:')
1520 call assert_fails('call job_start("")', 'E474:')
1521endfunc
1522
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001523func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001524 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001525 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001526 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001527 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001528 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001529 call job_stop(g:job, 'kill')
1530 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001531 endtry
1532endfunc
1533
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001534" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001535func Test_partial_in_channel_cycle()
1536 let d = {}
1537 let d.a = function('string', [d])
1538 try
1539 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1540 catch
1541 call assert_exception('E901:')
1542 endtry
1543 unlet d
1544endfunc
1545
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001546func Test_using_freed_memory()
1547 let g:a = job_start(['ls'])
1548 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001549 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001550endfunc
1551
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001552func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001553 CheckExecutable cat
1554
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001555 sp test_channel.vim
1556 let g:linecount = line('$')
1557 close
1558 split testout
1559 1,$delete
1560 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001561 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001562 bwipe!
1563endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001564
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001565func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001566 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001567 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001568
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001569 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001570 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001571 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001572 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1573 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001574
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001575 bdel test_buffer
1576 call assert_equal([], getbufline(bufnr, 1, '$'))
1577
1578 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001579 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001580 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1581 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1582
1583 bwipe! test_buffer
1584endfunc
1585
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001586func Test_cmd_parsing()
1587 if !has('unix')
1588 return
1589 endif
1590 call assert_false(filereadable("file with space"))
1591 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001592 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001593 call delete("file with space")
1594
1595 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001596 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001597 call delete("file with space")
1598endfunc
1599
Bram Moolenaar82117982016-08-27 19:21:48 +02001600func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001601 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001602
1603 " Test lines from the job containing NUL are stored correctly in a buffer.
1604 new
1605 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1606 w! Xtestread
1607 bwipe!
1608 split testout
1609 1,$delete
1610 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1611 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001612 call assert_equal("asdf\nasdf", getline(1))
1613 call assert_equal("xxx\n", getline(2))
1614 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001615
1616 call delete('Xtestread')
1617 bwipe!
1618
1619 " Test lines from a buffer with NUL bytes are written correctly to the job.
1620 new mybuffer
1621 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1622 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 +02001623 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001624 bwipe!
1625 split Xtestwrite
1626 call assert_equal("asdf\nasdf", getline(1))
1627 call assert_equal("xxx\n", getline(2))
1628 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001629 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001630
1631 call delete('Xtestwrite')
1632 bwipe!
1633endfunc
1634
Bram Moolenaarec68a992016-10-03 21:37:41 +02001635func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001636 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001637 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001638 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001639 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001640 unlet g:linecount
1641endfunc
1642
1643func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001644 func s:close_cb(ch)
1645 while ch_status(a:ch) == 'buffered'
1646 let g:out .= ch_read(a:ch)
1647 endwhile
1648 endfunc
1649
1650 let g:out = ''
1651 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1652 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1653 call WaitForAssert({-> assert_equal('123', g:out)})
1654 unlet g:out
1655 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001656endfunc
1657
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001658func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001659 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001660 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001661 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001662 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001663 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001664endfunc
1665
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001666func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001667 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001668
1669 " Check that backslash in $COMSPEC is handled properly.
1670 let g:echostr = ''
1671 let cmd = $COMSPEC . ' /c echo 123'
1672 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1673 let info = job_info(job)
1674 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1675
1676 call WaitForAssert({-> assert_equal("123", g:echostr)})
1677 unlet g:echostr
1678endfunc
1679
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001680func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001681 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001682 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001683 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001684 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001685 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001686 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001687 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1688 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001689 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001690 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001691endfunc
1692
1693func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001694 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001695 if has('win32')
1696 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001697 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001698 else
1699 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001700 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001701 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001702 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001703 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001704 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001705 let expect = substitute(expect, '[/\\]$', '', '')
1706 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1707 if $CI != '' && stridx(g:envstr, '/private/') == 0
1708 let g:envstr = g:envstr[8:]
1709 endif
1710 call assert_equal(expect, g:envstr)
1711 finally
1712 call job_stop(job)
1713 unlet g:envstr
1714 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001715endfunc
1716
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001717function Ch_test_close_lambda(port)
1718 let handle = ch_open('localhost:' . a:port, s:chopt)
1719 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001720 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001721 return
1722 endif
1723 let g:Ch_close_ret = ''
1724 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1725
1726 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001727 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001728endfunc
1729
1730func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001731 call s:run_server('Ch_test_close_lambda')
1732endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001733
1734func s:test_list_args(cmd, out, remove_lf)
1735 try
1736 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001737 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 +02001738 call WaitFor('"" != g:out')
1739 if has('win32')
1740 let g:out = substitute(g:out, '\r', '', 'g')
1741 endif
1742 if a:remove_lf
1743 let g:out = substitute(g:out, '\n$', '', 'g')
1744 endif
1745 call assert_equal(a:out, g:out)
1746 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001747 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001748 unlet g:out
1749 endtry
1750endfunc
1751
1752func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001753 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1754 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1755 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1756 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1757 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1758 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\\world'')', "hello\\world", 0)
1760 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 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("h\"ello worl\"d")', 'h"ello worl"d', 0)
1763 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1764 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1765 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1766
1767 " tests which not contain spaces in the argument
1768 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1769 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1770 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1771 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1772 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1773 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1774 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1775 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1776 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1777endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001778
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001779func Test_keep_pty_open()
1780 if !has('unix')
1781 return
1782 endif
1783
Bram Moolenaare2956092019-01-25 21:01:17 +01001784 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1785 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001786 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1787 call assert_inrange(200, 1000, elapsed)
1788 call job_stop(job)
1789endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001790
1791func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001792 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001793 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001794
1795 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001796 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001797 endfunc
1798
1799 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001800 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001801 call Resume()
1802 endfunc
1803
1804 func TimerCb(timer)
1805 if has('win32')
1806 let cmd = ['cmd', '/c', 'echo.']
1807 else
1808 let cmd = ['echo']
1809 endif
1810 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1811 call substitute(repeat('a', 100000), '.', '', 'g')
1812 endfunc
1813
1814 " We should be interrupted before 'updatetime' elapsed.
1815 let g:val = 0
1816 call timer_start(1, 'TimerCb')
1817 let elapsed = Standby(&ut)
1818 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001819
1820 " Wait for both OutCb() and ExitCb() to have been called before deleting
1821 " them.
1822 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001823 call job_stop(g:job)
1824
1825 delfunc OutCb
1826 delfunc ExitCb
1827 delfunc TimerCb
1828 unlet! g:val
1829 unlet! g:job
1830endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001831
1832func Test_raw_large_data()
1833 try
1834 let g:out = ''
1835 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001836 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1837 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001838
Bram Moolenaare2956092019-01-25 21:01:17 +01001839 let outlen = 79999
1840 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02001841 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001842 call WaitFor({-> len(g:out) >= outlen}, 10000)
1843 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001844 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1845 finally
1846 call job_stop(job)
1847 unlet g:out
1848 endtry
1849endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001850
Bram Moolenaar65240682019-02-10 22:23:26 +01001851func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001852 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01001853
1854 try
1855 let job = job_start(s:python . " test_channel_pipe.py busy",
1856 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
1857 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
1858 finally
1859 call job_stop(job)
1860 endtry
1861endfunc
1862
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001863func Test_job_exitval_and_termsig()
1864 if !has('unix')
1865 return
1866 endif
1867
1868 " Terminate job normally
1869 let cmd = ['echo']
1870 let job = job_start(cmd)
1871 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1872 let info = job_info(job)
1873 call assert_equal(0, info.exitval)
1874 call assert_equal("", info.termsig)
1875
1876 " Terminate job by signal
1877 let cmd = ['sleep', '10']
1878 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01001879 " 10m usually works but 50m is needed when running Valgrind
1880 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001881 call job_stop(job)
1882 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1883 let info = job_info(job)
1884 call assert_equal(-1, info.exitval)
1885 call assert_equal("term", info.termsig)
1886endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01001887
1888func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001889 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01001890
1891 call writefile(['test'], 'Xtestin')
1892 let in_opts = [{},
1893 \ {'in_io': 'null'},
1894 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
1895 let out_opts = [{},
1896 \ {'out_io': 'null'},
1897 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
1898 let err_opts = [{},
1899 \ {'err_io': 'null'},
1900 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
1901 \ {'err_io': 'out'}]
1902 let opts = []
1903
1904 for in_opt in in_opts
1905 let x = copy(in_opt)
1906 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001907 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001908 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001909 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001910 let opts += [extend({'pty': 1}, x)]
1911 endfor
1912 endfor
1913 endfor
1914
1915 for opt in opts
1916 let job = job_start('echo', opt)
1917 let info = job_info(job)
1918 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
1919 \ get(opt, 'in_io', 'tty'),
1920 \ get(opt, 'out_io', 'tty'),
1921 \ get(opt, 'err_io', 'tty'))
1922
1923 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
1924 call assert_notequal('', info.tty_in, msg)
1925 else
1926 call assert_equal('', info.tty_in, msg)
1927 endif
1928 call assert_equal(info.tty_in, info.tty_out, msg)
1929
1930 call WaitForAssert({-> assert_equal('dead', job_status(job))})
1931 endfor
1932
1933 call delete('Xtestin')
1934 call delete('Xtestout')
1935 call delete('Xtesterr')
1936endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02001937
1938" Do this last, it stops any channel log.
1939func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02001940 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001941 call ch_log('Test_zz_nl_err_to_out_pipe()')
1942 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
1943 call assert_equal("run", job_status(job))
1944 try
1945 let handle = job_getchannel(job)
1946 call ch_sendraw(handle, "echo something\n")
1947 call assert_equal("something", ch_readraw(handle))
1948
1949 call ch_sendraw(handle, "echoerr wrong\n")
1950 call assert_equal("wrong", ch_readraw(handle))
1951 finally
1952 call job_stop(job)
1953 call ch_logfile('')
1954 let loglines = readfile('Xlog')
1955 call assert_true(len(loglines) > 10)
1956 let found_test = 0
1957 let found_send = 0
1958 let found_recv = 0
1959 let found_stop = 0
1960 for l in loglines
1961 if l =~ 'Test_zz_nl_err_to_out_pipe'
1962 let found_test = 1
1963 endif
1964 if l =~ 'SEND on.*echo something'
1965 let found_send = 1
1966 endif
1967 if l =~ 'RECV on.*something'
1968 let found_recv = 1
1969 endif
1970 if l =~ 'Stopping job with'
1971 let found_stop = 1
1972 endif
1973 endfor
1974 call assert_equal(1, found_test)
1975 call assert_equal(1, found_send)
1976 call assert_equal(1, found_recv)
1977 call assert_equal(1, found_stop)
1978 " On MS-Windows need to sleep for a moment to be able to delete the file.
1979 sleep 10m
1980 call delete('Xlog')
1981 endtry
1982endfunc
1983
Bram Moolenaara27655e2019-12-21 22:22:01 +01001984func Test_empty_job()
1985 " This was crashing on MS-Windows.
Bram Moolenaarba0a7472019-12-22 16:09:06 +01001986 call assert_fails('let job = job_start([""])', 'E474:')
1987 call assert_fails('let job = job_start([" "])', 'E474:')
Bram Moolenaar7c2a2f82019-12-22 18:28:51 +01001988 call assert_fails('let job = job_start("")', 'E474:')
1989 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaara27655e2019-12-21 22:22:01 +01001990endfunc
1991
Bram Moolenaarf386f082019-07-29 23:03:03 +02001992" Do this last, it stops any channel log.
1993func Test_zz_ch_log()
1994 call ch_logfile('Xlog', 'w')
1995 call ch_log('hello there')
1996 call ch_log('%s%s')
1997 call ch_logfile('')
1998 let text = readfile('Xlog')
1999 call assert_match("hello there", text[1])
2000 call assert_match("%s%s", text[2])
2001 call delete('Xlog')
2002endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002003
2004func Test_job_start_fails()
2005 " this was leaking memory
2006 call assert_fails("call job_start([''])", "E474:")
2007endfunc
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002008
2009func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002010 if has('win32')
2011 let cmd = 'cmd /c pause'
2012 else
2013 let cmd = 'grep foo'
2014 endif
2015 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002016 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002017 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002018 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002019 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002020 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002021 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002022 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002023 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002024 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002025 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002026 call assert_equal(-1, job_info(g:job).exitval)
2027endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002028
2029func Test_issue_5485()
2030 let $VAR1 = 'global'
2031 let g:Ch_reply = ""
2032 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2033 let g:Ch_job = l:job
2034 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2035 unlet $VAR1
2036endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002037
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002038func Test_job_trailing_space_unix()
2039 CheckUnix
2040 CheckExecutable cat
2041 let job = job_start("cat ", #{in_io: 'null'})
2042 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2043 call assert_equal(0, job_info(job).exitval)
2044endfunc
2045
Bram Moolenaar8b633132020-03-20 18:20:51 +01002046" vim: shiftwidth=2 sts=2 expandtab