blob: abc27d5eb24d19dd1474b0a50884fa6bad28052f [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 Moolenaar9a6e33a2016-02-16 19:25:12 +0100521endfunc
522
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100523func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100524 " Add a dummy close callback to avoid that messages are dropped when calling
525 " ch_canread().
526 " Also test the non-blocking option.
527 let job = job_start(s:python . " test_channel_pipe.py",
528 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
529 call assert_equal(v:t_job, type(job))
530 call assert_equal("run", job_status(job))
531
532 call assert_equal("open", ch_status(job))
533 call assert_equal("open", ch_status(job), {"part": "out"})
534
535 try
536 " Create a blob with the echo command and write it.
537 let blob = 0z00
538 let cmd = "echo something\n"
539 for i in range(0, len(cmd) - 1)
540 let blob[i] = char2nr(cmd[i])
541 endfor
542 call assert_equal(len(cmd), len(blob))
543 call ch_sendraw(job, blob)
544
545 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200546 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100547 let expected = 'something'
548 for i in range(0, len(expected) - 1)
549 call assert_equal(char2nr(expected[i]), msg[i])
550 endfor
551
552 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
553 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
554 finally
555 call job_stop(job)
556 endtry
557
558 let g:Ch_job = job
559 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
560 let info = job_info(job)
561 call assert_equal("dead", info.status)
562endfunc
563
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100564func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100565 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100566 call assert_equal("run", job_status(job))
567 try
568 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100569 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200570 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100571
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100572 call ch_sendraw(handle, "echoerr wrong\n")
573 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
574
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100575 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100576 call assert_equal("this", ch_readraw(handle))
577 call assert_equal("AND this", ch_readraw(handle))
578
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200579 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200580 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200581
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100582 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100583 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100584 finally
585 call job_stop(job)
586 endtry
587endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100588
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200589func Stop_g_job()
590 call job_stop(g:job)
591 if has('win32')
592 " On MS-Windows the server must close the file handle before we are able
593 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200594 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200595 sleep 10m
596 endif
597endfunc
598
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100599func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100600 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200601 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100602 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200603 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100604 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200605 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100606 call assert_equal("something", ch_readraw(handle))
607 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
608 call assert_equal("this", ch_readraw(handle))
609 call assert_equal("AND this", ch_readraw(handle))
610 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200611 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100612 call delete('Xinput')
613 endtry
614endfunc
615
Bram Moolenaare98d1212016-03-08 15:37:41 +0100616func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200617 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100618 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200619 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100620 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200621 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100622 call ch_sendraw(handle, "echo line one\n")
623 call ch_sendraw(handle, "echo line two\n")
624 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200625 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100626 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200627 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100628 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100629 call delete('Xoutput')
630 endtry
631endfunc
632
633func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200634 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100635 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200636 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100637 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200638 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100639 call ch_sendraw(handle, "echoerr line one\n")
640 call ch_sendraw(handle, "echoerr line two\n")
641 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200642 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100643 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200644 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call delete('Xoutput')
646 endtry
647endfunc
648
649func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200650 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100651 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200652 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100653 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200654 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100655 call ch_sendraw(handle, "echoerr line one\n")
656 call ch_sendraw(handle, "echo line two\n")
657 call ch_sendraw(handle, "double this\n")
658 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200659 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100660 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200661 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100662 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100663 call delete('Xoutput')
664 endtry
665endfunc
666
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200667func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200668 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200669endfunc
670
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200671func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200672 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200673 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200674 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100675 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100676 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200677 if a:do_msg
678 let expected[0] = 'Reading from channel output...'
679 else
680 let options['out_msg'] = 0
681 call remove(expected, 0)
682 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 else
684 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100685 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100686 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200687 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100688 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200689 if a:nomod
690 let options['out_modifiable'] = 0
691 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100692 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100693 call assert_equal("run", job_status(job))
694 try
695 let handle = job_getchannel(job)
696 call ch_sendraw(handle, "echo line one\n")
697 call ch_sendraw(handle, "echo line two\n")
698 call ch_sendraw(handle, "double this\n")
699 call ch_sendraw(handle, "quit\n")
700 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100701 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200702 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200703 if a:nomod
704 call assert_equal(0, &modifiable)
705 else
706 call assert_equal(1, &modifiable)
707 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200708 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100709 bwipe!
710 finally
711 call job_stop(job)
712 endtry
713endfunc
714
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100715func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200716 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100717endfunc
718
719func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200720 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100721endfunc
722
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200723func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200724 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200725endfunc
726
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200727func Test_pipe_to_buffer_name_nomsg()
728 call Run_test_pipe_to_buffer(1, 0, 1)
729endfunc
730
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200731func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200732 enew!
733 let test_lines = ['one', 'two']
734 call setline(1, test_lines)
735 call ch_log('Test_close_output_buffer()')
736 let options = {'out_io': 'buffer'}
737 let options['out_name'] = 'buffer-output'
738 let options['out_msg'] = 0
739 split buffer-output
740 let job = job_start(s:python . " test_channel_write.py", options)
741 call assert_equal("run", job_status(job))
742 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200743 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200744 quit!
745 sleep 100m
746 " Make sure the write didn't happen to the wrong buffer.
747 call assert_equal(test_lines, getline(1, line('$')))
748 call assert_equal(-1, bufwinnr('buffer-output'))
749 sbuf buffer-output
750 call assert_notequal(-1, bufwinnr('buffer-output'))
751 sleep 100m
752 close " no more writes
753 bwipe!
754 finally
755 call job_stop(job)
756 endtry
757endfunc
758
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200759func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100760 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200761 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100762 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100763 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200764 if a:do_msg
765 let expected[0] = 'Reading from channel error...'
766 else
767 let options['err_msg'] = 0
768 call remove(expected, 0)
769 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770 else
771 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100772 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100773 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200774 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100775 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200776 if a:nomod
777 let options['err_modifiable'] = 0
778 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100779 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100780 call assert_equal("run", job_status(job))
781 try
782 let handle = job_getchannel(job)
783 call ch_sendraw(handle, "echoerr line one\n")
784 call ch_sendraw(handle, "echoerr line two\n")
785 call ch_sendraw(handle, "doubleerr this\n")
786 call ch_sendraw(handle, "quit\n")
787 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200788 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200789 if a:nomod
790 call assert_equal(0, &modifiable)
791 else
792 call assert_equal(1, &modifiable)
793 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100794 bwipe!
795 finally
796 call job_stop(job)
797 endtry
798endfunc
799
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100800func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200801 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100802endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100803
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100804func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200805 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200806endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100807
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200808func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200809 call Run_test_pipe_err_to_buffer(1, 1, 1)
810endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100811
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200812func Test_pipe_err_to_buffer_name_nomsg()
813 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100814endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100815
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100816func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100817 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100818 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100819 call assert_equal("run", job_status(job))
820 try
821 let handle = job_getchannel(job)
822 call ch_sendraw(handle, "echo line one\n")
823 call ch_sendraw(handle, "echoerr line two\n")
824 call ch_sendraw(handle, "double this\n")
825 call ch_sendraw(handle, "doubleerr that\n")
826 call ch_sendraw(handle, "quit\n")
827 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200828 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 +0100829 bwipe!
830 finally
831 call job_stop(job)
832 endtry
833endfunc
834
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100835func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100836 sp pipe-input
837 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200838 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100839 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100840 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100841 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100842 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100843 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100844
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100845 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100846 call assert_equal("run", job_status(job))
847 try
848 let handle = job_getchannel(job)
849 call assert_equal('one', ch_read(handle))
850 call assert_equal('two', ch_read(handle))
851 call assert_equal('three', ch_read(handle))
852 bwipe!
853 finally
854 call job_stop(job)
855 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100856endfunc
857
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100858func Test_pipe_from_buffer_name()
859 call Run_test_pipe_from_buffer(1)
860endfunc
861
862func Test_pipe_from_buffer_nr()
863 call Run_test_pipe_from_buffer(0)
864endfunc
865
Bram Moolenaar0874a832016-09-01 15:11:51 +0200866func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200867 CheckExecutable sort
868
Bram Moolenaar0874a832016-09-01 15:11:51 +0200869 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
870 if a:use_buffer
871 split sortin
872 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
873 let options.in_io = 'buffer'
874 let options.in_name = 'sortin'
875 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200876 if !a:all
877 let options.in_top = 2
878 let options.in_bot = 4
879 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100880 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200881
882 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100883 call assert_equal("run", job_status(job))
884 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200885 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200886 endif
887
Bram Moolenaare2956092019-01-25 21:01:17 +0100888 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200889
Bram Moolenaard8b55492016-09-01 14:35:22 +0200890 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200891 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200892 call assert_equal('Reading from channel output...', getline(1))
893 if a:all
894 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
895 else
896 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
897 endif
898
Bram Moolenaare2956092019-01-25 21:01:17 +0100899 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200900 if a:use_buffer
901 bwipe! sortin
902 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200903 bwipe! sortout
904endfunc
905
906func Test_pipe_through_sort_all()
907 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200908 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200909endfunc
910
911func Test_pipe_through_sort_some()
912 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200913 call Run_pipe_through_sort(0, 1)
914endfunc
915
916func Test_pipe_through_sort_feed()
917 call ch_log('Test_pipe_through_sort_feed()')
918 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200919endfunc
920
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100921func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100922 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100923 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100924 call assert_equal("run", job_status(job))
925 try
926 let handle = job_getchannel(job)
927 call ch_sendraw(handle, "echo line one\n")
928 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200929 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200930 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100931 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
932 bwipe!
933 finally
934 call job_stop(job)
935 endtry
936endfunc
937
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100938func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100939 CheckFunction reltimefloat
940
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100941 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100942 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100943 call assert_equal("run", job_status(job))
944 try
945 let handle = job_getchannel(job)
946 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
947 call ch_sendraw(handle, "echo [-2, 12.34]\n")
948 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200949 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100950 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
951 bwipe!
952 finally
953 call job_stop(job)
954 endtry
955endfunc
956
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100957" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100958func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100959 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100960 if getline(line('$') - a:offset) == a:line
961 break
962 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100963 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100964 endfor
965endfunc
966
967func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100968 " Create two buffers, one to read from and one to write to.
969 split pipe-output
970 set buftype=nofile
971 split pipe-input
972 set buftype=nofile
973
974 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100975 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200976 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
977 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100978 call assert_equal("run", job_status(job))
979 try
980 exe "normal Gaecho hello\<CR>"
981 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100982 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100983 call assert_equal('hello', getline('$'))
984
985 exe bufwinnr('pipe-input') . "wincmd w"
986 exe "normal Gadouble this\<CR>"
987 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100988 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100989 call assert_equal('this', getline(line('$') - 1))
990 call assert_equal('AND this', getline('$'))
991
992 bwipe!
993 exe bufwinnr('pipe-input') . "wincmd w"
994 bwipe!
995 finally
996 call job_stop(job)
997 endtry
998endfunc
999
1000func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001001 " Create one buffer to read from and to write to.
1002 split pipe-io
1003 set buftype=nofile
1004
1005 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001006 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001007 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1008 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001009 call assert_equal("run", job_status(job))
1010 try
1011 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001012 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001013 call assert_equal('hello', getline(line('$') - 1))
1014
1015 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001016 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001017 call assert_equal('this', getline(line('$') - 2))
1018 call assert_equal('AND this', getline(line('$') - 1))
1019
1020 bwipe!
1021 finally
1022 call job_stop(job)
1023 endtry
1024endfunc
1025
Bram Moolenaar4641a122019-07-29 22:10:23 +02001026func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001027 CheckScreendump
1028
Bram Moolenaar4641a122019-07-29 22:10:23 +02001029 let lines =<< trim END
1030 new Xscrollbuffer
1031 call setline(1, range(1, 200))
1032 $
1033 redraw
1034 wincmd w
1035 call deletebufline('Xscrollbuffer', 1, '$')
1036 if has('win32')
1037 let cmd = ['cmd', '/c', 'echo sometext']
1038 else
1039 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1040 endif
1041 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1042 END
1043 call writefile(lines, 'XtestBufferScroll')
1044 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaarf386f082019-07-29 23:03:03 +02001045 call term_wait(buf, 100)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001046 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1047
1048 " clean up
1049 call StopVimInTerminal(buf)
1050 call delete('XtestBufferScroll')
1051endfunc
1052
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001053func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001054 " We cannot check that no I/O works, we only check that the job starts
1055 " properly.
1056 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001057 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001058 call assert_equal("run", job_status(job))
1059 try
1060 call assert_equal('something', ch_read(job))
1061 finally
1062 call job_stop(job)
1063 endtry
1064
1065 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001066 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001067 call assert_equal("run", job_status(job))
1068 try
1069 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1070 finally
1071 call job_stop(job)
1072 endtry
1073
1074 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001075 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001076 call assert_equal("run", job_status(job))
1077 try
1078 call assert_equal('something', ch_read(job))
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 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001085 call assert_equal("run", job_status(job))
1086 call job_stop(job)
1087
1088 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001089 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001090 call assert_equal("run", job_status(job))
1091 call assert_equal('channel fail', string(job_getchannel(job)))
1092 call assert_equal('fail', ch_status(job))
1093 call job_stop(job)
1094endfunc
1095
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001096func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001097 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1098 split testout
1099 let job = job_start([s:python, '-c',
1100 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001101 " the job may be done quickly, also accept "dead"
1102 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001103 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001104 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001105 let totlen = 0
1106 for line in getline(1, '$')
1107 call assert_equal('', substitute(line, '^\.*', '', ''))
1108 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001109 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001110 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001111 finally
1112 call job_stop(job)
1113 bwipe!
1114 endtry
1115endfunc
1116
Bram Moolenaarde279892016-03-11 22:19:44 +01001117func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001118 let job = job_start(s:python . " test_channel_pipe.py")
1119 call assert_equal("run", job_status(job))
1120 let handle = job_getchannel(job)
1121 try
1122 call ch_sendraw(handle, "echo something\n")
1123 call assert_equal("something", ch_readraw(handle))
1124 finally
1125 call job_stop(job)
1126 endtry
1127
1128 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1129 call assert_equal("run", job_status(job))
1130 let handle = job_getchannel(job)
1131 try
1132 call ch_sendraw(handle, "echo again\n")
1133 call assert_equal("again", ch_readraw(handle))
1134 finally
1135 call job_stop(job)
1136 endtry
1137endfunc
1138
Bram Moolenaar75f72652016-03-20 22:16:56 +01001139func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001140 let dict = {'thisis': 'dict: '}
1141 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001142 if type(a:msg) == v:t_string
1143 let g:Ch_outmsg = self.thisis . a:msg
1144 else
1145 let g:Ch_outobj = a:msg
1146 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001147 endfunc
1148 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001149 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001150 endfunc
1151 let job = job_start(s:python . " test_channel_pipe.py",
1152 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001153 \ 'out_mode': 'json',
1154 \ 'err_cb': dict.errHandler,
1155 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001156 call assert_equal("run", job_status(job))
1157 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001158 let g:Ch_outmsg = ''
1159 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001160 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1161 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001162 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1163 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001164
1165 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001166 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001167 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar36968392020-03-19 16:22:44 +01001168 " For unknown reason this can be very slow on Mac.
1169 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, 10000)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001170 finally
1171 call job_stop(job)
1172 endtry
1173endfunc
1174
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001175func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001176 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001177 let g:Ch_msg1 = ''
1178 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001179 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001180 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001181 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001182 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001183 let s:counter += 1
1184 endfunc
1185 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001186 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001187 let s:counter += 1
1188 endfunc
1189 let job = job_start(s:python . " test_channel_pipe.py quit now",
1190 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001191 \ 'close_cb': 'CloseHandler'})
1192 " the job may be done quickly, also accept "dead"
1193 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001194 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001195 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1196 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001197 finally
1198 call job_stop(job)
1199 delfunc OutHandler
1200 delfunc CloseHandler
1201 endtry
1202endfunc
1203
Bram Moolenaar437905c2016-04-26 19:01:05 +02001204func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001205 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001206 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001207 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001208 endfunc
1209 let job = job_start(s:python . " test_channel_pipe.py quit now",
1210 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001211 " the job may be done quickly, also accept "dead"
1212 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001213 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001214 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001215 finally
1216 call job_stop(job)
1217 delfunc CloseHandler
1218 endtry
1219endfunc
1220
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001221" Use channel in NL mode but received text does not end in NL.
1222func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001223 let g:Ch_received = ''
1224 func! CloseHandler(chan)
1225 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1226 let g:Ch_received .= ch_read(a:chan)
1227 endwhile
1228 endfunc
1229 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1230 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001231 " the job may be done quickly, also accept "dead"
1232 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001233 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001234 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001235 finally
1236 call job_stop(job)
1237 delfunc CloseHandler
1238 endtry
1239endfunc
1240
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001241func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001242 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001243 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1244 \ 'out_mode': 'json',
1245 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1246 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001247 call assert_equal("run", job_status(job))
1248 try
1249 let g:Ch_outmsg = ''
1250 let g:Ch_errmsg = ''
1251 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1252 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001253 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1254 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001255 finally
1256 call job_stop(job)
1257 endtry
1258endfunc
1259
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001260func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001261 let g:retdict = {'ret': {}}
1262 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001263 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001264 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001265 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001266 let self.ret['exit_cb'] = job_status(a:job)
1267 endfunc
1268
Bram Moolenaare2956092019-01-25 21:01:17 +01001269 let job = job_start([&shell, &shellcmdflag, 'echo'],
1270 \ {'close_cb': g:retdict.close_cb,
1271 \ 'exit_cb': g:retdict.exit_cb})
1272 " the job may be done quickly, also accept "dead"
1273 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001274 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001275 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001276 call assert_equal('dead', g:retdict.ret['exit_cb'])
1277 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001278endfunc
1279
Bram Moolenaard46ae142016-02-16 13:33:52 +01001280""""""""""
1281
Bram Moolenaar0b146882018-09-06 16:27:24 +02001282function ExitCbWipe(job, status)
1283 exe g:wipe_buf 'bw!'
1284endfunction
1285
1286" This caused a crash, because messages were handled while peeking for a
1287" character.
1288func Test_exit_cb_wipes_buf()
1289 if !has('timers')
1290 return
1291 endif
1292 set cursorline lazyredraw
1293 call test_override('redraw_flag', 1)
1294 new
1295 let g:wipe_buf = bufnr('')
1296
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001297 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001298 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001299 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1300 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1301 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1302 call timer_stop(timer)
1303
1304 set nocursorline nolazyredraw
1305 unlet g:wipe_buf
1306 call test_override('ALL', 0)
1307endfunc
1308
1309""""""""""
1310
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001311let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001312func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001313 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001314 unlet s:channelfd
1315endfunc
1316
1317" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001318func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001319 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001320 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001321 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001322endfunc
1323
1324func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001325 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001326 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001327endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001328
Bram Moolenaard46ae142016-02-16 13:33:52 +01001329""""""""""
1330
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001331let g:Ch_unletResponse = ''
1332func Ch_CloseHandler(handle, msg)
1333 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001334 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001335endfunc
1336
1337" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001338func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001339 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001340 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001341 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001342endfunc
1343
1344func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001345 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001346endfunc
1347
1348""""""""""
1349
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001350func Test_open_fail()
1351 silent! let ch = ch_open("noserver")
1352 echo ch
1353 let d = ch
1354endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001355
1356""""""""""
1357
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001358func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001359 " Wait up to a second for the port to open.
1360 let s:chopt.waittime = 1000
1361 let channel = ch_open('localhost:' . a:port, s:chopt)
1362 unlet s:chopt.waittime
1363 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001364 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001365 return
1366 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001367 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001368 call ch_close(channel)
1369endfunc
1370
1371func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001372 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001374endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001375
1376"""""""""
1377
1378function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001379 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001380endfunc
1381
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001382function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001383 let handle = ch_open('localhost:' . a:port, s:chopt)
1384 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001385 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001386 return
1387 endif
1388
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001389 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001390 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001391 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001392endfunc
1393
1394func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001395 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001396endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001397
1398"""""""""
1399
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001400let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001401function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001402 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001403endfunc
1404
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001405function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001406 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001407 let g:Ch_exit_job = g:currentJob
1408 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001409endfunc
1410
1411func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001412 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001413
Bram Moolenaarf386f082019-07-29 23:03:03 +02001414 " wait up to a second for the job to exit
1415 for i in range(100)
1416 if g:Ch_job_exit_ret == 'done'
1417 break
1418 endif
1419 sleep 10m
1420 " calling job_status() triggers the callback
1421 call job_status(g:Ch_exit_job)
1422 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001423
Bram Moolenaarf386f082019-07-29 23:03:03 +02001424 call assert_equal('done', g:Ch_job_exit_ret)
1425 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1426 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001427endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001428
Bram Moolenaar97792de2016-10-15 18:36:49 +02001429function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001430 if job_info(a:job).process == g:exit_cb_val.process
1431 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1432 endif
1433 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001434endfunction
1435
1436func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001437 CheckFunction reltimefloat
1438
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001439 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001440 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001441 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001442 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001443 let elapsed = reltimefloat(g:exit_cb_val.end)
1444 call assert_true(elapsed > 0.5)
1445 call assert_true(elapsed < 1.0)
1446
1447 " case: unreferenced job, using timer
1448 if !has('timers')
1449 return
1450 endif
1451
1452 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1453 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1454 let g:exit_cb_val.process = job_info(g:job).process
1455 unlet g:job
1456 call Standby(1000)
1457 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1458 let elapsed = reltimefloat(g:exit_cb_val.end)
1459 else
1460 let elapsed = 1.0
1461 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001462 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001463endfunc
1464
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001465"""""""""
1466
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001467let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001468function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001469 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001470endfunc
1471
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001472function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001473 let handle = ch_open('localhost:' . a:port, s:chopt)
1474 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001475 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001476 return
1477 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001478 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001479
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001480 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001481 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001482endfunc
1483
1484func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001485 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001486endfunc
1487
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001488function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001489 let handle = ch_open('localhost:' . a:port, s:chopt)
1490 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001491 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001492 return
1493 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001494 let g:Ch_d = {}
1495 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001496 let self.close_ret = 'closed'
1497 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001498 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001499
1500 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001501 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001502 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001503endfunc
1504
1505func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001506 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001507endfunc
1508
Bram Moolenaar80385682016-03-27 19:13:35 +02001509func Test_job_start_invalid()
1510 call assert_fails('call job_start($x)', 'E474:')
1511 call assert_fails('call job_start("")', 'E474:')
1512endfunc
1513
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001514func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001515 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001516 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001517 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001518 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001519 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001520 call job_stop(g:job, 'kill')
1521 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001522 endtry
1523endfunc
1524
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001525" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001526func Test_partial_in_channel_cycle()
1527 let d = {}
1528 let d.a = function('string', [d])
1529 try
1530 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1531 catch
1532 call assert_exception('E901:')
1533 endtry
1534 unlet d
1535endfunc
1536
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001537func Test_using_freed_memory()
1538 let g:a = job_start(['ls'])
1539 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001540 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001541endfunc
1542
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001543func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001544 CheckExecutable cat
1545
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001546 sp test_channel.vim
1547 let g:linecount = line('$')
1548 close
1549 split testout
1550 1,$delete
1551 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001552 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001553 bwipe!
1554endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001555
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001556func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001557 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001558 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001559
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001560 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001561 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001562 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001563 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1564 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001565
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001566 bdel test_buffer
1567 call assert_equal([], getbufline(bufnr, 1, '$'))
1568
1569 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001570 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001571 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1572 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1573
1574 bwipe! test_buffer
1575endfunc
1576
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001577func Test_cmd_parsing()
1578 if !has('unix')
1579 return
1580 endif
1581 call assert_false(filereadable("file with space"))
1582 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001583 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001584 call delete("file with space")
1585
1586 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001587 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001588 call delete("file with space")
1589endfunc
1590
Bram Moolenaar82117982016-08-27 19:21:48 +02001591func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001592 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001593
1594 " Test lines from the job containing NUL are stored correctly in a buffer.
1595 new
1596 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1597 w! Xtestread
1598 bwipe!
1599 split testout
1600 1,$delete
1601 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1602 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001603 call assert_equal("asdf\nasdf", getline(1))
1604 call assert_equal("xxx\n", getline(2))
1605 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001606
1607 call delete('Xtestread')
1608 bwipe!
1609
1610 " Test lines from a buffer with NUL bytes are written correctly to the job.
1611 new mybuffer
1612 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1613 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 +02001614 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001615 bwipe!
1616 split Xtestwrite
1617 call assert_equal("asdf\nasdf", getline(1))
1618 call assert_equal("xxx\n", getline(2))
1619 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001620 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001621
1622 call delete('Xtestwrite')
1623 bwipe!
1624endfunc
1625
Bram Moolenaarec68a992016-10-03 21:37:41 +02001626func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001627 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001628 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001629 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001630 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001631 unlet g:linecount
1632endfunc
1633
1634func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001635 func s:close_cb(ch)
1636 while ch_status(a:ch) == 'buffered'
1637 let g:out .= ch_read(a:ch)
1638 endwhile
1639 endfunc
1640
1641 let g:out = ''
1642 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1643 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1644 call WaitForAssert({-> assert_equal('123', g:out)})
1645 unlet g:out
1646 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001647endfunc
1648
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001649func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001650 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001651 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001652 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001653 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001654 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001655endfunc
1656
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001657func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001658 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001659
1660 " Check that backslash in $COMSPEC is handled properly.
1661 let g:echostr = ''
1662 let cmd = $COMSPEC . ' /c echo 123'
1663 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1664 let info = job_info(job)
1665 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1666
1667 call WaitForAssert({-> assert_equal("123", g:echostr)})
1668 unlet g:echostr
1669endfunc
1670
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001671func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001672 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001673 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001674 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001675 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001676 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001677 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001678 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1679 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001680 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001681 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001682endfunc
1683
1684func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001685 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001686 if has('win32')
1687 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001688 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001689 else
1690 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001691 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001692 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001693 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001694 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001695 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001696 let expect = substitute(expect, '[/\\]$', '', '')
1697 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1698 if $CI != '' && stridx(g:envstr, '/private/') == 0
1699 let g:envstr = g:envstr[8:]
1700 endif
1701 call assert_equal(expect, g:envstr)
1702 finally
1703 call job_stop(job)
1704 unlet g:envstr
1705 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001706endfunc
1707
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001708function Ch_test_close_lambda(port)
1709 let handle = ch_open('localhost:' . a:port, s:chopt)
1710 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001711 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001712 return
1713 endif
1714 let g:Ch_close_ret = ''
1715 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1716
1717 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001718 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001719endfunc
1720
1721func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001722 call s:run_server('Ch_test_close_lambda')
1723endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001724
1725func s:test_list_args(cmd, out, remove_lf)
1726 try
1727 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001728 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 +02001729 call WaitFor('"" != g:out')
1730 if has('win32')
1731 let g:out = substitute(g:out, '\r', '', 'g')
1732 endif
1733 if a:remove_lf
1734 let g:out = substitute(g:out, '\n$', '', 'g')
1735 endif
1736 call assert_equal(a:out, g:out)
1737 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001738 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001739 unlet g:out
1740 endtry
1741endfunc
1742
1743func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001744 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1745 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1746 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1747 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1748 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1749 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1750 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1751 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1752 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1753 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1754 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1755 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1756 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1757
1758 " tests which not contain spaces in the argument
1759 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1760 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1761 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1762 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1763 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1764 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1765 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1766 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1767 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1768endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001769
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001770func Test_keep_pty_open()
1771 if !has('unix')
1772 return
1773 endif
1774
Bram Moolenaare2956092019-01-25 21:01:17 +01001775 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1776 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001777 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1778 call assert_inrange(200, 1000, elapsed)
1779 call job_stop(job)
1780endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001781
1782func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001783 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001784 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001785
1786 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001787 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001788 endfunc
1789
1790 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001791 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001792 call Resume()
1793 endfunc
1794
1795 func TimerCb(timer)
1796 if has('win32')
1797 let cmd = ['cmd', '/c', 'echo.']
1798 else
1799 let cmd = ['echo']
1800 endif
1801 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1802 call substitute(repeat('a', 100000), '.', '', 'g')
1803 endfunc
1804
1805 " We should be interrupted before 'updatetime' elapsed.
1806 let g:val = 0
1807 call timer_start(1, 'TimerCb')
1808 let elapsed = Standby(&ut)
1809 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001810
1811 " Wait for both OutCb() and ExitCb() to have been called before deleting
1812 " them.
1813 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001814 call job_stop(g:job)
1815
1816 delfunc OutCb
1817 delfunc ExitCb
1818 delfunc TimerCb
1819 unlet! g:val
1820 unlet! g:job
1821endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001822
1823func Test_raw_large_data()
1824 try
1825 let g:out = ''
1826 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001827 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1828 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001829
Bram Moolenaare2956092019-01-25 21:01:17 +01001830 let outlen = 79999
1831 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02001832 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001833 call WaitFor({-> len(g:out) >= outlen}, 10000)
1834 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001835 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1836 finally
1837 call job_stop(job)
1838 unlet g:out
1839 endtry
1840endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001841
Bram Moolenaar65240682019-02-10 22:23:26 +01001842func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001843 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01001844
1845 try
1846 let job = job_start(s:python . " test_channel_pipe.py busy",
1847 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
1848 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
1849 finally
1850 call job_stop(job)
1851 endtry
1852endfunc
1853
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001854func Test_job_exitval_and_termsig()
1855 if !has('unix')
1856 return
1857 endif
1858
1859 " Terminate job normally
1860 let cmd = ['echo']
1861 let job = job_start(cmd)
1862 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1863 let info = job_info(job)
1864 call assert_equal(0, info.exitval)
1865 call assert_equal("", info.termsig)
1866
1867 " Terminate job by signal
1868 let cmd = ['sleep', '10']
1869 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01001870 " 10m usually works but 50m is needed when running Valgrind
1871 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001872 call job_stop(job)
1873 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1874 let info = job_info(job)
1875 call assert_equal(-1, info.exitval)
1876 call assert_equal("term", info.termsig)
1877endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01001878
1879func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001880 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01001881
1882 call writefile(['test'], 'Xtestin')
1883 let in_opts = [{},
1884 \ {'in_io': 'null'},
1885 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
1886 let out_opts = [{},
1887 \ {'out_io': 'null'},
1888 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
1889 let err_opts = [{},
1890 \ {'err_io': 'null'},
1891 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
1892 \ {'err_io': 'out'}]
1893 let opts = []
1894
1895 for in_opt in in_opts
1896 let x = copy(in_opt)
1897 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001898 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001899 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001900 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01001901 let opts += [extend({'pty': 1}, x)]
1902 endfor
1903 endfor
1904 endfor
1905
1906 for opt in opts
1907 let job = job_start('echo', opt)
1908 let info = job_info(job)
1909 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
1910 \ get(opt, 'in_io', 'tty'),
1911 \ get(opt, 'out_io', 'tty'),
1912 \ get(opt, 'err_io', 'tty'))
1913
1914 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
1915 call assert_notequal('', info.tty_in, msg)
1916 else
1917 call assert_equal('', info.tty_in, msg)
1918 endif
1919 call assert_equal(info.tty_in, info.tty_out, msg)
1920
1921 call WaitForAssert({-> assert_equal('dead', job_status(job))})
1922 endfor
1923
1924 call delete('Xtestin')
1925 call delete('Xtestout')
1926 call delete('Xtesterr')
1927endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02001928
1929" Do this last, it stops any channel log.
1930func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02001931 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001932 call ch_log('Test_zz_nl_err_to_out_pipe()')
1933 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
1934 call assert_equal("run", job_status(job))
1935 try
1936 let handle = job_getchannel(job)
1937 call ch_sendraw(handle, "echo something\n")
1938 call assert_equal("something", ch_readraw(handle))
1939
1940 call ch_sendraw(handle, "echoerr wrong\n")
1941 call assert_equal("wrong", ch_readraw(handle))
1942 finally
1943 call job_stop(job)
1944 call ch_logfile('')
1945 let loglines = readfile('Xlog')
1946 call assert_true(len(loglines) > 10)
1947 let found_test = 0
1948 let found_send = 0
1949 let found_recv = 0
1950 let found_stop = 0
1951 for l in loglines
1952 if l =~ 'Test_zz_nl_err_to_out_pipe'
1953 let found_test = 1
1954 endif
1955 if l =~ 'SEND on.*echo something'
1956 let found_send = 1
1957 endif
1958 if l =~ 'RECV on.*something'
1959 let found_recv = 1
1960 endif
1961 if l =~ 'Stopping job with'
1962 let found_stop = 1
1963 endif
1964 endfor
1965 call assert_equal(1, found_test)
1966 call assert_equal(1, found_send)
1967 call assert_equal(1, found_recv)
1968 call assert_equal(1, found_stop)
1969 " On MS-Windows need to sleep for a moment to be able to delete the file.
1970 sleep 10m
1971 call delete('Xlog')
1972 endtry
1973endfunc
1974
Bram Moolenaara27655e2019-12-21 22:22:01 +01001975func Test_empty_job()
1976 " This was crashing on MS-Windows.
Bram Moolenaarba0a7472019-12-22 16:09:06 +01001977 call assert_fails('let job = job_start([""])', 'E474:')
1978 call assert_fails('let job = job_start([" "])', 'E474:')
Bram Moolenaar7c2a2f82019-12-22 18:28:51 +01001979 call assert_fails('let job = job_start("")', 'E474:')
1980 call assert_fails('let job = job_start(" ")', 'E474:')
Bram Moolenaara27655e2019-12-21 22:22:01 +01001981endfunc
1982
Bram Moolenaarf386f082019-07-29 23:03:03 +02001983" Do this last, it stops any channel log.
1984func Test_zz_ch_log()
1985 call ch_logfile('Xlog', 'w')
1986 call ch_log('hello there')
1987 call ch_log('%s%s')
1988 call ch_logfile('')
1989 let text = readfile('Xlog')
1990 call assert_match("hello there", text[1])
1991 call assert_match("%s%s", text[2])
1992 call delete('Xlog')
1993endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01001994
1995func Test_job_start_fails()
1996 " this was leaking memory
1997 call assert_fails("call job_start([''])", "E474:")
1998endfunc
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01001999
2000func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002001 if has('win32')
2002 let cmd = 'cmd /c pause'
2003 else
2004 let cmd = 'grep foo'
2005 endif
2006 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002007 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002008 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002009 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002010 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002011 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002012 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002013 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002014 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002015 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002016 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002017 call assert_equal(-1, job_info(g:job).exitval)
2018endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002019
2020func Test_issue_5485()
2021 let $VAR1 = 'global'
2022 let g:Ch_reply = ""
2023 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2024 let g:Ch_job = l:job
2025 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2026 unlet $VAR1
2027endfunc