blob: 31c2e501756250853cabf6169b99afa4c3138746 [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
Bram Moolenaar4b785f62016-11-29 21:54:44 +010061 " nothing to read now
62 call assert_equal(0, ch_canread(handle))
63
Bram Moolenaarf1f07922016-08-26 17:58:53 +020064 " sending three messages quickly then reading should work
65 for i in range(3)
66 call ch_sendexpr(handle, 'echo hello ' . i)
67 endfor
68 call assert_equal('hello 0', ch_read(handle)[1])
69 call assert_equal('hello 1', ch_read(handle)[1])
70 call assert_equal('hello 2', ch_read(handle)[1])
71
Bram Moolenaard7ece102016-02-02 23:23:02 +010072 " Request that triggers sending two ex commands. These will usually be
73 " handled before getting the response, but it's not guaranteed, thus wait a
74 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010075 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020076 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010077 call assert_equal('added1', getline(line('$') - 1))
78 call assert_equal('added2', getline('$'))
79
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010080 " Request command "foo bar", which fails silently.
81 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020082 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020083 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010084
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010085 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020086 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010087 call assert_equal('added more', getline('$'))
88
Bram Moolenaara07fec92016-02-05 21:04:08 +010089 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020090 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
91 call WaitFor('exists("g:Ch_responseHandle")')
92 if !exists('g:Ch_responseHandle')
93 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010094 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020095 call assert_equal(handle, g:Ch_responseHandle)
96 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +010097 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +020098 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +010099
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200100 let g:Ch_responseMsg = ''
101 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
102 call WaitFor('exists("g:Ch_responseHandle")')
103 if !exists('g:Ch_responseHandle')
104 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100105 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200106 call assert_equal(handle, g:Ch_responseHandle)
107 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100108 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200109 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100110
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200111 " Using lambda.
112 let g:Ch_responseMsg = ''
113 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
114 call WaitFor('exists("g:Ch_responseHandle")')
115 if !exists('g:Ch_responseHandle')
116 call assert_false(1, 'g:Ch_responseHandle was not set')
117 else
118 call assert_equal(handle, g:Ch_responseHandle)
119 unlet g:Ch_responseHandle
120 endif
121 call assert_equal('got it', g:Ch_responseMsg)
122
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100123 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200124 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100125
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100126 " check setting options (without testing the effect)
127 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100128 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100129 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100130 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100131 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100132 call ch_setoptions(handle, {'drop': 'never'})
133 call ch_setoptions(handle, {'drop': 'auto'})
134 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100135
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100136 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100137 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100138 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100139 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100140
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100141 " Send an eval request with special characters.
142 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
143 sleep 10m
144 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
145
146 " Send an eval request to get a line with special characters.
147 call setline(3, "a\nb\<CR>c\x01d\x7fe")
148 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
149 sleep 10m
150 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
151
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100152 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100153 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100154 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100155 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100156
Bram Moolenaar55fab432016-02-07 16:53:13 +0100157 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100158 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100159 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100160 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100161
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100162 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100163 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100164 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100165 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100166
Bram Moolenaarf4160862016-02-05 23:09:12 +0100167 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100168 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200169 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100170 call assert_equal('one', getline(line('$') - 2))
171 call assert_equal('two', getline(line('$') - 1))
172 call assert_equal('three', getline('$'))
173
174 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
176 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100177
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100178 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100179
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100180 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100181 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
182 let start = reltime()
183 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
184 let elapsed = reltime(start)
185 call assert_true(reltimefloat(elapsed) > 0.3)
186 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100187
188 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100189 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100190 let resp = ch_read(handle)
191 call assert_equal(type([]), type(resp))
192 call assert_equal(type(11), type(resp[0]))
193 call assert_equal('waited', resp[1])
194
Bram Moolenaard7ece102016-02-02 23:23:02 +0100195 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100197endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100198
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100199func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100200 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200201 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100202endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100203
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100204" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200205func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100206 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200207 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100208 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100210 return
211 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100212
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100213 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100214
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100215 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100216 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100217 call assert_false(1, "Can't open second channel")
218 return
219 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
221 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100222
223 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100224 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100225
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226 call ch_close(newhandle)
227endfunc
228
229func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100230 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200231 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100232endfunc
233
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100234" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200235func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100236 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100237 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100238 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100239 return
240 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100241
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100242 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100243
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100244 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100245endfunc
246
247func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100248 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200249 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100250endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100251
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100252"""""""""
253
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200254func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100255 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200256 unlet g:Ch_reply
257 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100258endfunc
259
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200260func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100261 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100262 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100263 call assert_false(1, "Can't open channel")
264 return
265 endif
266
267 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100268 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200269 call WaitFor('"we called you" == g:Ch_reply')
270 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100271
272 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100273 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200274 call WaitFor('"we did call you" == g:Ch_reply')
275 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100276endfunc
277
278func Test_channel_handler()
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100279call ch_logfile('channellog', 'w')
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100280 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200281 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200282 let s:chopt.callback = 'Ch_handler'
283 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200284 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200285 let s:chopt.callback = function('Ch_handler')
286 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100287 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100288endfunc
289
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100290"""""""""
291
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200292let g:Ch_reply = ''
293func Ch_zeroHandler(chan, msg)
294 unlet g:Ch_reply
295 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100296endfunc
297
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200298let g:Ch_zero_reply = ''
299func Ch_oneHandler(chan, msg)
300 unlet g:Ch_zero_reply
301 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100302endfunc
303
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200304func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100305 let handle = ch_open('localhost:' . a:port, s:chopt)
306 if ch_status(handle) == "fail"
307 call assert_false(1, "Can't open channel")
308 return
309 endif
310
311 " Check that eval works.
312 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
313
314 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200315 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100316 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100317 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200318 call WaitFor('"zero index" == g:Ch_reply')
319 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100321 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200322 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 endif
324
325 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200326 let g:Ch_reply = ''
327 let g:Ch_zero_reply = ''
328 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
329 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100330 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200331 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100332 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200333 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100334 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200335 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336endfunc
337
338func Test_zero_reply()
339 call ch_log('Test_zero_reply()')
340 " Run with channel handler
341 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200342 let s:chopt.callback = 'Ch_zeroHandler'
343 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100344 unlet s:chopt.callback
345
346 " Run without channel handler
347 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100349endfunc
350
351"""""""""
352
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200353let g:Ch_reply1 = ""
354func Ch_handleRaw1(chan, msg)
355 unlet g:Ch_reply1
356 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100357endfunc
358
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200359let g:Ch_reply2 = ""
360func Ch_handleRaw2(chan, msg)
361 unlet g:Ch_reply2
362 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100363endfunc
364
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200365let g:Ch_reply3 = ""
366func Ch_handleRaw3(chan, msg)
367 unlet g:Ch_reply3
368 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100369endfunc
370
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200371func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100372 let handle = ch_open('localhost:' . a:port, s:chopt)
373 if ch_status(handle) == "fail"
374 call assert_false(1, "Can't open channel")
375 return
376 endif
377 call ch_setoptions(handle, {'mode': 'raw'})
378
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100379 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200380 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200381 call WaitFor('g:Ch_reply1 != ""')
382 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200383 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
384 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200385 call WaitFor('g:Ch_reply2 != ""')
386 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100387 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200388 call WaitFor('g:Ch_reply3 != ""')
389 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100390endfunc
391
392func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100393 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200394 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100395endfunc
396
397"""""""""
398
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100399" Test that trying to connect to a non-existing port fails quickly.
400func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100401 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100402 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100403 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100404 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100405 " Oops, port does exists.
406 call ch_close(handle)
407 else
408 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100409 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100410 endif
411
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100412 " We intend to use a socket that doesn't exist and wait for half a second
413 " before giving up. If the socket does exist it can fail in various ways.
414 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100415 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100416 try
417 let handle = ch_open('localhost:9867', {'waittime': 500})
418 if ch_status(handle) != "fail"
419 " Oops, port does exists.
420 call ch_close(handle)
421 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100422 " Failed connection should wait about 500 msec. Can be longer if the
423 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100424 let elapsed = reltime(start)
425 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100426 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100427 endif
428 catch
429 if v:exception !~ 'Connection reset by peer'
430 call assert_false(1, "Caught exception: " . v:exception)
431 endif
432 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100433endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100434
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100435"""""""""
436
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100437func Test_raw_pipe()
438 if !has('job')
439 return
440 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100441 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100442 " Add a dummy close callback to avoid that messages are dropped when calling
443 " ch_canread().
444 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100445 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200446 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100447 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200448
449 call assert_equal("open", ch_status(job))
450 call assert_equal("open", ch_status(job), {"part": "out"})
451 call assert_equal("open", ch_status(job), {"part": "err"})
452 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
453 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
454
455 let dict = ch_info(job)
456 call assert_true(dict.id != 0)
457 call assert_equal('open', dict.status)
458 call assert_equal('open', dict.out_status)
459 call assert_equal('RAW', dict.out_mode)
460 call assert_equal('pipe', dict.out_io)
461 call assert_equal('open', dict.err_status)
462 call assert_equal('RAW', dict.err_mode)
463 call assert_equal('pipe', dict.err_io)
464
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100465 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100466 " For a change use the job where a channel is expected.
467 call ch_sendraw(job, "echo something\n")
468 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100469 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
470
Bram Moolenaar151f6562016-03-07 21:19:38 +0100471 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100472 let g:handle = job_getchannel(job)
473 call WaitFor('ch_canread(g:handle)')
474 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100475 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100476 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
477
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200478 let g:Ch_reply = ""
479 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
480 call WaitFor('"" != g:Ch_reply')
481 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
482
Bram Moolenaar151f6562016-03-07 21:19:38 +0100483 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100484 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
485 finally
486 call job_stop(job)
487 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100488
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200489 let g:Ch_job = job
490 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100491 let info = job_info(job)
492 call assert_equal("dead", info.status)
493 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494endfunc
495
496func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100497 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100498 return
499 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100500 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100501 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100502 call assert_equal("run", job_status(job))
503 try
504 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100505 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100506 call assert_equal("something", ch_readraw(handle))
507
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100508 call ch_sendraw(handle, "echoerr wrong\n")
509 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
510
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100511 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100512 call assert_equal("this", ch_readraw(handle))
513 call assert_equal("AND this", ch_readraw(handle))
514
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200515 call ch_sendraw(handle, "split this line\n")
516 call assert_equal("this linethis linethis line", ch_readraw(handle))
517
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100518 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100519 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100520 finally
521 call job_stop(job)
522 endtry
523endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100524
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100525func Test_nl_err_to_out_pipe()
526 if !has('job')
527 return
528 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100529 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100530 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100531 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100532 call assert_equal("run", job_status(job))
533 try
534 let handle = job_getchannel(job)
535 call ch_sendraw(handle, "echo something\n")
536 call assert_equal("something", ch_readraw(handle))
537
538 call ch_sendraw(handle, "echoerr wrong\n")
539 call assert_equal("wrong", ch_readraw(handle))
540 finally
541 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100542 call ch_logfile('')
543 let loglines = readfile('Xlog')
544 call assert_true(len(loglines) > 10)
545 let found_test = 0
546 let found_send = 0
547 let found_recv = 0
548 let found_stop = 0
549 for l in loglines
550 if l =~ 'Test_nl_err_to_out_pipe'
551 let found_test = 1
552 endif
553 if l =~ 'SEND on.*echo something'
554 let found_send = 1
555 endif
556 if l =~ 'RECV on.*something'
557 let found_recv = 1
558 endif
559 if l =~ 'Stopping job with'
560 let found_stop = 1
561 endif
562 endfor
563 call assert_equal(1, found_test)
564 call assert_equal(1, found_send)
565 call assert_equal(1, found_recv)
566 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200567 " On MS-Windows need to sleep for a moment to be able to delete the file.
568 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100569 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100570 endtry
571endfunc
572
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200573func Stop_g_job()
574 call job_stop(g:job)
575 if has('win32')
576 " On MS-Windows the server must close the file handle before we are able
577 " to delete the file.
578 call WaitFor('job_status(g:job) == "dead"')
579 sleep 10m
580 endif
581endfunc
582
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100583func Test_nl_read_file()
584 if !has('job')
585 return
586 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100587 call ch_log('Test_nl_read_file()')
588 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200589 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100590 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200591 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100592 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200593 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100594 call assert_equal("something", ch_readraw(handle))
595 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
596 call assert_equal("this", ch_readraw(handle))
597 call assert_equal("AND this", ch_readraw(handle))
598 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200599 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100600 call delete('Xinput')
601 endtry
602endfunc
603
Bram Moolenaare98d1212016-03-08 15:37:41 +0100604func Test_nl_write_out_file()
605 if !has('job')
606 return
607 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100608 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200609 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100610 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200611 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100612 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200613 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100614 call ch_sendraw(handle, "echo line one\n")
615 call ch_sendraw(handle, "echo line two\n")
616 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200617 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100618 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
619 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200620 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100621 call delete('Xoutput')
622 endtry
623endfunc
624
625func Test_nl_write_err_file()
626 if !has('job')
627 return
628 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100629 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200630 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100631 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200632 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100633 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200634 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 call ch_sendraw(handle, "echoerr line one\n")
636 call ch_sendraw(handle, "echoerr line two\n")
637 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200638 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100639 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
640 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200641 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100642 call delete('Xoutput')
643 endtry
644endfunc
645
646func Test_nl_write_both_file()
647 if !has('job')
648 return
649 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100650 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200651 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100652 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200653 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100654 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200655 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 call ch_sendraw(handle, "echoerr line one\n")
657 call ch_sendraw(handle, "echo line two\n")
658 call ch_sendraw(handle, "double this\n")
659 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200660 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100661 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
662 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200663 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100664 call delete('Xoutput')
665 endtry
666endfunc
667
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200668func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200669 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200670endfunc
671
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200672func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100673 if !has('job')
674 return
675 endif
676 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200677 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200678 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200679 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100680 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100681 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200682 if a:do_msg
683 let expected[0] = 'Reading from channel output...'
684 else
685 let options['out_msg'] = 0
686 call remove(expected, 0)
687 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100688 else
689 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100690 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100691 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200692 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100693 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200694 if a:nomod
695 let options['out_modifiable'] = 0
696 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100697 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100698 call assert_equal("run", job_status(job))
699 try
700 let handle = job_getchannel(job)
701 call ch_sendraw(handle, "echo line one\n")
702 call ch_sendraw(handle, "echo line two\n")
703 call ch_sendraw(handle, "double this\n")
704 call ch_sendraw(handle, "quit\n")
705 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200706 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200707 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200708 if a:nomod
709 call assert_equal(0, &modifiable)
710 else
711 call assert_equal(1, &modifiable)
712 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200713 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100714 bwipe!
715 finally
716 call job_stop(job)
717 endtry
718endfunc
719
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100720func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200721 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100722endfunc
723
724func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200725 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726endfunc
727
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200728func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200729 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200730endfunc
731
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200732func Test_pipe_to_buffer_name_nomsg()
733 call Run_test_pipe_to_buffer(1, 0, 1)
734endfunc
735
736func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100737 if !has('job')
738 return
739 endif
740 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100741 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200742 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100743 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100744 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200745 if a:do_msg
746 let expected[0] = 'Reading from channel error...'
747 else
748 let options['err_msg'] = 0
749 call remove(expected, 0)
750 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100751 else
752 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100753 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100754 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200755 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100756 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200757 if a:nomod
758 let options['err_modifiable'] = 0
759 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100760 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100761 call assert_equal("run", job_status(job))
762 try
763 let handle = job_getchannel(job)
764 call ch_sendraw(handle, "echoerr line one\n")
765 call ch_sendraw(handle, "echoerr line two\n")
766 call ch_sendraw(handle, "doubleerr this\n")
767 call ch_sendraw(handle, "quit\n")
768 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200769 call WaitFor('line("$") >= 5')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200770 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200771 if a:nomod
772 call assert_equal(0, &modifiable)
773 else
774 call assert_equal(1, &modifiable)
775 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100776 bwipe!
777 finally
778 call job_stop(job)
779 endtry
780endfunc
781
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100782func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200783 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100784endfunc
785
786func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200787 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200788endfunc
789
790func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200791 call Run_test_pipe_err_to_buffer(1, 1, 1)
792endfunc
793
794func Test_pipe_err_to_buffer_name_nomsg()
795 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100796endfunc
797
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100798func Test_pipe_both_to_buffer()
799 if !has('job')
800 return
801 endif
802 call ch_log('Test_pipe_both_to_buffer()')
803 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100804 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100805 call assert_equal("run", job_status(job))
806 try
807 let handle = job_getchannel(job)
808 call ch_sendraw(handle, "echo line one\n")
809 call ch_sendraw(handle, "echoerr line two\n")
810 call ch_sendraw(handle, "double this\n")
811 call ch_sendraw(handle, "doubleerr that\n")
812 call ch_sendraw(handle, "quit\n")
813 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200814 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100815 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
816 bwipe!
817 finally
818 call job_stop(job)
819 endtry
820endfunc
821
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100822func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100823 if !has('job')
824 return
825 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100826 call ch_log('Test_pipe_from_buffer()')
827
828 sp pipe-input
829 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200830 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100831 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100832 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100833 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100834 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100835 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100836
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100837 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100838 call assert_equal("run", job_status(job))
839 try
840 let handle = job_getchannel(job)
841 call assert_equal('one', ch_read(handle))
842 call assert_equal('two', ch_read(handle))
843 call assert_equal('three', ch_read(handle))
844 bwipe!
845 finally
846 call job_stop(job)
847 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100848endfunc
849
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100850func Test_pipe_from_buffer_name()
851 call Run_test_pipe_from_buffer(1)
852endfunc
853
854func Test_pipe_from_buffer_nr()
855 call Run_test_pipe_from_buffer(0)
856endfunc
857
Bram Moolenaar0874a832016-09-01 15:11:51 +0200858func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200859 if !executable('sort') || !has('job')
860 return
861 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200862 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
863 if a:use_buffer
864 split sortin
865 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
866 let options.in_io = 'buffer'
867 let options.in_name = 'sortin'
868 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200869 if !a:all
870 let options.in_top = 2
871 let options.in_bot = 4
872 endif
873 let g:job = job_start('sort', options)
874 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200875
876 if !a:use_buffer
877 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
878 call ch_close_in(g:job)
879 endif
880
Bram Moolenaard8b55492016-09-01 14:35:22 +0200881 call WaitFor('job_status(g:job) == "dead"')
882 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200883
Bram Moolenaard8b55492016-09-01 14:35:22 +0200884 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200885 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200886 call assert_equal('Reading from channel output...', getline(1))
887 if a:all
888 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
889 else
890 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
891 endif
892
893 call job_stop(g:job)
894 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200895 if a:use_buffer
896 bwipe! sortin
897 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200898 bwipe! sortout
899endfunc
900
901func Test_pipe_through_sort_all()
902 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200903 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200904endfunc
905
906func Test_pipe_through_sort_some()
907 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200908 call Run_pipe_through_sort(0, 1)
909endfunc
910
911func Test_pipe_through_sort_feed()
912 call ch_log('Test_pipe_through_sort_feed()')
913 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200914endfunc
915
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100916func Test_pipe_to_nameless_buffer()
917 if !has('job')
918 return
919 endif
920 call ch_log('Test_pipe_to_nameless_buffer()')
921 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100922 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100923 call assert_equal("run", job_status(job))
924 try
925 let handle = job_getchannel(job)
926 call ch_sendraw(handle, "echo line one\n")
927 call ch_sendraw(handle, "echo line two\n")
928 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200929 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100930 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
931 bwipe!
932 finally
933 call job_stop(job)
934 endtry
935endfunc
936
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100937func Test_pipe_to_buffer_json()
938 if !has('job')
939 return
940 endif
941 call ch_log('Test_pipe_to_buffer_json()')
942 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100943 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100944 call assert_equal("run", job_status(job))
945 try
946 let handle = job_getchannel(job)
947 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
948 call ch_sendraw(handle, "echo [-2, 12.34]\n")
949 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200950 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100951 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
952 bwipe!
953 finally
954 call job_stop(job)
955 endtry
956endfunc
957
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100958" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100959func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100960 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100961 if getline(line('$') - a:offset) == a:line
962 break
963 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100964 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100965 endfor
966endfunc
967
968func Test_pipe_io_two_buffers()
969 if !has('job')
970 return
971 endif
972 call ch_log('Test_pipe_io_two_buffers()')
973
974 " Create two buffers, one to read from and one to write to.
975 split pipe-output
976 set buftype=nofile
977 split pipe-input
978 set buftype=nofile
979
980 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100981 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200982 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
983 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100984 call assert_equal("run", job_status(job))
985 try
986 exe "normal Gaecho hello\<CR>"
987 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100988 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100989 call assert_equal('hello', getline('$'))
990
991 exe bufwinnr('pipe-input') . "wincmd w"
992 exe "normal Gadouble this\<CR>"
993 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100994 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100995 call assert_equal('this', getline(line('$') - 1))
996 call assert_equal('AND this', getline('$'))
997
998 bwipe!
999 exe bufwinnr('pipe-input') . "wincmd w"
1000 bwipe!
1001 finally
1002 call job_stop(job)
1003 endtry
1004endfunc
1005
1006func Test_pipe_io_one_buffer()
1007 if !has('job')
1008 return
1009 endif
1010 call ch_log('Test_pipe_io_one_buffer()')
1011
1012 " Create one buffer to read from and to write to.
1013 split pipe-io
1014 set buftype=nofile
1015
1016 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001017 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001018 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1019 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001020 call assert_equal("run", job_status(job))
1021 try
1022 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001023 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001024 call assert_equal('hello', getline(line('$') - 1))
1025
1026 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001027 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001028 call assert_equal('this', getline(line('$') - 2))
1029 call assert_equal('AND this', getline(line('$') - 1))
1030
1031 bwipe!
1032 finally
1033 call job_stop(job)
1034 endtry
1035endfunc
1036
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001037func Test_pipe_null()
1038 if !has('job')
1039 return
1040 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001041 call ch_log('Test_pipe_null()')
1042
1043 " We cannot check that no I/O works, we only check that the job starts
1044 " properly.
1045 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001046 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001047 call assert_equal("run", job_status(job))
1048 try
1049 call assert_equal('something', ch_read(job))
1050 finally
1051 call job_stop(job)
1052 endtry
1053
1054 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001055 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001056 call assert_equal("run", job_status(job))
1057 try
1058 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1059 finally
1060 call job_stop(job)
1061 endtry
1062
1063 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001064 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001065 call assert_equal("run", job_status(job))
1066 try
1067 call assert_equal('something', ch_read(job))
1068 finally
1069 call job_stop(job)
1070 endtry
1071
1072 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001073 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001074 call assert_equal("run", job_status(job))
1075 call job_stop(job)
1076
1077 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001078 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001079 call assert_equal("run", job_status(job))
1080 call assert_equal('channel fail', string(job_getchannel(job)))
1081 call assert_equal('fail', ch_status(job))
1082 call job_stop(job)
1083endfunc
1084
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001085func Test_pipe_to_buffer_raw()
1086 if !has('job')
1087 return
1088 endif
1089 call ch_log('Test_raw_pipe_to_buffer()')
1090 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1091 split testout
1092 let job = job_start([s:python, '-c',
1093 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1094 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001095 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001096 try
1097 for line in getline(2, '$')
1098 let line = substitute(line, '^\.*', '', '')
1099 call assert_equal('', line)
1100 endfor
1101 finally
1102 call job_stop(job)
1103 bwipe!
1104 endtry
1105endfunc
1106
Bram Moolenaarde279892016-03-11 22:19:44 +01001107func Test_reuse_channel()
1108 if !has('job')
1109 return
1110 endif
1111 call ch_log('Test_reuse_channel()')
1112
1113 let job = job_start(s:python . " test_channel_pipe.py")
1114 call assert_equal("run", job_status(job))
1115 let handle = job_getchannel(job)
1116 try
1117 call ch_sendraw(handle, "echo something\n")
1118 call assert_equal("something", ch_readraw(handle))
1119 finally
1120 call job_stop(job)
1121 endtry
1122
1123 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1124 call assert_equal("run", job_status(job))
1125 let handle = job_getchannel(job)
1126 try
1127 call ch_sendraw(handle, "echo again\n")
1128 call assert_equal("again", ch_readraw(handle))
1129 finally
1130 call job_stop(job)
1131 endtry
1132endfunc
1133
Bram Moolenaar75f72652016-03-20 22:16:56 +01001134func Test_out_cb()
1135 if !has('job')
1136 return
1137 endif
1138 call ch_log('Test_out_cb()')
1139
1140 let dict = {'thisis': 'dict: '}
1141 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001142 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001143 endfunc
1144 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001145 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001146 endfunc
1147 let job = job_start(s:python . " test_channel_pipe.py",
1148 \ {'out_cb': dict.outHandler,
1149 \ 'out_mode': 'json',
1150 \ 'err_cb': dict.errHandler,
1151 \ 'err_mode': 'json'})
1152 call assert_equal("run", job_status(job))
1153 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001154 let g:Ch_outmsg = ''
1155 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001156 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1157 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001158 call WaitFor('g:Ch_outmsg != ""')
1159 call assert_equal("dict: hello", g:Ch_outmsg)
1160 call WaitFor('g:Ch_errmsg != ""')
1161 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001162 finally
1163 call job_stop(job)
1164 endtry
1165endfunc
1166
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001167func Test_out_close_cb()
1168 if !has('job')
1169 return
1170 endif
1171 call ch_log('Test_out_close_cb()')
1172
1173 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001174 let g:Ch_msg1 = ''
1175 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001176 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001177 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001178 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001179 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001180 let s:counter += 1
1181 endfunc
1182 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001183 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001184 let s:counter += 1
1185 endfunc
1186 let job = job_start(s:python . " test_channel_pipe.py quit now",
1187 \ {'out_cb': 'OutHandler',
1188 \ 'close_cb': 'CloseHandler'})
1189 call assert_equal("run", job_status(job))
1190 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001191 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1192 call assert_equal('quit', g:Ch_msg1)
1193 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001194 finally
1195 call job_stop(job)
1196 delfunc OutHandler
1197 delfunc CloseHandler
1198 endtry
1199endfunc
1200
Bram Moolenaar437905c2016-04-26 19:01:05 +02001201func Test_read_in_close_cb()
1202 if !has('job')
1203 return
1204 endif
1205 call ch_log('Test_read_in_close_cb()')
1206
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001207 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001208 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001209 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001210 endfunc
1211 let job = job_start(s:python . " test_channel_pipe.py quit now",
1212 \ {'close_cb': 'CloseHandler'})
1213 call assert_equal("run", job_status(job))
1214 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001215 call WaitFor('g:Ch_received != ""')
1216 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001217 finally
1218 call job_stop(job)
1219 delfunc CloseHandler
1220 endtry
1221endfunc
1222
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001223func Test_out_cb_lambda()
1224 if !has('job')
1225 return
1226 endif
1227 call ch_log('Test_out_cb_lambda()')
1228
1229 let job = job_start(s:python . " test_channel_pipe.py",
1230 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1231 \ 'out_mode': 'json',
1232 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1233 \ 'err_mode': 'json'})
1234 call assert_equal("run", job_status(job))
1235 try
1236 let g:Ch_outmsg = ''
1237 let g:Ch_errmsg = ''
1238 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1239 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1240 call WaitFor('g:Ch_outmsg != ""')
1241 call assert_equal("lambda: hello", g:Ch_outmsg)
1242 call WaitFor('g:Ch_errmsg != ""')
1243 call assert_equal("lambda: there", g:Ch_errmsg)
1244 finally
1245 call job_stop(job)
1246 endtry
1247endfunc
1248
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001249func Test_close_and_exit_cb()
1250 if !has('job')
1251 return
1252 endif
1253 call ch_log('Test_close_and_exit_cb')
1254
1255 let dict = {'ret': {}}
1256 func dict.close_cb(ch) dict
1257 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1258 endfunc
1259 func dict.exit_cb(job, status) dict
1260 let self.ret['exit_cb'] = job_status(a:job)
1261 endfunc
1262
1263 let g:job = job_start('echo', {
1264 \ 'close_cb': dict.close_cb,
1265 \ 'exit_cb': dict.exit_cb,
1266 \ })
1267 call assert_equal('run', job_status(g:job))
1268 unlet g:job
1269 call WaitFor('len(dict.ret) >= 2')
1270 call assert_equal(2, len(dict.ret))
1271 call assert_match('^\%(dead\|run\)', dict.ret['close_cb'])
1272 call assert_equal('dead', dict.ret['exit_cb'])
1273endfunc
1274
Bram Moolenaard46ae142016-02-16 13:33:52 +01001275""""""""""
1276
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001277let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001278func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001279 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001280 unlet s:channelfd
1281endfunc
1282
1283" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001284func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001285 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001286 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001287 call WaitFor('"what?" == g:Ch_unletResponse')
1288 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001289endfunc
1290
1291func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001292 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001293 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001294endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001295
Bram Moolenaard46ae142016-02-16 13:33:52 +01001296""""""""""
1297
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001298let g:Ch_unletResponse = ''
1299func Ch_CloseHandler(handle, msg)
1300 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001301 call ch_close(s:channelfd)
1302endfunc
1303
1304" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001305func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001306 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001307 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1308 call WaitFor('"what?" == g:Ch_unletResponse')
1309 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001310endfunc
1311
1312func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001313 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001314 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001315endfunc
1316
1317""""""""""
1318
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001319func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001320 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001321 silent! let ch = ch_open("noserver")
1322 echo ch
1323 let d = ch
1324endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001325
1326""""""""""
1327
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001328func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001329 " Wait up to a second for the port to open.
1330 let s:chopt.waittime = 1000
1331 let channel = ch_open('localhost:' . a:port, s:chopt)
1332 unlet s:chopt.waittime
1333 if ch_status(channel) == "fail"
1334 call assert_false(1, "Can't open channel")
1335 return
1336 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001337 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001338 call ch_close(channel)
1339endfunc
1340
1341func Test_open_delay()
1342 call ch_log('Test_open_delay()')
1343 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001344 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001345endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001346
1347"""""""""
1348
1349function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001350 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001351endfunc
1352
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001353function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001354 let handle = ch_open('localhost:' . a:port, s:chopt)
1355 if ch_status(handle) == "fail"
1356 call assert_false(1, "Can't open channel")
1357 return
1358 endif
1359
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001360 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001361 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001362 call WaitFor('len(g:Ch_call_ret) > 0')
1363 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001364endfunc
1365
1366func Test_call()
1367 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001368 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001369endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001370
1371"""""""""
1372
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001374function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001375 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001376endfunc
1377
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001378function Ch_test_exit_callback(port)
1379 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1380 let g:Ch_exit_job = g:currentJob
1381 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001382endfunc
1383
1384func Test_exit_callback()
1385 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001386 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001387 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001388
Bram Moolenaar9730f742016-02-28 19:50:51 +01001389 " wait up to a second for the job to exit
1390 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001391 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001392 break
1393 endif
1394 sleep 10m
1395 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001396 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001397 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001398
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001399 call assert_equal('done', g:Ch_job_exit_ret)
1400 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1401 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001402 endif
1403endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001404
Bram Moolenaar97792de2016-10-15 18:36:49 +02001405function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001406 if job_info(a:job).process == g:exit_cb_val.process
1407 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1408 endif
1409 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001410endfunction
1411
1412func Test_exit_callback_interval()
1413 if !has('job')
1414 return
1415 endif
1416
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001417 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001418 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001419 let g:exit_cb_val.process = job_info(job).process
1420 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1421 let elapsed = reltimefloat(g:exit_cb_val.end)
1422 call assert_true(elapsed > 0.5)
1423 call assert_true(elapsed < 1.0)
1424
1425 " case: unreferenced job, using timer
1426 if !has('timers')
1427 return
1428 endif
1429
1430 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1431 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1432 let g:exit_cb_val.process = job_info(g:job).process
1433 unlet g:job
1434 call Standby(1000)
1435 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1436 let elapsed = reltimefloat(g:exit_cb_val.end)
1437 else
1438 let elapsed = 1.0
1439 endif
1440 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001441 call assert_true(elapsed < 1.0)
1442endfunc
1443
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001444"""""""""
1445
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001446let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001447function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001448 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001449endfunc
1450
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001451function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001452 let handle = ch_open('localhost:' . a:port, s:chopt)
1453 if ch_status(handle) == "fail"
1454 call assert_false(1, "Can't open channel")
1455 return
1456 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001457 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001458
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001459 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001460 call WaitFor('"closed" == g:Ch_close_ret')
1461 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001462endfunc
1463
1464func Test_close_callback()
1465 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001466 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001467endfunc
1468
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001469function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001470 let handle = ch_open('localhost:' . a:port, s:chopt)
1471 if ch_status(handle) == "fail"
1472 call assert_false(1, "Can't open channel")
1473 return
1474 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001475 let g:Ch_d = {}
1476 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001477 let self.close_ret = 'closed'
1478 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001479 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001480
1481 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001482 call WaitFor('"closed" == g:Ch_d.close_ret')
1483 call assert_equal('closed', g:Ch_d.close_ret)
1484 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001485endfunc
1486
1487func Test_close_partial()
1488 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001489 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001490endfunc
1491
Bram Moolenaar80385682016-03-27 19:13:35 +02001492func Test_job_start_invalid()
1493 call assert_fails('call job_start($x)', 'E474:')
1494 call assert_fails('call job_start("")', 'E474:')
1495endfunc
1496
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001497func Test_job_stop_immediately()
1498 if !has('job')
1499 return
1500 endif
1501
1502 let job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
1503 try
1504 call job_stop(job)
1505 call WaitFor('"dead" == job_status(job)')
1506 call assert_equal('dead', job_status(job))
1507 finally
1508 call job_stop(job, 'kill')
1509 endtry
1510endfunc
1511
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001512" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001513func Test_partial_in_channel_cycle()
1514 let d = {}
1515 let d.a = function('string', [d])
1516 try
1517 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1518 catch
1519 call assert_exception('E901:')
1520 endtry
1521 unlet d
1522endfunc
1523
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001524func Test_using_freed_memory()
1525 let g:a = job_start(['ls'])
1526 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001527 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001528endfunc
1529
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001530func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001531 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001532 return
1533 endif
1534 sp test_channel.vim
1535 let g:linecount = line('$')
1536 close
1537 split testout
1538 1,$delete
1539 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001540 call WaitFor('line("$") > g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001541 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001542 bwipe!
1543endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001544
Bram Moolenaar82117982016-08-27 19:21:48 +02001545func Test_raw_passes_nul()
1546 if !executable('cat') || !has('job')
1547 return
1548 endif
1549
1550 " Test lines from the job containing NUL are stored correctly in a buffer.
1551 new
1552 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1553 w! Xtestread
1554 bwipe!
1555 split testout
1556 1,$delete
1557 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1558 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001559 call assert_equal("asdf\nasdf", getline(1))
1560 call assert_equal("xxx\n", getline(2))
1561 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001562
1563 call delete('Xtestread')
1564 bwipe!
1565
1566 " Test lines from a buffer with NUL bytes are written correctly to the job.
1567 new mybuffer
1568 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1569 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1570 call WaitFor('"dead" == job_status(g:Ch_job)')
1571 bwipe!
1572 split Xtestwrite
1573 call assert_equal("asdf\nasdf", getline(1))
1574 call assert_equal("xxx\n", getline(2))
1575 call assert_equal("\nyyy", getline(3))
1576
1577 call delete('Xtestwrite')
1578 bwipe!
1579endfunc
1580
Bram Moolenaarec68a992016-10-03 21:37:41 +02001581func MyLineCountCb(ch, msg)
1582 let g:linecount += 1
1583endfunc
1584
1585func Test_read_nonl_line()
1586 if !has('job')
1587 return
1588 endif
1589
1590 let g:linecount = 0
1591 if has('win32')
1592 " workaround: 'shellescape' does improper escaping double quotes
1593 let arg = 'import sys;sys.stdout.write(\"1\n2\n3\")'
1594 else
1595 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1596 endif
1597 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1598 call WaitFor('3 <= g:linecount')
1599 call assert_equal(3, g:linecount)
1600endfunc
1601
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001602func Test_read_from_terminated_job()
1603 if !has('job')
1604 return
1605 endif
1606
1607 let g:linecount = 0
1608 if has('win32')
1609 " workaround: 'shellescape' does improper escaping double quotes
1610 let arg = 'import os,sys;os.close(1);sys.stderr.write(\"test\n\")'
1611 else
1612 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
1613 endif
1614 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1615 call WaitFor('1 <= g:linecount')
1616 call assert_equal(1, g:linecount)
1617endfunc
1618
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001619function Ch_test_close_lambda(port)
1620 let handle = ch_open('localhost:' . a:port, s:chopt)
1621 if ch_status(handle) == "fail"
1622 call assert_false(1, "Can't open channel")
1623 return
1624 endif
1625 let g:Ch_close_ret = ''
1626 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1627
1628 call assert_equal('', ch_evalexpr(handle, 'close me'))
1629 call WaitFor('"closed" == g:Ch_close_ret')
1630 call assert_equal('closed', g:Ch_close_ret)
1631endfunc
1632
1633func Test_close_lambda()
1634 call ch_log('Test_close_lambda()')
1635 call s:run_server('Ch_test_close_lambda')
1636endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001637
Bram Moolenaar9730f742016-02-28 19:50:51 +01001638" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaara58c58b2016-07-23 14:01:15 +02001639" call ch_logfile('channellog', 'w')