blob: 85f80e25cce622a1dddc78852e2e9ae0aa858e00 [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 Moolenaar40ea1da2016-02-19 22:33:35 +0100132
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100133 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100134 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100135 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100136 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100137
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100138 " Send an eval request with special characters.
139 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
140 sleep 10m
141 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
142
143 " Send an eval request to get a line with special characters.
144 call setline(3, "a\nb\<CR>c\x01d\x7fe")
145 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
146 sleep 10m
147 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
148
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100149 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100150 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100151 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100152 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100153
Bram Moolenaar55fab432016-02-07 16:53:13 +0100154 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100155 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100156 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100157 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100158
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100159 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100160 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100161 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100162 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100163
Bram Moolenaarf4160862016-02-05 23:09:12 +0100164 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100165 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200166 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100167 call assert_equal('one', getline(line('$') - 2))
168 call assert_equal('two', getline(line('$') - 1))
169 call assert_equal('three', getline('$'))
170
171 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
173 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100174
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100176
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100177 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100178 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
179 let start = reltime()
180 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
181 let elapsed = reltime(start)
182 call assert_true(reltimefloat(elapsed) > 0.3)
183 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100184
185 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100186 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100187 let resp = ch_read(handle)
188 call assert_equal(type([]), type(resp))
189 call assert_equal(type(11), type(resp[0]))
190 call assert_equal('waited', resp[1])
191
Bram Moolenaard7ece102016-02-02 23:23:02 +0100192 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100193 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100194endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100195
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100196func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100197 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200198 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100199endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100200
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100201" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200202func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100203 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200204 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100206 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100207 return
208 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100210 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100211
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100212 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100213 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100214 call assert_false(1, "Can't open second channel")
215 return
216 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100217 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
218 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100219
220 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100221 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100222
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100223 call ch_close(newhandle)
224endfunc
225
226func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100227 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200228 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100229endfunc
230
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100231" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200232func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100233 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100234 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100236 return
237 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100238
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100240
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100241 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100242endfunc
243
244func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100245 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200246 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100247endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100248
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100249"""""""""
250
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200251func Ch_handler(chan, msg)
252 unlet g:Ch_reply
253 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100254endfunc
255
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200256func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100257 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100259 call assert_false(1, "Can't open channel")
260 return
261 endif
262
263 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100264 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200265 call WaitFor('"we called you" == g:Ch_reply')
266 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100267
268 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100269 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200270 call WaitFor('"we did call you" == g:Ch_reply')
271 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100272endfunc
273
274func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100275 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200276 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200277 let s:chopt.callback = 'Ch_handler'
278 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200279 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200280 let s:chopt.callback = function('Ch_handler')
281 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100282 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100283endfunc
284
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100285"""""""""
286
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200287let g:Ch_reply = ''
288func Ch_zeroHandler(chan, msg)
289 unlet g:Ch_reply
290 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100291endfunc
292
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200293let g:Ch_zero_reply = ''
294func Ch_oneHandler(chan, msg)
295 unlet g:Ch_zero_reply
296 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100297endfunc
298
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200299func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100300 let handle = ch_open('localhost:' . a:port, s:chopt)
301 if ch_status(handle) == "fail"
302 call assert_false(1, "Can't open channel")
303 return
304 endif
305
306 " Check that eval works.
307 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
308
309 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100311 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100312 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200313 call WaitFor('"zero index" == g:Ch_reply')
314 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100315 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100316 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200317 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100318 endif
319
320 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200321 let g:Ch_reply = ''
322 let g:Ch_zero_reply = ''
323 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
324 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100325 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200326 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100327 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200328 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100329 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200330 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100331endfunc
332
333func Test_zero_reply()
334 call ch_log('Test_zero_reply()')
335 " Run with channel handler
336 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200337 let s:chopt.callback = 'Ch_zeroHandler'
338 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100339 unlet s:chopt.callback
340
341 " Run without channel handler
342 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200343 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100344endfunc
345
346"""""""""
347
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348let g:Ch_reply1 = ""
349func Ch_handleRaw1(chan, msg)
350 unlet g:Ch_reply1
351 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100352endfunc
353
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200354let g:Ch_reply2 = ""
355func Ch_handleRaw2(chan, msg)
356 unlet g:Ch_reply2
357 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100358endfunc
359
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200360let g:Ch_reply3 = ""
361func Ch_handleRaw3(chan, msg)
362 unlet g:Ch_reply3
363 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100364endfunc
365
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200366func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100367 let handle = ch_open('localhost:' . a:port, s:chopt)
368 if ch_status(handle) == "fail"
369 call assert_false(1, "Can't open channel")
370 return
371 endif
372 call ch_setoptions(handle, {'mode': 'raw'})
373
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100374 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200375 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376 call WaitFor('g:Ch_reply1 != ""')
377 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200378 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
379 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200380 call WaitFor('g:Ch_reply2 != ""')
381 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100382 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200383 call WaitFor('g:Ch_reply3 != ""')
384 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100385endfunc
386
387func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100388 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200389 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100390endfunc
391
392"""""""""
393
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100394" Test that trying to connect to a non-existing port fails quickly.
395func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100396 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100397 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100398 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100399 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100400 " Oops, port does exists.
401 call ch_close(handle)
402 else
403 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100404 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100405 endif
406
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100407 " We intend to use a socket that doesn't exist and wait for half a second
408 " before giving up. If the socket does exist it can fail in various ways.
409 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100410 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100411 try
412 let handle = ch_open('localhost:9867', {'waittime': 500})
413 if ch_status(handle) != "fail"
414 " Oops, port does exists.
415 call ch_close(handle)
416 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100417 " Failed connection should wait about 500 msec. Can be longer if the
418 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100419 let elapsed = reltime(start)
420 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100421 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100422 endif
423 catch
424 if v:exception !~ 'Connection reset by peer'
425 call assert_false(1, "Caught exception: " . v:exception)
426 endif
427 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100429
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100430"""""""""
431
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100432func Test_raw_pipe()
433 if !has('job')
434 return
435 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100436 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100437 " Add a dummy close callback to avoid that messages are dropped when calling
438 " ch_canread().
439 let job = job_start(s:python . " test_channel_pipe.py",
440 \ {'mode': 'raw', 'close_cb': {chan -> 0}})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200441 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100442 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200443
444 call assert_equal("open", ch_status(job))
445 call assert_equal("open", ch_status(job), {"part": "out"})
446 call assert_equal("open", ch_status(job), {"part": "err"})
447 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
448 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
449
450 let dict = ch_info(job)
451 call assert_true(dict.id != 0)
452 call assert_equal('open', dict.status)
453 call assert_equal('open', dict.out_status)
454 call assert_equal('RAW', dict.out_mode)
455 call assert_equal('pipe', dict.out_io)
456 call assert_equal('open', dict.err_status)
457 call assert_equal('RAW', dict.err_mode)
458 call assert_equal('pipe', dict.err_io)
459
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100460 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100461 " For a change use the job where a channel is expected.
462 call ch_sendraw(job, "echo something\n")
463 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100464 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
465
Bram Moolenaar151f6562016-03-07 21:19:38 +0100466 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100467 let g:handle = job_getchannel(job)
468 call WaitFor('ch_canread(g:handle)')
469 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100470 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
472
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200473 let g:Ch_reply = ""
474 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
475 call WaitFor('"" != g:Ch_reply')
476 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
477
Bram Moolenaar151f6562016-03-07 21:19:38 +0100478 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100479 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
480 finally
481 call job_stop(job)
482 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100483
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200484 let g:Ch_job = job
485 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100486 let info = job_info(job)
487 call assert_equal("dead", info.status)
488 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100489endfunc
490
491func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100492 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100493 return
494 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100495 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100496 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100497 call assert_equal("run", job_status(job))
498 try
499 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100500 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100501 call assert_equal("something", ch_readraw(handle))
502
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100503 call ch_sendraw(handle, "echoerr wrong\n")
504 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
505
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100506 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100507 call assert_equal("this", ch_readraw(handle))
508 call assert_equal("AND this", ch_readraw(handle))
509
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200510 call ch_sendraw(handle, "split this line\n")
511 call assert_equal("this linethis linethis line", ch_readraw(handle))
512
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100514 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100515 finally
516 call job_stop(job)
517 endtry
518endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100519
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100520func Test_nl_err_to_out_pipe()
521 if !has('job')
522 return
523 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100524 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100525 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100526 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100527 call assert_equal("run", job_status(job))
528 try
529 let handle = job_getchannel(job)
530 call ch_sendraw(handle, "echo something\n")
531 call assert_equal("something", ch_readraw(handle))
532
533 call ch_sendraw(handle, "echoerr wrong\n")
534 call assert_equal("wrong", ch_readraw(handle))
535 finally
536 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100537 call ch_logfile('')
538 let loglines = readfile('Xlog')
539 call assert_true(len(loglines) > 10)
540 let found_test = 0
541 let found_send = 0
542 let found_recv = 0
543 let found_stop = 0
544 for l in loglines
545 if l =~ 'Test_nl_err_to_out_pipe'
546 let found_test = 1
547 endif
548 if l =~ 'SEND on.*echo something'
549 let found_send = 1
550 endif
551 if l =~ 'RECV on.*something'
552 let found_recv = 1
553 endif
554 if l =~ 'Stopping job with'
555 let found_stop = 1
556 endif
557 endfor
558 call assert_equal(1, found_test)
559 call assert_equal(1, found_send)
560 call assert_equal(1, found_recv)
561 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200562 " On MS-Windows need to sleep for a moment to be able to delete the file.
563 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100564 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100565 endtry
566endfunc
567
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200568func Stop_g_job()
569 call job_stop(g:job)
570 if has('win32')
571 " On MS-Windows the server must close the file handle before we are able
572 " to delete the file.
573 call WaitFor('job_status(g:job) == "dead"')
574 sleep 10m
575 endif
576endfunc
577
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100578func Test_nl_read_file()
579 if !has('job')
580 return
581 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100582 call ch_log('Test_nl_read_file()')
583 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200584 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100585 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200586 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100587 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200588 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100589 call assert_equal("something", ch_readraw(handle))
590 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
591 call assert_equal("this", ch_readraw(handle))
592 call assert_equal("AND this", ch_readraw(handle))
593 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200594 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100595 call delete('Xinput')
596 endtry
597endfunc
598
Bram Moolenaare98d1212016-03-08 15:37:41 +0100599func Test_nl_write_out_file()
600 if !has('job')
601 return
602 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100603 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200604 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100605 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200606 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100607 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200608 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100609 call ch_sendraw(handle, "echo line one\n")
610 call ch_sendraw(handle, "echo line two\n")
611 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200612 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100613 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
614 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200615 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100616 call delete('Xoutput')
617 endtry
618endfunc
619
620func Test_nl_write_err_file()
621 if !has('job')
622 return
623 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200625 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100626 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200627 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100628 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200629 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100630 call ch_sendraw(handle, "echoerr line one\n")
631 call ch_sendraw(handle, "echoerr line two\n")
632 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200633 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100634 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
635 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200636 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100637 call delete('Xoutput')
638 endtry
639endfunc
640
641func Test_nl_write_both_file()
642 if !has('job')
643 return
644 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200646 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100647 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200648 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100649 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200650 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100651 call ch_sendraw(handle, "echoerr line one\n")
652 call ch_sendraw(handle, "echo line two\n")
653 call ch_sendraw(handle, "double this\n")
654 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200655 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
657 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200658 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100659 call delete('Xoutput')
660 endtry
661endfunc
662
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200663func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200664 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200665endfunc
666
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200667func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100668 if !has('job')
669 return
670 endif
671 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200672 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200673 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200674 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100675 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100676 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200677 if a:do_msg
678 let expected[0] = 'Reading from channel output...'
679 else
680 let options['out_msg'] = 0
681 call remove(expected, 0)
682 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 else
684 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100685 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100686 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200687 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100688 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200689 if a:nomod
690 let options['out_modifiable'] = 0
691 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100692 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100693 call assert_equal("run", job_status(job))
694 try
695 let handle = job_getchannel(job)
696 call ch_sendraw(handle, "echo line one\n")
697 call ch_sendraw(handle, "echo line two\n")
698 call ch_sendraw(handle, "double this\n")
699 call ch_sendraw(handle, "quit\n")
700 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200701 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200702 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200703 if a:nomod
704 call assert_equal(0, &modifiable)
705 else
706 call assert_equal(1, &modifiable)
707 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200708 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100709 bwipe!
710 finally
711 call job_stop(job)
712 endtry
713endfunc
714
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100715func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200716 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100717endfunc
718
719func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200720 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100721endfunc
722
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200723func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200724 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200725endfunc
726
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200727func Test_pipe_to_buffer_name_nomsg()
728 call Run_test_pipe_to_buffer(1, 0, 1)
729endfunc
730
731func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100732 if !has('job')
733 return
734 endif
735 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100736 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200737 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100738 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100739 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200740 if a:do_msg
741 let expected[0] = 'Reading from channel error...'
742 else
743 let options['err_msg'] = 0
744 call remove(expected, 0)
745 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100746 else
747 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100748 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100749 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200750 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100751 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200752 if a:nomod
753 let options['err_modifiable'] = 0
754 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100755 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100756 call assert_equal("run", job_status(job))
757 try
758 let handle = job_getchannel(job)
759 call ch_sendraw(handle, "echoerr line one\n")
760 call ch_sendraw(handle, "echoerr line two\n")
761 call ch_sendraw(handle, "doubleerr this\n")
762 call ch_sendraw(handle, "quit\n")
763 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200764 call WaitFor('line("$") >= 5')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200765 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200766 if a:nomod
767 call assert_equal(0, &modifiable)
768 else
769 call assert_equal(1, &modifiable)
770 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100771 bwipe!
772 finally
773 call job_stop(job)
774 endtry
775endfunc
776
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100777func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200778 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100779endfunc
780
781func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200782 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200783endfunc
784
785func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200786 call Run_test_pipe_err_to_buffer(1, 1, 1)
787endfunc
788
789func Test_pipe_err_to_buffer_name_nomsg()
790 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100791endfunc
792
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100793func Test_pipe_both_to_buffer()
794 if !has('job')
795 return
796 endif
797 call ch_log('Test_pipe_both_to_buffer()')
798 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100799 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100800 call assert_equal("run", job_status(job))
801 try
802 let handle = job_getchannel(job)
803 call ch_sendraw(handle, "echo line one\n")
804 call ch_sendraw(handle, "echoerr line two\n")
805 call ch_sendraw(handle, "double this\n")
806 call ch_sendraw(handle, "doubleerr that\n")
807 call ch_sendraw(handle, "quit\n")
808 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200809 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100810 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
811 bwipe!
812 finally
813 call job_stop(job)
814 endtry
815endfunc
816
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100817func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100818 if !has('job')
819 return
820 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100821 call ch_log('Test_pipe_from_buffer()')
822
823 sp pipe-input
824 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200825 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100826 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100827 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100828 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100829 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100830 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100831
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100832 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100833 call assert_equal("run", job_status(job))
834 try
835 let handle = job_getchannel(job)
836 call assert_equal('one', ch_read(handle))
837 call assert_equal('two', ch_read(handle))
838 call assert_equal('three', ch_read(handle))
839 bwipe!
840 finally
841 call job_stop(job)
842 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100843endfunc
844
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100845func Test_pipe_from_buffer_name()
846 call Run_test_pipe_from_buffer(1)
847endfunc
848
849func Test_pipe_from_buffer_nr()
850 call Run_test_pipe_from_buffer(0)
851endfunc
852
Bram Moolenaar0874a832016-09-01 15:11:51 +0200853func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200854 if !executable('sort') || !has('job')
855 return
856 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200857 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
858 if a:use_buffer
859 split sortin
860 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
861 let options.in_io = 'buffer'
862 let options.in_name = 'sortin'
863 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200864 if !a:all
865 let options.in_top = 2
866 let options.in_bot = 4
867 endif
868 let g:job = job_start('sort', options)
869 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200870
871 if !a:use_buffer
872 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
873 call ch_close_in(g:job)
874 endif
875
Bram Moolenaard8b55492016-09-01 14:35:22 +0200876 call WaitFor('job_status(g:job) == "dead"')
877 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200878
Bram Moolenaard8b55492016-09-01 14:35:22 +0200879 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200880 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200881 call assert_equal('Reading from channel output...', getline(1))
882 if a:all
883 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
884 else
885 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
886 endif
887
888 call job_stop(g:job)
889 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200890 if a:use_buffer
891 bwipe! sortin
892 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200893 bwipe! sortout
894endfunc
895
896func Test_pipe_through_sort_all()
897 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200898 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200899endfunc
900
901func Test_pipe_through_sort_some()
902 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200903 call Run_pipe_through_sort(0, 1)
904endfunc
905
906func Test_pipe_through_sort_feed()
907 call ch_log('Test_pipe_through_sort_feed()')
908 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200909endfunc
910
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100911func Test_pipe_to_nameless_buffer()
912 if !has('job')
913 return
914 endif
915 call ch_log('Test_pipe_to_nameless_buffer()')
916 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100917 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100918 call assert_equal("run", job_status(job))
919 try
920 let handle = job_getchannel(job)
921 call ch_sendraw(handle, "echo line one\n")
922 call ch_sendraw(handle, "echo line two\n")
923 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200924 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100925 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
926 bwipe!
927 finally
928 call job_stop(job)
929 endtry
930endfunc
931
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100932func Test_pipe_to_buffer_json()
933 if !has('job')
934 return
935 endif
936 call ch_log('Test_pipe_to_buffer_json()')
937 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100938 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100939 call assert_equal("run", job_status(job))
940 try
941 let handle = job_getchannel(job)
942 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
943 call ch_sendraw(handle, "echo [-2, 12.34]\n")
944 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200945 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100946 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
947 bwipe!
948 finally
949 call job_stop(job)
950 endtry
951endfunc
952
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100953" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100954func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100955 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100956 if getline(line('$') - a:offset) == a:line
957 break
958 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100959 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100960 endfor
961endfunc
962
963func Test_pipe_io_two_buffers()
964 if !has('job')
965 return
966 endif
967 call ch_log('Test_pipe_io_two_buffers()')
968
969 " Create two buffers, one to read from and one to write to.
970 split pipe-output
971 set buftype=nofile
972 split pipe-input
973 set buftype=nofile
974
975 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100976 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200977 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
978 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100979 call assert_equal("run", job_status(job))
980 try
981 exe "normal Gaecho hello\<CR>"
982 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100983 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100984 call assert_equal('hello', getline('$'))
985
986 exe bufwinnr('pipe-input') . "wincmd w"
987 exe "normal Gadouble this\<CR>"
988 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100989 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100990 call assert_equal('this', getline(line('$') - 1))
991 call assert_equal('AND this', getline('$'))
992
993 bwipe!
994 exe bufwinnr('pipe-input') . "wincmd w"
995 bwipe!
996 finally
997 call job_stop(job)
998 endtry
999endfunc
1000
1001func Test_pipe_io_one_buffer()
1002 if !has('job')
1003 return
1004 endif
1005 call ch_log('Test_pipe_io_one_buffer()')
1006
1007 " Create one buffer to read from and to write to.
1008 split pipe-io
1009 set buftype=nofile
1010
1011 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001012 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001013 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1014 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001015 call assert_equal("run", job_status(job))
1016 try
1017 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001018 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001019 call assert_equal('hello', getline(line('$') - 1))
1020
1021 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001022 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001023 call assert_equal('this', getline(line('$') - 2))
1024 call assert_equal('AND this', getline(line('$') - 1))
1025
1026 bwipe!
1027 finally
1028 call job_stop(job)
1029 endtry
1030endfunc
1031
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001032func Test_pipe_null()
1033 if !has('job')
1034 return
1035 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001036 call ch_log('Test_pipe_null()')
1037
1038 " We cannot check that no I/O works, we only check that the job starts
1039 " properly.
1040 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001041 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001042 call assert_equal("run", job_status(job))
1043 try
1044 call assert_equal('something', ch_read(job))
1045 finally
1046 call job_stop(job)
1047 endtry
1048
1049 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001050 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001051 call assert_equal("run", job_status(job))
1052 try
1053 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1054 finally
1055 call job_stop(job)
1056 endtry
1057
1058 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001059 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001060 call assert_equal("run", job_status(job))
1061 try
1062 call assert_equal('something', ch_read(job))
1063 finally
1064 call job_stop(job)
1065 endtry
1066
1067 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001068 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001069 call assert_equal("run", job_status(job))
1070 call job_stop(job)
1071
1072 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001073 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001074 call assert_equal("run", job_status(job))
1075 call assert_equal('channel fail', string(job_getchannel(job)))
1076 call assert_equal('fail', ch_status(job))
1077 call job_stop(job)
1078endfunc
1079
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001080func Test_pipe_to_buffer_raw()
1081 if !has('job')
1082 return
1083 endif
1084 call ch_log('Test_raw_pipe_to_buffer()')
1085 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1086 split testout
1087 let job = job_start([s:python, '-c',
1088 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1089 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001090 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001091 try
1092 for line in getline(2, '$')
1093 let line = substitute(line, '^\.*', '', '')
1094 call assert_equal('', line)
1095 endfor
1096 finally
1097 call job_stop(job)
1098 bwipe!
1099 endtry
1100endfunc
1101
Bram Moolenaarde279892016-03-11 22:19:44 +01001102func Test_reuse_channel()
1103 if !has('job')
1104 return
1105 endif
1106 call ch_log('Test_reuse_channel()')
1107
1108 let job = job_start(s:python . " test_channel_pipe.py")
1109 call assert_equal("run", job_status(job))
1110 let handle = job_getchannel(job)
1111 try
1112 call ch_sendraw(handle, "echo something\n")
1113 call assert_equal("something", ch_readraw(handle))
1114 finally
1115 call job_stop(job)
1116 endtry
1117
1118 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1119 call assert_equal("run", job_status(job))
1120 let handle = job_getchannel(job)
1121 try
1122 call ch_sendraw(handle, "echo again\n")
1123 call assert_equal("again", ch_readraw(handle))
1124 finally
1125 call job_stop(job)
1126 endtry
1127endfunc
1128
Bram Moolenaar75f72652016-03-20 22:16:56 +01001129func Test_out_cb()
1130 if !has('job')
1131 return
1132 endif
1133 call ch_log('Test_out_cb()')
1134
1135 let dict = {'thisis': 'dict: '}
1136 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001137 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001138 endfunc
1139 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001140 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001141 endfunc
1142 let job = job_start(s:python . " test_channel_pipe.py",
1143 \ {'out_cb': dict.outHandler,
1144 \ 'out_mode': 'json',
1145 \ 'err_cb': dict.errHandler,
1146 \ 'err_mode': 'json'})
1147 call assert_equal("run", job_status(job))
1148 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001149 let g:Ch_outmsg = ''
1150 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001151 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1152 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001153 call WaitFor('g:Ch_outmsg != ""')
1154 call assert_equal("dict: hello", g:Ch_outmsg)
1155 call WaitFor('g:Ch_errmsg != ""')
1156 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001157 finally
1158 call job_stop(job)
1159 endtry
1160endfunc
1161
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001162func Test_out_close_cb()
1163 if !has('job')
1164 return
1165 endif
1166 call ch_log('Test_out_close_cb()')
1167
1168 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001169 let g:Ch_msg1 = ''
1170 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001171 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001172 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001173 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001174 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001175 let s:counter += 1
1176 endfunc
1177 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001178 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001179 let s:counter += 1
1180 endfunc
1181 let job = job_start(s:python . " test_channel_pipe.py quit now",
1182 \ {'out_cb': 'OutHandler',
1183 \ 'close_cb': 'CloseHandler'})
1184 call assert_equal("run", job_status(job))
1185 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001186 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1187 call assert_equal('quit', g:Ch_msg1)
1188 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001189 finally
1190 call job_stop(job)
1191 delfunc OutHandler
1192 delfunc CloseHandler
1193 endtry
1194endfunc
1195
Bram Moolenaar437905c2016-04-26 19:01:05 +02001196func Test_read_in_close_cb()
1197 if !has('job')
1198 return
1199 endif
1200 call ch_log('Test_read_in_close_cb()')
1201
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001202 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001203 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001204 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001205 endfunc
1206 let job = job_start(s:python . " test_channel_pipe.py quit now",
1207 \ {'close_cb': 'CloseHandler'})
1208 call assert_equal("run", job_status(job))
1209 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001210 call WaitFor('g:Ch_received != ""')
1211 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001212 finally
1213 call job_stop(job)
1214 delfunc CloseHandler
1215 endtry
1216endfunc
1217
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001218func Test_out_cb_lambda()
1219 if !has('job')
1220 return
1221 endif
1222 call ch_log('Test_out_cb_lambda()')
1223
1224 let job = job_start(s:python . " test_channel_pipe.py",
1225 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1226 \ 'out_mode': 'json',
1227 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1228 \ 'err_mode': 'json'})
1229 call assert_equal("run", job_status(job))
1230 try
1231 let g:Ch_outmsg = ''
1232 let g:Ch_errmsg = ''
1233 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1234 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1235 call WaitFor('g:Ch_outmsg != ""')
1236 call assert_equal("lambda: hello", g:Ch_outmsg)
1237 call WaitFor('g:Ch_errmsg != ""')
1238 call assert_equal("lambda: there", g:Ch_errmsg)
1239 finally
1240 call job_stop(job)
1241 endtry
1242endfunc
1243
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001244func Test_close_and_exit_cb()
1245 if !has('job')
1246 return
1247 endif
1248 call ch_log('Test_close_and_exit_cb')
1249
1250 let dict = {'ret': {}}
1251 func dict.close_cb(ch) dict
1252 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1253 endfunc
1254 func dict.exit_cb(job, status) dict
1255 let self.ret['exit_cb'] = job_status(a:job)
1256 endfunc
1257
1258 let g:job = job_start('echo', {
1259 \ 'close_cb': dict.close_cb,
1260 \ 'exit_cb': dict.exit_cb,
1261 \ })
1262 call assert_equal('run', job_status(g:job))
1263 unlet g:job
1264 call WaitFor('len(dict.ret) >= 2')
1265 call assert_equal(2, len(dict.ret))
1266 call assert_match('^\%(dead\|run\)', dict.ret['close_cb'])
1267 call assert_equal('dead', dict.ret['exit_cb'])
1268endfunc
1269
Bram Moolenaard46ae142016-02-16 13:33:52 +01001270""""""""""
1271
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001272let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001273func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001274 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001275 unlet s:channelfd
1276endfunc
1277
1278" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001279func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001280 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001281 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001282 call WaitFor('"what?" == g:Ch_unletResponse')
1283 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001284endfunc
1285
1286func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001287 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001288 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001289endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001290
Bram Moolenaard46ae142016-02-16 13:33:52 +01001291""""""""""
1292
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001293let g:Ch_unletResponse = ''
1294func Ch_CloseHandler(handle, msg)
1295 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001296 call ch_close(s:channelfd)
1297endfunc
1298
1299" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001300func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001301 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001302 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1303 call WaitFor('"what?" == g:Ch_unletResponse')
1304 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001305endfunc
1306
1307func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001308 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001309 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001310endfunc
1311
1312""""""""""
1313
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001314func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001315 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001316 silent! let ch = ch_open("noserver")
1317 echo ch
1318 let d = ch
1319endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001320
1321""""""""""
1322
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001323func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001324 " Wait up to a second for the port to open.
1325 let s:chopt.waittime = 1000
1326 let channel = ch_open('localhost:' . a:port, s:chopt)
1327 unlet s:chopt.waittime
1328 if ch_status(channel) == "fail"
1329 call assert_false(1, "Can't open channel")
1330 return
1331 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001332 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001333 call ch_close(channel)
1334endfunc
1335
1336func Test_open_delay()
1337 call ch_log('Test_open_delay()')
1338 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001339 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001340endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001341
1342"""""""""
1343
1344function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001345 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001346endfunc
1347
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001348function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001349 let handle = ch_open('localhost:' . a:port, s:chopt)
1350 if ch_status(handle) == "fail"
1351 call assert_false(1, "Can't open channel")
1352 return
1353 endif
1354
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001355 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001356 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001357 call WaitFor('len(g:Ch_call_ret) > 0')
1358 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001359endfunc
1360
1361func Test_call()
1362 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001363 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001364endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001365
1366"""""""""
1367
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001368let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001369function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001370 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001371endfunc
1372
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373function Ch_test_exit_callback(port)
1374 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1375 let g:Ch_exit_job = g:currentJob
1376 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001377endfunc
1378
1379func Test_exit_callback()
1380 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001381 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001382 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001383
Bram Moolenaar9730f742016-02-28 19:50:51 +01001384 " wait up to a second for the job to exit
1385 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001386 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001387 break
1388 endif
1389 sleep 10m
1390 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001391 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001392 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001393
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001394 call assert_equal('done', g:Ch_job_exit_ret)
1395 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1396 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001397 endif
1398endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001399
Bram Moolenaar97792de2016-10-15 18:36:49 +02001400function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001401 if job_info(a:job).process == g:exit_cb_val.process
1402 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1403 endif
1404 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001405endfunction
1406
1407func Test_exit_callback_interval()
1408 if !has('job')
1409 return
1410 endif
1411
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001412 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001413 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001414 let g:exit_cb_val.process = job_info(job).process
1415 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1416 let elapsed = reltimefloat(g:exit_cb_val.end)
1417 call assert_true(elapsed > 0.5)
1418 call assert_true(elapsed < 1.0)
1419
1420 " case: unreferenced job, using timer
1421 if !has('timers')
1422 return
1423 endif
1424
1425 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1426 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1427 let g:exit_cb_val.process = job_info(g:job).process
1428 unlet g:job
1429 call Standby(1000)
1430 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1431 let elapsed = reltimefloat(g:exit_cb_val.end)
1432 else
1433 let elapsed = 1.0
1434 endif
1435 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001436 call assert_true(elapsed < 1.0)
1437endfunc
1438
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001439"""""""""
1440
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001441let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001442function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001443 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001444endfunc
1445
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001446function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001447 let handle = ch_open('localhost:' . a:port, s:chopt)
1448 if ch_status(handle) == "fail"
1449 call assert_false(1, "Can't open channel")
1450 return
1451 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001452 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001453
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001454 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001455 call WaitFor('"closed" == g:Ch_close_ret')
1456 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001457endfunc
1458
1459func Test_close_callback()
1460 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001461 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001462endfunc
1463
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001464function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001465 let handle = ch_open('localhost:' . a:port, s:chopt)
1466 if ch_status(handle) == "fail"
1467 call assert_false(1, "Can't open channel")
1468 return
1469 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001470 let g:Ch_d = {}
1471 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001472 let self.close_ret = 'closed'
1473 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001475
1476 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001477 call WaitFor('"closed" == g:Ch_d.close_ret')
1478 call assert_equal('closed', g:Ch_d.close_ret)
1479 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001480endfunc
1481
1482func Test_close_partial()
1483 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001484 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001485endfunc
1486
Bram Moolenaar80385682016-03-27 19:13:35 +02001487func Test_job_start_invalid()
1488 call assert_fails('call job_start($x)', 'E474:')
1489 call assert_fails('call job_start("")', 'E474:')
1490endfunc
1491
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001492func Test_job_stop_immediately()
1493 if !has('job')
1494 return
1495 endif
1496
1497 let job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
1498 try
1499 call job_stop(job)
1500 call WaitFor('"dead" == job_status(job)')
1501 call assert_equal('dead', job_status(job))
1502 finally
1503 call job_stop(job, 'kill')
1504 endtry
1505endfunc
1506
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001507" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001508func Test_partial_in_channel_cycle()
1509 let d = {}
1510 let d.a = function('string', [d])
1511 try
1512 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1513 catch
1514 call assert_exception('E901:')
1515 endtry
1516 unlet d
1517endfunc
1518
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001519func Test_using_freed_memory()
1520 let g:a = job_start(['ls'])
1521 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001522 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001523endfunc
1524
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001525func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001526 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001527 return
1528 endif
1529 sp test_channel.vim
1530 let g:linecount = line('$')
1531 close
1532 split testout
1533 1,$delete
1534 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001535 call WaitFor('line("$") > g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001536 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001537 bwipe!
1538endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001539
Bram Moolenaar82117982016-08-27 19:21:48 +02001540func Test_raw_passes_nul()
1541 if !executable('cat') || !has('job')
1542 return
1543 endif
1544
1545 " Test lines from the job containing NUL are stored correctly in a buffer.
1546 new
1547 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1548 w! Xtestread
1549 bwipe!
1550 split testout
1551 1,$delete
1552 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1553 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001554 call assert_equal("asdf\nasdf", getline(1))
1555 call assert_equal("xxx\n", getline(2))
1556 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001557
1558 call delete('Xtestread')
1559 bwipe!
1560
1561 " Test lines from a buffer with NUL bytes are written correctly to the job.
1562 new mybuffer
1563 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1564 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1565 call WaitFor('"dead" == job_status(g:Ch_job)')
1566 bwipe!
1567 split Xtestwrite
1568 call assert_equal("asdf\nasdf", getline(1))
1569 call assert_equal("xxx\n", getline(2))
1570 call assert_equal("\nyyy", getline(3))
1571
1572 call delete('Xtestwrite')
1573 bwipe!
1574endfunc
1575
Bram Moolenaarec68a992016-10-03 21:37:41 +02001576func MyLineCountCb(ch, msg)
1577 let g:linecount += 1
1578endfunc
1579
1580func Test_read_nonl_line()
1581 if !has('job')
1582 return
1583 endif
1584
1585 let g:linecount = 0
1586 if has('win32')
1587 " workaround: 'shellescape' does improper escaping double quotes
1588 let arg = 'import sys;sys.stdout.write(\"1\n2\n3\")'
1589 else
1590 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1591 endif
1592 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1593 call WaitFor('3 <= g:linecount')
1594 call assert_equal(3, g:linecount)
1595endfunc
1596
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001597func Test_read_from_terminated_job()
1598 if !has('job')
1599 return
1600 endif
1601
1602 let g:linecount = 0
1603 if has('win32')
1604 " workaround: 'shellescape' does improper escaping double quotes
1605 let arg = 'import os,sys;os.close(1);sys.stderr.write(\"test\n\")'
1606 else
1607 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
1608 endif
1609 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1610 call WaitFor('1 <= g:linecount')
1611 call assert_equal(1, g:linecount)
1612endfunc
1613
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001614function Ch_test_close_lambda(port)
1615 let handle = ch_open('localhost:' . a:port, s:chopt)
1616 if ch_status(handle) == "fail"
1617 call assert_false(1, "Can't open channel")
1618 return
1619 endif
1620 let g:Ch_close_ret = ''
1621 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1622
1623 call assert_equal('', ch_evalexpr(handle, 'close me'))
1624 call WaitFor('"closed" == g:Ch_close_ret')
1625 call assert_equal('closed', g:Ch_close_ret)
1626endfunc
1627
1628func Test_close_lambda()
1629 call ch_log('Test_close_lambda()')
1630 call s:run_server('Ch_test_close_lambda')
1631endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001632
Bram Moolenaar9730f742016-02-28 19:50:51 +01001633" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaara58c58b2016-07-23 14:01:15 +02001634" call ch_logfile('channellog', 'w')