blob: b86ca13ca03cfb63dbcb2b6108f86b5dd8a5d674 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
Bram Moolenaare2469252016-02-04 10:54:34 +01004if !has('channel')
5 finish
6endif
7
Bram Moolenaar321efdd2016-07-15 17:09:11 +02008source shared.vim
9
10let s:python = PythonProg()
11if s:python == ''
Bram Moolenaard6a8d482016-02-10 20:32:20 +010012 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010013 finish
14endif
15
Bram Moolenaare74e8e72016-02-16 22:01:30 +010016let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010017
Bram Moolenaard6a8d482016-02-10 20:32:20 +010018" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010019func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020020 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010021endfunc
22
Bram Moolenaar321efdd2016-07-15 17:09:11 +020023let g:Ch_responseMsg = ''
24func Ch_requestHandler(handle, msg)
25 let g:Ch_responseHandle = a:handle
26 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010027endfunc
28
Bram Moolenaar321efdd2016-07-15 17:09:11 +020029func Ch_communicate(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010030 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010031 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +010032 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010033 return
34 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010035 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010036 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010037 call assert_equal('no process', string(ch_getjob(handle)))
38 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010039 let dict = ch_info(handle)
40 call assert_true(dict.id != 0)
41 call assert_equal('open', dict.status)
42 call assert_equal(a:port, string(dict.port))
43 call assert_equal('open', dict.sock_status)
44 call assert_equal('socket', dict.sock_io)
45
Bram Moolenaard7ece102016-02-02 23:23:02 +010046 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010047 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010048
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010049 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010050 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
51 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
52 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
53
54 " split command should work
55 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020056 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010057 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010058
Bram Moolenaard7ece102016-02-02 23:23:02 +010059 " Request that triggers sending two ex commands. These will usually be
60 " handled before getting the response, but it's not guaranteed, thus wait a
61 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010062 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020063 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010064 call assert_equal('added1', getline(line('$') - 1))
65 call assert_equal('added2', getline('$'))
66
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010067 " Request command "foo bar", which fails silently.
68 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020069 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020070 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010071
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010072 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020073 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010074 call assert_equal('added more', getline('$'))
75
Bram Moolenaara07fec92016-02-05 21:04:08 +010076 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020077 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
78 call WaitFor('exists("g:Ch_responseHandle")')
79 if !exists('g:Ch_responseHandle')
80 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010081 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020082 call assert_equal(handle, g:Ch_responseHandle)
83 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +010084 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +020085 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +010086
Bram Moolenaar321efdd2016-07-15 17:09:11 +020087 let g:Ch_responseMsg = ''
88 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
89 call WaitFor('exists("g:Ch_responseHandle")')
90 if !exists('g:Ch_responseHandle')
91 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010092 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020093 call assert_equal(handle, g:Ch_responseHandle)
94 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +010095 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +020096 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010097
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +010098 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +020099 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100100
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100101 " check setting options (without testing the effect)
102 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100103 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100104 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100105 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100106 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100107
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100108 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100109 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100110 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100111 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100112
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100113 " Send an eval request with special characters.
114 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
115 sleep 10m
116 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
117
118 " Send an eval request to get a line with special characters.
119 call setline(3, "a\nb\<CR>c\x01d\x7fe")
120 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
121 sleep 10m
122 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
123
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100124 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100125 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100126 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100127 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100128
Bram Moolenaar55fab432016-02-07 16:53:13 +0100129 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100130 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100131 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100132 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100133
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100134 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100135 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100136 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100137 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100138
Bram Moolenaarf4160862016-02-05 23:09:12 +0100139 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100140 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200141 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100142 call assert_equal('one', getline(line('$') - 2))
143 call assert_equal('two', getline(line('$') - 1))
144 call assert_equal('three', getline('$'))
145
146 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100147 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
148 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100149
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100150 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100151
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100152 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100153 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
154 let start = reltime()
155 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
156 let elapsed = reltime(start)
157 call assert_true(reltimefloat(elapsed) > 0.3)
158 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100159
160 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100162 let resp = ch_read(handle)
163 call assert_equal(type([]), type(resp))
164 call assert_equal(type(11), type(resp[0]))
165 call assert_equal('waited', resp[1])
166
Bram Moolenaard7ece102016-02-02 23:23:02 +0100167 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100168 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100169endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100170
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100171func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200173 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100174endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100175
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100176" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200177func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100178 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100180 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100181 return
182 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100183
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100184 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100185
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100186 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100187 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100188 call assert_false(1, "Can't open second channel")
189 return
190 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100191 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
192 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100193
194 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100195 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100196
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100197 call ch_close(newhandle)
198endfunc
199
200func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100201 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200202 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100203endfunc
204
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100205" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200206func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100207 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100208 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100210 return
211 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100212
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100213 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100214
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100215 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100216endfunc
217
218func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100219 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200220 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100221endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100222
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100223"""""""""
224
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200225let g:Ch_reply = ""
226func Ch_handler(chan, msg)
227 unlet g:Ch_reply
228 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100229endfunc
230
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200231func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100232 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100233 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100234 call assert_false(1, "Can't open channel")
235 return
236 endif
237
238 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200240 call WaitFor('"we called you" == g:Ch_reply')
241 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100242
243 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100244 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200245 call WaitFor('"we did call you" == g:Ch_reply')
246 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100247endfunc
248
249func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100250 call ch_log('Test_channel_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200251 let s:chopt.callback = 'Ch_handler'
252 call s:run_server('Ch_channel_handler')
253 let s:chopt.callback = function('Ch_handler')
254 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100255 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100256endfunc
257
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100258"""""""""
259
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200260let g:Ch_reply = ''
261func Ch_zeroHandler(chan, msg)
262 unlet g:Ch_reply
263 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100264endfunc
265
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200266let g:Ch_zero_reply = ''
267func Ch_oneHandler(chan, msg)
268 unlet g:Ch_zero_reply
269 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100270endfunc
271
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200272func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100273 let handle = ch_open('localhost:' . a:port, s:chopt)
274 if ch_status(handle) == "fail"
275 call assert_false(1, "Can't open channel")
276 return
277 endif
278
279 " Check that eval works.
280 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
281
282 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200283 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100284 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100285 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200286 call WaitFor('"zero index" == g:Ch_reply')
287 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100288 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100289 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200290 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100291 endif
292
293 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200294 let g:Ch_reply = ''
295 let g:Ch_zero_reply = ''
296 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
297 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100298 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200299 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100300 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200301 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100302 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200303 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100304endfunc
305
306func Test_zero_reply()
307 call ch_log('Test_zero_reply()')
308 " Run with channel handler
309 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310 let s:chopt.callback = 'Ch_zeroHandler'
311 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100312 unlet s:chopt.callback
313
314 " Run without channel handler
315 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200316 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100317endfunc
318
319"""""""""
320
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200321let g:Ch_reply1 = ""
322func Ch_handleRaw1(chan, msg)
323 unlet g:Ch_reply1
324 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100325endfunc
326
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200327let g:Ch_reply2 = ""
328func Ch_handleRaw2(chan, msg)
329 unlet g:Ch_reply2
330 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100331endfunc
332
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333let g:Ch_reply3 = ""
334func Ch_handleRaw3(chan, msg)
335 unlet g:Ch_reply3
336 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100337endfunc
338
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100340 let handle = ch_open('localhost:' . a:port, s:chopt)
341 if ch_status(handle) == "fail"
342 call assert_false(1, "Can't open channel")
343 return
344 endif
345 call ch_setoptions(handle, {'mode': 'raw'})
346
347 " The message are sent raw, we do our own JSON strings here.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 'Ch_handleRaw1'})
349 call WaitFor('g:Ch_reply1 != ""')
350 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
351 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 'Ch_handleRaw2'})
352 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 'Ch_handleRaw3'})
353 call WaitFor('g:Ch_reply2 != ""')
354 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100355 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200356 call WaitFor('g:Ch_reply3 != ""')
357 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100358endfunc
359
360func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100361 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200362 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100363endfunc
364
365"""""""""
366
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100367" Test that trying to connect to a non-existing port fails quickly.
368func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100369 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100370 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100371 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100372 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100373 " Oops, port does exists.
374 call ch_close(handle)
375 else
376 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100377 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100378 endif
379
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100380 " We intend to use a socket that doesn't exist and wait for half a second
381 " before giving up. If the socket does exist it can fail in various ways.
382 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100383 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100384 try
385 let handle = ch_open('localhost:9867', {'waittime': 500})
386 if ch_status(handle) != "fail"
387 " Oops, port does exists.
388 call ch_close(handle)
389 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100390 " Failed connection should wait about 500 msec. Can be longer if the
391 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100392 let elapsed = reltime(start)
393 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100394 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100395 endif
396 catch
397 if v:exception !~ 'Connection reset by peer'
398 call assert_false(1, "Caught exception: " . v:exception)
399 endif
400 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100401endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100402
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100403"""""""""
404
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100405func Test_raw_pipe()
406 if !has('job')
407 return
408 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100409 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100410 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
411 call assert_equal("run", job_status(job))
412 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100413 " For a change use the job where a channel is expected.
414 call ch_sendraw(job, "echo something\n")
415 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100416 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
417
Bram Moolenaar151f6562016-03-07 21:19:38 +0100418 call ch_sendraw(job, "double this\n")
419 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100420 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
421
Bram Moolenaar151f6562016-03-07 21:19:38 +0100422 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100423 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
424 finally
425 call job_stop(job)
426 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100427
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200428 let g:Ch_job = job
429 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100430 let info = job_info(job)
431 call assert_equal("dead", info.status)
432 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100433endfunc
434
435func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100436 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100437 return
438 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100439 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100440 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100441 call assert_equal("run", job_status(job))
442 try
443 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100444 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100445 call assert_equal("something", ch_readraw(handle))
446
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100447 call ch_sendraw(handle, "echoerr wrong\n")
448 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
449
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100450 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100451 call assert_equal("this", ch_readraw(handle))
452 call assert_equal("AND this", ch_readraw(handle))
453
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200454 call ch_sendraw(handle, "split this line\n")
455 call assert_equal("this linethis linethis line", ch_readraw(handle))
456
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100457 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100458 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100459 finally
460 call job_stop(job)
461 endtry
462endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100463
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100464func Test_nl_err_to_out_pipe()
465 if !has('job')
466 return
467 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100468 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100469 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100470 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100471 call assert_equal("run", job_status(job))
472 try
473 let handle = job_getchannel(job)
474 call ch_sendraw(handle, "echo something\n")
475 call assert_equal("something", ch_readraw(handle))
476
477 call ch_sendraw(handle, "echoerr wrong\n")
478 call assert_equal("wrong", ch_readraw(handle))
479 finally
480 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100481 call ch_logfile('')
482 let loglines = readfile('Xlog')
483 call assert_true(len(loglines) > 10)
484 let found_test = 0
485 let found_send = 0
486 let found_recv = 0
487 let found_stop = 0
488 for l in loglines
489 if l =~ 'Test_nl_err_to_out_pipe'
490 let found_test = 1
491 endif
492 if l =~ 'SEND on.*echo something'
493 let found_send = 1
494 endif
495 if l =~ 'RECV on.*something'
496 let found_recv = 1
497 endif
498 if l =~ 'Stopping job with'
499 let found_stop = 1
500 endif
501 endfor
502 call assert_equal(1, found_test)
503 call assert_equal(1, found_send)
504 call assert_equal(1, found_recv)
505 call assert_equal(1, found_stop)
506 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100507 endtry
508endfunc
509
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100510func Test_nl_read_file()
511 if !has('job')
512 return
513 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100514 call ch_log('Test_nl_read_file()')
515 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
516 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100517 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100518 call assert_equal("run", job_status(job))
519 try
520 let handle = job_getchannel(job)
521 call assert_equal("something", ch_readraw(handle))
522 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
523 call assert_equal("this", ch_readraw(handle))
524 call assert_equal("AND this", ch_readraw(handle))
525 finally
526 call job_stop(job)
527 call delete('Xinput')
528 endtry
529endfunc
530
Bram Moolenaare98d1212016-03-08 15:37:41 +0100531func Test_nl_write_out_file()
532 if !has('job')
533 return
534 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100535 call ch_log('Test_nl_write_out_file()')
536 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100537 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100538 call assert_equal("run", job_status(job))
539 try
540 let handle = job_getchannel(job)
541 call ch_sendraw(handle, "echo line one\n")
542 call ch_sendraw(handle, "echo line two\n")
543 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200544 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100545 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
546 finally
547 call job_stop(job)
548 call delete('Xoutput')
549 endtry
550endfunc
551
552func Test_nl_write_err_file()
553 if !has('job')
554 return
555 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100556 call ch_log('Test_nl_write_err_file()')
557 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100558 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100559 call assert_equal("run", job_status(job))
560 try
561 let handle = job_getchannel(job)
562 call ch_sendraw(handle, "echoerr line one\n")
563 call ch_sendraw(handle, "echoerr line two\n")
564 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200565 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100566 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
567 finally
568 call job_stop(job)
569 call delete('Xoutput')
570 endtry
571endfunc
572
573func Test_nl_write_both_file()
574 if !has('job')
575 return
576 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100577 call ch_log('Test_nl_write_both_file()')
578 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100579 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100580 call assert_equal("run", job_status(job))
581 try
582 let handle = job_getchannel(job)
583 call ch_sendraw(handle, "echoerr line one\n")
584 call ch_sendraw(handle, "echo line two\n")
585 call ch_sendraw(handle, "double this\n")
586 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200587 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100588 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
589 finally
590 call job_stop(job)
591 call delete('Xoutput')
592 endtry
593endfunc
594
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200595func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200596 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200597endfunc
598
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200599func Run_test_pipe_to_buffer(use_name, nomod)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100600 if !has('job')
601 return
602 endif
603 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200604 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200605 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100606 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100607 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100608 let firstline = 'Reading from channel output...'
609 else
610 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100611 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100612 quit
613 let firstline = ''
614 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200615 if a:nomod
616 let options['out_modifiable'] = 0
617 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100618 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100619 call assert_equal("run", job_status(job))
620 try
621 let handle = job_getchannel(job)
622 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")
625 call ch_sendraw(handle, "quit\n")
626 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200627 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100628 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200629 if a:nomod
630 call assert_equal(0, &modifiable)
631 else
632 call assert_equal(1, &modifiable)
633 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200634 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100635 bwipe!
636 finally
637 call job_stop(job)
638 endtry
639endfunc
640
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100641func Test_pipe_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200642 call Run_test_pipe_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100643endfunc
644
645func Test_pipe_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200646 call Run_test_pipe_to_buffer(0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100647endfunc
648
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200649func Test_pipe_to_buffer_name_nomod()
650 call Run_test_pipe_to_buffer(1, 1)
651endfunc
652
653func Run_test_pipe_err_to_buffer(use_name, nomod)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100654 if !has('job')
655 return
656 endif
657 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100658 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100659 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100660 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100661 let firstline = 'Reading from channel error...'
662 else
663 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100664 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100665 quit
666 let firstline = ''
667 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200668 if a:nomod
669 let options['err_modifiable'] = 0
670 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100671 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100672 call assert_equal("run", job_status(job))
673 try
674 let handle = job_getchannel(job)
675 call ch_sendraw(handle, "echoerr line one\n")
676 call ch_sendraw(handle, "echoerr line two\n")
677 call ch_sendraw(handle, "doubleerr this\n")
678 call ch_sendraw(handle, "quit\n")
679 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200680 call WaitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100681 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200682 if a:nomod
683 call assert_equal(0, &modifiable)
684 else
685 call assert_equal(1, &modifiable)
686 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100687 bwipe!
688 finally
689 call job_stop(job)
690 endtry
691endfunc
692
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100693func Test_pipe_err_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200694 call Run_test_pipe_err_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695endfunc
696
697func Test_pipe_err_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200698 call Run_test_pipe_err_to_buffer(0, 0)
699endfunc
700
701func Test_pipe_err_to_buffer_name_nomod()
702 call Run_test_pipe_err_to_buffer(1, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100703endfunc
704
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100705func Test_pipe_both_to_buffer()
706 if !has('job')
707 return
708 endif
709 call ch_log('Test_pipe_both_to_buffer()')
710 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100711 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100712 call assert_equal("run", job_status(job))
713 try
714 let handle = job_getchannel(job)
715 call ch_sendraw(handle, "echo line one\n")
716 call ch_sendraw(handle, "echoerr line two\n")
717 call ch_sendraw(handle, "double this\n")
718 call ch_sendraw(handle, "doubleerr that\n")
719 call ch_sendraw(handle, "quit\n")
720 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200721 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100722 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
723 bwipe!
724 finally
725 call job_stop(job)
726 endtry
727endfunc
728
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100729func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100730 if !has('job')
731 return
732 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100733 call ch_log('Test_pipe_from_buffer()')
734
735 sp pipe-input
736 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200737 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100738 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100739 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100740 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100741 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100742 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100743
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100744 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100745 call assert_equal("run", job_status(job))
746 try
747 let handle = job_getchannel(job)
748 call assert_equal('one', ch_read(handle))
749 call assert_equal('two', ch_read(handle))
750 call assert_equal('three', ch_read(handle))
751 bwipe!
752 finally
753 call job_stop(job)
754 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100755endfunc
756
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757func Test_pipe_from_buffer_name()
758 call Run_test_pipe_from_buffer(1)
759endfunc
760
761func Test_pipe_from_buffer_nr()
762 call Run_test_pipe_from_buffer(0)
763endfunc
764
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100765func Test_pipe_to_nameless_buffer()
766 if !has('job')
767 return
768 endif
769 call ch_log('Test_pipe_to_nameless_buffer()')
770 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100771 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100772 call assert_equal("run", job_status(job))
773 try
774 let handle = job_getchannel(job)
775 call ch_sendraw(handle, "echo line one\n")
776 call ch_sendraw(handle, "echo line two\n")
777 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200778 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100779 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
780 bwipe!
781 finally
782 call job_stop(job)
783 endtry
784endfunc
785
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100786func Test_pipe_to_buffer_json()
787 if !has('job')
788 return
789 endif
790 call ch_log('Test_pipe_to_buffer_json()')
791 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100792 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100793 call assert_equal("run", job_status(job))
794 try
795 let handle = job_getchannel(job)
796 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
797 call ch_sendraw(handle, "echo [-2, 12.34]\n")
798 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200799 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100800 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
801 bwipe!
802 finally
803 call job_stop(job)
804 endtry
805endfunc
806
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100807" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100808func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100809 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100810 if getline(line('$') - a:offset) == a:line
811 break
812 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100813 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100814 endfor
815endfunc
816
817func Test_pipe_io_two_buffers()
818 if !has('job')
819 return
820 endif
821 call ch_log('Test_pipe_io_two_buffers()')
822
823 " Create two buffers, one to read from and one to write to.
824 split pipe-output
825 set buftype=nofile
826 split pipe-input
827 set buftype=nofile
828
829 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100830 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200831 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
832 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100833 call assert_equal("run", job_status(job))
834 try
835 exe "normal Gaecho hello\<CR>"
836 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100837 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100838 call assert_equal('hello', getline('$'))
839
840 exe bufwinnr('pipe-input') . "wincmd w"
841 exe "normal Gadouble this\<CR>"
842 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100843 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100844 call assert_equal('this', getline(line('$') - 1))
845 call assert_equal('AND this', getline('$'))
846
847 bwipe!
848 exe bufwinnr('pipe-input') . "wincmd w"
849 bwipe!
850 finally
851 call job_stop(job)
852 endtry
853endfunc
854
855func Test_pipe_io_one_buffer()
856 if !has('job')
857 return
858 endif
859 call ch_log('Test_pipe_io_one_buffer()')
860
861 " Create one buffer to read from and to write to.
862 split pipe-io
863 set buftype=nofile
864
865 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100866 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200867 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
868 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100869 call assert_equal("run", job_status(job))
870 try
871 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100872 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100873 call assert_equal('hello', getline(line('$') - 1))
874
875 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100876 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100877 call assert_equal('this', getline(line('$') - 2))
878 call assert_equal('AND this', getline(line('$') - 1))
879
880 bwipe!
881 finally
882 call job_stop(job)
883 endtry
884endfunc
885
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100886func Test_pipe_null()
887 if !has('job')
888 return
889 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100890 call ch_log('Test_pipe_null()')
891
892 " We cannot check that no I/O works, we only check that the job starts
893 " properly.
894 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100895 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100896 call assert_equal("run", job_status(job))
897 try
898 call assert_equal('something', ch_read(job))
899 finally
900 call job_stop(job)
901 endtry
902
903 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100904 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100905 call assert_equal("run", job_status(job))
906 try
907 call assert_equal('err-out', ch_read(job, {"part": "err"}))
908 finally
909 call job_stop(job)
910 endtry
911
912 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100913 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100914 call assert_equal("run", job_status(job))
915 try
916 call assert_equal('something', ch_read(job))
917 finally
918 call job_stop(job)
919 endtry
920
921 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100922 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100923 call assert_equal("run", job_status(job))
924 call job_stop(job)
925
926 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100927 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100928 call assert_equal("run", job_status(job))
929 call assert_equal('channel fail', string(job_getchannel(job)))
930 call assert_equal('fail', ch_status(job))
931 call job_stop(job)
932endfunc
933
Bram Moolenaaradb78a72016-06-27 21:10:31 +0200934func Test_pipe_to_buffer_raw()
935 if !has('job')
936 return
937 endif
938 call ch_log('Test_raw_pipe_to_buffer()')
939 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
940 split testout
941 let job = job_start([s:python, '-c',
942 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
943 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200944 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +0200945 try
946 for line in getline(2, '$')
947 let line = substitute(line, '^\.*', '', '')
948 call assert_equal('', line)
949 endfor
950 finally
951 call job_stop(job)
952 bwipe!
953 endtry
954endfunc
955
Bram Moolenaarde279892016-03-11 22:19:44 +0100956func Test_reuse_channel()
957 if !has('job')
958 return
959 endif
960 call ch_log('Test_reuse_channel()')
961
962 let job = job_start(s:python . " test_channel_pipe.py")
963 call assert_equal("run", job_status(job))
964 let handle = job_getchannel(job)
965 try
966 call ch_sendraw(handle, "echo something\n")
967 call assert_equal("something", ch_readraw(handle))
968 finally
969 call job_stop(job)
970 endtry
971
972 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
973 call assert_equal("run", job_status(job))
974 let handle = job_getchannel(job)
975 try
976 call ch_sendraw(handle, "echo again\n")
977 call assert_equal("again", ch_readraw(handle))
978 finally
979 call job_stop(job)
980 endtry
981endfunc
982
Bram Moolenaar75f72652016-03-20 22:16:56 +0100983func Test_out_cb()
984 if !has('job')
985 return
986 endif
987 call ch_log('Test_out_cb()')
988
989 let dict = {'thisis': 'dict: '}
990 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200991 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +0100992 endfunc
993 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200994 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +0100995 endfunc
996 let job = job_start(s:python . " test_channel_pipe.py",
997 \ {'out_cb': dict.outHandler,
998 \ 'out_mode': 'json',
999 \ 'err_cb': dict.errHandler,
1000 \ 'err_mode': 'json'})
1001 call assert_equal("run", job_status(job))
1002 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001003 let g:Ch_outmsg = ''
1004 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001005 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1006 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001007 call WaitFor('g:Ch_outmsg != ""')
1008 call assert_equal("dict: hello", g:Ch_outmsg)
1009 call WaitFor('g:Ch_errmsg != ""')
1010 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001011 finally
1012 call job_stop(job)
1013 endtry
1014endfunc
1015
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001016func Test_out_close_cb()
1017 if !has('job')
1018 return
1019 endif
1020 call ch_log('Test_out_close_cb()')
1021
1022 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001023 let g:Ch_msg1 = ''
1024 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001025 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001026 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001027 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001028 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001029 let s:counter += 1
1030 endfunc
1031 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001032 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001033 let s:counter += 1
1034 endfunc
1035 let job = job_start(s:python . " test_channel_pipe.py quit now",
1036 \ {'out_cb': 'OutHandler',
1037 \ 'close_cb': 'CloseHandler'})
1038 call assert_equal("run", job_status(job))
1039 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001040 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1041 call assert_equal('quit', g:Ch_msg1)
1042 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001043 finally
1044 call job_stop(job)
1045 delfunc OutHandler
1046 delfunc CloseHandler
1047 endtry
1048endfunc
1049
Bram Moolenaar437905c2016-04-26 19:01:05 +02001050func Test_read_in_close_cb()
1051 if !has('job')
1052 return
1053 endif
1054 call ch_log('Test_read_in_close_cb()')
1055
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001056 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001057 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001058 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001059 endfunc
1060 let job = job_start(s:python . " test_channel_pipe.py quit now",
1061 \ {'close_cb': 'CloseHandler'})
1062 call assert_equal("run", job_status(job))
1063 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001064 call WaitFor('g:Ch_received != ""')
1065 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001066 finally
1067 call job_stop(job)
1068 delfunc CloseHandler
1069 endtry
1070endfunc
1071
Bram Moolenaard46ae142016-02-16 13:33:52 +01001072""""""""""
1073
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001074let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001075func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001076 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001077 unlet s:channelfd
1078endfunc
1079
1080" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001081func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001082 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001083 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001084 call WaitFor('"what?" == g:Ch_unletResponse')
1085 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001086endfunc
1087
1088func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001089 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001090 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001091endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001092
Bram Moolenaard46ae142016-02-16 13:33:52 +01001093""""""""""
1094
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001095let g:Ch_unletResponse = ''
1096func Ch_CloseHandler(handle, msg)
1097 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001098 call ch_close(s:channelfd)
1099endfunc
1100
1101" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001102func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001103 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001104 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1105 call WaitFor('"what?" == g:Ch_unletResponse')
1106 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001107endfunc
1108
1109func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001110 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001111 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001112endfunc
1113
1114""""""""""
1115
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001116func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001117 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001118 silent! let ch = ch_open("noserver")
1119 echo ch
1120 let d = ch
1121endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001122
1123""""""""""
1124
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001125func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001126 " Wait up to a second for the port to open.
1127 let s:chopt.waittime = 1000
1128 let channel = ch_open('localhost:' . a:port, s:chopt)
1129 unlet s:chopt.waittime
1130 if ch_status(channel) == "fail"
1131 call assert_false(1, "Can't open channel")
1132 return
1133 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001134 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001135 call ch_close(channel)
1136endfunc
1137
1138func Test_open_delay()
1139 call ch_log('Test_open_delay()')
1140 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001141 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001142endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001143
1144"""""""""
1145
1146function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001147 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001148endfunc
1149
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001150function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001151 let handle = ch_open('localhost:' . a:port, s:chopt)
1152 if ch_status(handle) == "fail"
1153 call assert_false(1, "Can't open channel")
1154 return
1155 endif
1156
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001157 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001158 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001159 call WaitFor('len(g:Ch_call_ret) > 0')
1160 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001161endfunc
1162
1163func Test_call()
1164 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001165 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001166endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001167
1168"""""""""
1169
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001170let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001171function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001172 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001173endfunc
1174
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001175function Ch_test_exit_callback(port)
1176 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1177 let g:Ch_exit_job = g:currentJob
1178 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001179endfunc
1180
1181func Test_exit_callback()
1182 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001183 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001184 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001185
Bram Moolenaar9730f742016-02-28 19:50:51 +01001186 " wait up to a second for the job to exit
1187 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001188 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001189 break
1190 endif
1191 sleep 10m
1192 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001193 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001194 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001195
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001196 call assert_equal('done', g:Ch_job_exit_ret)
1197 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1198 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001199 endif
1200endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001201
1202"""""""""
1203
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001204let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001205function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001206 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001207endfunc
1208
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001209function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001210 let handle = ch_open('localhost:' . a:port, s:chopt)
1211 if ch_status(handle) == "fail"
1212 call assert_false(1, "Can't open channel")
1213 return
1214 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001215 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001216
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001217 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001218 call WaitFor('"closed" == g:Ch_close_ret')
1219 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001220endfunc
1221
1222func Test_close_callback()
1223 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001224 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001225endfunc
1226
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001227function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001228 let handle = ch_open('localhost:' . a:port, s:chopt)
1229 if ch_status(handle) == "fail"
1230 call assert_false(1, "Can't open channel")
1231 return
1232 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001233 let g:Ch_d = {}
1234 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001235 let self.close_ret = 'closed'
1236 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001237 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001238
1239 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001240 call WaitFor('"closed" == g:Ch_d.close_ret')
1241 call assert_equal('closed', g:Ch_d.close_ret)
1242 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001243endfunc
1244
1245func Test_close_partial()
1246 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001247 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001248endfunc
1249
Bram Moolenaar80385682016-03-27 19:13:35 +02001250func Test_job_start_invalid()
1251 call assert_fails('call job_start($x)', 'E474:')
1252 call assert_fails('call job_start("")', 'E474:')
1253endfunc
1254
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001255" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001256func Test_partial_in_channel_cycle()
1257 let d = {}
1258 let d.a = function('string', [d])
1259 try
1260 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1261 catch
1262 call assert_exception('E901:')
1263 endtry
1264 unlet d
1265endfunc
1266
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001267func Test_using_freed_memory()
1268 let g:a = job_start(['ls'])
1269 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001270 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001271endfunc
1272
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001273func Test_collapse_buffers()
1274 if !executable('cat')
1275 return
1276 endif
1277 sp test_channel.vim
1278 let g:linecount = line('$')
1279 close
1280 split testout
1281 1,$delete
1282 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001283 call WaitFor('line("$") > g:linecount')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001284 call assert_true(line('$') > g:linecount)
1285 bwipe!
1286endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001287
1288
Bram Moolenaar9730f742016-02-28 19:50:51 +01001289" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001290 call ch_logfile('channellog', 'w')