blob: e00cc502b5627e3f8eaedba56e35fa9faaa46e20 [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 Moolenaard6a8d482016-02-10 20:32:20 +010011 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaare74e8e72016-02-16 22:01:30 +010015let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010016
Bram Moolenaard6a8d482016-02-10 20:32:20 +010017" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010018func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020019 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010020endfunc
21
Bram Moolenaar321efdd2016-07-15 17:09:11 +020022let g:Ch_responseMsg = ''
23func Ch_requestHandler(handle, msg)
24 let g:Ch_responseHandle = a:handle
25 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010026endfunc
27
Bram Moolenaar321efdd2016-07-15 17:09:11 +020028func Ch_communicate(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010029 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010030 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +010031 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010032 return
33 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010034 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010035 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010036 call assert_equal('no process', string(ch_getjob(handle)))
37 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010038 let dict = ch_info(handle)
39 call assert_true(dict.id != 0)
40 call assert_equal('open', dict.status)
41 call assert_equal(a:port, string(dict.port))
42 call assert_equal('open', dict.sock_status)
43 call assert_equal('socket', dict.sock_io)
44
Bram Moolenaard7ece102016-02-02 23:23:02 +010045 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010046 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010047
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010048 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010049 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
50 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
51 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
52
53 " split command should work
54 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020055 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010056 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010057
Bram Moolenaard7ece102016-02-02 23:23:02 +010058 " Request that triggers sending two ex commands. These will usually be
59 " handled before getting the response, but it's not guaranteed, thus wait a
60 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010061 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020062 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010063 call assert_equal('added1', getline(line('$') - 1))
64 call assert_equal('added2', getline('$'))
65
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010066 " Request command "foo bar", which fails silently.
67 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020068 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020069 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010070
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010071 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020072 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010073 call assert_equal('added more', getline('$'))
74
Bram Moolenaara07fec92016-02-05 21:04:08 +010075 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020076 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
77 call WaitFor('exists("g:Ch_responseHandle")')
78 if !exists('g:Ch_responseHandle')
79 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010080 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020081 call assert_equal(handle, g:Ch_responseHandle)
82 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +010083 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +020084 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +010085
Bram Moolenaar321efdd2016-07-15 17:09:11 +020086 let g:Ch_responseMsg = ''
87 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
88 call WaitFor('exists("g:Ch_responseHandle")')
89 if !exists('g:Ch_responseHandle')
90 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010091 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020092 call assert_equal(handle, g:Ch_responseHandle)
93 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +010094 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +020095 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010096
Bram Moolenaar069c1e72016-07-15 21:25:08 +020097 " Using lambda.
98 let g:Ch_responseMsg = ''
99 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
100 call WaitFor('exists("g:Ch_responseHandle")')
101 if !exists('g:Ch_responseHandle')
102 call assert_false(1, 'g:Ch_responseHandle was not set')
103 else
104 call assert_equal(handle, g:Ch_responseHandle)
105 unlet g:Ch_responseHandle
106 endif
107 call assert_equal('got it', g:Ch_responseMsg)
108
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100109 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200110 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100111
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100112 " check setting options (without testing the effect)
113 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100114 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100115 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100116 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100117 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100118
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100119 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100120 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100121 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100122 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100123
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100124 " Send an eval request with special characters.
125 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
126 sleep 10m
127 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
128
129 " Send an eval request to get a line with special characters.
130 call setline(3, "a\nb\<CR>c\x01d\x7fe")
131 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
132 sleep 10m
133 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
134
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100135 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100136 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100137 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100138 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100139
Bram Moolenaar55fab432016-02-07 16:53:13 +0100140 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100141 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100142 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100143 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100144
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100145 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100146 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100147 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100148 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100149
Bram Moolenaarf4160862016-02-05 23:09:12 +0100150 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100151 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200152 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100153 call assert_equal('one', getline(line('$') - 2))
154 call assert_equal('two', getline(line('$') - 1))
155 call assert_equal('three', getline('$'))
156
157 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100158 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
159 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100160
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100162
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100163 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100164 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
165 let start = reltime()
166 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
167 let elapsed = reltime(start)
168 call assert_true(reltimefloat(elapsed) > 0.3)
169 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100170
171 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100173 let resp = ch_read(handle)
174 call assert_equal(type([]), type(resp))
175 call assert_equal(type(11), type(resp[0]))
176 call assert_equal('waited', resp[1])
177
Bram Moolenaard7ece102016-02-02 23:23:02 +0100178 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100179 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100180endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100181
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100182func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100183 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200184 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100185endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100186
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100187" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200188func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100189 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200190 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100191 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100192 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100193 return
194 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100195
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100197
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100198 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100199 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100200 call assert_false(1, "Can't open second channel")
201 return
202 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
204 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100205
206 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100207 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100208
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209 call ch_close(newhandle)
210endfunc
211
212func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100213 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200214 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100215endfunc
216
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100217" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200218func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100220 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100222 return
223 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100224
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100225 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100226
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100227 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100228endfunc
229
230func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100231 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200232 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100233endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100234
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100235"""""""""
236
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200237let g:Ch_reply = ""
238func Ch_handler(chan, msg)
239 unlet g:Ch_reply
240 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100241endfunc
242
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200243func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100244 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100245 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100246 call assert_false(1, "Can't open channel")
247 return
248 endif
249
250 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100251 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200252 call WaitFor('"we called you" == g:Ch_reply')
253 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100254
255 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100256 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200257 call WaitFor('"we did call you" == g:Ch_reply')
258 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100259endfunc
260
261func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100262 call ch_log('Test_channel_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200263 let s:chopt.callback = 'Ch_handler'
264 call s:run_server('Ch_channel_handler')
265 let s:chopt.callback = function('Ch_handler')
266 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100267 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100268endfunc
269
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100270"""""""""
271
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200272let g:Ch_reply = ''
273func Ch_zeroHandler(chan, msg)
274 unlet g:Ch_reply
275 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100276endfunc
277
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200278let g:Ch_zero_reply = ''
279func Ch_oneHandler(chan, msg)
280 unlet g:Ch_zero_reply
281 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100282endfunc
283
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200284func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100285 let handle = ch_open('localhost:' . a:port, s:chopt)
286 if ch_status(handle) == "fail"
287 call assert_false(1, "Can't open channel")
288 return
289 endif
290
291 " Check that eval works.
292 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
293
294 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200295 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100296 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100297 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200298 call WaitFor('"zero index" == g:Ch_reply')
299 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100300 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100301 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200302 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100303 endif
304
305 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200306 let g:Ch_reply = ''
307 let g:Ch_zero_reply = ''
308 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
309 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100310 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200311 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100312 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200313 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100314 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200315 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100316endfunc
317
318func Test_zero_reply()
319 call ch_log('Test_zero_reply()')
320 " Run with channel handler
321 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322 let s:chopt.callback = 'Ch_zeroHandler'
323 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100324 unlet s:chopt.callback
325
326 " Run without channel handler
327 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200328 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100329endfunc
330
331"""""""""
332
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333let g:Ch_reply1 = ""
334func Ch_handleRaw1(chan, msg)
335 unlet g:Ch_reply1
336 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100337endfunc
338
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339let g:Ch_reply2 = ""
340func Ch_handleRaw2(chan, msg)
341 unlet g:Ch_reply2
342 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100343endfunc
344
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200345let g:Ch_reply3 = ""
346func Ch_handleRaw3(chan, msg)
347 unlet g:Ch_reply3
348 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100349endfunc
350
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100352 let handle = ch_open('localhost:' . a:port, s:chopt)
353 if ch_status(handle) == "fail"
354 call assert_false(1, "Can't open channel")
355 return
356 endif
357 call ch_setoptions(handle, {'mode': 'raw'})
358
359 " The message are sent raw, we do our own JSON strings here.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200360 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 'Ch_handleRaw1'})
361 call WaitFor('g:Ch_reply1 != ""')
362 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
363 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 'Ch_handleRaw2'})
364 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 'Ch_handleRaw3'})
365 call WaitFor('g:Ch_reply2 != ""')
366 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100367 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200368 call WaitFor('g:Ch_reply3 != ""')
369 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100370endfunc
371
372func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100373 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200374 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100375endfunc
376
377"""""""""
378
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100379" Test that trying to connect to a non-existing port fails quickly.
380func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100381 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100382 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100383 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100384 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100385 " Oops, port does exists.
386 call ch_close(handle)
387 else
388 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100389 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100390 endif
391
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100392 " We intend to use a socket that doesn't exist and wait for half a second
393 " before giving up. If the socket does exist it can fail in various ways.
394 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100395 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100396 try
397 let handle = ch_open('localhost:9867', {'waittime': 500})
398 if ch_status(handle) != "fail"
399 " Oops, port does exists.
400 call ch_close(handle)
401 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100402 " Failed connection should wait about 500 msec. Can be longer if the
403 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100404 let elapsed = reltime(start)
405 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100406 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100407 endif
408 catch
409 if v:exception !~ 'Connection reset by peer'
410 call assert_false(1, "Caught exception: " . v:exception)
411 endif
412 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100413endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100414
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100415"""""""""
416
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100417func Test_raw_pipe()
418 if !has('job')
419 return
420 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100421 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100422 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200423 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100424 call assert_equal("run", job_status(job))
425 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100426 " For a change use the job where a channel is expected.
427 call ch_sendraw(job, "echo something\n")
428 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100429 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
430
Bram Moolenaar151f6562016-03-07 21:19:38 +0100431 call ch_sendraw(job, "double this\n")
432 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100433 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
434
Bram Moolenaar151f6562016-03-07 21:19:38 +0100435 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100436 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
437 finally
438 call job_stop(job)
439 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100440
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200441 let g:Ch_job = job
442 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100443 let info = job_info(job)
444 call assert_equal("dead", info.status)
445 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100446endfunc
447
448func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100449 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100450 return
451 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100452 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100453 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100454 call assert_equal("run", job_status(job))
455 try
456 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100457 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100458 call assert_equal("something", ch_readraw(handle))
459
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100460 call ch_sendraw(handle, "echoerr wrong\n")
461 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
462
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100463 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100464 call assert_equal("this", ch_readraw(handle))
465 call assert_equal("AND this", ch_readraw(handle))
466
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200467 call ch_sendraw(handle, "split this line\n")
468 call assert_equal("this linethis linethis line", ch_readraw(handle))
469
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100470 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100472 finally
473 call job_stop(job)
474 endtry
475endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100476
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100477func Test_nl_err_to_out_pipe()
478 if !has('job')
479 return
480 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100481 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100482 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100483 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100484 call assert_equal("run", job_status(job))
485 try
486 let handle = job_getchannel(job)
487 call ch_sendraw(handle, "echo something\n")
488 call assert_equal("something", ch_readraw(handle))
489
490 call ch_sendraw(handle, "echoerr wrong\n")
491 call assert_equal("wrong", ch_readraw(handle))
492 finally
493 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100494 call ch_logfile('')
495 let loglines = readfile('Xlog')
496 call assert_true(len(loglines) > 10)
497 let found_test = 0
498 let found_send = 0
499 let found_recv = 0
500 let found_stop = 0
501 for l in loglines
502 if l =~ 'Test_nl_err_to_out_pipe'
503 let found_test = 1
504 endif
505 if l =~ 'SEND on.*echo something'
506 let found_send = 1
507 endif
508 if l =~ 'RECV on.*something'
509 let found_recv = 1
510 endif
511 if l =~ 'Stopping job with'
512 let found_stop = 1
513 endif
514 endfor
515 call assert_equal(1, found_test)
516 call assert_equal(1, found_send)
517 call assert_equal(1, found_recv)
518 call assert_equal(1, found_stop)
519 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100520 endtry
521endfunc
522
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100523func Test_nl_read_file()
524 if !has('job')
525 return
526 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100527 call ch_log('Test_nl_read_file()')
528 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
529 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100530 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100531 call assert_equal("run", job_status(job))
532 try
533 let handle = job_getchannel(job)
534 call assert_equal("something", ch_readraw(handle))
535 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
536 call assert_equal("this", ch_readraw(handle))
537 call assert_equal("AND this", ch_readraw(handle))
538 finally
539 call job_stop(job)
540 call delete('Xinput')
541 endtry
542endfunc
543
Bram Moolenaare98d1212016-03-08 15:37:41 +0100544func Test_nl_write_out_file()
545 if !has('job')
546 return
547 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100548 call ch_log('Test_nl_write_out_file()')
549 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100550 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100551 call assert_equal("run", job_status(job))
552 try
553 let handle = job_getchannel(job)
554 call ch_sendraw(handle, "echo line one\n")
555 call ch_sendraw(handle, "echo line two\n")
556 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200557 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100558 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
559 finally
560 call job_stop(job)
561 call delete('Xoutput')
562 endtry
563endfunc
564
565func Test_nl_write_err_file()
566 if !has('job')
567 return
568 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100569 call ch_log('Test_nl_write_err_file()')
570 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100571 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100572 call assert_equal("run", job_status(job))
573 try
574 let handle = job_getchannel(job)
575 call ch_sendraw(handle, "echoerr line one\n")
576 call ch_sendraw(handle, "echoerr line two\n")
577 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200578 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100579 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
580 finally
581 call job_stop(job)
582 call delete('Xoutput')
583 endtry
584endfunc
585
586func Test_nl_write_both_file()
587 if !has('job')
588 return
589 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100590 call ch_log('Test_nl_write_both_file()')
591 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100592 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100593 call assert_equal("run", job_status(job))
594 try
595 let handle = job_getchannel(job)
596 call ch_sendraw(handle, "echoerr line one\n")
597 call ch_sendraw(handle, "echo line two\n")
598 call ch_sendraw(handle, "double this\n")
599 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200600 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100601 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
602 finally
603 call job_stop(job)
604 call delete('Xoutput')
605 endtry
606endfunc
607
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200608func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200609 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200610endfunc
611
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200612func Run_test_pipe_to_buffer(use_name, nomod)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100613 if !has('job')
614 return
615 endif
616 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200617 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200618 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100619 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100620 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100621 let firstline = 'Reading from channel output...'
622 else
623 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100624 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100625 quit
626 let firstline = ''
627 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200628 if a:nomod
629 let options['out_modifiable'] = 0
630 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100631 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100632 call assert_equal("run", job_status(job))
633 try
634 let handle = job_getchannel(job)
635 call ch_sendraw(handle, "echo line one\n")
636 call ch_sendraw(handle, "echo line two\n")
637 call ch_sendraw(handle, "double this\n")
638 call ch_sendraw(handle, "quit\n")
639 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200640 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100641 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200642 if a:nomod
643 call assert_equal(0, &modifiable)
644 else
645 call assert_equal(1, &modifiable)
646 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200647 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100648 bwipe!
649 finally
650 call job_stop(job)
651 endtry
652endfunc
653
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100654func Test_pipe_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200655 call Run_test_pipe_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100656endfunc
657
658func Test_pipe_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200659 call Run_test_pipe_to_buffer(0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100660endfunc
661
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200662func Test_pipe_to_buffer_name_nomod()
663 call Run_test_pipe_to_buffer(1, 1)
664endfunc
665
666func Run_test_pipe_err_to_buffer(use_name, nomod)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100667 if !has('job')
668 return
669 endif
670 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100671 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100672 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100673 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100674 let firstline = 'Reading from channel error...'
675 else
676 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100677 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100678 quit
679 let firstline = ''
680 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200681 if a:nomod
682 let options['err_modifiable'] = 0
683 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100684 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100685 call assert_equal("run", job_status(job))
686 try
687 let handle = job_getchannel(job)
688 call ch_sendraw(handle, "echoerr line one\n")
689 call ch_sendraw(handle, "echoerr line two\n")
690 call ch_sendraw(handle, "doubleerr this\n")
691 call ch_sendraw(handle, "quit\n")
692 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200693 call WaitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100694 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200695 if a:nomod
696 call assert_equal(0, &modifiable)
697 else
698 call assert_equal(1, &modifiable)
699 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100700 bwipe!
701 finally
702 call job_stop(job)
703 endtry
704endfunc
705
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100706func Test_pipe_err_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200707 call Run_test_pipe_err_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100708endfunc
709
710func Test_pipe_err_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200711 call Run_test_pipe_err_to_buffer(0, 0)
712endfunc
713
714func Test_pipe_err_to_buffer_name_nomod()
715 call Run_test_pipe_err_to_buffer(1, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100716endfunc
717
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100718func Test_pipe_both_to_buffer()
719 if !has('job')
720 return
721 endif
722 call ch_log('Test_pipe_both_to_buffer()')
723 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100724 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100725 call assert_equal("run", job_status(job))
726 try
727 let handle = job_getchannel(job)
728 call ch_sendraw(handle, "echo line one\n")
729 call ch_sendraw(handle, "echoerr line two\n")
730 call ch_sendraw(handle, "double this\n")
731 call ch_sendraw(handle, "doubleerr that\n")
732 call ch_sendraw(handle, "quit\n")
733 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200734 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100735 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
736 bwipe!
737 finally
738 call job_stop(job)
739 endtry
740endfunc
741
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100742func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100743 if !has('job')
744 return
745 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100746 call ch_log('Test_pipe_from_buffer()')
747
748 sp pipe-input
749 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200750 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100751 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100752 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100753 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100754 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100755 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100756
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100758 call assert_equal("run", job_status(job))
759 try
760 let handle = job_getchannel(job)
761 call assert_equal('one', ch_read(handle))
762 call assert_equal('two', ch_read(handle))
763 call assert_equal('three', ch_read(handle))
764 bwipe!
765 finally
766 call job_stop(job)
767 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100768endfunc
769
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770func Test_pipe_from_buffer_name()
771 call Run_test_pipe_from_buffer(1)
772endfunc
773
774func Test_pipe_from_buffer_nr()
775 call Run_test_pipe_from_buffer(0)
776endfunc
777
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100778func Test_pipe_to_nameless_buffer()
779 if !has('job')
780 return
781 endif
782 call ch_log('Test_pipe_to_nameless_buffer()')
783 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100784 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100785 call assert_equal("run", job_status(job))
786 try
787 let handle = job_getchannel(job)
788 call ch_sendraw(handle, "echo line one\n")
789 call ch_sendraw(handle, "echo line two\n")
790 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200791 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100792 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
793 bwipe!
794 finally
795 call job_stop(job)
796 endtry
797endfunc
798
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100799func Test_pipe_to_buffer_json()
800 if !has('job')
801 return
802 endif
803 call ch_log('Test_pipe_to_buffer_json()')
804 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100805 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100806 call assert_equal("run", job_status(job))
807 try
808 let handle = job_getchannel(job)
809 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
810 call ch_sendraw(handle, "echo [-2, 12.34]\n")
811 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200812 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100813 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
814 bwipe!
815 finally
816 call job_stop(job)
817 endtry
818endfunc
819
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100820" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100821func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100822 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100823 if getline(line('$') - a:offset) == a:line
824 break
825 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100826 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100827 endfor
828endfunc
829
830func Test_pipe_io_two_buffers()
831 if !has('job')
832 return
833 endif
834 call ch_log('Test_pipe_io_two_buffers()')
835
836 " Create two buffers, one to read from and one to write to.
837 split pipe-output
838 set buftype=nofile
839 split pipe-input
840 set buftype=nofile
841
842 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100843 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200844 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
845 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100846 call assert_equal("run", job_status(job))
847 try
848 exe "normal Gaecho hello\<CR>"
849 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100850 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100851 call assert_equal('hello', getline('$'))
852
853 exe bufwinnr('pipe-input') . "wincmd w"
854 exe "normal Gadouble this\<CR>"
855 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100856 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100857 call assert_equal('this', getline(line('$') - 1))
858 call assert_equal('AND this', getline('$'))
859
860 bwipe!
861 exe bufwinnr('pipe-input') . "wincmd w"
862 bwipe!
863 finally
864 call job_stop(job)
865 endtry
866endfunc
867
868func Test_pipe_io_one_buffer()
869 if !has('job')
870 return
871 endif
872 call ch_log('Test_pipe_io_one_buffer()')
873
874 " Create one buffer to read from and to write to.
875 split pipe-io
876 set buftype=nofile
877
878 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100879 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200880 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
881 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100882 call assert_equal("run", job_status(job))
883 try
884 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100885 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100886 call assert_equal('hello', getline(line('$') - 1))
887
888 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100889 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100890 call assert_equal('this', getline(line('$') - 2))
891 call assert_equal('AND this', getline(line('$') - 1))
892
893 bwipe!
894 finally
895 call job_stop(job)
896 endtry
897endfunc
898
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100899func Test_pipe_null()
900 if !has('job')
901 return
902 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100903 call ch_log('Test_pipe_null()')
904
905 " We cannot check that no I/O works, we only check that the job starts
906 " properly.
907 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100908 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100909 call assert_equal("run", job_status(job))
910 try
911 call assert_equal('something', ch_read(job))
912 finally
913 call job_stop(job)
914 endtry
915
916 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100917 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100918 call assert_equal("run", job_status(job))
919 try
920 call assert_equal('err-out', ch_read(job, {"part": "err"}))
921 finally
922 call job_stop(job)
923 endtry
924
925 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100926 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100927 call assert_equal("run", job_status(job))
928 try
929 call assert_equal('something', ch_read(job))
930 finally
931 call job_stop(job)
932 endtry
933
934 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100935 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100936 call assert_equal("run", job_status(job))
937 call job_stop(job)
938
939 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100940 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100941 call assert_equal("run", job_status(job))
942 call assert_equal('channel fail', string(job_getchannel(job)))
943 call assert_equal('fail', ch_status(job))
944 call job_stop(job)
945endfunc
946
Bram Moolenaaradb78a72016-06-27 21:10:31 +0200947func Test_pipe_to_buffer_raw()
948 if !has('job')
949 return
950 endif
951 call ch_log('Test_raw_pipe_to_buffer()')
952 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
953 split testout
954 let job = job_start([s:python, '-c',
955 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
956 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200957 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +0200958 try
959 for line in getline(2, '$')
960 let line = substitute(line, '^\.*', '', '')
961 call assert_equal('', line)
962 endfor
963 finally
964 call job_stop(job)
965 bwipe!
966 endtry
967endfunc
968
Bram Moolenaarde279892016-03-11 22:19:44 +0100969func Test_reuse_channel()
970 if !has('job')
971 return
972 endif
973 call ch_log('Test_reuse_channel()')
974
975 let job = job_start(s:python . " test_channel_pipe.py")
976 call assert_equal("run", job_status(job))
977 let handle = job_getchannel(job)
978 try
979 call ch_sendraw(handle, "echo something\n")
980 call assert_equal("something", ch_readraw(handle))
981 finally
982 call job_stop(job)
983 endtry
984
985 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
986 call assert_equal("run", job_status(job))
987 let handle = job_getchannel(job)
988 try
989 call ch_sendraw(handle, "echo again\n")
990 call assert_equal("again", ch_readraw(handle))
991 finally
992 call job_stop(job)
993 endtry
994endfunc
995
Bram Moolenaar75f72652016-03-20 22:16:56 +0100996func Test_out_cb()
997 if !has('job')
998 return
999 endif
1000 call ch_log('Test_out_cb()')
1001
1002 let dict = {'thisis': 'dict: '}
1003 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001004 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001005 endfunc
1006 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001007 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001008 endfunc
1009 let job = job_start(s:python . " test_channel_pipe.py",
1010 \ {'out_cb': dict.outHandler,
1011 \ 'out_mode': 'json',
1012 \ 'err_cb': dict.errHandler,
1013 \ 'err_mode': 'json'})
1014 call assert_equal("run", job_status(job))
1015 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001016 let g:Ch_outmsg = ''
1017 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001018 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1019 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001020 call WaitFor('g:Ch_outmsg != ""')
1021 call assert_equal("dict: hello", g:Ch_outmsg)
1022 call WaitFor('g:Ch_errmsg != ""')
1023 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001024 finally
1025 call job_stop(job)
1026 endtry
1027endfunc
1028
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001029func Test_out_close_cb()
1030 if !has('job')
1031 return
1032 endif
1033 call ch_log('Test_out_close_cb()')
1034
1035 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001036 let g:Ch_msg1 = ''
1037 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001038 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001039 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001040 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001041 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001042 let s:counter += 1
1043 endfunc
1044 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001045 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001046 let s:counter += 1
1047 endfunc
1048 let job = job_start(s:python . " test_channel_pipe.py quit now",
1049 \ {'out_cb': 'OutHandler',
1050 \ 'close_cb': 'CloseHandler'})
1051 call assert_equal("run", job_status(job))
1052 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001053 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1054 call assert_equal('quit', g:Ch_msg1)
1055 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001056 finally
1057 call job_stop(job)
1058 delfunc OutHandler
1059 delfunc CloseHandler
1060 endtry
1061endfunc
1062
Bram Moolenaar437905c2016-04-26 19:01:05 +02001063func Test_read_in_close_cb()
1064 if !has('job')
1065 return
1066 endif
1067 call ch_log('Test_read_in_close_cb()')
1068
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001069 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001070 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001071 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001072 endfunc
1073 let job = job_start(s:python . " test_channel_pipe.py quit now",
1074 \ {'close_cb': 'CloseHandler'})
1075 call assert_equal("run", job_status(job))
1076 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001077 call WaitFor('g:Ch_received != ""')
1078 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001079 finally
1080 call job_stop(job)
1081 delfunc CloseHandler
1082 endtry
1083endfunc
1084
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001085func Test_out_cb_lambda()
1086 if !has('job')
1087 return
1088 endif
1089 call ch_log('Test_out_cb_lambda()')
1090
1091 let job = job_start(s:python . " test_channel_pipe.py",
1092 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1093 \ 'out_mode': 'json',
1094 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1095 \ 'err_mode': 'json'})
1096 call assert_equal("run", job_status(job))
1097 try
1098 let g:Ch_outmsg = ''
1099 let g:Ch_errmsg = ''
1100 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1101 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1102 call WaitFor('g:Ch_outmsg != ""')
1103 call assert_equal("lambda: hello", g:Ch_outmsg)
1104 call WaitFor('g:Ch_errmsg != ""')
1105 call assert_equal("lambda: there", g:Ch_errmsg)
1106 finally
1107 call job_stop(job)
1108 endtry
1109endfunc
1110
Bram Moolenaard46ae142016-02-16 13:33:52 +01001111""""""""""
1112
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001113let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001114func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001115 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001116 unlet s:channelfd
1117endfunc
1118
1119" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001120func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001121 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001122 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001123 call WaitFor('"what?" == g:Ch_unletResponse')
1124 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001125endfunc
1126
1127func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001128 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001129 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001130endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001131
Bram Moolenaard46ae142016-02-16 13:33:52 +01001132""""""""""
1133
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001134let g:Ch_unletResponse = ''
1135func Ch_CloseHandler(handle, msg)
1136 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001137 call ch_close(s:channelfd)
1138endfunc
1139
1140" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001141func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001142 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001143 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1144 call WaitFor('"what?" == g:Ch_unletResponse')
1145 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001146endfunc
1147
1148func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001149 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001150 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001151endfunc
1152
1153""""""""""
1154
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001155func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001156 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001157 silent! let ch = ch_open("noserver")
1158 echo ch
1159 let d = ch
1160endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001161
1162""""""""""
1163
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001164func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001165 " Wait up to a second for the port to open.
1166 let s:chopt.waittime = 1000
1167 let channel = ch_open('localhost:' . a:port, s:chopt)
1168 unlet s:chopt.waittime
1169 if ch_status(channel) == "fail"
1170 call assert_false(1, "Can't open channel")
1171 return
1172 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001173 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001174 call ch_close(channel)
1175endfunc
1176
1177func Test_open_delay()
1178 call ch_log('Test_open_delay()')
1179 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001180 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001181endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001182
1183"""""""""
1184
1185function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001186 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001187endfunc
1188
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001189function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001190 let handle = ch_open('localhost:' . a:port, s:chopt)
1191 if ch_status(handle) == "fail"
1192 call assert_false(1, "Can't open channel")
1193 return
1194 endif
1195
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001196 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001197 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001198 call WaitFor('len(g:Ch_call_ret) > 0')
1199 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001200endfunc
1201
1202func Test_call()
1203 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001204 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001205endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001206
1207"""""""""
1208
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001209let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001210function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001211 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001212endfunc
1213
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001214function Ch_test_exit_callback(port)
1215 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1216 let g:Ch_exit_job = g:currentJob
1217 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001218endfunc
1219
1220func Test_exit_callback()
1221 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001222 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001223 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001224
Bram Moolenaar9730f742016-02-28 19:50:51 +01001225 " wait up to a second for the job to exit
1226 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001227 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001228 break
1229 endif
1230 sleep 10m
1231 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001232 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001233 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001234
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001235 call assert_equal('done', g:Ch_job_exit_ret)
1236 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1237 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001238 endif
1239endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001240
1241"""""""""
1242
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001243let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001244function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001245 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001246endfunc
1247
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001248function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001249 let handle = ch_open('localhost:' . a:port, s:chopt)
1250 if ch_status(handle) == "fail"
1251 call assert_false(1, "Can't open channel")
1252 return
1253 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001254 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001255
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001256 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001257 call WaitFor('"closed" == g:Ch_close_ret')
1258 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001259endfunc
1260
1261func Test_close_callback()
1262 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001263 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001264endfunc
1265
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001266function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001267 let handle = ch_open('localhost:' . a:port, s:chopt)
1268 if ch_status(handle) == "fail"
1269 call assert_false(1, "Can't open channel")
1270 return
1271 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001272 let g:Ch_d = {}
1273 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001274 let self.close_ret = 'closed'
1275 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001276 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001277
1278 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001279 call WaitFor('"closed" == g:Ch_d.close_ret')
1280 call assert_equal('closed', g:Ch_d.close_ret)
1281 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001282endfunc
1283
1284func Test_close_partial()
1285 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001286 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001287endfunc
1288
Bram Moolenaar80385682016-03-27 19:13:35 +02001289func Test_job_start_invalid()
1290 call assert_fails('call job_start($x)', 'E474:')
1291 call assert_fails('call job_start("")', 'E474:')
1292endfunc
1293
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001294" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001295func Test_partial_in_channel_cycle()
1296 let d = {}
1297 let d.a = function('string', [d])
1298 try
1299 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1300 catch
1301 call assert_exception('E901:')
1302 endtry
1303 unlet d
1304endfunc
1305
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001306func Test_using_freed_memory()
1307 let g:a = job_start(['ls'])
1308 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001309 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001310endfunc
1311
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001312func Test_collapse_buffers()
1313 if !executable('cat')
1314 return
1315 endif
1316 sp test_channel.vim
1317 let g:linecount = line('$')
1318 close
1319 split testout
1320 1,$delete
1321 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001322 call WaitFor('line("$") > g:linecount')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001323 call assert_true(line('$') > g:linecount)
1324 bwipe!
1325endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001326
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001327function Ch_test_close_lambda(port)
1328 let handle = ch_open('localhost:' . a:port, s:chopt)
1329 if ch_status(handle) == "fail"
1330 call assert_false(1, "Can't open channel")
1331 return
1332 endif
1333 let g:Ch_close_ret = ''
1334 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1335
1336 call assert_equal('', ch_evalexpr(handle, 'close me'))
1337 call WaitFor('"closed" == g:Ch_close_ret')
1338 call assert_equal('closed', g:Ch_close_ret)
1339endfunc
1340
1341func Test_close_lambda()
1342 call ch_log('Test_close_lambda()')
1343 call s:run_server('Ch_test_close_lambda')
1344endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001345
Bram Moolenaar9730f742016-02-28 19:50:51 +01001346" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001347 call ch_logfile('channellog', 'w')