blob: 805fa8dcaceee94264cb6895078780f5157a6b46 [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 Moolenaar819524702018-02-27 19:10:00 +010026" Return a list of open files.
27" Can be used to make sure no resources leaked.
28" Returns an empty list on systems where this is not supported.
29func s:get_resources()
30 let pid = getpid()
31
32 if has('mac')
33 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
34 elseif isdirectory('/proc/' . pid . '/fd/')
35 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
36 else
37 return []
38 endif
39endfunc
40
Bram Moolenaar321efdd2016-07-15 17:09:11 +020041let g:Ch_responseMsg = ''
42func Ch_requestHandler(handle, msg)
43 let g:Ch_responseHandle = a:handle
44 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010045endfunc
46
Bram Moolenaar321efdd2016-07-15 17:09:11 +020047func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010048 " Avoid dropping messages, since we don't use a callback here.
49 let s:chopt.drop = 'never'
Bram Moolenaard6a8d482016-02-10 20:32:20 +010050 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010051 unlet s:chopt.drop
Bram Moolenaar77073442016-02-13 23:23:53 +010052 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010053 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010054 return
55 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010056 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010057 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010058 call assert_equal('no process', string(ch_getjob(handle)))
59 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010060 let dict = ch_info(handle)
61 call assert_true(dict.id != 0)
62 call assert_equal('open', dict.status)
63 call assert_equal(a:port, string(dict.port))
64 call assert_equal('open', dict.sock_status)
65 call assert_equal('socket', dict.sock_io)
66
Bram Moolenaard7ece102016-02-02 23:23:02 +010067 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010068 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010069
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010070 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010071 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
72 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
73 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
74
75 " split command should work
76 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020077 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010078 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010079
Bram Moolenaarf1f07922016-08-26 17:58:53 +020080 " string with ][ should work
81 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
82
Bram Moolenaar4b785f62016-11-29 21:54:44 +010083 " nothing to read now
84 call assert_equal(0, ch_canread(handle))
85
Bram Moolenaarf1f07922016-08-26 17:58:53 +020086 " sending three messages quickly then reading should work
87 for i in range(3)
88 call ch_sendexpr(handle, 'echo hello ' . i)
89 endfor
90 call assert_equal('hello 0', ch_read(handle)[1])
91 call assert_equal('hello 1', ch_read(handle)[1])
92 call assert_equal('hello 2', ch_read(handle)[1])
93
Bram Moolenaard7ece102016-02-02 23:23:02 +010094 " Request that triggers sending two ex commands. These will usually be
95 " handled before getting the response, but it's not guaranteed, thus wait a
96 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010097 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020098 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010099 call assert_equal('added1', getline(line('$') - 1))
100 call assert_equal('added2', getline('$'))
101
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100102 " Request command "foo bar", which fails silently.
103 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200104 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200105 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100106
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100107 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200108 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100109 call assert_equal('added more', getline('$'))
110
Bram Moolenaara07fec92016-02-05 21:04:08 +0100111 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200112 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
113 call WaitFor('exists("g:Ch_responseHandle")')
114 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100115 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100116 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200117 call assert_equal(handle, g:Ch_responseHandle)
118 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100119 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200120 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100121
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200122 let g:Ch_responseMsg = ''
123 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
124 call WaitFor('exists("g:Ch_responseHandle")')
125 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100126 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100127 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200128 call assert_equal(handle, g:Ch_responseHandle)
129 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100130 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200131 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100132
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200133 " Using lambda.
134 let g:Ch_responseMsg = ''
135 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
136 call WaitFor('exists("g:Ch_responseHandle")')
137 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100138 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200139 else
140 call assert_equal(handle, g:Ch_responseHandle)
141 unlet g:Ch_responseHandle
142 endif
143 call assert_equal('got it', g:Ch_responseMsg)
144
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100145 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200146 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100147
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100148 " check setting options (without testing the effect)
149 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100150 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100151 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100152 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100153 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100154 call ch_setoptions(handle, {'drop': 'never'})
155 call ch_setoptions(handle, {'drop': 'auto'})
156 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100157
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100158 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100159 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100160 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100162
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100163 " Send an eval request with special characters.
164 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
165 sleep 10m
166 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
167
168 " Send an eval request to get a line with special characters.
169 call setline(3, "a\nb\<CR>c\x01d\x7fe")
170 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
171 sleep 10m
172 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
173
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100174 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100176 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100177 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100178
Bram Moolenaar55fab432016-02-07 16:53:13 +0100179 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100181 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100182 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100183
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100184 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100186 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100187 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100188
Bram Moolenaarf4160862016-02-05 23:09:12 +0100189 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200191 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100192 call assert_equal('one', getline(line('$') - 2))
193 call assert_equal('two', getline(line('$') - 1))
194 call assert_equal('three', getline('$'))
195
196 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100197 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
198 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100199
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100200 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100201
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100202 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100203 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
204 let start = reltime()
205 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
206 let elapsed = reltime(start)
207 call assert_true(reltimefloat(elapsed) > 0.3)
208 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100209
210 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100211 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100212 let resp = ch_read(handle)
213 call assert_equal(type([]), type(resp))
214 call assert_equal(type(11), type(resp[0]))
215 call assert_equal('waited', resp[1])
216
Bram Moolenaard7ece102016-02-02 23:23:02 +0100217 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100219endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100220
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100222 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200223 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100224endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100225
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100226" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200227func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100228 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200229 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100230 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100231 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100232 return
233 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100234
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100235 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100236
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100237 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100238 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100239 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100240 return
241 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100242 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
243 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100244
245 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100246 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100247
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248 call ch_close(newhandle)
249endfunc
250
251func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100252 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200253 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100254endfunc
255
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100256" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200257func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100258 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100259 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100260 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100261 return
262 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100263
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100264 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100265
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100266 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100267endfunc
268
269func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100270 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200271 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100272endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100273
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100274"""""""""
275
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200276func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100277 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200278 unlet g:Ch_reply
279 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280endfunc
281
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200282func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100283 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100284 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100285 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100286 return
287 endif
288
289 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100290 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200291 call WaitFor('"we called you" == g:Ch_reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292
293 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100294 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200295 call WaitFor('"we did call you" == g:Ch_reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296endfunc
297
298func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100299 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200300 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200301 let s:chopt.callback = 'Ch_handler'
302 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200303 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200304 let s:chopt.callback = function('Ch_handler')
305 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100306 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100307endfunc
308
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100309"""""""""
310
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200311let g:Ch_reply = ''
312func Ch_zeroHandler(chan, msg)
313 unlet g:Ch_reply
314 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100315endfunc
316
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200317let g:Ch_zero_reply = ''
318func Ch_oneHandler(chan, msg)
319 unlet g:Ch_zero_reply
320 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100321endfunc
322
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200323func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100324 let handle = ch_open('localhost:' . a:port, s:chopt)
325 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100326 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100327 return
328 endif
329
330 " Check that eval works.
331 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
332
333 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200334 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200337 call WaitFor('"zero index" == g:Ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100338 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100339 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200340 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100341 endif
342
343 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200344 let g:Ch_reply = ''
345 let g:Ch_zero_reply = ''
346 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
347 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100348 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200349 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100352 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100353endfunc
354
355func Test_zero_reply()
356 call ch_log('Test_zero_reply()')
357 " Run with channel handler
358 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200359 let s:chopt.callback = 'Ch_zeroHandler'
360 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100361 unlet s:chopt.callback
362
363 " Run without channel handler
364 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200365 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100366endfunc
367
368"""""""""
369
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200370let g:Ch_reply1 = ""
371func Ch_handleRaw1(chan, msg)
372 unlet g:Ch_reply1
373 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100374endfunc
375
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376let g:Ch_reply2 = ""
377func Ch_handleRaw2(chan, msg)
378 unlet g:Ch_reply2
379 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100380endfunc
381
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200382let g:Ch_reply3 = ""
383func Ch_handleRaw3(chan, msg)
384 unlet g:Ch_reply3
385 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100386endfunc
387
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200388func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100389 let handle = ch_open('localhost:' . a:port, s:chopt)
390 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100391 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100392 return
393 endif
394 call ch_setoptions(handle, {'mode': 'raw'})
395
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100396 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200397 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200398 call WaitFor('g:Ch_reply1 != ""')
399 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200400 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
401 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200402 call WaitFor('g:Ch_reply2 != ""')
403 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100404 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200405 call WaitFor('g:Ch_reply3 != ""')
406 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100407endfunc
408
409func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100410 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200411 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100412endfunc
413
414"""""""""
415
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100416" Test that trying to connect to a non-existing port fails quickly.
417func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100418 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100419 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100420 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100421 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100422 " Oops, port does exists.
423 call ch_close(handle)
424 else
425 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100426 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100427 endif
428
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100429 " We intend to use a socket that doesn't exist and wait for half a second
430 " before giving up. If the socket does exist it can fail in various ways.
431 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100432 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100433 try
434 let handle = ch_open('localhost:9867', {'waittime': 500})
435 if ch_status(handle) != "fail"
436 " Oops, port does exists.
437 call ch_close(handle)
438 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100439 " Failed connection should wait about 500 msec. Can be longer if the
440 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100441 let elapsed = reltime(start)
442 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100443 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100444 endif
445 catch
446 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100447 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100448 endif
449 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100450endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100451
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100452"""""""""
453
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100454func Test_raw_pipe()
455 if !has('job')
456 return
457 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100458 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100459 " Add a dummy close callback to avoid that messages are dropped when calling
460 " ch_canread().
461 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100462 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200463 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100464 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200465
466 call assert_equal("open", ch_status(job))
467 call assert_equal("open", ch_status(job), {"part": "out"})
468 call assert_equal("open", ch_status(job), {"part": "err"})
469 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
470 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
471
472 let dict = ch_info(job)
473 call assert_true(dict.id != 0)
474 call assert_equal('open', dict.status)
475 call assert_equal('open', dict.out_status)
476 call assert_equal('RAW', dict.out_mode)
477 call assert_equal('pipe', dict.out_io)
478 call assert_equal('open', dict.err_status)
479 call assert_equal('RAW', dict.err_mode)
480 call assert_equal('pipe', dict.err_io)
481
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100482 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100483 " For a change use the job where a channel is expected.
484 call ch_sendraw(job, "echo something\n")
485 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100486 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
487
Bram Moolenaar151f6562016-03-07 21:19:38 +0100488 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100489 let g:handle = job_getchannel(job)
490 call WaitFor('ch_canread(g:handle)')
491 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100492 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100493 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
494
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200495 let g:Ch_reply = ""
496 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
497 call WaitFor('"" != g:Ch_reply')
498 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
499
Bram Moolenaar151f6562016-03-07 21:19:38 +0100500 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100501 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
502 finally
503 call job_stop(job)
504 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100505
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200506 let g:Ch_job = job
507 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100508 let info = job_info(job)
509 call assert_equal("dead", info.status)
510 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200511 call assert_equal(2, len(info.cmd))
512 call assert_equal("test_channel_pipe.py", info.cmd[1])
513
514 let found = 0
515 for j in job_info()
516 if j == job
517 let found += 1
518 endif
519 endfor
520 call assert_equal(1, found)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100521endfunc
522
523func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100524 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525 return
526 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100527 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100528 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100529 call assert_equal("run", job_status(job))
530 try
531 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100532 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100533 call assert_equal("something", ch_readraw(handle))
534
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100535 call ch_sendraw(handle, "echoerr wrong\n")
536 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
537
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100538 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100539 call assert_equal("this", ch_readraw(handle))
540 call assert_equal("AND this", ch_readraw(handle))
541
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200542 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar620ca2d2017-12-09 19:13:13 +0100543 call assert_equal("this linethis linethis line", ch_read(handle))
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200544
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100545 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100546 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100547 finally
548 call job_stop(job)
549 endtry
550endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100551
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100552func Test_nl_err_to_out_pipe()
553 if !has('job')
554 return
555 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100556 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100557 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100558 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100559 call assert_equal("run", job_status(job))
560 try
561 let handle = job_getchannel(job)
562 call ch_sendraw(handle, "echo something\n")
563 call assert_equal("something", ch_readraw(handle))
564
565 call ch_sendraw(handle, "echoerr wrong\n")
566 call assert_equal("wrong", ch_readraw(handle))
567 finally
568 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100569 call ch_logfile('')
570 let loglines = readfile('Xlog')
571 call assert_true(len(loglines) > 10)
572 let found_test = 0
573 let found_send = 0
574 let found_recv = 0
575 let found_stop = 0
576 for l in loglines
577 if l =~ 'Test_nl_err_to_out_pipe'
578 let found_test = 1
579 endif
580 if l =~ 'SEND on.*echo something'
581 let found_send = 1
582 endif
583 if l =~ 'RECV on.*something'
584 let found_recv = 1
585 endif
586 if l =~ 'Stopping job with'
587 let found_stop = 1
588 endif
589 endfor
590 call assert_equal(1, found_test)
591 call assert_equal(1, found_send)
592 call assert_equal(1, found_recv)
593 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200594 " On MS-Windows need to sleep for a moment to be able to delete the file.
595 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100596 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100597 endtry
598endfunc
599
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200600func Stop_g_job()
601 call job_stop(g:job)
602 if has('win32')
603 " On MS-Windows the server must close the file handle before we are able
604 " to delete the file.
605 call WaitFor('job_status(g:job) == "dead"')
606 sleep 10m
607 endif
608endfunc
609
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100610func Test_nl_read_file()
611 if !has('job')
612 return
613 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100614 call ch_log('Test_nl_read_file()')
615 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200616 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100617 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200618 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100619 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200620 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100621 call assert_equal("something", ch_readraw(handle))
622 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
623 call assert_equal("this", ch_readraw(handle))
624 call assert_equal("AND this", ch_readraw(handle))
625 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200626 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100627 call delete('Xinput')
628 endtry
629endfunc
630
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631func Test_nl_write_out_file()
632 if !has('job')
633 return
634 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 call ch_log('Test_nl_write_out_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 \ {'out_io': 'file', 'out_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, "echo line one\n")
642 call ch_sendraw(handle, "echo line two\n")
643 call ch_sendraw(handle, "double 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 Moolenaar819524702018-02-27 19:10:00 +0100648 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100649 call delete('Xoutput')
650 endtry
651endfunc
652
653func Test_nl_write_err_file()
654 if !has('job')
655 return
656 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100657 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200658 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100659 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200660 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100661 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200662 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100663 call ch_sendraw(handle, "echoerr line one\n")
664 call ch_sendraw(handle, "echoerr line two\n")
665 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200666 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100667 call assert_equal(['line one', 'line two', 'this', 'AND this'], 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
674func Test_nl_write_both_file()
675 if !has('job')
676 return
677 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100678 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200679 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100680 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200681 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100682 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200683 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100684 call ch_sendraw(handle, "echoerr line one\n")
685 call ch_sendraw(handle, "echo line two\n")
686 call ch_sendraw(handle, "double this\n")
687 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200688 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100689 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
690 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200691 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100692 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100693 call delete('Xoutput')
694 endtry
695endfunc
696
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200697func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200698 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200699endfunc
700
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200701func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100702 if !has('job')
703 return
704 endif
705 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200706 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200707 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200708 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100709 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100710 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200711 if a:do_msg
712 let expected[0] = 'Reading from channel output...'
713 else
714 let options['out_msg'] = 0
715 call remove(expected, 0)
716 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100717 else
718 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100719 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100720 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200721 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100722 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200723 if a:nomod
724 let options['out_modifiable'] = 0
725 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100727 call assert_equal("run", job_status(job))
728 try
729 let handle = job_getchannel(job)
730 call ch_sendraw(handle, "echo line one\n")
731 call ch_sendraw(handle, "echo line two\n")
732 call ch_sendraw(handle, "double this\n")
733 call ch_sendraw(handle, "quit\n")
734 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100735 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200736 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200737 if a:nomod
738 call assert_equal(0, &modifiable)
739 else
740 call assert_equal(1, &modifiable)
741 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200742 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100743 bwipe!
744 finally
745 call job_stop(job)
746 endtry
747endfunc
748
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100749func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200750 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100751endfunc
752
753func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200754 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100755endfunc
756
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200757func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200758 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200759endfunc
760
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200761func Test_pipe_to_buffer_name_nomsg()
762 call Run_test_pipe_to_buffer(1, 0, 1)
763endfunc
764
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200765func Test_close_output_buffer()
766 if !has('job')
767 return
768 endif
769 enew!
770 let test_lines = ['one', 'two']
771 call setline(1, test_lines)
772 call ch_log('Test_close_output_buffer()')
773 let options = {'out_io': 'buffer'}
774 let options['out_name'] = 'buffer-output'
775 let options['out_msg'] = 0
776 split buffer-output
777 let job = job_start(s:python . " test_channel_write.py", options)
778 call assert_equal("run", job_status(job))
779 try
780 call WaitFor('line("$") == 3')
781 call assert_equal(3, line('$'))
782 quit!
783 sleep 100m
784 " Make sure the write didn't happen to the wrong buffer.
785 call assert_equal(test_lines, getline(1, line('$')))
786 call assert_equal(-1, bufwinnr('buffer-output'))
787 sbuf buffer-output
788 call assert_notequal(-1, bufwinnr('buffer-output'))
789 sleep 100m
790 close " no more writes
791 bwipe!
792 finally
793 call job_stop(job)
794 endtry
795endfunc
796
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200797func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100798 if !has('job')
799 return
800 endif
801 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100802 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200803 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100804 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100805 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200806 if a:do_msg
807 let expected[0] = 'Reading from channel error...'
808 else
809 let options['err_msg'] = 0
810 call remove(expected, 0)
811 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100812 else
813 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100814 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100815 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200816 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100817 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200818 if a:nomod
819 let options['err_modifiable'] = 0
820 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100821 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100822 call assert_equal("run", job_status(job))
823 try
824 let handle = job_getchannel(job)
825 call ch_sendraw(handle, "echoerr line one\n")
826 call ch_sendraw(handle, "echoerr line two\n")
827 call ch_sendraw(handle, "doubleerr this\n")
828 call ch_sendraw(handle, "quit\n")
829 sp pipe-err
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100830 call WaitFor('line("$") == ' . len(expected))
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200831 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200832 if a:nomod
833 call assert_equal(0, &modifiable)
834 else
835 call assert_equal(1, &modifiable)
836 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100837 bwipe!
838 finally
839 call job_stop(job)
840 endtry
841endfunc
842
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100843func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200844 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100845endfunc
846
847func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200848 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200849endfunc
850
851func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200852 call Run_test_pipe_err_to_buffer(1, 1, 1)
853endfunc
854
855func Test_pipe_err_to_buffer_name_nomsg()
856 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100857endfunc
858
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100859func Test_pipe_both_to_buffer()
860 if !has('job')
861 return
862 endif
863 call ch_log('Test_pipe_both_to_buffer()')
864 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100865 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100866 call assert_equal("run", job_status(job))
867 try
868 let handle = job_getchannel(job)
869 call ch_sendraw(handle, "echo line one\n")
870 call ch_sendraw(handle, "echoerr line two\n")
871 call ch_sendraw(handle, "double this\n")
872 call ch_sendraw(handle, "doubleerr that\n")
873 call ch_sendraw(handle, "quit\n")
874 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200875 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100876 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
877 bwipe!
878 finally
879 call job_stop(job)
880 endtry
881endfunc
882
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100883func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100884 if !has('job')
885 return
886 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100887 call ch_log('Test_pipe_from_buffer()')
888
889 sp pipe-input
890 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200891 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100892 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100893 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100894 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100895 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100896 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100897
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100898 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100899 call assert_equal("run", job_status(job))
900 try
901 let handle = job_getchannel(job)
902 call assert_equal('one', ch_read(handle))
903 call assert_equal('two', ch_read(handle))
904 call assert_equal('three', ch_read(handle))
905 bwipe!
906 finally
907 call job_stop(job)
908 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100909endfunc
910
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100911func Test_pipe_from_buffer_name()
912 call Run_test_pipe_from_buffer(1)
913endfunc
914
915func Test_pipe_from_buffer_nr()
916 call Run_test_pipe_from_buffer(0)
917endfunc
918
Bram Moolenaar0874a832016-09-01 15:11:51 +0200919func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200920 if !executable('sort') || !has('job')
921 return
922 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200923 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
924 if a:use_buffer
925 split sortin
926 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
927 let options.in_io = 'buffer'
928 let options.in_name = 'sortin'
929 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200930 if !a:all
931 let options.in_top = 2
932 let options.in_bot = 4
933 endif
934 let g:job = job_start('sort', options)
935 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200936
937 if !a:use_buffer
938 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
939 call ch_close_in(g:job)
940 endif
941
Bram Moolenaard8b55492016-09-01 14:35:22 +0200942 call WaitFor('job_status(g:job) == "dead"')
943 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200944
Bram Moolenaard8b55492016-09-01 14:35:22 +0200945 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200946 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200947 call assert_equal('Reading from channel output...', getline(1))
948 if a:all
949 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
950 else
951 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
952 endif
953
954 call job_stop(g:job)
955 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200956 if a:use_buffer
957 bwipe! sortin
958 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200959 bwipe! sortout
960endfunc
961
962func Test_pipe_through_sort_all()
963 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200964 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200965endfunc
966
967func Test_pipe_through_sort_some()
968 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200969 call Run_pipe_through_sort(0, 1)
970endfunc
971
972func Test_pipe_through_sort_feed()
973 call ch_log('Test_pipe_through_sort_feed()')
974 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200975endfunc
976
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100977func Test_pipe_to_nameless_buffer()
978 if !has('job')
979 return
980 endif
981 call ch_log('Test_pipe_to_nameless_buffer()')
982 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100983 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100984 call assert_equal("run", job_status(job))
985 try
986 let handle = job_getchannel(job)
987 call ch_sendraw(handle, "echo line one\n")
988 call ch_sendraw(handle, "echo line two\n")
989 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200990 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100991 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
992 bwipe!
993 finally
994 call job_stop(job)
995 endtry
996endfunc
997
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100998func Test_pipe_to_buffer_json()
999 if !has('job')
1000 return
1001 endif
1002 call ch_log('Test_pipe_to_buffer_json()')
1003 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001004 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001005 call assert_equal("run", job_status(job))
1006 try
1007 let handle = job_getchannel(job)
1008 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
1009 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1010 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001011 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001012 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1013 bwipe!
1014 finally
1015 call job_stop(job)
1016 endtry
1017endfunc
1018
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001019" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001020func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001021 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001022 if getline(line('$') - a:offset) == a:line
1023 break
1024 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001025 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001026 endfor
1027endfunc
1028
1029func Test_pipe_io_two_buffers()
1030 if !has('job')
1031 return
1032 endif
1033 call ch_log('Test_pipe_io_two_buffers()')
1034
1035 " Create two buffers, one to read from and one to write to.
1036 split pipe-output
1037 set buftype=nofile
1038 split pipe-input
1039 set buftype=nofile
1040
1041 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001042 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001043 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1044 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001045 call assert_equal("run", job_status(job))
1046 try
1047 exe "normal Gaecho hello\<CR>"
1048 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001049 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001050 call assert_equal('hello', getline('$'))
1051
1052 exe bufwinnr('pipe-input') . "wincmd w"
1053 exe "normal Gadouble this\<CR>"
1054 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001055 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001056 call assert_equal('this', getline(line('$') - 1))
1057 call assert_equal('AND this', getline('$'))
1058
1059 bwipe!
1060 exe bufwinnr('pipe-input') . "wincmd w"
1061 bwipe!
1062 finally
1063 call job_stop(job)
1064 endtry
1065endfunc
1066
1067func Test_pipe_io_one_buffer()
1068 if !has('job')
1069 return
1070 endif
1071 call ch_log('Test_pipe_io_one_buffer()')
1072
1073 " Create one buffer to read from and to write to.
1074 split pipe-io
1075 set buftype=nofile
1076
1077 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001078 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001079 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1080 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001081 call assert_equal("run", job_status(job))
1082 try
1083 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001084 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001085 call assert_equal('hello', getline(line('$') - 1))
1086
1087 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001088 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001089 call assert_equal('this', getline(line('$') - 2))
1090 call assert_equal('AND this', getline(line('$') - 1))
1091
1092 bwipe!
1093 finally
1094 call job_stop(job)
1095 endtry
1096endfunc
1097
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001098func Test_pipe_null()
1099 if !has('job')
1100 return
1101 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001102 call ch_log('Test_pipe_null()')
1103
1104 " We cannot check that no I/O works, we only check that the job starts
1105 " properly.
1106 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001107 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001108 call assert_equal("run", job_status(job))
1109 try
1110 call assert_equal('something', ch_read(job))
1111 finally
1112 call job_stop(job)
1113 endtry
1114
1115 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001116 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001117 call assert_equal("run", job_status(job))
1118 try
1119 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1120 finally
1121 call job_stop(job)
1122 endtry
1123
1124 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001125 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001126 call assert_equal("run", job_status(job))
1127 try
1128 call assert_equal('something', ch_read(job))
1129 finally
1130 call job_stop(job)
1131 endtry
1132
1133 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001134 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001135 call assert_equal("run", job_status(job))
1136 call job_stop(job)
1137
1138 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001139 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001140 call assert_equal("run", job_status(job))
1141 call assert_equal('channel fail', string(job_getchannel(job)))
1142 call assert_equal('fail', ch_status(job))
1143 call job_stop(job)
1144endfunc
1145
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001146func Test_pipe_to_buffer_raw()
1147 if !has('job')
1148 return
1149 endif
1150 call ch_log('Test_raw_pipe_to_buffer()')
1151 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1152 split testout
1153 let job = job_start([s:python, '-c',
1154 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1155 call assert_equal("run", job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001156 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001157 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001158 let totlen = 0
1159 for line in getline(1, '$')
1160 call assert_equal('', substitute(line, '^\.*', '', ''))
1161 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001162 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001163 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001164 finally
1165 call job_stop(job)
1166 bwipe!
1167 endtry
1168endfunc
1169
Bram Moolenaarde279892016-03-11 22:19:44 +01001170func Test_reuse_channel()
1171 if !has('job')
1172 return
1173 endif
1174 call ch_log('Test_reuse_channel()')
1175
1176 let job = job_start(s:python . " test_channel_pipe.py")
1177 call assert_equal("run", job_status(job))
1178 let handle = job_getchannel(job)
1179 try
1180 call ch_sendraw(handle, "echo something\n")
1181 call assert_equal("something", ch_readraw(handle))
1182 finally
1183 call job_stop(job)
1184 endtry
1185
1186 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1187 call assert_equal("run", job_status(job))
1188 let handle = job_getchannel(job)
1189 try
1190 call ch_sendraw(handle, "echo again\n")
1191 call assert_equal("again", ch_readraw(handle))
1192 finally
1193 call job_stop(job)
1194 endtry
1195endfunc
1196
Bram Moolenaar75f72652016-03-20 22:16:56 +01001197func Test_out_cb()
1198 if !has('job')
1199 return
1200 endif
1201 call ch_log('Test_out_cb()')
1202
1203 let dict = {'thisis': 'dict: '}
1204 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001205 if type(a:msg) == v:t_string
1206 let g:Ch_outmsg = self.thisis . a:msg
1207 else
1208 let g:Ch_outobj = a:msg
1209 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001210 endfunc
1211 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001212 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001213 endfunc
1214 let job = job_start(s:python . " test_channel_pipe.py",
1215 \ {'out_cb': dict.outHandler,
1216 \ 'out_mode': 'json',
1217 \ 'err_cb': dict.errHandler,
1218 \ 'err_mode': 'json'})
1219 call assert_equal("run", job_status(job))
1220 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001221 let g:Ch_outmsg = ''
1222 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001223 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1224 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001225 call WaitFor('g:Ch_outmsg != ""')
1226 call assert_equal("dict: hello", g:Ch_outmsg)
1227 call WaitFor('g:Ch_errmsg != ""')
1228 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001229
1230 " Receive a json object split in pieces
1231 unlet! g:Ch_outobj
1232 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
1233 call WaitFor('exists("g:Ch_outobj")')
1234 call assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001235 finally
1236 call job_stop(job)
1237 endtry
1238endfunc
1239
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001240func Test_out_close_cb()
1241 if !has('job')
1242 return
1243 endif
1244 call ch_log('Test_out_close_cb()')
1245
1246 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001247 let g:Ch_msg1 = ''
1248 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001249 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001250 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001251 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001252 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001253 let s:counter += 1
1254 endfunc
1255 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001256 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001257 let s:counter += 1
1258 endfunc
1259 let job = job_start(s:python . " test_channel_pipe.py quit now",
1260 \ {'out_cb': 'OutHandler',
1261 \ 'close_cb': 'CloseHandler'})
1262 call assert_equal("run", job_status(job))
1263 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001264 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1265 call assert_equal('quit', g:Ch_msg1)
1266 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001267 finally
1268 call job_stop(job)
1269 delfunc OutHandler
1270 delfunc CloseHandler
1271 endtry
1272endfunc
1273
Bram Moolenaar437905c2016-04-26 19:01:05 +02001274func Test_read_in_close_cb()
1275 if !has('job')
1276 return
1277 endif
1278 call ch_log('Test_read_in_close_cb()')
1279
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001280 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001281 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001282 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001283 endfunc
1284 let job = job_start(s:python . " test_channel_pipe.py quit now",
1285 \ {'close_cb': 'CloseHandler'})
1286 call assert_equal("run", job_status(job))
1287 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001288 call WaitFor('g:Ch_received != ""')
1289 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001290 finally
1291 call job_stop(job)
1292 delfunc CloseHandler
1293 endtry
1294endfunc
1295
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001296" Use channel in NL mode but received text does not end in NL.
1297func Test_read_in_close_cb_incomplete()
1298 if !has('job')
1299 return
1300 endif
1301 call ch_log('Test_read_in_close_cb_incomplete()')
1302
1303 let g:Ch_received = ''
1304 func! CloseHandler(chan)
1305 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1306 let g:Ch_received .= ch_read(a:chan)
1307 endwhile
1308 endfunc
1309 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1310 \ {'close_cb': 'CloseHandler'})
1311 call assert_equal("run", job_status(job))
1312 try
1313 call WaitFor('g:Ch_received != ""')
1314 call assert_equal('incomplete', g:Ch_received)
1315 finally
1316 call job_stop(job)
1317 delfunc CloseHandler
1318 endtry
1319endfunc
1320
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001321func Test_out_cb_lambda()
1322 if !has('job')
1323 return
1324 endif
1325 call ch_log('Test_out_cb_lambda()')
1326
1327 let job = job_start(s:python . " test_channel_pipe.py",
1328 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1329 \ 'out_mode': 'json',
1330 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1331 \ 'err_mode': 'json'})
1332 call assert_equal("run", job_status(job))
1333 try
1334 let g:Ch_outmsg = ''
1335 let g:Ch_errmsg = ''
1336 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1337 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1338 call WaitFor('g:Ch_outmsg != ""')
1339 call assert_equal("lambda: hello", g:Ch_outmsg)
1340 call WaitFor('g:Ch_errmsg != ""')
1341 call assert_equal("lambda: there", g:Ch_errmsg)
1342 finally
1343 call job_stop(job)
1344 endtry
1345endfunc
1346
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001347func Test_close_and_exit_cb()
1348 if !has('job')
1349 return
1350 endif
1351 call ch_log('Test_close_and_exit_cb')
1352
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001353 let g:retdict = {'ret': {}}
1354 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001355 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1356 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001357 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001358 let self.ret['exit_cb'] = job_status(a:job)
1359 endfunc
1360
1361 let g:job = job_start('echo', {
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001362 \ 'close_cb': g:retdict.close_cb,
1363 \ 'exit_cb': g:retdict.exit_cb,
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001364 \ })
1365 call assert_equal('run', job_status(g:job))
1366 unlet g:job
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001367 call WaitFor('len(g:retdict.ret) >= 2')
1368 call assert_equal(2, len(g:retdict.ret))
1369 call assert_match('^\%(dead\|run\)', g:retdict.ret['close_cb'])
1370 call assert_equal('dead', g:retdict.ret['exit_cb'])
1371 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001372endfunc
1373
Bram Moolenaard46ae142016-02-16 13:33:52 +01001374""""""""""
1375
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001376let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001377func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001378 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001379 unlet s:channelfd
1380endfunc
1381
1382" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001383func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001384 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001385 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001386 call WaitFor('"what?" == g:Ch_unletResponse')
1387 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001388endfunc
1389
1390func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001391 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001392 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001393endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001394
Bram Moolenaard46ae142016-02-16 13:33:52 +01001395""""""""""
1396
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001397let g:Ch_unletResponse = ''
1398func Ch_CloseHandler(handle, msg)
1399 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001400 call ch_close(s:channelfd)
1401endfunc
1402
1403" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001404func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001405 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001406 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1407 call WaitFor('"what?" == g:Ch_unletResponse')
1408 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001409endfunc
1410
1411func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001412 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001413 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001414endfunc
1415
1416""""""""""
1417
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001418func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001419 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001420 silent! let ch = ch_open("noserver")
1421 echo ch
1422 let d = ch
1423endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001424
1425""""""""""
1426
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001427func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001428 " Wait up to a second for the port to open.
1429 let s:chopt.waittime = 1000
1430 let channel = ch_open('localhost:' . a:port, s:chopt)
1431 unlet s:chopt.waittime
1432 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001433 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001434 return
1435 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001436 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001437 call ch_close(channel)
1438endfunc
1439
1440func Test_open_delay()
1441 call ch_log('Test_open_delay()')
1442 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001443 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001444endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001445
1446"""""""""
1447
1448function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001449 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001450endfunc
1451
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001452function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001453 let handle = ch_open('localhost:' . a:port, s:chopt)
1454 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001455 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001456 return
1457 endif
1458
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001459 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001460 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001461 call WaitFor('len(g:Ch_call_ret) > 0')
1462 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001463endfunc
1464
1465func Test_call()
1466 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001467 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001468endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001469
1470"""""""""
1471
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001472let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001473function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001474 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001475endfunc
1476
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001477function Ch_test_exit_callback(port)
1478 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1479 let g:Ch_exit_job = g:currentJob
1480 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001481endfunc
1482
1483func Test_exit_callback()
1484 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001485 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001486 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001487
Bram Moolenaar9730f742016-02-28 19:50:51 +01001488 " wait up to a second for the job to exit
1489 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001490 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001491 break
1492 endif
1493 sleep 10m
1494 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001495 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001496 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001497
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001498 call assert_equal('done', g:Ch_job_exit_ret)
1499 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1500 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001501 endif
1502endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001503
Bram Moolenaar97792de2016-10-15 18:36:49 +02001504function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001505 if job_info(a:job).process == g:exit_cb_val.process
1506 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1507 endif
1508 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001509endfunction
1510
1511func Test_exit_callback_interval()
1512 if !has('job')
1513 return
1514 endif
1515
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001516 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001517 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001518 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001519 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001520 let elapsed = reltimefloat(g:exit_cb_val.end)
1521 call assert_true(elapsed > 0.5)
1522 call assert_true(elapsed < 1.0)
1523
1524 " case: unreferenced job, using timer
1525 if !has('timers')
1526 return
1527 endif
1528
1529 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1530 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1531 let g:exit_cb_val.process = job_info(g:job).process
1532 unlet g:job
1533 call Standby(1000)
1534 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1535 let elapsed = reltimefloat(g:exit_cb_val.end)
1536 else
1537 let elapsed = 1.0
1538 endif
1539 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001540 call assert_true(elapsed < 1.0)
1541endfunc
1542
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001543"""""""""
1544
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001545let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001546function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001547 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001548endfunc
1549
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001550function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001551 let handle = ch_open('localhost:' . a:port, s:chopt)
1552 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001553 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001554 return
1555 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001556 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001557
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001558 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001559 call WaitFor('"closed" == g:Ch_close_ret')
1560 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001561endfunc
1562
1563func Test_close_callback()
1564 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001565 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001566endfunc
1567
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001568function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001569 let handle = ch_open('localhost:' . a:port, s:chopt)
1570 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001571 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001572 return
1573 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001574 let g:Ch_d = {}
1575 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001576 let self.close_ret = 'closed'
1577 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001578 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001579
1580 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001581 call WaitFor('"closed" == g:Ch_d.close_ret')
1582 call assert_equal('closed', g:Ch_d.close_ret)
1583 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001584endfunc
1585
1586func Test_close_partial()
1587 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001588 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001589endfunc
1590
Bram Moolenaar80385682016-03-27 19:13:35 +02001591func Test_job_start_invalid()
1592 call assert_fails('call job_start($x)', 'E474:')
1593 call assert_fails('call job_start("")', 'E474:')
1594endfunc
1595
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001596func Test_job_stop_immediately()
1597 if !has('job')
1598 return
1599 endif
1600
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001601 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001602 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001603 call job_stop(g:job)
1604 call WaitFor('"dead" == job_status(g:job)')
1605 call assert_equal('dead', job_status(g:job))
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001606 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001607 call job_stop(g:job, 'kill')
1608 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001609 endtry
1610endfunc
1611
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001612" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001613func Test_partial_in_channel_cycle()
1614 let d = {}
1615 let d.a = function('string', [d])
1616 try
1617 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1618 catch
1619 call assert_exception('E901:')
1620 endtry
1621 unlet d
1622endfunc
1623
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001624func Test_using_freed_memory()
1625 let g:a = job_start(['ls'])
1626 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001627 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001628endfunc
1629
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001630func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001631 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001632 return
1633 endif
1634 sp test_channel.vim
1635 let g:linecount = line('$')
1636 close
1637 split testout
1638 1,$delete
1639 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001640 call WaitFor('line("$") >= g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001641 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001642 bwipe!
1643endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001644
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001645func Test_cmd_parsing()
1646 if !has('unix')
1647 return
1648 endif
1649 call assert_false(filereadable("file with space"))
1650 let job = job_start('touch "file with space"')
1651 call WaitFor('filereadable("file with space")')
1652 call assert_true(filereadable("file with space"))
1653 call delete("file with space")
1654
1655 let job = job_start('touch file\ with\ space')
1656 call WaitFor('filereadable("file with space")')
1657 call assert_true(filereadable("file with space"))
1658 call delete("file with space")
1659endfunc
1660
Bram Moolenaar82117982016-08-27 19:21:48 +02001661func Test_raw_passes_nul()
1662 if !executable('cat') || !has('job')
1663 return
1664 endif
1665
1666 " Test lines from the job containing NUL are stored correctly in a buffer.
1667 new
1668 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1669 w! Xtestread
1670 bwipe!
1671 split testout
1672 1,$delete
1673 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1674 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001675 call assert_equal("asdf\nasdf", getline(1))
1676 call assert_equal("xxx\n", getline(2))
1677 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001678
1679 call delete('Xtestread')
1680 bwipe!
1681
1682 " Test lines from a buffer with NUL bytes are written correctly to the job.
1683 new mybuffer
1684 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1685 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1686 call WaitFor('"dead" == job_status(g:Ch_job)')
1687 bwipe!
1688 split Xtestwrite
1689 call assert_equal("asdf\nasdf", getline(1))
1690 call assert_equal("xxx\n", getline(2))
1691 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001692 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001693
1694 call delete('Xtestwrite')
1695 bwipe!
1696endfunc
1697
Bram Moolenaarec68a992016-10-03 21:37:41 +02001698func MyLineCountCb(ch, msg)
1699 let g:linecount += 1
1700endfunc
1701
1702func Test_read_nonl_line()
1703 if !has('job')
1704 return
1705 endif
1706
1707 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001708 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001709 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1710 call WaitFor('3 <= g:linecount')
1711 call assert_equal(3, g:linecount)
1712endfunc
1713
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001714func Test_read_from_terminated_job()
1715 if !has('job')
1716 return
1717 endif
1718
1719 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001720 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001721 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1722 call WaitFor('1 <= g:linecount')
1723 call assert_equal(1, g:linecount)
1724endfunc
1725
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001726func Test_env()
1727 if !has('job')
1728 return
1729 endif
1730
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001731 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001732 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001733 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001734 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001735 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001736 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001737 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1738 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001739 call WaitFor('"" != g:envstr')
1740 call assert_equal("bar", g:envstr)
1741 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001742endfunc
1743
1744func Test_cwd()
1745 if !has('job')
1746 return
1747 endif
1748
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001749 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001750 if has('win32')
1751 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001752 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001753 else
1754 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001755 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001756 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001757 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001758 try
1759 call WaitFor('"" != g:envstr')
1760 let expect = substitute(expect, '[/\\]$', '', '')
1761 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1762 if $CI != '' && stridx(g:envstr, '/private/') == 0
1763 let g:envstr = g:envstr[8:]
1764 endif
1765 call assert_equal(expect, g:envstr)
1766 finally
1767 call job_stop(job)
1768 unlet g:envstr
1769 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001770endfunc
1771
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001772function Ch_test_close_lambda(port)
1773 let handle = ch_open('localhost:' . a:port, s:chopt)
1774 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001775 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001776 return
1777 endif
1778 let g:Ch_close_ret = ''
1779 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1780
1781 call assert_equal('', ch_evalexpr(handle, 'close me'))
1782 call WaitFor('"closed" == g:Ch_close_ret')
1783 call assert_equal('closed', g:Ch_close_ret)
1784endfunc
1785
1786func Test_close_lambda()
1787 call ch_log('Test_close_lambda()')
1788 call s:run_server('Ch_test_close_lambda')
1789endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001790
1791func s:test_list_args(cmd, out, remove_lf)
1792 try
1793 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001794 let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001795 call WaitFor('"" != g:out')
1796 if has('win32')
1797 let g:out = substitute(g:out, '\r', '', 'g')
1798 endif
1799 if a:remove_lf
1800 let g:out = substitute(g:out, '\n$', '', 'g')
1801 endif
1802 call assert_equal(a:out, g:out)
1803 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001804 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001805 unlet g:out
1806 endtry
1807endfunc
1808
1809func Test_list_args()
1810 if !has('job')
1811 return
1812 endif
1813
1814 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1815 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1816 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1817 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1818 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1819 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1820 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1821 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1822 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1823 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1824 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1825 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1826 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1827
1828 " tests which not contain spaces in the argument
1829 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1830 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1831 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1832 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1833 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1834 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1835 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1836 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1837 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1838endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001839
1840" Do this last, it stops any channel log.
1841func Test_zz_ch_log()
1842 call ch_logfile('Xlog', 'w')
1843 call ch_log('hello there')
1844 call ch_log('%s%s')
1845 call ch_logfile('')
1846 let text = readfile('Xlog')
1847 call assert_match("hello there", text[1])
1848 call assert_match("%s%s", text[2])
1849 call delete('Xlog')
1850endfunc