blob: 3694fc24adec5fdf9aa9dd20a2486019c9307819 [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 Moolenaar37175402017-03-18 20:18:45 +010011 " Can't run this test without Python.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaar37175402017-03-18 20:18:45 +010015" Uncomment the next line to see what happens. Output is in
16" src/testdir/channellog.
17" call ch_logfile('channellog', 'w')
18
Bram Moolenaare74e8e72016-02-16 22:01:30 +010019let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010020
Bram Moolenaard6a8d482016-02-10 20:32:20 +010021" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010022func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020023 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010024endfunc
25
Bram Moolenaar321efdd2016-07-15 17:09:11 +020026let g:Ch_responseMsg = ''
27func Ch_requestHandler(handle, msg)
28 let g:Ch_responseHandle = a:handle
29 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010030endfunc
31
Bram Moolenaar321efdd2016-07-15 17:09:11 +020032func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010033 " Avoid dropping messages, since we don't use a callback here.
34 let s:chopt.drop = 'never'
Bram Moolenaard6a8d482016-02-10 20:32:20 +010035 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010036 unlet s:chopt.drop
Bram Moolenaar77073442016-02-13 23:23:53 +010037 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010038 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010039 return
40 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010041 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010042 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010043 call assert_equal('no process', string(ch_getjob(handle)))
44 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010045 let dict = ch_info(handle)
46 call assert_true(dict.id != 0)
47 call assert_equal('open', dict.status)
48 call assert_equal(a:port, string(dict.port))
49 call assert_equal('open', dict.sock_status)
50 call assert_equal('socket', dict.sock_io)
51
Bram Moolenaard7ece102016-02-02 23:23:02 +010052 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010053 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010054
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010055 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010056 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
57 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
58 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
59
60 " split command should work
61 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020062 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010063 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010064
Bram Moolenaarf1f07922016-08-26 17:58:53 +020065 " string with ][ should work
66 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
67
Bram Moolenaar4b785f62016-11-29 21:54:44 +010068 " nothing to read now
69 call assert_equal(0, ch_canread(handle))
70
Bram Moolenaarf1f07922016-08-26 17:58:53 +020071 " sending three messages quickly then reading should work
72 for i in range(3)
73 call ch_sendexpr(handle, 'echo hello ' . i)
74 endfor
75 call assert_equal('hello 0', ch_read(handle)[1])
76 call assert_equal('hello 1', ch_read(handle)[1])
77 call assert_equal('hello 2', ch_read(handle)[1])
78
Bram Moolenaard7ece102016-02-02 23:23:02 +010079 " Request that triggers sending two ex commands. These will usually be
80 " handled before getting the response, but it's not guaranteed, thus wait a
81 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010082 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020083 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010084 call assert_equal('added1', getline(line('$') - 1))
85 call assert_equal('added2', getline('$'))
86
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010087 " Request command "foo bar", which fails silently.
88 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020089 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020090 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010091
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010092 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020093 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010094 call assert_equal('added more', getline('$'))
95
Bram Moolenaara07fec92016-02-05 21:04:08 +010096 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020097 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
98 call WaitFor('exists("g:Ch_responseHandle")')
99 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100100 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100101 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200102 call assert_equal(handle, g:Ch_responseHandle)
103 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100104 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200105 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100106
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200107 let g:Ch_responseMsg = ''
108 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
109 call WaitFor('exists("g:Ch_responseHandle")')
110 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100111 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100112 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200113 call assert_equal(handle, g:Ch_responseHandle)
114 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100115 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200116 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100117
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200118 " Using lambda.
119 let g:Ch_responseMsg = ''
120 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
121 call WaitFor('exists("g:Ch_responseHandle")')
122 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100123 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200124 else
125 call assert_equal(handle, g:Ch_responseHandle)
126 unlet g:Ch_responseHandle
127 endif
128 call assert_equal('got it', g:Ch_responseMsg)
129
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100130 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200131 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100132
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100133 " check setting options (without testing the effect)
134 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100135 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100136 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100137 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100138 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100139 call ch_setoptions(handle, {'drop': 'never'})
140 call ch_setoptions(handle, {'drop': 'auto'})
141 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100142
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100143 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100144 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100145 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100146 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100147
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100148 " Send an eval request with special characters.
149 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
150 sleep 10m
151 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
152
153 " Send an eval request to get a line with special characters.
154 call setline(3, "a\nb\<CR>c\x01d\x7fe")
155 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
156 sleep 10m
157 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
158
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100159 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100160 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100161 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100162 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163
Bram Moolenaar55fab432016-02-07 16:53:13 +0100164 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100165 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100166 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100167 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100168
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100169 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100170 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100171 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100172 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100173
Bram Moolenaarf4160862016-02-05 23:09:12 +0100174 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200176 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100177 call assert_equal('one', getline(line('$') - 2))
178 call assert_equal('two', getline(line('$') - 1))
179 call assert_equal('three', getline('$'))
180
181 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
183 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100184
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100186
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100187 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100188 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
189 let start = reltime()
190 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
191 let elapsed = reltime(start)
192 call assert_true(reltimefloat(elapsed) > 0.3)
193 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100194
195 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100197 let resp = ch_read(handle)
198 call assert_equal(type([]), type(resp))
199 call assert_equal(type(11), type(resp[0]))
200 call assert_equal('waited', resp[1])
201
Bram Moolenaard7ece102016-02-02 23:23:02 +0100202 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100204endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100205
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100206func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200208 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100209endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100210
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100211" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200212func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100213 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200214 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100215 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100216 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100217 return
218 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100221
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100222 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100223 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100224 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100225 return
226 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
228 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100229
230 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100231 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100232
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100233 call ch_close(newhandle)
234endfunc
235
236func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100237 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200238 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100239endfunc
240
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100241" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200242func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100243 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100245 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100246 return
247 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100249 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100250
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100251 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100252endfunc
253
254func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100255 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200256 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100257endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100258
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100259"""""""""
260
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200261func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100262 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200263 unlet g:Ch_reply
264 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100265endfunc
266
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200267func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100268 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100269 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100270 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100271 return
272 endif
273
274 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100275 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200276 call WaitFor('"we called you" == g:Ch_reply')
277 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100278
279 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100280 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200281 call WaitFor('"we did call you" == g:Ch_reply')
282 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100283endfunc
284
285func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100286 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200287 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200288 let s:chopt.callback = 'Ch_handler'
289 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200290 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200291 let s:chopt.callback = function('Ch_handler')
292 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100293 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100294endfunc
295
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100296"""""""""
297
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200298let g:Ch_reply = ''
299func Ch_zeroHandler(chan, msg)
300 unlet g:Ch_reply
301 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100302endfunc
303
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200304let g:Ch_zero_reply = ''
305func Ch_oneHandler(chan, msg)
306 unlet g:Ch_zero_reply
307 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100308endfunc
309
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100311 let handle = ch_open('localhost:' . a:port, s:chopt)
312 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100313 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100314 return
315 endif
316
317 " Check that eval works.
318 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
319
320 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200321 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100322 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200324 call WaitFor('"zero index" == g:Ch_reply')
325 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100327 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200328 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100329 endif
330
331 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200332 let g:Ch_reply = ''
333 let g:Ch_zero_reply = ''
334 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
335 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200337 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100338 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200339 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200341 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100342endfunc
343
344func Test_zero_reply()
345 call ch_log('Test_zero_reply()')
346 " Run with channel handler
347 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200348 let s:chopt.callback = 'Ch_zeroHandler'
349 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 unlet s:chopt.callback
351
352 " Run without channel handler
353 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200354 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100355endfunc
356
357"""""""""
358
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200359let g:Ch_reply1 = ""
360func Ch_handleRaw1(chan, msg)
361 unlet g:Ch_reply1
362 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100363endfunc
364
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200365let g:Ch_reply2 = ""
366func Ch_handleRaw2(chan, msg)
367 unlet g:Ch_reply2
368 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100369endfunc
370
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200371let g:Ch_reply3 = ""
372func Ch_handleRaw3(chan, msg)
373 unlet g:Ch_reply3
374 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100375endfunc
376
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200377func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100378 let handle = ch_open('localhost:' . a:port, s:chopt)
379 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100380 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100381 return
382 endif
383 call ch_setoptions(handle, {'mode': 'raw'})
384
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100385 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200386 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200387 call WaitFor('g:Ch_reply1 != ""')
388 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200389 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
390 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200391 call WaitFor('g:Ch_reply2 != ""')
392 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100393 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200394 call WaitFor('g:Ch_reply3 != ""')
395 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100396endfunc
397
398func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100399 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200400 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100401endfunc
402
403"""""""""
404
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100405" Test that trying to connect to a non-existing port fails quickly.
406func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100407 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100408 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100409 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100410 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100411 " Oops, port does exists.
412 call ch_close(handle)
413 else
414 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100415 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100416 endif
417
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100418 " We intend to use a socket that doesn't exist and wait for half a second
419 " before giving up. If the socket does exist it can fail in various ways.
420 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100421 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100422 try
423 let handle = ch_open('localhost:9867', {'waittime': 500})
424 if ch_status(handle) != "fail"
425 " Oops, port does exists.
426 call ch_close(handle)
427 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100428 " Failed connection should wait about 500 msec. Can be longer if the
429 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100430 let elapsed = reltime(start)
431 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100432 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100433 endif
434 catch
435 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100436 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100437 endif
438 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100439endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100440
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100441"""""""""
442
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100443func Test_raw_pipe()
444 if !has('job')
445 return
446 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100447 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100448 " Add a dummy close callback to avoid that messages are dropped when calling
449 " ch_canread().
450 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100451 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200452 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100453 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200454
455 call assert_equal("open", ch_status(job))
456 call assert_equal("open", ch_status(job), {"part": "out"})
457 call assert_equal("open", ch_status(job), {"part": "err"})
458 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
459 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
460
461 let dict = ch_info(job)
462 call assert_true(dict.id != 0)
463 call assert_equal('open', dict.status)
464 call assert_equal('open', dict.out_status)
465 call assert_equal('RAW', dict.out_mode)
466 call assert_equal('pipe', dict.out_io)
467 call assert_equal('open', dict.err_status)
468 call assert_equal('RAW', dict.err_mode)
469 call assert_equal('pipe', dict.err_io)
470
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100472 " For a change use the job where a channel is expected.
473 call ch_sendraw(job, "echo something\n")
474 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100475 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
476
Bram Moolenaar151f6562016-03-07 21:19:38 +0100477 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100478 let g:handle = job_getchannel(job)
479 call WaitFor('ch_canread(g:handle)')
480 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100481 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100482 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
483
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200484 let g:Ch_reply = ""
485 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
486 call WaitFor('"" != g:Ch_reply')
487 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
488
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
491 finally
492 call job_stop(job)
493 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100494
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200495 let g:Ch_job = job
496 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100497 let info = job_info(job)
498 call assert_equal("dead", info.status)
499 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100500endfunc
501
502func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100503 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100504 return
505 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100506 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100507 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508 call assert_equal("run", job_status(job))
509 try
510 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100511 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100512 call assert_equal("something", ch_readraw(handle))
513
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100514 call ch_sendraw(handle, "echoerr wrong\n")
515 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
516
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100517 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100518 call assert_equal("this", ch_readraw(handle))
519 call assert_equal("AND this", ch_readraw(handle))
520
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200521 call ch_sendraw(handle, "split this line\n")
522 call assert_equal("this linethis linethis line", ch_readraw(handle))
523
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100524 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100525 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100526 finally
527 call job_stop(job)
528 endtry
529endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100530
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100531func Test_nl_err_to_out_pipe()
532 if !has('job')
533 return
534 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100535 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100536 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100537 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100538 call assert_equal("run", job_status(job))
539 try
540 let handle = job_getchannel(job)
541 call ch_sendraw(handle, "echo something\n")
542 call assert_equal("something", ch_readraw(handle))
543
544 call ch_sendraw(handle, "echoerr wrong\n")
545 call assert_equal("wrong", ch_readraw(handle))
546 finally
547 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100548 call ch_logfile('')
549 let loglines = readfile('Xlog')
550 call assert_true(len(loglines) > 10)
551 let found_test = 0
552 let found_send = 0
553 let found_recv = 0
554 let found_stop = 0
555 for l in loglines
556 if l =~ 'Test_nl_err_to_out_pipe'
557 let found_test = 1
558 endif
559 if l =~ 'SEND on.*echo something'
560 let found_send = 1
561 endif
562 if l =~ 'RECV on.*something'
563 let found_recv = 1
564 endif
565 if l =~ 'Stopping job with'
566 let found_stop = 1
567 endif
568 endfor
569 call assert_equal(1, found_test)
570 call assert_equal(1, found_send)
571 call assert_equal(1, found_recv)
572 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200573 " On MS-Windows need to sleep for a moment to be able to delete the file.
574 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100575 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100576 endtry
577endfunc
578
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200579func Stop_g_job()
580 call job_stop(g:job)
581 if has('win32')
582 " On MS-Windows the server must close the file handle before we are able
583 " to delete the file.
584 call WaitFor('job_status(g:job) == "dead"')
585 sleep 10m
586 endif
587endfunc
588
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100589func Test_nl_read_file()
590 if !has('job')
591 return
592 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100593 call ch_log('Test_nl_read_file()')
594 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200595 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100596 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200597 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100598 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200599 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100600 call assert_equal("something", ch_readraw(handle))
601 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
602 call assert_equal("this", ch_readraw(handle))
603 call assert_equal("AND this", ch_readraw(handle))
604 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200605 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100606 call delete('Xinput')
607 endtry
608endfunc
609
Bram Moolenaare98d1212016-03-08 15:37:41 +0100610func Test_nl_write_out_file()
611 if !has('job')
612 return
613 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100614 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200615 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100616 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200617 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100618 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200619 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100620 call ch_sendraw(handle, "echo line one\n")
621 call ch_sendraw(handle, "echo line two\n")
622 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200623 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
625 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200626 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100627 call delete('Xoutput')
628 endtry
629endfunc
630
631func Test_nl_write_err_file()
632 if !has('job')
633 return
634 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200636 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100637 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200638 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100639 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200640 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100641 call ch_sendraw(handle, "echoerr line one\n")
642 call ch_sendraw(handle, "echoerr line two\n")
643 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200644 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
646 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 call delete('Xoutput')
649 endtry
650endfunc
651
652func Test_nl_write_both_file()
653 if !has('job')
654 return
655 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200657 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100658 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100660 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200661 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100662 call ch_sendraw(handle, "echoerr line one\n")
663 call ch_sendraw(handle, "echo line two\n")
664 call ch_sendraw(handle, "double this\n")
665 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200666 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100667 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
668 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200669 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100670 call delete('Xoutput')
671 endtry
672endfunc
673
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200674func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200675 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200676endfunc
677
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200678func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100679 if !has('job')
680 return
681 endif
682 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200683 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200684 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200685 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100686 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100687 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200688 if a:do_msg
689 let expected[0] = 'Reading from channel output...'
690 else
691 let options['out_msg'] = 0
692 call remove(expected, 0)
693 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100694 else
695 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100696 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100697 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200698 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100699 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200700 if a:nomod
701 let options['out_modifiable'] = 0
702 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100703 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100704 call assert_equal("run", job_status(job))
705 try
706 let handle = job_getchannel(job)
707 call ch_sendraw(handle, "echo line one\n")
708 call ch_sendraw(handle, "echo line two\n")
709 call ch_sendraw(handle, "double this\n")
710 call ch_sendraw(handle, "quit\n")
711 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200712 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200713 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200714 if a:nomod
715 call assert_equal(0, &modifiable)
716 else
717 call assert_equal(1, &modifiable)
718 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200719 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100720 bwipe!
721 finally
722 call job_stop(job)
723 endtry
724endfunc
725
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200727 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100728endfunc
729
730func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200731 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100732endfunc
733
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200734func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200735 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200736endfunc
737
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200738func Test_pipe_to_buffer_name_nomsg()
739 call Run_test_pipe_to_buffer(1, 0, 1)
740endfunc
741
742func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100743 if !has('job')
744 return
745 endif
746 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100747 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200748 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100749 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100750 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200751 if a:do_msg
752 let expected[0] = 'Reading from channel error...'
753 else
754 let options['err_msg'] = 0
755 call remove(expected, 0)
756 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757 else
758 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100759 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100760 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200761 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100762 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200763 if a:nomod
764 let options['err_modifiable'] = 0
765 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100766 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100767 call assert_equal("run", job_status(job))
768 try
769 let handle = job_getchannel(job)
770 call ch_sendraw(handle, "echoerr line one\n")
771 call ch_sendraw(handle, "echoerr line two\n")
772 call ch_sendraw(handle, "doubleerr this\n")
773 call ch_sendraw(handle, "quit\n")
774 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200775 call WaitFor('line("$") >= 5')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200776 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200777 if a:nomod
778 call assert_equal(0, &modifiable)
779 else
780 call assert_equal(1, &modifiable)
781 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100782 bwipe!
783 finally
784 call job_stop(job)
785 endtry
786endfunc
787
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100788func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200789 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100790endfunc
791
792func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200793 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200794endfunc
795
796func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200797 call Run_test_pipe_err_to_buffer(1, 1, 1)
798endfunc
799
800func Test_pipe_err_to_buffer_name_nomsg()
801 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100802endfunc
803
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100804func Test_pipe_both_to_buffer()
805 if !has('job')
806 return
807 endif
808 call ch_log('Test_pipe_both_to_buffer()')
809 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100810 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100811 call assert_equal("run", job_status(job))
812 try
813 let handle = job_getchannel(job)
814 call ch_sendraw(handle, "echo line one\n")
815 call ch_sendraw(handle, "echoerr line two\n")
816 call ch_sendraw(handle, "double this\n")
817 call ch_sendraw(handle, "doubleerr that\n")
818 call ch_sendraw(handle, "quit\n")
819 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200820 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100821 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
822 bwipe!
823 finally
824 call job_stop(job)
825 endtry
826endfunc
827
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100828func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100829 if !has('job')
830 return
831 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100832 call ch_log('Test_pipe_from_buffer()')
833
834 sp pipe-input
835 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200836 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100837 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100838 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100839 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100840 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100841 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100842
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100843 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100844 call assert_equal("run", job_status(job))
845 try
846 let handle = job_getchannel(job)
847 call assert_equal('one', ch_read(handle))
848 call assert_equal('two', ch_read(handle))
849 call assert_equal('three', ch_read(handle))
850 bwipe!
851 finally
852 call job_stop(job)
853 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100854endfunc
855
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100856func Test_pipe_from_buffer_name()
857 call Run_test_pipe_from_buffer(1)
858endfunc
859
860func Test_pipe_from_buffer_nr()
861 call Run_test_pipe_from_buffer(0)
862endfunc
863
Bram Moolenaar0874a832016-09-01 15:11:51 +0200864func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200865 if !executable('sort') || !has('job')
866 return
867 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200868 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
869 if a:use_buffer
870 split sortin
871 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
872 let options.in_io = 'buffer'
873 let options.in_name = 'sortin'
874 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200875 if !a:all
876 let options.in_top = 2
877 let options.in_bot = 4
878 endif
879 let g:job = job_start('sort', options)
880 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200881
882 if !a:use_buffer
883 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
884 call ch_close_in(g:job)
885 endif
886
Bram Moolenaard8b55492016-09-01 14:35:22 +0200887 call WaitFor('job_status(g:job) == "dead"')
888 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200889
Bram Moolenaard8b55492016-09-01 14:35:22 +0200890 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200891 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200892 call assert_equal('Reading from channel output...', getline(1))
893 if a:all
894 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
895 else
896 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
897 endif
898
899 call job_stop(g:job)
900 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200901 if a:use_buffer
902 bwipe! sortin
903 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200904 bwipe! sortout
905endfunc
906
907func Test_pipe_through_sort_all()
908 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200910endfunc
911
912func Test_pipe_through_sort_some()
913 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200914 call Run_pipe_through_sort(0, 1)
915endfunc
916
917func Test_pipe_through_sort_feed()
918 call ch_log('Test_pipe_through_sort_feed()')
919 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200920endfunc
921
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100922func Test_pipe_to_nameless_buffer()
923 if !has('job')
924 return
925 endif
926 call ch_log('Test_pipe_to_nameless_buffer()')
927 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100928 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100929 call assert_equal("run", job_status(job))
930 try
931 let handle = job_getchannel(job)
932 call ch_sendraw(handle, "echo line one\n")
933 call ch_sendraw(handle, "echo line two\n")
934 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200935 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100936 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
937 bwipe!
938 finally
939 call job_stop(job)
940 endtry
941endfunc
942
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100943func Test_pipe_to_buffer_json()
944 if !has('job')
945 return
946 endif
947 call ch_log('Test_pipe_to_buffer_json()')
948 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100949 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100950 call assert_equal("run", job_status(job))
951 try
952 let handle = job_getchannel(job)
953 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
954 call ch_sendraw(handle, "echo [-2, 12.34]\n")
955 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200956 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100957 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
958 bwipe!
959 finally
960 call job_stop(job)
961 endtry
962endfunc
963
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100964" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100965func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100966 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100967 if getline(line('$') - a:offset) == a:line
968 break
969 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100970 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100971 endfor
972endfunc
973
974func Test_pipe_io_two_buffers()
975 if !has('job')
976 return
977 endif
978 call ch_log('Test_pipe_io_two_buffers()')
979
980 " Create two buffers, one to read from and one to write to.
981 split pipe-output
982 set buftype=nofile
983 split pipe-input
984 set buftype=nofile
985
986 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100987 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200988 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
989 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100990 call assert_equal("run", job_status(job))
991 try
992 exe "normal Gaecho hello\<CR>"
993 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100994 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100995 call assert_equal('hello', getline('$'))
996
997 exe bufwinnr('pipe-input') . "wincmd w"
998 exe "normal Gadouble this\<CR>"
999 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001000 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001001 call assert_equal('this', getline(line('$') - 1))
1002 call assert_equal('AND this', getline('$'))
1003
1004 bwipe!
1005 exe bufwinnr('pipe-input') . "wincmd w"
1006 bwipe!
1007 finally
1008 call job_stop(job)
1009 endtry
1010endfunc
1011
1012func Test_pipe_io_one_buffer()
1013 if !has('job')
1014 return
1015 endif
1016 call ch_log('Test_pipe_io_one_buffer()')
1017
1018 " Create one buffer to read from and to write to.
1019 split pipe-io
1020 set buftype=nofile
1021
1022 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001023 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001024 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1025 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001026 call assert_equal("run", job_status(job))
1027 try
1028 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001029 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001030 call assert_equal('hello', getline(line('$') - 1))
1031
1032 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001033 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001034 call assert_equal('this', getline(line('$') - 2))
1035 call assert_equal('AND this', getline(line('$') - 1))
1036
1037 bwipe!
1038 finally
1039 call job_stop(job)
1040 endtry
1041endfunc
1042
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001043func Test_pipe_null()
1044 if !has('job')
1045 return
1046 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001047 call ch_log('Test_pipe_null()')
1048
1049 " We cannot check that no I/O works, we only check that the job starts
1050 " properly.
1051 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001052 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001053 call assert_equal("run", job_status(job))
1054 try
1055 call assert_equal('something', ch_read(job))
1056 finally
1057 call job_stop(job)
1058 endtry
1059
1060 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001061 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001062 call assert_equal("run", job_status(job))
1063 try
1064 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1065 finally
1066 call job_stop(job)
1067 endtry
1068
1069 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001070 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001071 call assert_equal("run", job_status(job))
1072 try
1073 call assert_equal('something', ch_read(job))
1074 finally
1075 call job_stop(job)
1076 endtry
1077
1078 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001079 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001080 call assert_equal("run", job_status(job))
1081 call job_stop(job)
1082
1083 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001084 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001085 call assert_equal("run", job_status(job))
1086 call assert_equal('channel fail', string(job_getchannel(job)))
1087 call assert_equal('fail', ch_status(job))
1088 call job_stop(job)
1089endfunc
1090
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001091func Test_pipe_to_buffer_raw()
1092 if !has('job')
1093 return
1094 endif
1095 call ch_log('Test_raw_pipe_to_buffer()')
1096 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1097 split testout
1098 let job = job_start([s:python, '-c',
1099 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1100 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001101 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001102 try
1103 for line in getline(2, '$')
1104 let line = substitute(line, '^\.*', '', '')
1105 call assert_equal('', line)
1106 endfor
1107 finally
1108 call job_stop(job)
1109 bwipe!
1110 endtry
1111endfunc
1112
Bram Moolenaarde279892016-03-11 22:19:44 +01001113func Test_reuse_channel()
1114 if !has('job')
1115 return
1116 endif
1117 call ch_log('Test_reuse_channel()')
1118
1119 let job = job_start(s:python . " test_channel_pipe.py")
1120 call assert_equal("run", job_status(job))
1121 let handle = job_getchannel(job)
1122 try
1123 call ch_sendraw(handle, "echo something\n")
1124 call assert_equal("something", ch_readraw(handle))
1125 finally
1126 call job_stop(job)
1127 endtry
1128
1129 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1130 call assert_equal("run", job_status(job))
1131 let handle = job_getchannel(job)
1132 try
1133 call ch_sendraw(handle, "echo again\n")
1134 call assert_equal("again", ch_readraw(handle))
1135 finally
1136 call job_stop(job)
1137 endtry
1138endfunc
1139
Bram Moolenaar75f72652016-03-20 22:16:56 +01001140func Test_out_cb()
1141 if !has('job')
1142 return
1143 endif
1144 call ch_log('Test_out_cb()')
1145
1146 let dict = {'thisis': 'dict: '}
1147 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001148 if type(a:msg) == v:t_string
1149 let g:Ch_outmsg = self.thisis . a:msg
1150 else
1151 let g:Ch_outobj = a:msg
1152 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001153 endfunc
1154 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001155 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001156 endfunc
1157 let job = job_start(s:python . " test_channel_pipe.py",
1158 \ {'out_cb': dict.outHandler,
1159 \ 'out_mode': 'json',
1160 \ 'err_cb': dict.errHandler,
1161 \ 'err_mode': 'json'})
1162 call assert_equal("run", job_status(job))
1163 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001164 let g:Ch_outmsg = ''
1165 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001166 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1167 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001168 call WaitFor('g:Ch_outmsg != ""')
1169 call assert_equal("dict: hello", g:Ch_outmsg)
1170 call WaitFor('g:Ch_errmsg != ""')
1171 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001172
1173 " Receive a json object split in pieces
1174 unlet! g:Ch_outobj
1175 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
1176 call WaitFor('exists("g:Ch_outobj")')
1177 call assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001178 finally
1179 call job_stop(job)
1180 endtry
1181endfunc
1182
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001183func Test_out_close_cb()
1184 if !has('job')
1185 return
1186 endif
1187 call ch_log('Test_out_close_cb()')
1188
1189 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001190 let g:Ch_msg1 = ''
1191 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001192 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001193 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001194 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001195 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001196 let s:counter += 1
1197 endfunc
1198 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001199 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001200 let s:counter += 1
1201 endfunc
1202 let job = job_start(s:python . " test_channel_pipe.py quit now",
1203 \ {'out_cb': 'OutHandler',
1204 \ 'close_cb': 'CloseHandler'})
1205 call assert_equal("run", job_status(job))
1206 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001207 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1208 call assert_equal('quit', g:Ch_msg1)
1209 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001210 finally
1211 call job_stop(job)
1212 delfunc OutHandler
1213 delfunc CloseHandler
1214 endtry
1215endfunc
1216
Bram Moolenaar437905c2016-04-26 19:01:05 +02001217func Test_read_in_close_cb()
1218 if !has('job')
1219 return
1220 endif
1221 call ch_log('Test_read_in_close_cb()')
1222
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001223 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001224 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001225 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001226 endfunc
1227 let job = job_start(s:python . " test_channel_pipe.py quit now",
1228 \ {'close_cb': 'CloseHandler'})
1229 call assert_equal("run", job_status(job))
1230 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001231 call WaitFor('g:Ch_received != ""')
1232 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001233 finally
1234 call job_stop(job)
1235 delfunc CloseHandler
1236 endtry
1237endfunc
1238
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001239func Test_out_cb_lambda()
1240 if !has('job')
1241 return
1242 endif
1243 call ch_log('Test_out_cb_lambda()')
1244
1245 let job = job_start(s:python . " test_channel_pipe.py",
1246 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1247 \ 'out_mode': 'json',
1248 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1249 \ 'err_mode': 'json'})
1250 call assert_equal("run", job_status(job))
1251 try
1252 let g:Ch_outmsg = ''
1253 let g:Ch_errmsg = ''
1254 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1255 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1256 call WaitFor('g:Ch_outmsg != ""')
1257 call assert_equal("lambda: hello", g:Ch_outmsg)
1258 call WaitFor('g:Ch_errmsg != ""')
1259 call assert_equal("lambda: there", g:Ch_errmsg)
1260 finally
1261 call job_stop(job)
1262 endtry
1263endfunc
1264
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001265func Test_close_and_exit_cb()
1266 if !has('job')
1267 return
1268 endif
1269 call ch_log('Test_close_and_exit_cb')
1270
1271 let dict = {'ret': {}}
1272 func dict.close_cb(ch) dict
1273 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1274 endfunc
1275 func dict.exit_cb(job, status) dict
1276 let self.ret['exit_cb'] = job_status(a:job)
1277 endfunc
1278
1279 let g:job = job_start('echo', {
1280 \ 'close_cb': dict.close_cb,
1281 \ 'exit_cb': dict.exit_cb,
1282 \ })
1283 call assert_equal('run', job_status(g:job))
1284 unlet g:job
1285 call WaitFor('len(dict.ret) >= 2')
1286 call assert_equal(2, len(dict.ret))
1287 call assert_match('^\%(dead\|run\)', dict.ret['close_cb'])
1288 call assert_equal('dead', dict.ret['exit_cb'])
1289endfunc
1290
Bram Moolenaard46ae142016-02-16 13:33:52 +01001291""""""""""
1292
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001293let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001294func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001295 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001296 unlet 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_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001301 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001302 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001303 call WaitFor('"what?" == g:Ch_unletResponse')
1304 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001305endfunc
1306
1307func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001308 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001309 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001310endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001311
Bram Moolenaard46ae142016-02-16 13:33:52 +01001312""""""""""
1313
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001314let g:Ch_unletResponse = ''
1315func Ch_CloseHandler(handle, msg)
1316 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001317 call ch_close(s:channelfd)
1318endfunc
1319
1320" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001321func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001322 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001323 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1324 call WaitFor('"what?" == g:Ch_unletResponse')
1325 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001326endfunc
1327
1328func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001329 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001330 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001331endfunc
1332
1333""""""""""
1334
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001335func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001336 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001337 silent! let ch = ch_open("noserver")
1338 echo ch
1339 let d = ch
1340endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001341
1342""""""""""
1343
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001344func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001345 " Wait up to a second for the port to open.
1346 let s:chopt.waittime = 1000
1347 let channel = ch_open('localhost:' . a:port, s:chopt)
1348 unlet s:chopt.waittime
1349 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001350 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001351 return
1352 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001353 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001354 call ch_close(channel)
1355endfunc
1356
1357func Test_open_delay()
1358 call ch_log('Test_open_delay()')
1359 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001360 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001361endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001362
1363"""""""""
1364
1365function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001366 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001367endfunc
1368
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001369function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001370 let handle = ch_open('localhost:' . a:port, s:chopt)
1371 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001372 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001373 return
1374 endif
1375
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001376 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001377 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001378 call WaitFor('len(g:Ch_call_ret) > 0')
1379 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001380endfunc
1381
1382func Test_call()
1383 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001384 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001385endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001386
1387"""""""""
1388
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001389let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001390function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001391 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001392endfunc
1393
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001394function Ch_test_exit_callback(port)
1395 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1396 let g:Ch_exit_job = g:currentJob
1397 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001398endfunc
1399
1400func Test_exit_callback()
1401 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001402 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001403 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001404
Bram Moolenaar9730f742016-02-28 19:50:51 +01001405 " wait up to a second for the job to exit
1406 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001407 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001408 break
1409 endif
1410 sleep 10m
1411 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001412 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001413 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001414
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001415 call assert_equal('done', g:Ch_job_exit_ret)
1416 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1417 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001418 endif
1419endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001420
Bram Moolenaar97792de2016-10-15 18:36:49 +02001421function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001422 if job_info(a:job).process == g:exit_cb_val.process
1423 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1424 endif
1425 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001426endfunction
1427
1428func Test_exit_callback_interval()
1429 if !has('job')
1430 return
1431 endif
1432
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001433 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001434 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001435 let g:exit_cb_val.process = job_info(job).process
1436 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1437 let elapsed = reltimefloat(g:exit_cb_val.end)
1438 call assert_true(elapsed > 0.5)
1439 call assert_true(elapsed < 1.0)
1440
1441 " case: unreferenced job, using timer
1442 if !has('timers')
1443 return
1444 endif
1445
1446 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1447 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1448 let g:exit_cb_val.process = job_info(g:job).process
1449 unlet g:job
1450 call Standby(1000)
1451 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1452 let elapsed = reltimefloat(g:exit_cb_val.end)
1453 else
1454 let elapsed = 1.0
1455 endif
1456 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001457 call assert_true(elapsed < 1.0)
1458endfunc
1459
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001460"""""""""
1461
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001462let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001463function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001464 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001465endfunc
1466
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001467function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001468 let handle = ch_open('localhost:' . a:port, s:chopt)
1469 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001470 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001471 return
1472 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001473 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001474
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001475 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001476 call WaitFor('"closed" == g:Ch_close_ret')
1477 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001478endfunc
1479
1480func Test_close_callback()
1481 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001482 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001483endfunc
1484
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001485function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001486 let handle = ch_open('localhost:' . a:port, s:chopt)
1487 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001488 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001489 return
1490 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001491 let g:Ch_d = {}
1492 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001493 let self.close_ret = 'closed'
1494 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001495 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001496
1497 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001498 call WaitFor('"closed" == g:Ch_d.close_ret')
1499 call assert_equal('closed', g:Ch_d.close_ret)
1500 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001501endfunc
1502
1503func Test_close_partial()
1504 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001505 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001506endfunc
1507
Bram Moolenaar80385682016-03-27 19:13:35 +02001508func Test_job_start_invalid()
1509 call assert_fails('call job_start($x)', 'E474:')
1510 call assert_fails('call job_start("")', 'E474:')
1511endfunc
1512
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001513func Test_job_stop_immediately()
1514 if !has('job')
1515 return
1516 endif
1517
1518 let job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
1519 try
1520 call job_stop(job)
1521 call WaitFor('"dead" == job_status(job)')
1522 call assert_equal('dead', job_status(job))
1523 finally
1524 call job_stop(job, 'kill')
1525 endtry
1526endfunc
1527
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001528" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001529func Test_partial_in_channel_cycle()
1530 let d = {}
1531 let d.a = function('string', [d])
1532 try
1533 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1534 catch
1535 call assert_exception('E901:')
1536 endtry
1537 unlet d
1538endfunc
1539
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001540func Test_using_freed_memory()
1541 let g:a = job_start(['ls'])
1542 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001543 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001544endfunc
1545
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001546func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001547 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001548 return
1549 endif
1550 sp test_channel.vim
1551 let g:linecount = line('$')
1552 close
1553 split testout
1554 1,$delete
1555 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001556 call WaitFor('line("$") > g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001557 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001558 bwipe!
1559endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001560
Bram Moolenaar82117982016-08-27 19:21:48 +02001561func Test_raw_passes_nul()
1562 if !executable('cat') || !has('job')
1563 return
1564 endif
1565
1566 " Test lines from the job containing NUL are stored correctly in a buffer.
1567 new
1568 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1569 w! Xtestread
1570 bwipe!
1571 split testout
1572 1,$delete
1573 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1574 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001575 call assert_equal("asdf\nasdf", getline(1))
1576 call assert_equal("xxx\n", getline(2))
1577 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001578
1579 call delete('Xtestread')
1580 bwipe!
1581
1582 " Test lines from a buffer with NUL bytes are written correctly to the job.
1583 new mybuffer
1584 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1585 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1586 call WaitFor('"dead" == job_status(g:Ch_job)')
1587 bwipe!
1588 split Xtestwrite
1589 call assert_equal("asdf\nasdf", getline(1))
1590 call assert_equal("xxx\n", getline(2))
1591 call assert_equal("\nyyy", getline(3))
1592
1593 call delete('Xtestwrite')
1594 bwipe!
1595endfunc
1596
Bram Moolenaarec68a992016-10-03 21:37:41 +02001597func MyLineCountCb(ch, msg)
1598 let g:linecount += 1
1599endfunc
1600
1601func Test_read_nonl_line()
1602 if !has('job')
1603 return
1604 endif
1605
1606 let g:linecount = 0
1607 if has('win32')
1608 " workaround: 'shellescape' does improper escaping double quotes
1609 let arg = 'import sys;sys.stdout.write(\"1\n2\n3\")'
1610 else
1611 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1612 endif
1613 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1614 call WaitFor('3 <= g:linecount')
1615 call assert_equal(3, g:linecount)
1616endfunc
1617
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001618func Test_read_from_terminated_job()
1619 if !has('job')
1620 return
1621 endif
1622
1623 let g:linecount = 0
1624 if has('win32')
1625 " workaround: 'shellescape' does improper escaping double quotes
1626 let arg = 'import os,sys;os.close(1);sys.stderr.write(\"test\n\")'
1627 else
1628 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
1629 endif
1630 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1631 call WaitFor('1 <= g:linecount')
1632 call assert_equal(1, g:linecount)
1633endfunc
1634
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001635function Ch_test_close_lambda(port)
1636 let handle = ch_open('localhost:' . a:port, s:chopt)
1637 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001638 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001639 return
1640 endif
1641 let g:Ch_close_ret = ''
1642 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1643
1644 call assert_equal('', ch_evalexpr(handle, 'close me'))
1645 call WaitFor('"closed" == g:Ch_close_ret')
1646 call assert_equal('closed', g:Ch_close_ret)
1647endfunc
1648
1649func Test_close_lambda()
1650 call ch_log('Test_close_lambda()')
1651 call s:run_server('Ch_test_close_lambda')
1652endfunc