blob: 6ecbc789aa6d52f2e626cfb8513e591406a4ba20 [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 Moolenaarf1f07922016-08-26 17:58:53 +020058 " string with ][ should work
59 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
60
61 " sending three messages quickly then reading should work
62 for i in range(3)
63 call ch_sendexpr(handle, 'echo hello ' . i)
64 endfor
65 call assert_equal('hello 0', ch_read(handle)[1])
66 call assert_equal('hello 1', ch_read(handle)[1])
67 call assert_equal('hello 2', ch_read(handle)[1])
68
Bram Moolenaard7ece102016-02-02 23:23:02 +010069 " Request that triggers sending two ex commands. These will usually be
70 " handled before getting the response, but it's not guaranteed, thus wait a
71 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010072 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020073 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010074 call assert_equal('added1', getline(line('$') - 1))
75 call assert_equal('added2', getline('$'))
76
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010077 " Request command "foo bar", which fails silently.
78 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020079 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020080 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010081
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010082 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020083 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010084 call assert_equal('added more', getline('$'))
85
Bram Moolenaara07fec92016-02-05 21:04:08 +010086 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020087 call ch_sendexpr(handle, 'hello!', {'callback': '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 Moolenaara07fec92016-02-05 21:04:08 +010096
Bram Moolenaar321efdd2016-07-15 17:09:11 +020097 let g:Ch_responseMsg = ''
98 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
99 call WaitFor('exists("g:Ch_responseHandle")')
100 if !exists('g:Ch_responseHandle')
101 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100102 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200103 call assert_equal(handle, g:Ch_responseHandle)
104 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100105 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200106 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100107
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200108 " Using lambda.
109 let g:Ch_responseMsg = ''
110 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
111 call WaitFor('exists("g:Ch_responseHandle")')
112 if !exists('g:Ch_responseHandle')
113 call assert_false(1, 'g:Ch_responseHandle was not set')
114 else
115 call assert_equal(handle, g:Ch_responseHandle)
116 unlet g:Ch_responseHandle
117 endif
118 call assert_equal('got it', g:Ch_responseMsg)
119
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100120 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200121 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100122
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100123 " check setting options (without testing the effect)
124 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100125 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100126 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100127 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100128 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100129
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100130 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100131 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100132 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100133 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100134
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100135 " Send an eval request with special characters.
136 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
137 sleep 10m
138 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
139
140 " Send an eval request to get a line with special characters.
141 call setline(3, "a\nb\<CR>c\x01d\x7fe")
142 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
143 sleep 10m
144 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
145
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100146 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100147 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100148 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100149 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100150
Bram Moolenaar55fab432016-02-07 16:53:13 +0100151 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100152 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100153 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100154 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100155
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100156 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100157 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100158 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100159 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100160
Bram Moolenaarf4160862016-02-05 23:09:12 +0100161 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100162 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200163 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100164 call assert_equal('one', getline(line('$') - 2))
165 call assert_equal('two', getline(line('$') - 1))
166 call assert_equal('three', getline('$'))
167
168 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100169 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
170 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100171
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100173
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100174 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100175 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
176 let start = reltime()
177 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
178 let elapsed = reltime(start)
179 call assert_true(reltimefloat(elapsed) > 0.3)
180 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100181
182 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100183 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100184 let resp = ch_read(handle)
185 call assert_equal(type([]), type(resp))
186 call assert_equal(type(11), type(resp[0]))
187 call assert_equal('waited', resp[1])
188
Bram Moolenaard7ece102016-02-02 23:23:02 +0100189 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100191endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100192
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100193func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200195 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100196endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100197
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100198" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200199func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100200 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200201 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100202 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100203 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100204 return
205 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100206
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100207 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100208
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100210 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100211 call assert_false(1, "Can't open second channel")
212 return
213 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100214 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
215 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100216
217 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100219
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220 call ch_close(newhandle)
221endfunc
222
223func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100224 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200225 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100226endfunc
227
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100228" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200229func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100230 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100233 return
234 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100236 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100237
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100238 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239endfunc
240
241func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100242 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200243 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100244endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100245
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100246"""""""""
247
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200248let g:Ch_reply = ""
249func Ch_handler(chan, msg)
250 unlet g:Ch_reply
251 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100252endfunc
253
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200254func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100255 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100256 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100257 call assert_false(1, "Can't open channel")
258 return
259 endif
260
261 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100262 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200263 call WaitFor('"we called you" == g:Ch_reply')
264 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100265
266 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100267 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200268 call WaitFor('"we did call you" == g:Ch_reply')
269 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100270endfunc
271
272func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100273 call ch_log('Test_channel_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200274 let s:chopt.callback = 'Ch_handler'
275 call s:run_server('Ch_channel_handler')
276 let s:chopt.callback = function('Ch_handler')
277 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100278 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100279endfunc
280
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100281"""""""""
282
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200283let g:Ch_reply = ''
284func Ch_zeroHandler(chan, msg)
285 unlet g:Ch_reply
286 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100287endfunc
288
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200289let g:Ch_zero_reply = ''
290func Ch_oneHandler(chan, msg)
291 unlet g:Ch_zero_reply
292 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100293endfunc
294
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200295func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100296 let handle = ch_open('localhost:' . a:port, s:chopt)
297 if ch_status(handle) == "fail"
298 call assert_false(1, "Can't open channel")
299 return
300 endif
301
302 " Check that eval works.
303 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
304
305 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200306 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100307 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100308 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200309 call WaitFor('"zero index" == g:Ch_reply')
310 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100311 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100312 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200313 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100314 endif
315
316 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200317 let g:Ch_reply = ''
318 let g:Ch_zero_reply = ''
319 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
320 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100321 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200324 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100325 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200326 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100327endfunc
328
329func Test_zero_reply()
330 call ch_log('Test_zero_reply()')
331 " Run with channel handler
332 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333 let s:chopt.callback = 'Ch_zeroHandler'
334 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 unlet s:chopt.callback
336
337 " Run without channel handler
338 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340endfunc
341
342"""""""""
343
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200344let g:Ch_reply1 = ""
345func Ch_handleRaw1(chan, msg)
346 unlet g:Ch_reply1
347 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100348endfunc
349
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200350let g:Ch_reply2 = ""
351func Ch_handleRaw2(chan, msg)
352 unlet g:Ch_reply2
353 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100354endfunc
355
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200356let g:Ch_reply3 = ""
357func Ch_handleRaw3(chan, msg)
358 unlet g:Ch_reply3
359 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100360endfunc
361
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200362func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100363 let handle = ch_open('localhost:' . a:port, s:chopt)
364 if ch_status(handle) == "fail"
365 call assert_false(1, "Can't open channel")
366 return
367 endif
368 call ch_setoptions(handle, {'mode': 'raw'})
369
370 " The message are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200371 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200372 call WaitFor('g:Ch_reply1 != ""')
373 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200374 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
375 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376 call WaitFor('g:Ch_reply2 != ""')
377 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100378 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200379 call WaitFor('g:Ch_reply3 != ""')
380 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100381endfunc
382
383func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100384 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200385 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100386endfunc
387
388"""""""""
389
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100390" Test that trying to connect to a non-existing port fails quickly.
391func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100392 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100393 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100394 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100395 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100396 " Oops, port does exists.
397 call ch_close(handle)
398 else
399 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100400 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100401 endif
402
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100403 " We intend to use a socket that doesn't exist and wait for half a second
404 " before giving up. If the socket does exist it can fail in various ways.
405 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100406 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100407 try
408 let handle = ch_open('localhost:9867', {'waittime': 500})
409 if ch_status(handle) != "fail"
410 " Oops, port does exists.
411 call ch_close(handle)
412 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100413 " Failed connection should wait about 500 msec. Can be longer if the
414 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100415 let elapsed = reltime(start)
416 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100417 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100418 endif
419 catch
420 if v:exception !~ 'Connection reset by peer'
421 call assert_false(1, "Caught exception: " . v:exception)
422 endif
423 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100424endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100425
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100426"""""""""
427
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100428func Test_raw_pipe()
429 if !has('job')
430 return
431 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100432 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100433 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200434 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100435 call assert_equal("run", job_status(job))
436 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100437 " For a change use the job where a channel is expected.
438 call ch_sendraw(job, "echo something\n")
439 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100440 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
441
Bram Moolenaar151f6562016-03-07 21:19:38 +0100442 call ch_sendraw(job, "double this\n")
443 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100444 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
445
Bram Moolenaar151f6562016-03-07 21:19:38 +0100446 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100447 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
448 finally
449 call job_stop(job)
450 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100451
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200452 let g:Ch_job = job
453 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100454 let info = job_info(job)
455 call assert_equal("dead", info.status)
456 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100457endfunc
458
459func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100460 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100461 return
462 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100463 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100464 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100465 call assert_equal("run", job_status(job))
466 try
467 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100468 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100469 call assert_equal("something", ch_readraw(handle))
470
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100471 call ch_sendraw(handle, "echoerr wrong\n")
472 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
473
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100474 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100475 call assert_equal("this", ch_readraw(handle))
476 call assert_equal("AND this", ch_readraw(handle))
477
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200478 call ch_sendraw(handle, "split this line\n")
479 call assert_equal("this linethis linethis line", ch_readraw(handle))
480
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100481 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100482 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100483 finally
484 call job_stop(job)
485 endtry
486endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100487
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100488func Test_nl_err_to_out_pipe()
489 if !has('job')
490 return
491 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100492 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100493 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100494 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100495 call assert_equal("run", job_status(job))
496 try
497 let handle = job_getchannel(job)
498 call ch_sendraw(handle, "echo something\n")
499 call assert_equal("something", ch_readraw(handle))
500
501 call ch_sendraw(handle, "echoerr wrong\n")
502 call assert_equal("wrong", ch_readraw(handle))
503 finally
504 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100505 call ch_logfile('')
506 let loglines = readfile('Xlog')
507 call assert_true(len(loglines) > 10)
508 let found_test = 0
509 let found_send = 0
510 let found_recv = 0
511 let found_stop = 0
512 for l in loglines
513 if l =~ 'Test_nl_err_to_out_pipe'
514 let found_test = 1
515 endif
516 if l =~ 'SEND on.*echo something'
517 let found_send = 1
518 endif
519 if l =~ 'RECV on.*something'
520 let found_recv = 1
521 endif
522 if l =~ 'Stopping job with'
523 let found_stop = 1
524 endif
525 endfor
526 call assert_equal(1, found_test)
527 call assert_equal(1, found_send)
528 call assert_equal(1, found_recv)
529 call assert_equal(1, found_stop)
530 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100531 endtry
532endfunc
533
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100534func Test_nl_read_file()
535 if !has('job')
536 return
537 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100538 call ch_log('Test_nl_read_file()')
539 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
540 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100541 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100542 call assert_equal("run", job_status(job))
543 try
544 let handle = job_getchannel(job)
545 call assert_equal("something", ch_readraw(handle))
546 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
547 call assert_equal("this", ch_readraw(handle))
548 call assert_equal("AND this", ch_readraw(handle))
549 finally
550 call job_stop(job)
551 call delete('Xinput')
552 endtry
553endfunc
554
Bram Moolenaare98d1212016-03-08 15:37:41 +0100555func Test_nl_write_out_file()
556 if !has('job')
557 return
558 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100559 call ch_log('Test_nl_write_out_file()')
560 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100561 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100562 call assert_equal("run", job_status(job))
563 try
564 let handle = job_getchannel(job)
565 call ch_sendraw(handle, "echo line one\n")
566 call ch_sendraw(handle, "echo line two\n")
567 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200568 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100569 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
570 finally
571 call job_stop(job)
572 call delete('Xoutput')
573 endtry
574endfunc
575
576func Test_nl_write_err_file()
577 if !has('job')
578 return
579 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100580 call ch_log('Test_nl_write_err_file()')
581 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100582 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100583 call assert_equal("run", job_status(job))
584 try
585 let handle = job_getchannel(job)
586 call ch_sendraw(handle, "echoerr line one\n")
587 call ch_sendraw(handle, "echoerr line two\n")
588 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200589 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100590 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
591 finally
592 call job_stop(job)
593 call delete('Xoutput')
594 endtry
595endfunc
596
597func Test_nl_write_both_file()
598 if !has('job')
599 return
600 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100601 call ch_log('Test_nl_write_both_file()')
602 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100603 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100604 call assert_equal("run", job_status(job))
605 try
606 let handle = job_getchannel(job)
607 call ch_sendraw(handle, "echoerr line one\n")
608 call ch_sendraw(handle, "echo line two\n")
609 call ch_sendraw(handle, "double this\n")
610 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200611 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100612 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
613 finally
614 call job_stop(job)
615 call delete('Xoutput')
616 endtry
617endfunc
618
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200619func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200620 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200621endfunc
622
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200623func Run_test_pipe_to_buffer(use_name, nomod)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100624 if !has('job')
625 return
626 endif
627 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200628 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200629 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100630 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100631 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100632 let firstline = 'Reading from channel output...'
633 else
634 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100635 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100636 quit
637 let firstline = ''
638 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200639 if a:nomod
640 let options['out_modifiable'] = 0
641 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100642 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100643 call assert_equal("run", job_status(job))
644 try
645 let handle = job_getchannel(job)
646 call ch_sendraw(handle, "echo line one\n")
647 call ch_sendraw(handle, "echo line two\n")
648 call ch_sendraw(handle, "double this\n")
649 call ch_sendraw(handle, "quit\n")
650 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200651 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100652 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200653 if a:nomod
654 call assert_equal(0, &modifiable)
655 else
656 call assert_equal(1, &modifiable)
657 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200658 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100659 bwipe!
660 finally
661 call job_stop(job)
662 endtry
663endfunc
664
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100665func Test_pipe_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200666 call Run_test_pipe_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100667endfunc
668
669func Test_pipe_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200670 call Run_test_pipe_to_buffer(0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100671endfunc
672
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200673func Test_pipe_to_buffer_name_nomod()
674 call Run_test_pipe_to_buffer(1, 1)
675endfunc
676
677func Run_test_pipe_err_to_buffer(use_name, nomod)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100678 if !has('job')
679 return
680 endif
681 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100682 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100684 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100685 let firstline = 'Reading from channel error...'
686 else
687 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100688 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100689 quit
690 let firstline = ''
691 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200692 if a:nomod
693 let options['err_modifiable'] = 0
694 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100696 call assert_equal("run", job_status(job))
697 try
698 let handle = job_getchannel(job)
699 call ch_sendraw(handle, "echoerr line one\n")
700 call ch_sendraw(handle, "echoerr line two\n")
701 call ch_sendraw(handle, "doubleerr this\n")
702 call ch_sendraw(handle, "quit\n")
703 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200704 call WaitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100705 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200706 if a:nomod
707 call assert_equal(0, &modifiable)
708 else
709 call assert_equal(1, &modifiable)
710 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100711 bwipe!
712 finally
713 call job_stop(job)
714 endtry
715endfunc
716
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100717func Test_pipe_err_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200718 call Run_test_pipe_err_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100719endfunc
720
721func Test_pipe_err_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200722 call Run_test_pipe_err_to_buffer(0, 0)
723endfunc
724
725func Test_pipe_err_to_buffer_name_nomod()
726 call Run_test_pipe_err_to_buffer(1, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100727endfunc
728
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100729func Test_pipe_both_to_buffer()
730 if !has('job')
731 return
732 endif
733 call ch_log('Test_pipe_both_to_buffer()')
734 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100735 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100736 call assert_equal("run", job_status(job))
737 try
738 let handle = job_getchannel(job)
739 call ch_sendraw(handle, "echo line one\n")
740 call ch_sendraw(handle, "echoerr line two\n")
741 call ch_sendraw(handle, "double this\n")
742 call ch_sendraw(handle, "doubleerr that\n")
743 call ch_sendraw(handle, "quit\n")
744 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200745 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100746 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
747 bwipe!
748 finally
749 call job_stop(job)
750 endtry
751endfunc
752
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100753func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100754 if !has('job')
755 return
756 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100757 call ch_log('Test_pipe_from_buffer()')
758
759 sp pipe-input
760 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200761 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100762 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100763 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100764 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100765 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100766 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100767
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100768 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100769 call assert_equal("run", job_status(job))
770 try
771 let handle = job_getchannel(job)
772 call assert_equal('one', ch_read(handle))
773 call assert_equal('two', ch_read(handle))
774 call assert_equal('three', ch_read(handle))
775 bwipe!
776 finally
777 call job_stop(job)
778 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100779endfunc
780
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100781func Test_pipe_from_buffer_name()
782 call Run_test_pipe_from_buffer(1)
783endfunc
784
785func Test_pipe_from_buffer_nr()
786 call Run_test_pipe_from_buffer(0)
787endfunc
788
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100789func Test_pipe_to_nameless_buffer()
790 if !has('job')
791 return
792 endif
793 call ch_log('Test_pipe_to_nameless_buffer()')
794 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100795 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100796 call assert_equal("run", job_status(job))
797 try
798 let handle = job_getchannel(job)
799 call ch_sendraw(handle, "echo line one\n")
800 call ch_sendraw(handle, "echo line two\n")
801 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200802 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100803 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
804 bwipe!
805 finally
806 call job_stop(job)
807 endtry
808endfunc
809
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100810func Test_pipe_to_buffer_json()
811 if !has('job')
812 return
813 endif
814 call ch_log('Test_pipe_to_buffer_json()')
815 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100816 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100817 call assert_equal("run", job_status(job))
818 try
819 let handle = job_getchannel(job)
820 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
821 call ch_sendraw(handle, "echo [-2, 12.34]\n")
822 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200823 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100824 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
825 bwipe!
826 finally
827 call job_stop(job)
828 endtry
829endfunc
830
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100831" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100832func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100833 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100834 if getline(line('$') - a:offset) == a:line
835 break
836 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100837 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100838 endfor
839endfunc
840
841func Test_pipe_io_two_buffers()
842 if !has('job')
843 return
844 endif
845 call ch_log('Test_pipe_io_two_buffers()')
846
847 " Create two buffers, one to read from and one to write to.
848 split pipe-output
849 set buftype=nofile
850 split pipe-input
851 set buftype=nofile
852
853 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100854 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200855 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
856 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100857 call assert_equal("run", job_status(job))
858 try
859 exe "normal Gaecho hello\<CR>"
860 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100861 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100862 call assert_equal('hello', getline('$'))
863
864 exe bufwinnr('pipe-input') . "wincmd w"
865 exe "normal Gadouble this\<CR>"
866 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100867 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100868 call assert_equal('this', getline(line('$') - 1))
869 call assert_equal('AND this', getline('$'))
870
871 bwipe!
872 exe bufwinnr('pipe-input') . "wincmd w"
873 bwipe!
874 finally
875 call job_stop(job)
876 endtry
877endfunc
878
879func Test_pipe_io_one_buffer()
880 if !has('job')
881 return
882 endif
883 call ch_log('Test_pipe_io_one_buffer()')
884
885 " Create one buffer to read from and to write to.
886 split pipe-io
887 set buftype=nofile
888
889 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100890 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200891 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
892 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100893 call assert_equal("run", job_status(job))
894 try
895 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100896 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100897 call assert_equal('hello', getline(line('$') - 1))
898
899 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100900 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100901 call assert_equal('this', getline(line('$') - 2))
902 call assert_equal('AND this', getline(line('$') - 1))
903
904 bwipe!
905 finally
906 call job_stop(job)
907 endtry
908endfunc
909
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100910func Test_pipe_null()
911 if !has('job')
912 return
913 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100914 call ch_log('Test_pipe_null()')
915
916 " We cannot check that no I/O works, we only check that the job starts
917 " properly.
918 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100919 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100920 call assert_equal("run", job_status(job))
921 try
922 call assert_equal('something', ch_read(job))
923 finally
924 call job_stop(job)
925 endtry
926
927 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100928 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100929 call assert_equal("run", job_status(job))
930 try
931 call assert_equal('err-out', ch_read(job, {"part": "err"}))
932 finally
933 call job_stop(job)
934 endtry
935
936 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100937 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100938 call assert_equal("run", job_status(job))
939 try
940 call assert_equal('something', ch_read(job))
941 finally
942 call job_stop(job)
943 endtry
944
945 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100946 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100947 call assert_equal("run", job_status(job))
948 call job_stop(job)
949
950 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100951 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100952 call assert_equal("run", job_status(job))
953 call assert_equal('channel fail', string(job_getchannel(job)))
954 call assert_equal('fail', ch_status(job))
955 call job_stop(job)
956endfunc
957
Bram Moolenaaradb78a72016-06-27 21:10:31 +0200958func Test_pipe_to_buffer_raw()
959 if !has('job')
960 return
961 endif
962 call ch_log('Test_raw_pipe_to_buffer()')
963 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
964 split testout
965 let job = job_start([s:python, '-c',
966 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
967 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200968 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +0200969 try
970 for line in getline(2, '$')
971 let line = substitute(line, '^\.*', '', '')
972 call assert_equal('', line)
973 endfor
974 finally
975 call job_stop(job)
976 bwipe!
977 endtry
978endfunc
979
Bram Moolenaarde279892016-03-11 22:19:44 +0100980func Test_reuse_channel()
981 if !has('job')
982 return
983 endif
984 call ch_log('Test_reuse_channel()')
985
986 let job = job_start(s:python . " test_channel_pipe.py")
987 call assert_equal("run", job_status(job))
988 let handle = job_getchannel(job)
989 try
990 call ch_sendraw(handle, "echo something\n")
991 call assert_equal("something", ch_readraw(handle))
992 finally
993 call job_stop(job)
994 endtry
995
996 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
997 call assert_equal("run", job_status(job))
998 let handle = job_getchannel(job)
999 try
1000 call ch_sendraw(handle, "echo again\n")
1001 call assert_equal("again", ch_readraw(handle))
1002 finally
1003 call job_stop(job)
1004 endtry
1005endfunc
1006
Bram Moolenaar75f72652016-03-20 22:16:56 +01001007func Test_out_cb()
1008 if !has('job')
1009 return
1010 endif
1011 call ch_log('Test_out_cb()')
1012
1013 let dict = {'thisis': 'dict: '}
1014 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001015 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001016 endfunc
1017 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001018 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001019 endfunc
1020 let job = job_start(s:python . " test_channel_pipe.py",
1021 \ {'out_cb': dict.outHandler,
1022 \ 'out_mode': 'json',
1023 \ 'err_cb': dict.errHandler,
1024 \ 'err_mode': 'json'})
1025 call assert_equal("run", job_status(job))
1026 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001027 let g:Ch_outmsg = ''
1028 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001029 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1030 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001031 call WaitFor('g:Ch_outmsg != ""')
1032 call assert_equal("dict: hello", g:Ch_outmsg)
1033 call WaitFor('g:Ch_errmsg != ""')
1034 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001035 finally
1036 call job_stop(job)
1037 endtry
1038endfunc
1039
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001040func Test_out_close_cb()
1041 if !has('job')
1042 return
1043 endif
1044 call ch_log('Test_out_close_cb()')
1045
1046 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001047 let g:Ch_msg1 = ''
1048 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001049 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001050 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001051 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001052 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001053 let s:counter += 1
1054 endfunc
1055 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001056 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001057 let s:counter += 1
1058 endfunc
1059 let job = job_start(s:python . " test_channel_pipe.py quit now",
1060 \ {'out_cb': 'OutHandler',
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_closemsg != 0 && g:Ch_msg1 != ""')
1065 call assert_equal('quit', g:Ch_msg1)
1066 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001067 finally
1068 call job_stop(job)
1069 delfunc OutHandler
1070 delfunc CloseHandler
1071 endtry
1072endfunc
1073
Bram Moolenaar437905c2016-04-26 19:01:05 +02001074func Test_read_in_close_cb()
1075 if !has('job')
1076 return
1077 endif
1078 call ch_log('Test_read_in_close_cb()')
1079
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001080 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001081 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001082 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001083 endfunc
1084 let job = job_start(s:python . " test_channel_pipe.py quit now",
1085 \ {'close_cb': 'CloseHandler'})
1086 call assert_equal("run", job_status(job))
1087 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001088 call WaitFor('g:Ch_received != ""')
1089 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001090 finally
1091 call job_stop(job)
1092 delfunc CloseHandler
1093 endtry
1094endfunc
1095
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001096func Test_out_cb_lambda()
1097 if !has('job')
1098 return
1099 endif
1100 call ch_log('Test_out_cb_lambda()')
1101
1102 let job = job_start(s:python . " test_channel_pipe.py",
1103 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1104 \ 'out_mode': 'json',
1105 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1106 \ 'err_mode': 'json'})
1107 call assert_equal("run", job_status(job))
1108 try
1109 let g:Ch_outmsg = ''
1110 let g:Ch_errmsg = ''
1111 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1112 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1113 call WaitFor('g:Ch_outmsg != ""')
1114 call assert_equal("lambda: hello", g:Ch_outmsg)
1115 call WaitFor('g:Ch_errmsg != ""')
1116 call assert_equal("lambda: there", g:Ch_errmsg)
1117 finally
1118 call job_stop(job)
1119 endtry
1120endfunc
1121
Bram Moolenaard46ae142016-02-16 13:33:52 +01001122""""""""""
1123
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001124let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001125func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001126 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001127 unlet s:channelfd
1128endfunc
1129
1130" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001131func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001132 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001133 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001134 call WaitFor('"what?" == g:Ch_unletResponse')
1135 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001136endfunc
1137
1138func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001139 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001140 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001141endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001142
Bram Moolenaard46ae142016-02-16 13:33:52 +01001143""""""""""
1144
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001145let g:Ch_unletResponse = ''
1146func Ch_CloseHandler(handle, msg)
1147 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001148 call ch_close(s:channelfd)
1149endfunc
1150
1151" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001152func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001153 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001154 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1155 call WaitFor('"what?" == g:Ch_unletResponse')
1156 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001157endfunc
1158
1159func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001160 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001161 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001162endfunc
1163
1164""""""""""
1165
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001166func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001167 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001168 silent! let ch = ch_open("noserver")
1169 echo ch
1170 let d = ch
1171endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001172
1173""""""""""
1174
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001175func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001176 " Wait up to a second for the port to open.
1177 let s:chopt.waittime = 1000
1178 let channel = ch_open('localhost:' . a:port, s:chopt)
1179 unlet s:chopt.waittime
1180 if ch_status(channel) == "fail"
1181 call assert_false(1, "Can't open channel")
1182 return
1183 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001184 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001185 call ch_close(channel)
1186endfunc
1187
1188func Test_open_delay()
1189 call ch_log('Test_open_delay()')
1190 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001191 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001192endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001193
1194"""""""""
1195
1196function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001197 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001198endfunc
1199
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001200function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001201 let handle = ch_open('localhost:' . a:port, s:chopt)
1202 if ch_status(handle) == "fail"
1203 call assert_false(1, "Can't open channel")
1204 return
1205 endif
1206
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001207 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001208 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001209 call WaitFor('len(g:Ch_call_ret) > 0')
1210 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001211endfunc
1212
1213func Test_call()
1214 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001215 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001216endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001217
1218"""""""""
1219
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001220let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001221function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001222 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001223endfunc
1224
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001225function Ch_test_exit_callback(port)
1226 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1227 let g:Ch_exit_job = g:currentJob
1228 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001229endfunc
1230
1231func Test_exit_callback()
1232 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001233 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001234 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001235
Bram Moolenaar9730f742016-02-28 19:50:51 +01001236 " wait up to a second for the job to exit
1237 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001238 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001239 break
1240 endif
1241 sleep 10m
1242 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001243 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001244 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001245
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001246 call assert_equal('done', g:Ch_job_exit_ret)
1247 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1248 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001249 endif
1250endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001251
1252"""""""""
1253
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001254let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001255function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001256 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001257endfunc
1258
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001259function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001260 let handle = ch_open('localhost:' . a:port, s:chopt)
1261 if ch_status(handle) == "fail"
1262 call assert_false(1, "Can't open channel")
1263 return
1264 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001265 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001266
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001267 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001268 call WaitFor('"closed" == g:Ch_close_ret')
1269 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001270endfunc
1271
1272func Test_close_callback()
1273 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001274 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001275endfunc
1276
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001277function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001278 let handle = ch_open('localhost:' . a:port, s:chopt)
1279 if ch_status(handle) == "fail"
1280 call assert_false(1, "Can't open channel")
1281 return
1282 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001283 let g:Ch_d = {}
1284 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001285 let self.close_ret = 'closed'
1286 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001287 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001288
1289 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001290 call WaitFor('"closed" == g:Ch_d.close_ret')
1291 call assert_equal('closed', g:Ch_d.close_ret)
1292 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001293endfunc
1294
1295func Test_close_partial()
1296 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001297 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001298endfunc
1299
Bram Moolenaar80385682016-03-27 19:13:35 +02001300func Test_job_start_invalid()
1301 call assert_fails('call job_start($x)', 'E474:')
1302 call assert_fails('call job_start("")', 'E474:')
1303endfunc
1304
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001305" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001306func Test_partial_in_channel_cycle()
1307 let d = {}
1308 let d.a = function('string', [d])
1309 try
1310 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1311 catch
1312 call assert_exception('E901:')
1313 endtry
1314 unlet d
1315endfunc
1316
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001317func Test_using_freed_memory()
1318 let g:a = job_start(['ls'])
1319 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001320 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001321endfunc
1322
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001323func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001324 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001325 return
1326 endif
1327 sp test_channel.vim
1328 let g:linecount = line('$')
1329 close
1330 split testout
1331 1,$delete
1332 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001333 call WaitFor('line("$") > g:linecount')
Bram Moolenaar89eaa412016-07-31 14:17:27 +02001334 call assert_inrange(g:linecount + 1, g:linecount + 2, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001335 bwipe!
1336endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001337
Bram Moolenaar82117982016-08-27 19:21:48 +02001338func Test_raw_passes_nul()
1339 if !executable('cat') || !has('job')
1340 return
1341 endif
1342
1343 " Test lines from the job containing NUL are stored correctly in a buffer.
1344 new
1345 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1346 w! Xtestread
1347 bwipe!
1348 split testout
1349 1,$delete
1350 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1351 call WaitFor('line("$") > 2')
1352 call assert_equal("asdf\nasdf", getline(2))
1353 call assert_equal("xxx\n", getline(3))
1354 call assert_equal("\nyyy", getline(4))
1355
1356 call delete('Xtestread')
1357 bwipe!
1358
1359 " Test lines from a buffer with NUL bytes are written correctly to the job.
1360 new mybuffer
1361 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1362 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1363 call WaitFor('"dead" == job_status(g:Ch_job)')
1364 bwipe!
1365 split Xtestwrite
1366 call assert_equal("asdf\nasdf", getline(1))
1367 call assert_equal("xxx\n", getline(2))
1368 call assert_equal("\nyyy", getline(3))
1369
1370 call delete('Xtestwrite')
1371 bwipe!
1372endfunc
1373
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001374function Ch_test_close_lambda(port)
1375 let handle = ch_open('localhost:' . a:port, s:chopt)
1376 if ch_status(handle) == "fail"
1377 call assert_false(1, "Can't open channel")
1378 return
1379 endif
1380 let g:Ch_close_ret = ''
1381 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1382
1383 call assert_equal('', ch_evalexpr(handle, 'close me'))
1384 call WaitFor('"closed" == g:Ch_close_ret')
1385 call assert_equal('closed', g:Ch_close_ret)
1386endfunc
1387
1388func Test_close_lambda()
1389 call ch_log('Test_close_lambda()')
1390 call s:run_server('Ch_test_close_lambda')
1391endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001392
Bram Moolenaar9730f742016-02-28 19:50:51 +01001393" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaara58c58b2016-07-23 14:01:15 +02001394" call ch_logfile('channellog', 'w')