blob: 371afc79975650e8c9c3b69dc41d5e33e827da18 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaare2469252016-02-04 10:54:34 +01003if !has('channel')
4 finish
5endif
6
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
8
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010011 " Can't run this test without Python.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaar37175402017-03-18 20:18:45 +010015" Uncomment the next line to see what happens. Output is in
16" src/testdir/channellog.
17" call ch_logfile('channellog', 'w')
18
Bram Moolenaare74e8e72016-02-16 22:01:30 +010019let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010020
Bram Moolenaard6a8d482016-02-10 20:32:20 +010021" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010022func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020023 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010024endfunc
25
Bram Moolenaar819524702018-02-27 19:10:00 +010026" Return a list of open files.
27" Can be used to make sure no resources leaked.
28" Returns an empty list on systems where this is not supported.
29func s:get_resources()
30 let pid = getpid()
31
32 if has('mac')
33 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
34 elseif isdirectory('/proc/' . pid . '/fd/')
35 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
36 else
37 return []
38 endif
39endfunc
40
Bram Moolenaar321efdd2016-07-15 17:09:11 +020041let g:Ch_responseMsg = ''
42func Ch_requestHandler(handle, msg)
43 let g:Ch_responseHandle = a:handle
44 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010045endfunc
46
Bram Moolenaar321efdd2016-07-15 17:09:11 +020047func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010048 " Avoid dropping messages, since we don't use a callback here.
49 let s:chopt.drop = 'never'
Bram Moolenaard6a8d482016-02-10 20:32:20 +010050 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010051 unlet s:chopt.drop
Bram Moolenaar77073442016-02-13 23:23:53 +010052 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010053 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010054 return
55 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010056 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010057 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010058 call assert_equal('no process', string(ch_getjob(handle)))
59 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010060 let dict = ch_info(handle)
61 call assert_true(dict.id != 0)
62 call assert_equal('open', dict.status)
63 call assert_equal(a:port, string(dict.port))
64 call assert_equal('open', dict.sock_status)
65 call assert_equal('socket', dict.sock_io)
66
Bram Moolenaard7ece102016-02-02 23:23:02 +010067 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010068 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010069
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010070 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010071 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
72 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
73 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
74
75 " split command should work
76 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020077 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010078 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010079
Bram Moolenaarf1f07922016-08-26 17:58:53 +020080 " string with ][ should work
81 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
82
Bram Moolenaar4b785f62016-11-29 21:54:44 +010083 " nothing to read now
84 call assert_equal(0, ch_canread(handle))
85
Bram Moolenaarf1f07922016-08-26 17:58:53 +020086 " sending three messages quickly then reading should work
87 for i in range(3)
88 call ch_sendexpr(handle, 'echo hello ' . i)
89 endfor
90 call assert_equal('hello 0', ch_read(handle)[1])
91 call assert_equal('hello 1', ch_read(handle)[1])
92 call assert_equal('hello 2', ch_read(handle)[1])
93
Bram Moolenaard7ece102016-02-02 23:23:02 +010094 " Request that triggers sending two ex commands. These will usually be
95 " handled before getting the response, but it's not guaranteed, thus wait a
96 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010097 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +020098 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +010099 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100100
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100101 " Request command "foo bar", which fails silently.
102 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200103 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100104
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100105 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200106 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100107
Bram Moolenaara07fec92016-02-05 21:04:08 +0100108 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200109 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
110 call WaitFor('exists("g:Ch_responseHandle")')
111 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100112 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100113 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200114 call assert_equal(handle, g:Ch_responseHandle)
115 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100116 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200117 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100118
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200119 let g:Ch_responseMsg = ''
120 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
121 call WaitFor('exists("g:Ch_responseHandle")')
122 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100123 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100124 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200125 call assert_equal(handle, g:Ch_responseHandle)
126 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100127 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200128 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100129
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200130 " Using lambda.
131 let g:Ch_responseMsg = ''
132 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
133 call WaitFor('exists("g:Ch_responseHandle")')
134 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100135 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200136 else
137 call assert_equal(handle, g:Ch_responseHandle)
138 unlet g:Ch_responseHandle
139 endif
140 call assert_equal('got it', g:Ch_responseMsg)
141
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100142 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200143 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100144
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100145 " check setting options (without testing the effect)
146 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100147 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100148 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100149 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100150 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100151 call ch_setoptions(handle, {'drop': 'never'})
152 call ch_setoptions(handle, {'drop': 'auto'})
153 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100154
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100155 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100156 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100157 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100158 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100159
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100160 " Send an eval request with special characters.
161 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
162 sleep 10m
163 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
164
165 " Send an eval request to get a line with special characters.
166 call setline(3, "a\nb\<CR>c\x01d\x7fe")
167 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
168 sleep 10m
169 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
170
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100171 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100173 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100174 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100175
Bram Moolenaar55fab432016-02-07 16:53:13 +0100176 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100178 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100179 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100180
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100181 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100183 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100184 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100185
Bram Moolenaarf4160862016-02-05 23:09:12 +0100186 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200188 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100189 call assert_equal('one', getline(line('$') - 2))
190 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100191
192 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100193 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
194 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100195
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100197
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100198 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100199 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
200 let start = reltime()
201 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
202 let elapsed = reltime(start)
203 call assert_true(reltimefloat(elapsed) > 0.3)
204 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100205
206 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100207 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100208 let resp = ch_read(handle)
209 call assert_equal(type([]), type(resp))
210 call assert_equal(type(11), type(resp[0]))
211 call assert_equal('waited', resp[1])
212
Bram Moolenaard7ece102016-02-02 23:23:02 +0100213 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100214 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100215endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100216
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100217func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100218 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200219 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100220endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100221
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100222" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200223func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100224 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200225 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100226 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100227 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100228 return
229 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100230
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100231 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100232
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100233 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100234 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100235 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236 return
237 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100238 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
239 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100240
241 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100242 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100243
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100244 call ch_close(newhandle)
245endfunc
246
247func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100248 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200249 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100250endfunc
251
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100252" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200253func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100254 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100255 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100256 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100257 return
258 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100259
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100260 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100261
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100262 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100263endfunc
264
265func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100266 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200267 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100268endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100269
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100270"""""""""
271
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200272func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100273 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200274 unlet g:Ch_reply
275 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100276endfunc
277
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200278func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100279 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100280 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100281 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100282 return
283 endif
284
285 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100286 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200287 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100288
289 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100290 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200291 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292endfunc
293
294func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100295 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200296 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200297 let s:chopt.callback = 'Ch_handler'
298 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200299 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200300 let s:chopt.callback = function('Ch_handler')
301 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100302 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100303endfunc
304
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100305"""""""""
306
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200307let g:Ch_reply = ''
308func Ch_zeroHandler(chan, msg)
309 unlet g:Ch_reply
310 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100311endfunc
312
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200313let g:Ch_zero_reply = ''
314func Ch_oneHandler(chan, msg)
315 unlet g:Ch_zero_reply
316 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100317endfunc
318
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200319func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320 let handle = ch_open('localhost:' . a:port, s:chopt)
321 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100322 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 return
324 endif
325
326 " Check that eval works.
327 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
328
329 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200330 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100331 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100332 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200333 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100334 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100335 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200336 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 endif
338
339 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200340 let g:Ch_reply = ''
341 let g:Ch_zero_reply = ''
342 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200343 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100344 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200345 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100346 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200347 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100348 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100349endfunc
350
351func Test_zero_reply()
352 call ch_log('Test_zero_reply()')
353 " Run with channel handler
354 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200355 let s:chopt.callback = 'Ch_zeroHandler'
356 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100357 unlet s:chopt.callback
358
359 " Run without channel handler
360 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200361 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100362endfunc
363
364"""""""""
365
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200366let g:Ch_reply1 = ""
367func Ch_handleRaw1(chan, msg)
368 unlet g:Ch_reply1
369 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100370endfunc
371
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200372let g:Ch_reply2 = ""
373func Ch_handleRaw2(chan, msg)
374 unlet g:Ch_reply2
375 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100376endfunc
377
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200378let g:Ch_reply3 = ""
379func Ch_handleRaw3(chan, msg)
380 unlet g:Ch_reply3
381 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100382endfunc
383
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200384func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100385 let handle = ch_open('localhost:' . a:port, s:chopt)
386 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100387 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100388 return
389 endif
390 call ch_setoptions(handle, {'mode': 'raw'})
391
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100392 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200393 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200394 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200395 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
396 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200397 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100398 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200399 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100400endfunc
401
402func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100403 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200404 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100405endfunc
406
407"""""""""
408
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100409" Test that trying to connect to a non-existing port fails quickly.
410func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100411 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100412 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100413 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100414 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100415 " Oops, port does exists.
416 call ch_close(handle)
417 else
418 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100419 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100420 endif
421
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100422 " We intend to use a socket that doesn't exist and wait for half a second
423 " before giving up. If the socket does exist it can fail in various ways.
424 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100425 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100426 try
427 let handle = ch_open('localhost:9867', {'waittime': 500})
428 if ch_status(handle) != "fail"
429 " Oops, port does exists.
430 call ch_close(handle)
431 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100432 " Failed connection should wait about 500 msec. Can be longer if the
433 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100434 let elapsed = reltime(start)
435 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100436 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 endif
438 catch
439 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100440 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100441 endif
442 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100443endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100444
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100445"""""""""
446
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100447func Test_raw_pipe()
448 if !has('job')
449 return
450 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100451 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100452 " Add a dummy close callback to avoid that messages are dropped when calling
453 " ch_canread().
454 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100455 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200456 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100457 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200458
459 call assert_equal("open", ch_status(job))
460 call assert_equal("open", ch_status(job), {"part": "out"})
461 call assert_equal("open", ch_status(job), {"part": "err"})
462 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
463 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
464
465 let dict = ch_info(job)
466 call assert_true(dict.id != 0)
467 call assert_equal('open', dict.status)
468 call assert_equal('open', dict.out_status)
469 call assert_equal('RAW', dict.out_mode)
470 call assert_equal('pipe', dict.out_io)
471 call assert_equal('open', dict.err_status)
472 call assert_equal('RAW', dict.err_mode)
473 call assert_equal('pipe', dict.err_io)
474
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100475 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100476 " For a change use the job where a channel is expected.
477 call ch_sendraw(job, "echo something\n")
478 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100479 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
480
Bram Moolenaar151f6562016-03-07 21:19:38 +0100481 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100482 let g:handle = job_getchannel(job)
483 call WaitFor('ch_canread(g:handle)')
484 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100485 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100486 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
487
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200488 let g:Ch_reply = ""
489 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200490 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200491
Bram Moolenaar151f6562016-03-07 21:19:38 +0100492 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100493 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
494 finally
495 call job_stop(job)
496 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100497
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200498 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200499 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar8950a562016-03-12 15:22:55 +0100500 let info = job_info(job)
501 call assert_equal("dead", info.status)
502 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200503 call assert_equal(2, len(info.cmd))
504 call assert_equal("test_channel_pipe.py", info.cmd[1])
505
506 let found = 0
507 for j in job_info()
508 if j == job
509 let found += 1
510 endif
511 endfor
512 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100513endfunc
514
515func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100516 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100517 return
518 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100519 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100520 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100521 call assert_equal("run", job_status(job))
522 try
523 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100524 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100525 call assert_equal("something", ch_readraw(handle))
526
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100527 call ch_sendraw(handle, "echoerr wrong\n")
528 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
529
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100530 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100531 call assert_equal("this", ch_readraw(handle))
532 call assert_equal("AND this", ch_readraw(handle))
533
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200534 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar620ca2d2017-12-09 19:13:13 +0100535 call assert_equal("this linethis linethis line", ch_read(handle))
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200536
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100537 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100538 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100539 finally
540 call job_stop(job)
541 endtry
542endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100543
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100544func Test_nl_err_to_out_pipe()
545 if !has('job')
546 return
547 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100548 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100549 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100550 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100551 call assert_equal("run", job_status(job))
552 try
553 let handle = job_getchannel(job)
554 call ch_sendraw(handle, "echo something\n")
555 call assert_equal("something", ch_readraw(handle))
556
557 call ch_sendraw(handle, "echoerr wrong\n")
558 call assert_equal("wrong", ch_readraw(handle))
559 finally
560 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100561 call ch_logfile('')
562 let loglines = readfile('Xlog')
563 call assert_true(len(loglines) > 10)
564 let found_test = 0
565 let found_send = 0
566 let found_recv = 0
567 let found_stop = 0
568 for l in loglines
569 if l =~ 'Test_nl_err_to_out_pipe'
570 let found_test = 1
571 endif
572 if l =~ 'SEND on.*echo something'
573 let found_send = 1
574 endif
575 if l =~ 'RECV on.*something'
576 let found_recv = 1
577 endif
578 if l =~ 'Stopping job with'
579 let found_stop = 1
580 endif
581 endfor
582 call assert_equal(1, found_test)
583 call assert_equal(1, found_send)
584 call assert_equal(1, found_recv)
585 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200586 " On MS-Windows need to sleep for a moment to be able to delete the file.
587 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100588 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100589 endtry
590endfunc
591
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200592func Stop_g_job()
593 call job_stop(g:job)
594 if has('win32')
595 " On MS-Windows the server must close the file handle before we are able
596 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200597 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200598 sleep 10m
599 endif
600endfunc
601
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100602func Test_nl_read_file()
603 if !has('job')
604 return
605 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100606 call ch_log('Test_nl_read_file()')
607 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200608 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100609 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200610 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100611 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200612 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100613 call assert_equal("something", ch_readraw(handle))
614 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
615 call assert_equal("this", ch_readraw(handle))
616 call assert_equal("AND this", ch_readraw(handle))
617 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200618 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100619 call delete('Xinput')
620 endtry
621endfunc
622
Bram Moolenaare98d1212016-03-08 15:37:41 +0100623func Test_nl_write_out_file()
624 if !has('job')
625 return
626 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100627 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200628 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100629 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200630 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200632 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100633 call ch_sendraw(handle, "echo line one\n")
634 call ch_sendraw(handle, "echo line two\n")
635 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200636 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100637 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200638 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100639 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100640 call delete('Xoutput')
641 endtry
642endfunc
643
644func Test_nl_write_err_file()
645 if !has('job')
646 return
647 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200649 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100650 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200651 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100652 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100654 call ch_sendraw(handle, "echoerr line one\n")
655 call ch_sendraw(handle, "echoerr line two\n")
656 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200657 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100658 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100660 call delete('Xoutput')
661 endtry
662endfunc
663
664func Test_nl_write_both_file()
665 if !has('job')
666 return
667 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100668 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200669 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100670 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200671 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100672 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200673 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100674 call ch_sendraw(handle, "echoerr line one\n")
675 call ch_sendraw(handle, "echo line two\n")
676 call ch_sendraw(handle, "double this\n")
677 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200678 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100679 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200680 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100681 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100682 call delete('Xoutput')
683 endtry
684endfunc
685
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200686func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200687 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200688endfunc
689
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200690func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100691 if !has('job')
692 return
693 endif
694 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200695 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200696 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200697 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100698 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100699 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200700 if a:do_msg
701 let expected[0] = 'Reading from channel output...'
702 else
703 let options['out_msg'] = 0
704 call remove(expected, 0)
705 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100706 else
707 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100708 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100709 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200710 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100711 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200712 if a:nomod
713 let options['out_modifiable'] = 0
714 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100715 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100716 call assert_equal("run", job_status(job))
717 try
718 let handle = job_getchannel(job)
719 call ch_sendraw(handle, "echo line one\n")
720 call ch_sendraw(handle, "echo line two\n")
721 call ch_sendraw(handle, "double this\n")
722 call ch_sendraw(handle, "quit\n")
723 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100724 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200725 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200726 if a:nomod
727 call assert_equal(0, &modifiable)
728 else
729 call assert_equal(1, &modifiable)
730 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200731 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100732 bwipe!
733 finally
734 call job_stop(job)
735 endtry
736endfunc
737
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100738func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200739 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100740endfunc
741
742func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200743 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100744endfunc
745
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200746func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200747 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200748endfunc
749
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200750func Test_pipe_to_buffer_name_nomsg()
751 call Run_test_pipe_to_buffer(1, 0, 1)
752endfunc
753
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200754func Test_close_output_buffer()
755 if !has('job')
756 return
757 endif
758 enew!
759 let test_lines = ['one', 'two']
760 call setline(1, test_lines)
761 call ch_log('Test_close_output_buffer()')
762 let options = {'out_io': 'buffer'}
763 let options['out_name'] = 'buffer-output'
764 let options['out_msg'] = 0
765 split buffer-output
766 let job = job_start(s:python . " test_channel_write.py", options)
767 call assert_equal("run", job_status(job))
768 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200769 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200770 quit!
771 sleep 100m
772 " Make sure the write didn't happen to the wrong buffer.
773 call assert_equal(test_lines, getline(1, line('$')))
774 call assert_equal(-1, bufwinnr('buffer-output'))
775 sbuf buffer-output
776 call assert_notequal(-1, bufwinnr('buffer-output'))
777 sleep 100m
778 close " no more writes
779 bwipe!
780 finally
781 call job_stop(job)
782 endtry
783endfunc
784
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200785func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100786 if !has('job')
787 return
788 endif
789 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100790 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200791 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100792 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100793 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200794 if a:do_msg
795 let expected[0] = 'Reading from channel error...'
796 else
797 let options['err_msg'] = 0
798 call remove(expected, 0)
799 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100800 else
801 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100802 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100803 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200804 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100805 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200806 if a:nomod
807 let options['err_modifiable'] = 0
808 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100809 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100810 call assert_equal("run", job_status(job))
811 try
812 let handle = job_getchannel(job)
813 call ch_sendraw(handle, "echoerr line one\n")
814 call ch_sendraw(handle, "echoerr line two\n")
815 call ch_sendraw(handle, "doubleerr this\n")
816 call ch_sendraw(handle, "quit\n")
817 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200818 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200819 if a:nomod
820 call assert_equal(0, &modifiable)
821 else
822 call assert_equal(1, &modifiable)
823 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100824 bwipe!
825 finally
826 call job_stop(job)
827 endtry
828endfunc
829
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100830func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200831 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100832endfunc
833
834func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200835 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200836endfunc
837
838func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200839 call Run_test_pipe_err_to_buffer(1, 1, 1)
840endfunc
841
842func Test_pipe_err_to_buffer_name_nomsg()
843 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100844endfunc
845
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100846func Test_pipe_both_to_buffer()
847 if !has('job')
848 return
849 endif
850 call ch_log('Test_pipe_both_to_buffer()')
851 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100852 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100853 call assert_equal("run", job_status(job))
854 try
855 let handle = job_getchannel(job)
856 call ch_sendraw(handle, "echo line one\n")
857 call ch_sendraw(handle, "echoerr line two\n")
858 call ch_sendraw(handle, "double this\n")
859 call ch_sendraw(handle, "doubleerr that\n")
860 call ch_sendraw(handle, "quit\n")
861 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200862 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 +0100863 bwipe!
864 finally
865 call job_stop(job)
866 endtry
867endfunc
868
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100869func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100870 if !has('job')
871 return
872 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100873 call ch_log('Test_pipe_from_buffer()')
874
875 sp pipe-input
876 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200877 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100878 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100879 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100880 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100881 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100882 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100883
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100884 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100885 call assert_equal("run", job_status(job))
886 try
887 let handle = job_getchannel(job)
888 call assert_equal('one', ch_read(handle))
889 call assert_equal('two', ch_read(handle))
890 call assert_equal('three', ch_read(handle))
891 bwipe!
892 finally
893 call job_stop(job)
894 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100895endfunc
896
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100897func Test_pipe_from_buffer_name()
898 call Run_test_pipe_from_buffer(1)
899endfunc
900
901func Test_pipe_from_buffer_nr()
902 call Run_test_pipe_from_buffer(0)
903endfunc
904
Bram Moolenaar0874a832016-09-01 15:11:51 +0200905func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200906 if !executable('sort') || !has('job')
907 return
908 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
910 if a:use_buffer
911 split sortin
912 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
913 let options.in_io = 'buffer'
914 let options.in_name = 'sortin'
915 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200916 if !a:all
917 let options.in_top = 2
918 let options.in_bot = 4
919 endif
920 let g:job = job_start('sort', options)
921 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200922
923 if !a:use_buffer
924 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
925 call ch_close_in(g:job)
926 endif
927
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200928 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200929
Bram Moolenaard8b55492016-09-01 14:35:22 +0200930 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200931 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200932 call assert_equal('Reading from channel output...', getline(1))
933 if a:all
934 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
935 else
936 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
937 endif
938
939 call job_stop(g:job)
940 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200941 if a:use_buffer
942 bwipe! sortin
943 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200944 bwipe! sortout
945endfunc
946
947func Test_pipe_through_sort_all()
948 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200949 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200950endfunc
951
952func Test_pipe_through_sort_some()
953 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200954 call Run_pipe_through_sort(0, 1)
955endfunc
956
957func Test_pipe_through_sort_feed()
958 call ch_log('Test_pipe_through_sort_feed()')
959 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200960endfunc
961
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100962func Test_pipe_to_nameless_buffer()
963 if !has('job')
964 return
965 endif
966 call ch_log('Test_pipe_to_nameless_buffer()')
967 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100968 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100969 call assert_equal("run", job_status(job))
970 try
971 let handle = job_getchannel(job)
972 call ch_sendraw(handle, "echo line one\n")
973 call ch_sendraw(handle, "echo line two\n")
974 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200975 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100976 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
977 bwipe!
978 finally
979 call job_stop(job)
980 endtry
981endfunc
982
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100983func Test_pipe_to_buffer_json()
984 if !has('job')
985 return
986 endif
987 call ch_log('Test_pipe_to_buffer_json()')
988 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100989 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100990 call assert_equal("run", job_status(job))
991 try
992 let handle = job_getchannel(job)
993 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
994 call ch_sendraw(handle, "echo [-2, 12.34]\n")
995 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200996 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100997 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
998 bwipe!
999 finally
1000 call job_stop(job)
1001 endtry
1002endfunc
1003
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001004" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001005func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001006 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001007 if getline(line('$') - a:offset) == a:line
1008 break
1009 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001010 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001011 endfor
1012endfunc
1013
1014func Test_pipe_io_two_buffers()
1015 if !has('job')
1016 return
1017 endif
1018 call ch_log('Test_pipe_io_two_buffers()')
1019
1020 " Create two buffers, one to read from and one to write to.
1021 split pipe-output
1022 set buftype=nofile
1023 split pipe-input
1024 set buftype=nofile
1025
1026 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001027 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001028 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1029 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001030 call assert_equal("run", job_status(job))
1031 try
1032 exe "normal Gaecho hello\<CR>"
1033 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001034 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001035 call assert_equal('hello', getline('$'))
1036
1037 exe bufwinnr('pipe-input') . "wincmd w"
1038 exe "normal Gadouble this\<CR>"
1039 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001040 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001041 call assert_equal('this', getline(line('$') - 1))
1042 call assert_equal('AND this', getline('$'))
1043
1044 bwipe!
1045 exe bufwinnr('pipe-input') . "wincmd w"
1046 bwipe!
1047 finally
1048 call job_stop(job)
1049 endtry
1050endfunc
1051
1052func Test_pipe_io_one_buffer()
1053 if !has('job')
1054 return
1055 endif
1056 call ch_log('Test_pipe_io_one_buffer()')
1057
1058 " Create one buffer to read from and to write to.
1059 split pipe-io
1060 set buftype=nofile
1061
1062 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001063 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001064 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1065 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001066 call assert_equal("run", job_status(job))
1067 try
1068 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001069 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001070 call assert_equal('hello', getline(line('$') - 1))
1071
1072 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001073 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001074 call assert_equal('this', getline(line('$') - 2))
1075 call assert_equal('AND this', getline(line('$') - 1))
1076
1077 bwipe!
1078 finally
1079 call job_stop(job)
1080 endtry
1081endfunc
1082
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001083func Test_pipe_null()
1084 if !has('job')
1085 return
1086 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001087 call ch_log('Test_pipe_null()')
1088
1089 " We cannot check that no I/O works, we only check that the job starts
1090 " properly.
1091 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001092 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001093 call assert_equal("run", job_status(job))
1094 try
1095 call assert_equal('something', ch_read(job))
1096 finally
1097 call job_stop(job)
1098 endtry
1099
1100 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001101 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001102 call assert_equal("run", job_status(job))
1103 try
1104 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1105 finally
1106 call job_stop(job)
1107 endtry
1108
1109 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001110 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001111 call assert_equal("run", job_status(job))
1112 try
1113 call assert_equal('something', ch_read(job))
1114 finally
1115 call job_stop(job)
1116 endtry
1117
1118 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001119 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001120 call assert_equal("run", job_status(job))
1121 call job_stop(job)
1122
1123 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001124 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001125 call assert_equal("run", job_status(job))
1126 call assert_equal('channel fail', string(job_getchannel(job)))
1127 call assert_equal('fail', ch_status(job))
1128 call job_stop(job)
1129endfunc
1130
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001131func Test_pipe_to_buffer_raw()
1132 if !has('job')
1133 return
1134 endif
1135 call ch_log('Test_raw_pipe_to_buffer()')
1136 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1137 split testout
1138 let job = job_start([s:python, '-c',
1139 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1140 call assert_equal("run", job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001141 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001142 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001143 let totlen = 0
1144 for line in getline(1, '$')
1145 call assert_equal('', substitute(line, '^\.*', '', ''))
1146 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001147 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001148 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001149 finally
1150 call job_stop(job)
1151 bwipe!
1152 endtry
1153endfunc
1154
Bram Moolenaarde279892016-03-11 22:19:44 +01001155func Test_reuse_channel()
1156 if !has('job')
1157 return
1158 endif
1159 call ch_log('Test_reuse_channel()')
1160
1161 let job = job_start(s:python . " test_channel_pipe.py")
1162 call assert_equal("run", job_status(job))
1163 let handle = job_getchannel(job)
1164 try
1165 call ch_sendraw(handle, "echo something\n")
1166 call assert_equal("something", ch_readraw(handle))
1167 finally
1168 call job_stop(job)
1169 endtry
1170
1171 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1172 call assert_equal("run", job_status(job))
1173 let handle = job_getchannel(job)
1174 try
1175 call ch_sendraw(handle, "echo again\n")
1176 call assert_equal("again", ch_readraw(handle))
1177 finally
1178 call job_stop(job)
1179 endtry
1180endfunc
1181
Bram Moolenaar75f72652016-03-20 22:16:56 +01001182func Test_out_cb()
1183 if !has('job')
1184 return
1185 endif
1186 call ch_log('Test_out_cb()')
1187
1188 let dict = {'thisis': 'dict: '}
1189 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001190 if type(a:msg) == v:t_string
1191 let g:Ch_outmsg = self.thisis . a:msg
1192 else
1193 let g:Ch_outobj = a:msg
1194 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001195 endfunc
1196 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001197 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001198 endfunc
1199 let job = job_start(s:python . " test_channel_pipe.py",
1200 \ {'out_cb': dict.outHandler,
1201 \ 'out_mode': 'json',
1202 \ 'err_cb': dict.errHandler,
1203 \ 'err_mode': 'json'})
1204 call assert_equal("run", job_status(job))
1205 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001206 let g:Ch_outmsg = ''
1207 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001208 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1209 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001210 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1211 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001212
1213 " Receive a json object split in pieces
1214 unlet! g:Ch_outobj
1215 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001216 let g:Ch_outobj = ''
1217 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001218 finally
1219 call job_stop(job)
1220 endtry
1221endfunc
1222
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001223func Test_out_close_cb()
1224 if !has('job')
1225 return
1226 endif
1227 call ch_log('Test_out_close_cb()')
1228
1229 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001230 let g:Ch_msg1 = ''
1231 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001232 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001233 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001234 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001235 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001236 let s:counter += 1
1237 endfunc
1238 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001239 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001240 let s:counter += 1
1241 endfunc
1242 let job = job_start(s:python . " test_channel_pipe.py quit now",
1243 \ {'out_cb': 'OutHandler',
1244 \ 'close_cb': 'CloseHandler'})
1245 call assert_equal("run", job_status(job))
1246 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001247 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1248 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001249 finally
1250 call job_stop(job)
1251 delfunc OutHandler
1252 delfunc CloseHandler
1253 endtry
1254endfunc
1255
Bram Moolenaar437905c2016-04-26 19:01:05 +02001256func Test_read_in_close_cb()
1257 if !has('job')
1258 return
1259 endif
1260 call ch_log('Test_read_in_close_cb()')
1261
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001262 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001263 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001264 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001265 endfunc
1266 let job = job_start(s:python . " test_channel_pipe.py quit now",
1267 \ {'close_cb': 'CloseHandler'})
1268 call assert_equal("run", job_status(job))
1269 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001270 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001271 finally
1272 call job_stop(job)
1273 delfunc CloseHandler
1274 endtry
1275endfunc
1276
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001277" Use channel in NL mode but received text does not end in NL.
1278func Test_read_in_close_cb_incomplete()
1279 if !has('job')
1280 return
1281 endif
1282 call ch_log('Test_read_in_close_cb_incomplete()')
1283
1284 let g:Ch_received = ''
1285 func! CloseHandler(chan)
1286 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1287 let g:Ch_received .= ch_read(a:chan)
1288 endwhile
1289 endfunc
1290 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1291 \ {'close_cb': 'CloseHandler'})
1292 call assert_equal("run", job_status(job))
1293 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001294 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001295 finally
1296 call job_stop(job)
1297 delfunc CloseHandler
1298 endtry
1299endfunc
1300
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001301func Test_out_cb_lambda()
1302 if !has('job')
1303 return
1304 endif
1305 call ch_log('Test_out_cb_lambda()')
1306
1307 let job = job_start(s:python . " test_channel_pipe.py",
1308 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1309 \ 'out_mode': 'json',
1310 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1311 \ 'err_mode': 'json'})
1312 call assert_equal("run", job_status(job))
1313 try
1314 let g:Ch_outmsg = ''
1315 let g:Ch_errmsg = ''
1316 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1317 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001318 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1319 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001320 finally
1321 call job_stop(job)
1322 endtry
1323endfunc
1324
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001325func Test_close_and_exit_cb()
1326 if !has('job')
1327 return
1328 endif
1329 call ch_log('Test_close_and_exit_cb')
1330
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001331 let g:retdict = {'ret': {}}
1332 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001333 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1334 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001335 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001336 let self.ret['exit_cb'] = job_status(a:job)
1337 endfunc
1338
1339 let g:job = job_start('echo', {
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001340 \ 'close_cb': g:retdict.close_cb,
1341 \ 'exit_cb': g:retdict.exit_cb,
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001342 \ })
1343 call assert_equal('run', job_status(g:job))
1344 unlet g:job
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001345 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001346 call assert_match('^\%(dead\|run\)', g:retdict.ret['close_cb'])
1347 call assert_equal('dead', g:retdict.ret['exit_cb'])
1348 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001349endfunc
1350
Bram Moolenaard46ae142016-02-16 13:33:52 +01001351""""""""""
1352
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001353let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001354func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001355 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001356 unlet s:channelfd
1357endfunc
1358
1359" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001360func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001361 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001362 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001363 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001364endfunc
1365
1366func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001367 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001368 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001369endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001370
Bram Moolenaard46ae142016-02-16 13:33:52 +01001371""""""""""
1372
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373let g:Ch_unletResponse = ''
1374func Ch_CloseHandler(handle, msg)
1375 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001376 call ch_close(s:channelfd)
1377endfunc
1378
1379" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001380func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001381 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001382 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001383 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001384endfunc
1385
1386func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001387 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001388 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001389endfunc
1390
1391""""""""""
1392
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001393func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001394 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001395 silent! let ch = ch_open("noserver")
1396 echo ch
1397 let d = ch
1398endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001399
1400""""""""""
1401
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001402func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001403 " Wait up to a second for the port to open.
1404 let s:chopt.waittime = 1000
1405 let channel = ch_open('localhost:' . a:port, s:chopt)
1406 unlet s:chopt.waittime
1407 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001408 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001409 return
1410 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001411 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001412 call ch_close(channel)
1413endfunc
1414
1415func Test_open_delay()
1416 call ch_log('Test_open_delay()')
1417 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001418 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001419endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001420
1421"""""""""
1422
1423function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001424 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001425endfunc
1426
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001427function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001428 let handle = ch_open('localhost:' . a:port, s:chopt)
1429 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001430 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001431 return
1432 endif
1433
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001434 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001435 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001436 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarece61b02016-02-20 21:39:05 +01001437endfunc
1438
1439func Test_call()
1440 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001441 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001442endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001443
1444"""""""""
1445
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001446let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001447function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001448 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001449endfunc
1450
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001451function Ch_test_exit_callback(port)
1452 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1453 let g:Ch_exit_job = g:currentJob
1454 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001455endfunc
1456
1457func Test_exit_callback()
1458 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001459 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001461
Bram Moolenaar9730f742016-02-28 19:50:51 +01001462 " wait up to a second for the job to exit
1463 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001464 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001465 break
1466 endif
1467 sleep 10m
1468 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001469 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001470 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001471
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001472 call assert_equal('done', g:Ch_job_exit_ret)
1473 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1474 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001475 endif
1476endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001477
Bram Moolenaar97792de2016-10-15 18:36:49 +02001478function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001479 if job_info(a:job).process == g:exit_cb_val.process
1480 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1481 endif
1482 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001483endfunction
1484
1485func Test_exit_callback_interval()
1486 if !has('job')
1487 return
1488 endif
1489
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001490 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001491 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001492 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001493 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001494 let elapsed = reltimefloat(g:exit_cb_val.end)
1495 call assert_true(elapsed > 0.5)
1496 call assert_true(elapsed < 1.0)
1497
1498 " case: unreferenced job, using timer
1499 if !has('timers')
1500 return
1501 endif
1502
1503 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1504 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1505 let g:exit_cb_val.process = job_info(g:job).process
1506 unlet g:job
1507 call Standby(1000)
1508 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1509 let elapsed = reltimefloat(g:exit_cb_val.end)
1510 else
1511 let elapsed = 1.0
1512 endif
1513 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001514 call assert_true(elapsed < 1.0)
1515endfunc
1516
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001517"""""""""
1518
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001519let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001520function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001521 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001522endfunc
1523
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001524function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001525 let handle = ch_open('localhost:' . a:port, s:chopt)
1526 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001527 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001528 return
1529 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001530 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001531
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001532 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001533 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001534endfunc
1535
1536func Test_close_callback()
1537 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001538 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001539endfunc
1540
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001541function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001542 let handle = ch_open('localhost:' . a:port, s:chopt)
1543 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001544 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001545 return
1546 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001547 let g:Ch_d = {}
1548 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001549 let self.close_ret = 'closed'
1550 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001551 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001552
1553 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001554 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001555 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001556endfunc
1557
1558func Test_close_partial()
1559 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001560 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001561endfunc
1562
Bram Moolenaar80385682016-03-27 19:13:35 +02001563func Test_job_start_invalid()
1564 call assert_fails('call job_start($x)', 'E474:')
1565 call assert_fails('call job_start("")', 'E474:')
1566endfunc
1567
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001568func Test_job_stop_immediately()
1569 if !has('job')
1570 return
1571 endif
1572
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001573 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001574 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001575 call job_stop(g:job)
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001576 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001577 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001578 call job_stop(g:job, 'kill')
1579 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001580 endtry
1581endfunc
1582
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001583" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001584func Test_partial_in_channel_cycle()
1585 let d = {}
1586 let d.a = function('string', [d])
1587 try
1588 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1589 catch
1590 call assert_exception('E901:')
1591 endtry
1592 unlet d
1593endfunc
1594
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001595func Test_using_freed_memory()
1596 let g:a = job_start(['ls'])
1597 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001598 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001599endfunc
1600
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001601func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001602 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001603 return
1604 endif
1605 sp test_channel.vim
1606 let g:linecount = line('$')
1607 close
1608 split testout
1609 1,$delete
1610 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001611 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001612 bwipe!
1613endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001614
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001615func Test_cmd_parsing()
1616 if !has('unix')
1617 return
1618 endif
1619 call assert_false(filereadable("file with space"))
1620 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001621 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001622 call delete("file with space")
1623
1624 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001625 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001626 call delete("file with space")
1627endfunc
1628
Bram Moolenaar82117982016-08-27 19:21:48 +02001629func Test_raw_passes_nul()
1630 if !executable('cat') || !has('job')
1631 return
1632 endif
1633
1634 " Test lines from the job containing NUL are stored correctly in a buffer.
1635 new
1636 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1637 w! Xtestread
1638 bwipe!
1639 split testout
1640 1,$delete
1641 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1642 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001643 call assert_equal("asdf\nasdf", getline(1))
1644 call assert_equal("xxx\n", getline(2))
1645 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001646
1647 call delete('Xtestread')
1648 bwipe!
1649
1650 " Test lines from a buffer with NUL bytes are written correctly to the job.
1651 new mybuffer
1652 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1653 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 +02001654 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001655 bwipe!
1656 split Xtestwrite
1657 call assert_equal("asdf\nasdf", getline(1))
1658 call assert_equal("xxx\n", getline(2))
1659 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001660 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001661
1662 call delete('Xtestwrite')
1663 bwipe!
1664endfunc
1665
Bram Moolenaarec68a992016-10-03 21:37:41 +02001666func MyLineCountCb(ch, msg)
1667 let g:linecount += 1
1668endfunc
1669
1670func Test_read_nonl_line()
1671 if !has('job')
1672 return
1673 endif
1674
1675 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001676 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001677 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001678 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaarec68a992016-10-03 21:37:41 +02001679endfunc
1680
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001681func Test_read_from_terminated_job()
1682 if !has('job')
1683 return
1684 endif
1685
1686 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001687 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001688 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001689 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001690endfunc
1691
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001692func Test_env()
1693 if !has('job')
1694 return
1695 endif
1696
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001697 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001698 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001699 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001700 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001701 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001702 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001703 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1704 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001705 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001706 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001707endfunc
1708
1709func Test_cwd()
1710 if !has('job')
1711 return
1712 endif
1713
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001714 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001715 if has('win32')
1716 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001717 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001718 else
1719 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001720 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001721 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001722 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001723 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001724 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001725 let expect = substitute(expect, '[/\\]$', '', '')
1726 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1727 if $CI != '' && stridx(g:envstr, '/private/') == 0
1728 let g:envstr = g:envstr[8:]
1729 endif
1730 call assert_equal(expect, g:envstr)
1731 finally
1732 call job_stop(job)
1733 unlet g:envstr
1734 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001735endfunc
1736
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001737function Ch_test_close_lambda(port)
1738 let handle = ch_open('localhost:' . a:port, s:chopt)
1739 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001740 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001741 return
1742 endif
1743 let g:Ch_close_ret = ''
1744 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1745
1746 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001747 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001748endfunc
1749
1750func Test_close_lambda()
1751 call ch_log('Test_close_lambda()')
1752 call s:run_server('Ch_test_close_lambda')
1753endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001754
1755func s:test_list_args(cmd, out, remove_lf)
1756 try
1757 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001758 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 +02001759 call WaitFor('"" != g:out')
1760 if has('win32')
1761 let g:out = substitute(g:out, '\r', '', 'g')
1762 endif
1763 if a:remove_lf
1764 let g:out = substitute(g:out, '\n$', '', 'g')
1765 endif
1766 call assert_equal(a:out, g:out)
1767 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001768 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001769 unlet g:out
1770 endtry
1771endfunc
1772
1773func Test_list_args()
1774 if !has('job')
1775 return
1776 endif
1777
1778 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1779 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1780 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1781 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1782 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1783 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1784 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1785 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1786 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1787 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1788 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1789 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1790 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1791
1792 " tests which not contain spaces in the argument
1793 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1794 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1795 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1796 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1797 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1798 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1799 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1800 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1801 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1802endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001803
1804" Do this last, it stops any channel log.
1805func Test_zz_ch_log()
1806 call ch_logfile('Xlog', 'w')
1807 call ch_log('hello there')
1808 call ch_log('%s%s')
1809 call ch_logfile('')
1810 let text = readfile('Xlog')
1811 call assert_match("hello there", text[1])
1812 call assert_match("%s%s", text[2])
1813 call delete('Xlog')
1814endfunc
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001815
1816func Test_keep_pty_open()
1817 if !has('unix')
1818 return
1819 endif
1820
1821 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"', {'out_io': 'null', 'err_io': 'null', 'pty': 1})
1822 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1823 call assert_inrange(200, 1000, elapsed)
1824 call job_stop(job)
1825endfunc