blob: 235312e1de2f0577d761713ebe708aa0da64f67a [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 Moolenaar9a6e33a2016-02-16 19:25:12 +0100511endfunc
512
513func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100514 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100515 return
516 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100517 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100518 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519 call assert_equal("run", job_status(job))
520 try
521 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100522 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100523 call assert_equal("something", ch_readraw(handle))
524
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100525 call ch_sendraw(handle, "echoerr wrong\n")
526 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
527
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100528 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100529 call assert_equal("this", ch_readraw(handle))
530 call assert_equal("AND this", ch_readraw(handle))
531
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200532 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar620ca2d2017-12-09 19:13:13 +0100533 call assert_equal("this linethis linethis line", ch_read(handle))
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200534
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100535 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100536 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100537 finally
538 call job_stop(job)
539 endtry
540endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100541
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100542func Test_nl_err_to_out_pipe()
543 if !has('job')
544 return
545 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100546 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100547 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100548 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100549 call assert_equal("run", job_status(job))
550 try
551 let handle = job_getchannel(job)
552 call ch_sendraw(handle, "echo something\n")
553 call assert_equal("something", ch_readraw(handle))
554
555 call ch_sendraw(handle, "echoerr wrong\n")
556 call assert_equal("wrong", ch_readraw(handle))
557 finally
558 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100559 call ch_logfile('')
560 let loglines = readfile('Xlog')
561 call assert_true(len(loglines) > 10)
562 let found_test = 0
563 let found_send = 0
564 let found_recv = 0
565 let found_stop = 0
566 for l in loglines
567 if l =~ 'Test_nl_err_to_out_pipe'
568 let found_test = 1
569 endif
570 if l =~ 'SEND on.*echo something'
571 let found_send = 1
572 endif
573 if l =~ 'RECV on.*something'
574 let found_recv = 1
575 endif
576 if l =~ 'Stopping job with'
577 let found_stop = 1
578 endif
579 endfor
580 call assert_equal(1, found_test)
581 call assert_equal(1, found_send)
582 call assert_equal(1, found_recv)
583 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200584 " On MS-Windows need to sleep for a moment to be able to delete the file.
585 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100586 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100587 endtry
588endfunc
589
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200590func Stop_g_job()
591 call job_stop(g:job)
592 if has('win32')
593 " On MS-Windows the server must close the file handle before we are able
594 " to delete the file.
595 call WaitFor('job_status(g:job) == "dead"')
596 sleep 10m
597 endif
598endfunc
599
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100600func Test_nl_read_file()
601 if !has('job')
602 return
603 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100604 call ch_log('Test_nl_read_file()')
605 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200606 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100607 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200608 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100609 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200610 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100611 call assert_equal("something", ch_readraw(handle))
612 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
613 call assert_equal("this", ch_readraw(handle))
614 call assert_equal("AND this", ch_readraw(handle))
615 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200616 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100617 call delete('Xinput')
618 endtry
619endfunc
620
Bram Moolenaare98d1212016-03-08 15:37:41 +0100621func Test_nl_write_out_file()
622 if !has('job')
623 return
624 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100625 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200626 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100627 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200628 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100629 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200630 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100631 call ch_sendraw(handle, "echo line one\n")
632 call ch_sendraw(handle, "echo line two\n")
633 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200634 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100635 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
636 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200637 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100638 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100639 call delete('Xoutput')
640 endtry
641endfunc
642
643func Test_nl_write_err_file()
644 if !has('job')
645 return
646 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100647 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200648 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100649 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200650 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100651 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200652 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100653 call ch_sendraw(handle, "echoerr line one\n")
654 call ch_sendraw(handle, "echoerr line two\n")
655 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200656 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100657 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
658 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200659 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100660 call delete('Xoutput')
661 endtry
662endfunc
663
664func Test_nl_write_both_file()
665 if !has('job')
666 return
667 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100668 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200669 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100670 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200671 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100672 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200673 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100674 call ch_sendraw(handle, "echoerr line one\n")
675 call ch_sendraw(handle, "echo line two\n")
676 call ch_sendraw(handle, "double this\n")
677 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200678 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100679 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
680 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200681 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100682 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100683 call delete('Xoutput')
684 endtry
685endfunc
686
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200687func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200688 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200689endfunc
690
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200691func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100692 if !has('job')
693 return
694 endif
695 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200696 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200697 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200698 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100699 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100700 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200701 if a:do_msg
702 let expected[0] = 'Reading from channel output...'
703 else
704 let options['out_msg'] = 0
705 call remove(expected, 0)
706 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100707 else
708 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100709 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100710 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200711 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100712 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200713 if a:nomod
714 let options['out_modifiable'] = 0
715 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100716 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100717 call assert_equal("run", job_status(job))
718 try
719 let handle = job_getchannel(job)
720 call ch_sendraw(handle, "echo line one\n")
721 call ch_sendraw(handle, "echo line two\n")
722 call ch_sendraw(handle, "double this\n")
723 call ch_sendraw(handle, "quit\n")
724 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100725 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200726 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200727 if a:nomod
728 call assert_equal(0, &modifiable)
729 else
730 call assert_equal(1, &modifiable)
731 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200732 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100733 bwipe!
734 finally
735 call job_stop(job)
736 endtry
737endfunc
738
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100739func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200740 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100741endfunc
742
743func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200744 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100745endfunc
746
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200747func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200748 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200749endfunc
750
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200751func Test_pipe_to_buffer_name_nomsg()
752 call Run_test_pipe_to_buffer(1, 0, 1)
753endfunc
754
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200755func Test_close_output_buffer()
756 if !has('job')
757 return
758 endif
759 enew!
760 let test_lines = ['one', 'two']
761 call setline(1, test_lines)
762 call ch_log('Test_close_output_buffer()')
763 let options = {'out_io': 'buffer'}
764 let options['out_name'] = 'buffer-output'
765 let options['out_msg'] = 0
766 split buffer-output
767 let job = job_start(s:python . " test_channel_write.py", options)
768 call assert_equal("run", job_status(job))
769 try
770 call WaitFor('line("$") == 3')
771 call assert_equal(3, line('$'))
772 quit!
773 sleep 100m
774 " Make sure the write didn't happen to the wrong buffer.
775 call assert_equal(test_lines, getline(1, line('$')))
776 call assert_equal(-1, bufwinnr('buffer-output'))
777 sbuf buffer-output
778 call assert_notequal(-1, bufwinnr('buffer-output'))
779 sleep 100m
780 close " no more writes
781 bwipe!
782 finally
783 call job_stop(job)
784 endtry
785endfunc
786
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200787func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100788 if !has('job')
789 return
790 endif
791 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100792 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200793 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100794 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100795 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200796 if a:do_msg
797 let expected[0] = 'Reading from channel error...'
798 else
799 let options['err_msg'] = 0
800 call remove(expected, 0)
801 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100802 else
803 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100804 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100805 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200806 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100807 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200808 if a:nomod
809 let options['err_modifiable'] = 0
810 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100811 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100812 call assert_equal("run", job_status(job))
813 try
814 let handle = job_getchannel(job)
815 call ch_sendraw(handle, "echoerr line one\n")
816 call ch_sendraw(handle, "echoerr line two\n")
817 call ch_sendraw(handle, "doubleerr this\n")
818 call ch_sendraw(handle, "quit\n")
819 sp pipe-err
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100820 call WaitFor('line("$") == ' . len(expected))
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200821 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200822 if a:nomod
823 call assert_equal(0, &modifiable)
824 else
825 call assert_equal(1, &modifiable)
826 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100827 bwipe!
828 finally
829 call job_stop(job)
830 endtry
831endfunc
832
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100833func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200834 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100835endfunc
836
837func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200838 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200839endfunc
840
841func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200842 call Run_test_pipe_err_to_buffer(1, 1, 1)
843endfunc
844
845func Test_pipe_err_to_buffer_name_nomsg()
846 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100847endfunc
848
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100849func Test_pipe_both_to_buffer()
850 if !has('job')
851 return
852 endif
853 call ch_log('Test_pipe_both_to_buffer()')
854 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100855 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100856 call assert_equal("run", job_status(job))
857 try
858 let handle = job_getchannel(job)
859 call ch_sendraw(handle, "echo line one\n")
860 call ch_sendraw(handle, "echoerr line two\n")
861 call ch_sendraw(handle, "double this\n")
862 call ch_sendraw(handle, "doubleerr that\n")
863 call ch_sendraw(handle, "quit\n")
864 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200865 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100866 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
867 bwipe!
868 finally
869 call job_stop(job)
870 endtry
871endfunc
872
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100873func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100874 if !has('job')
875 return
876 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100877 call ch_log('Test_pipe_from_buffer()')
878
879 sp pipe-input
880 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200881 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100882 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100883 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100884 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100885 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100886 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100887
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100888 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100889 call assert_equal("run", job_status(job))
890 try
891 let handle = job_getchannel(job)
892 call assert_equal('one', ch_read(handle))
893 call assert_equal('two', ch_read(handle))
894 call assert_equal('three', ch_read(handle))
895 bwipe!
896 finally
897 call job_stop(job)
898 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100899endfunc
900
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100901func Test_pipe_from_buffer_name()
902 call Run_test_pipe_from_buffer(1)
903endfunc
904
905func Test_pipe_from_buffer_nr()
906 call Run_test_pipe_from_buffer(0)
907endfunc
908
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200910 if !executable('sort') || !has('job')
911 return
912 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200913 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
914 if a:use_buffer
915 split sortin
916 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
917 let options.in_io = 'buffer'
918 let options.in_name = 'sortin'
919 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200920 if !a:all
921 let options.in_top = 2
922 let options.in_bot = 4
923 endif
924 let g:job = job_start('sort', options)
925 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200926
927 if !a:use_buffer
928 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
929 call ch_close_in(g:job)
930 endif
931
Bram Moolenaard8b55492016-09-01 14:35:22 +0200932 call WaitFor('job_status(g:job) == "dead"')
933 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200934
Bram Moolenaard8b55492016-09-01 14:35:22 +0200935 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200936 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200937 call assert_equal('Reading from channel output...', getline(1))
938 if a:all
939 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
940 else
941 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
942 endif
943
944 call job_stop(g:job)
945 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200946 if a:use_buffer
947 bwipe! sortin
948 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200949 bwipe! sortout
950endfunc
951
952func Test_pipe_through_sort_all()
953 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200954 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200955endfunc
956
957func Test_pipe_through_sort_some()
958 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200959 call Run_pipe_through_sort(0, 1)
960endfunc
961
962func Test_pipe_through_sort_feed()
963 call ch_log('Test_pipe_through_sort_feed()')
964 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200965endfunc
966
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100967func Test_pipe_to_nameless_buffer()
968 if !has('job')
969 return
970 endif
971 call ch_log('Test_pipe_to_nameless_buffer()')
972 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100973 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100974 call assert_equal("run", job_status(job))
975 try
976 let handle = job_getchannel(job)
977 call ch_sendraw(handle, "echo line one\n")
978 call ch_sendraw(handle, "echo line two\n")
979 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200980 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100981 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
982 bwipe!
983 finally
984 call job_stop(job)
985 endtry
986endfunc
987
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100988func Test_pipe_to_buffer_json()
989 if !has('job')
990 return
991 endif
992 call ch_log('Test_pipe_to_buffer_json()')
993 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100994 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100995 call assert_equal("run", job_status(job))
996 try
997 let handle = job_getchannel(job)
998 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
999 call ch_sendraw(handle, "echo [-2, 12.34]\n")
1000 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001001 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001002 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
1003 bwipe!
1004 finally
1005 call job_stop(job)
1006 endtry
1007endfunc
1008
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001009" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001010func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001011 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001012 if getline(line('$') - a:offset) == a:line
1013 break
1014 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001015 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001016 endfor
1017endfunc
1018
1019func Test_pipe_io_two_buffers()
1020 if !has('job')
1021 return
1022 endif
1023 call ch_log('Test_pipe_io_two_buffers()')
1024
1025 " Create two buffers, one to read from and one to write to.
1026 split pipe-output
1027 set buftype=nofile
1028 split pipe-input
1029 set buftype=nofile
1030
1031 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001032 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001033 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
1034 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001035 call assert_equal("run", job_status(job))
1036 try
1037 exe "normal Gaecho hello\<CR>"
1038 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001039 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001040 call assert_equal('hello', getline('$'))
1041
1042 exe bufwinnr('pipe-input') . "wincmd w"
1043 exe "normal Gadouble this\<CR>"
1044 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001045 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001046 call assert_equal('this', getline(line('$') - 1))
1047 call assert_equal('AND this', getline('$'))
1048
1049 bwipe!
1050 exe bufwinnr('pipe-input') . "wincmd w"
1051 bwipe!
1052 finally
1053 call job_stop(job)
1054 endtry
1055endfunc
1056
1057func Test_pipe_io_one_buffer()
1058 if !has('job')
1059 return
1060 endif
1061 call ch_log('Test_pipe_io_one_buffer()')
1062
1063 " Create one buffer to read from and to write to.
1064 split pipe-io
1065 set buftype=nofile
1066
1067 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001068 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001069 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1070 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001071 call assert_equal("run", job_status(job))
1072 try
1073 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001074 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001075 call assert_equal('hello', getline(line('$') - 1))
1076
1077 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001078 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001079 call assert_equal('this', getline(line('$') - 2))
1080 call assert_equal('AND this', getline(line('$') - 1))
1081
1082 bwipe!
1083 finally
1084 call job_stop(job)
1085 endtry
1086endfunc
1087
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001088func Test_pipe_null()
1089 if !has('job')
1090 return
1091 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001092 call ch_log('Test_pipe_null()')
1093
1094 " We cannot check that no I/O works, we only check that the job starts
1095 " properly.
1096 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001097 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001098 call assert_equal("run", job_status(job))
1099 try
1100 call assert_equal('something', ch_read(job))
1101 finally
1102 call job_stop(job)
1103 endtry
1104
1105 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001106 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001107 call assert_equal("run", job_status(job))
1108 try
1109 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1110 finally
1111 call job_stop(job)
1112 endtry
1113
1114 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001115 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001116 call assert_equal("run", job_status(job))
1117 try
1118 call assert_equal('something', ch_read(job))
1119 finally
1120 call job_stop(job)
1121 endtry
1122
1123 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001124 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001125 call assert_equal("run", job_status(job))
1126 call job_stop(job)
1127
1128 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001129 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001130 call assert_equal("run", job_status(job))
1131 call assert_equal('channel fail', string(job_getchannel(job)))
1132 call assert_equal('fail', ch_status(job))
1133 call job_stop(job)
1134endfunc
1135
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001136func Test_pipe_to_buffer_raw()
1137 if !has('job')
1138 return
1139 endif
1140 call ch_log('Test_raw_pipe_to_buffer()')
1141 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1142 split testout
1143 let job = job_start([s:python, '-c',
1144 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1145 call assert_equal("run", job_status(job))
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001146 call WaitFor('len(join(getline(1, "$"), "")) >= 10000', 3000)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001147 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001148 let totlen = 0
1149 for line in getline(1, '$')
1150 call assert_equal('', substitute(line, '^\.*', '', ''))
1151 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001152 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001153 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001154 finally
1155 call job_stop(job)
1156 bwipe!
1157 endtry
1158endfunc
1159
Bram Moolenaarde279892016-03-11 22:19:44 +01001160func Test_reuse_channel()
1161 if !has('job')
1162 return
1163 endif
1164 call ch_log('Test_reuse_channel()')
1165
1166 let job = job_start(s:python . " test_channel_pipe.py")
1167 call assert_equal("run", job_status(job))
1168 let handle = job_getchannel(job)
1169 try
1170 call ch_sendraw(handle, "echo something\n")
1171 call assert_equal("something", ch_readraw(handle))
1172 finally
1173 call job_stop(job)
1174 endtry
1175
1176 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1177 call assert_equal("run", job_status(job))
1178 let handle = job_getchannel(job)
1179 try
1180 call ch_sendraw(handle, "echo again\n")
1181 call assert_equal("again", ch_readraw(handle))
1182 finally
1183 call job_stop(job)
1184 endtry
1185endfunc
1186
Bram Moolenaar75f72652016-03-20 22:16:56 +01001187func Test_out_cb()
1188 if !has('job')
1189 return
1190 endif
1191 call ch_log('Test_out_cb()')
1192
1193 let dict = {'thisis': 'dict: '}
1194 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001195 if type(a:msg) == v:t_string
1196 let g:Ch_outmsg = self.thisis . a:msg
1197 else
1198 let g:Ch_outobj = a:msg
1199 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001200 endfunc
1201 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001202 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001203 endfunc
1204 let job = job_start(s:python . " test_channel_pipe.py",
1205 \ {'out_cb': dict.outHandler,
1206 \ 'out_mode': 'json',
1207 \ 'err_cb': dict.errHandler,
1208 \ 'err_mode': 'json'})
1209 call assert_equal("run", job_status(job))
1210 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001211 let g:Ch_outmsg = ''
1212 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001213 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1214 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001215 call WaitFor('g:Ch_outmsg != ""')
1216 call assert_equal("dict: hello", g:Ch_outmsg)
1217 call WaitFor('g:Ch_errmsg != ""')
1218 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001219
1220 " Receive a json object split in pieces
1221 unlet! g:Ch_outobj
1222 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
1223 call WaitFor('exists("g:Ch_outobj")')
1224 call assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001225 finally
1226 call job_stop(job)
1227 endtry
1228endfunc
1229
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001230func Test_out_close_cb()
1231 if !has('job')
1232 return
1233 endif
1234 call ch_log('Test_out_close_cb()')
1235
1236 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001237 let g:Ch_msg1 = ''
1238 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001239 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001240 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001241 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001242 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001243 let s:counter += 1
1244 endfunc
1245 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001246 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001247 let s:counter += 1
1248 endfunc
1249 let job = job_start(s:python . " test_channel_pipe.py quit now",
1250 \ {'out_cb': 'OutHandler',
1251 \ 'close_cb': 'CloseHandler'})
1252 call assert_equal("run", job_status(job))
1253 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001254 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1255 call assert_equal('quit', g:Ch_msg1)
1256 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001257 finally
1258 call job_stop(job)
1259 delfunc OutHandler
1260 delfunc CloseHandler
1261 endtry
1262endfunc
1263
Bram Moolenaar437905c2016-04-26 19:01:05 +02001264func Test_read_in_close_cb()
1265 if !has('job')
1266 return
1267 endif
1268 call ch_log('Test_read_in_close_cb()')
1269
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001270 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001271 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001272 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001273 endfunc
1274 let job = job_start(s:python . " test_channel_pipe.py quit now",
1275 \ {'close_cb': 'CloseHandler'})
1276 call assert_equal("run", job_status(job))
1277 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001278 call WaitFor('g:Ch_received != ""')
1279 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001280 finally
1281 call job_stop(job)
1282 delfunc CloseHandler
1283 endtry
1284endfunc
1285
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001286" Use channel in NL mode but received text does not end in NL.
1287func Test_read_in_close_cb_incomplete()
1288 if !has('job')
1289 return
1290 endif
1291 call ch_log('Test_read_in_close_cb_incomplete()')
1292
1293 let g:Ch_received = ''
1294 func! CloseHandler(chan)
1295 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1296 let g:Ch_received .= ch_read(a:chan)
1297 endwhile
1298 endfunc
1299 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1300 \ {'close_cb': 'CloseHandler'})
1301 call assert_equal("run", job_status(job))
1302 try
1303 call WaitFor('g:Ch_received != ""')
1304 call assert_equal('incomplete', g:Ch_received)
1305 finally
1306 call job_stop(job)
1307 delfunc CloseHandler
1308 endtry
1309endfunc
1310
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001311func Test_out_cb_lambda()
1312 if !has('job')
1313 return
1314 endif
1315 call ch_log('Test_out_cb_lambda()')
1316
1317 let job = job_start(s:python . " test_channel_pipe.py",
1318 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1319 \ 'out_mode': 'json',
1320 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1321 \ 'err_mode': 'json'})
1322 call assert_equal("run", job_status(job))
1323 try
1324 let g:Ch_outmsg = ''
1325 let g:Ch_errmsg = ''
1326 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1327 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1328 call WaitFor('g:Ch_outmsg != ""')
1329 call assert_equal("lambda: hello", g:Ch_outmsg)
1330 call WaitFor('g:Ch_errmsg != ""')
1331 call assert_equal("lambda: there", g:Ch_errmsg)
1332 finally
1333 call job_stop(job)
1334 endtry
1335endfunc
1336
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001337func Test_close_and_exit_cb()
1338 if !has('job')
1339 return
1340 endif
1341 call ch_log('Test_close_and_exit_cb')
1342
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001343 let g:retdict = {'ret': {}}
1344 func g:retdict.close_cb(ch) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001345 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1346 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001347 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001348 let self.ret['exit_cb'] = job_status(a:job)
1349 endfunc
1350
1351 let g:job = job_start('echo', {
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001352 \ 'close_cb': g:retdict.close_cb,
1353 \ 'exit_cb': g:retdict.exit_cb,
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001354 \ })
1355 call assert_equal('run', job_status(g:job))
1356 unlet g:job
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001357 call WaitFor('len(g:retdict.ret) >= 2')
1358 call assert_equal(2, len(g:retdict.ret))
1359 call assert_match('^\%(dead\|run\)', g:retdict.ret['close_cb'])
1360 call assert_equal('dead', g:retdict.ret['exit_cb'])
1361 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001362endfunc
1363
Bram Moolenaard46ae142016-02-16 13:33:52 +01001364""""""""""
1365
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001366let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001367func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001368 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001369 unlet s:channelfd
1370endfunc
1371
1372" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001374 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001375 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001376 call WaitFor('"what?" == g:Ch_unletResponse')
1377 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001378endfunc
1379
1380func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001381 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001382 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001383endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001384
Bram Moolenaard46ae142016-02-16 13:33:52 +01001385""""""""""
1386
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001387let g:Ch_unletResponse = ''
1388func Ch_CloseHandler(handle, msg)
1389 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001390 call ch_close(s:channelfd)
1391endfunc
1392
1393" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001394func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001395 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001396 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1397 call WaitFor('"what?" == g:Ch_unletResponse')
1398 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001399endfunc
1400
1401func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001402 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001403 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001404endfunc
1405
1406""""""""""
1407
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001408func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001409 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001410 silent! let ch = ch_open("noserver")
1411 echo ch
1412 let d = ch
1413endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001414
1415""""""""""
1416
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001417func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001418 " Wait up to a second for the port to open.
1419 let s:chopt.waittime = 1000
1420 let channel = ch_open('localhost:' . a:port, s:chopt)
1421 unlet s:chopt.waittime
1422 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001423 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001424 return
1425 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001426 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001427 call ch_close(channel)
1428endfunc
1429
1430func Test_open_delay()
1431 call ch_log('Test_open_delay()')
1432 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001433 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001434endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001435
1436"""""""""
1437
1438function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001439 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001440endfunc
1441
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001442function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001443 let handle = ch_open('localhost:' . a:port, s:chopt)
1444 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001445 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001446 return
1447 endif
1448
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001449 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001450 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001451 call WaitFor('len(g:Ch_call_ret) > 0')
1452 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001453endfunc
1454
1455func Test_call()
1456 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001457 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001458endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001459
1460"""""""""
1461
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001462let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001463function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001464 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001465endfunc
1466
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001467function Ch_test_exit_callback(port)
1468 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1469 let g:Ch_exit_job = g:currentJob
1470 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001471endfunc
1472
1473func Test_exit_callback()
1474 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001475 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001476 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001477
Bram Moolenaar9730f742016-02-28 19:50:51 +01001478 " wait up to a second for the job to exit
1479 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001480 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001481 break
1482 endif
1483 sleep 10m
1484 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001485 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001486 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001487
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001488 call assert_equal('done', g:Ch_job_exit_ret)
1489 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1490 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001491 endif
1492endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001493
Bram Moolenaar97792de2016-10-15 18:36:49 +02001494function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001495 if job_info(a:job).process == g:exit_cb_val.process
1496 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1497 endif
1498 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001499endfunction
1500
1501func Test_exit_callback_interval()
1502 if !has('job')
1503 return
1504 endif
1505
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001506 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001507 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001508 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaarab8b1c12017-11-04 19:24:31 +01001509 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0', 2000)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001510 let elapsed = reltimefloat(g:exit_cb_val.end)
1511 call assert_true(elapsed > 0.5)
1512 call assert_true(elapsed < 1.0)
1513
1514 " case: unreferenced job, using timer
1515 if !has('timers')
1516 return
1517 endif
1518
1519 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1520 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1521 let g:exit_cb_val.process = job_info(g:job).process
1522 unlet g:job
1523 call Standby(1000)
1524 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1525 let elapsed = reltimefloat(g:exit_cb_val.end)
1526 else
1527 let elapsed = 1.0
1528 endif
1529 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001530 call assert_true(elapsed < 1.0)
1531endfunc
1532
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001533"""""""""
1534
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001535let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001536function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001537 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001538endfunc
1539
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001540function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001541 let handle = ch_open('localhost:' . a:port, s:chopt)
1542 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001543 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001544 return
1545 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001546 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001547
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001548 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001549 call WaitFor('"closed" == g:Ch_close_ret')
1550 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001551endfunc
1552
1553func Test_close_callback()
1554 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001555 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001556endfunc
1557
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001558function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001559 let handle = ch_open('localhost:' . a:port, s:chopt)
1560 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001561 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001562 return
1563 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001564 let g:Ch_d = {}
1565 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001566 let self.close_ret = 'closed'
1567 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001568 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001569
1570 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001571 call WaitFor('"closed" == g:Ch_d.close_ret')
1572 call assert_equal('closed', g:Ch_d.close_ret)
1573 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001574endfunc
1575
1576func Test_close_partial()
1577 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001578 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001579endfunc
1580
Bram Moolenaar80385682016-03-27 19:13:35 +02001581func Test_job_start_invalid()
1582 call assert_fails('call job_start($x)', 'E474:')
1583 call assert_fails('call job_start("")', 'E474:')
1584endfunc
1585
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001586func Test_job_stop_immediately()
1587 if !has('job')
1588 return
1589 endif
1590
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001591 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001592 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001593 call job_stop(g:job)
1594 call WaitFor('"dead" == job_status(g:job)')
1595 call assert_equal('dead', job_status(g:job))
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001596 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001597 call job_stop(g:job, 'kill')
1598 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001599 endtry
1600endfunc
1601
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001602" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001603func Test_partial_in_channel_cycle()
1604 let d = {}
1605 let d.a = function('string', [d])
1606 try
1607 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1608 catch
1609 call assert_exception('E901:')
1610 endtry
1611 unlet d
1612endfunc
1613
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001614func Test_using_freed_memory()
1615 let g:a = job_start(['ls'])
1616 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001617 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001618endfunc
1619
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001620func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001621 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001622 return
1623 endif
1624 sp test_channel.vim
1625 let g:linecount = line('$')
1626 close
1627 split testout
1628 1,$delete
1629 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001630 call WaitFor('line("$") >= g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001631 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001632 bwipe!
1633endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001634
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001635func Test_cmd_parsing()
1636 if !has('unix')
1637 return
1638 endif
1639 call assert_false(filereadable("file with space"))
1640 let job = job_start('touch "file with space"')
1641 call WaitFor('filereadable("file with space")')
1642 call assert_true(filereadable("file with space"))
1643 call delete("file with space")
1644
1645 let job = job_start('touch file\ with\ space')
1646 call WaitFor('filereadable("file with space")')
1647 call assert_true(filereadable("file with space"))
1648 call delete("file with space")
1649endfunc
1650
Bram Moolenaar82117982016-08-27 19:21:48 +02001651func Test_raw_passes_nul()
1652 if !executable('cat') || !has('job')
1653 return
1654 endif
1655
1656 " Test lines from the job containing NUL are stored correctly in a buffer.
1657 new
1658 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1659 w! Xtestread
1660 bwipe!
1661 split testout
1662 1,$delete
1663 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1664 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001665 call assert_equal("asdf\nasdf", getline(1))
1666 call assert_equal("xxx\n", getline(2))
1667 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001668
1669 call delete('Xtestread')
1670 bwipe!
1671
1672 " Test lines from a buffer with NUL bytes are written correctly to the job.
1673 new mybuffer
1674 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1675 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1676 call WaitFor('"dead" == job_status(g:Ch_job)')
1677 bwipe!
1678 split Xtestwrite
1679 call assert_equal("asdf\nasdf", getline(1))
1680 call assert_equal("xxx\n", getline(2))
1681 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001682 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001683
1684 call delete('Xtestwrite')
1685 bwipe!
1686endfunc
1687
Bram Moolenaarec68a992016-10-03 21:37:41 +02001688func MyLineCountCb(ch, msg)
1689 let g:linecount += 1
1690endfunc
1691
1692func Test_read_nonl_line()
1693 if !has('job')
1694 return
1695 endif
1696
1697 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001698 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaarec68a992016-10-03 21:37:41 +02001699 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1700 call WaitFor('3 <= g:linecount')
1701 call assert_equal(3, g:linecount)
1702endfunc
1703
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001704func Test_read_from_terminated_job()
1705 if !has('job')
1706 return
1707 endif
1708
1709 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001710 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001711 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1712 call WaitFor('1 <= g:linecount')
1713 call assert_equal(1, g:linecount)
1714endfunc
1715
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001716func Test_env()
1717 if !has('job')
1718 return
1719 endif
1720
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001721 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001722 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001723 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001724 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001725 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001726 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001727 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1728 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001729 call WaitFor('"" != g:envstr')
1730 call assert_equal("bar", g:envstr)
1731 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001732endfunc
1733
1734func Test_cwd()
1735 if !has('job')
1736 return
1737 endif
1738
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001739 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001740 if has('win32')
1741 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001742 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001743 else
1744 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001745 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001746 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001747 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001748 try
1749 call WaitFor('"" != g:envstr')
1750 let expect = substitute(expect, '[/\\]$', '', '')
1751 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1752 if $CI != '' && stridx(g:envstr, '/private/') == 0
1753 let g:envstr = g:envstr[8:]
1754 endif
1755 call assert_equal(expect, g:envstr)
1756 finally
1757 call job_stop(job)
1758 unlet g:envstr
1759 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001760endfunc
1761
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001762function Ch_test_close_lambda(port)
1763 let handle = ch_open('localhost:' . a:port, s:chopt)
1764 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001765 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001766 return
1767 endif
1768 let g:Ch_close_ret = ''
1769 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1770
1771 call assert_equal('', ch_evalexpr(handle, 'close me'))
1772 call WaitFor('"closed" == g:Ch_close_ret')
1773 call assert_equal('closed', g:Ch_close_ret)
1774endfunc
1775
1776func Test_close_lambda()
1777 call ch_log('Test_close_lambda()')
1778 call s:run_server('Ch_test_close_lambda')
1779endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001780
1781func s:test_list_args(cmd, out, remove_lf)
1782 try
1783 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001784 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 +02001785 call WaitFor('"" != g:out')
1786 if has('win32')
1787 let g:out = substitute(g:out, '\r', '', 'g')
1788 endif
1789 if a:remove_lf
1790 let g:out = substitute(g:out, '\n$', '', 'g')
1791 endif
1792 call assert_equal(a:out, g:out)
1793 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001794 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001795 unlet g:out
1796 endtry
1797endfunc
1798
1799func Test_list_args()
1800 if !has('job')
1801 return
1802 endif
1803
1804 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1805 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1806 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1807 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1808 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1809 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1810 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1811 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1812 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1813 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1814 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1815 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1816 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1817
1818 " tests which not contain spaces in the argument
1819 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1820 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1821 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1822 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1823 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1824 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1825 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1826 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1827 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1828endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001829
1830" Do this last, it stops any channel log.
1831func Test_zz_ch_log()
1832 call ch_logfile('Xlog', 'w')
1833 call ch_log('hello there')
1834 call ch_log('%s%s')
1835 call ch_logfile('')
1836 let text = readfile('Xlog')
1837 call assert_match("hello there", text[1])
1838 call assert_match("%s%s", text[2])
1839 call delete('Xlog')
1840endfunc