blob: 7baac1d677141e2e77de640e101cf0464bb104f8 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaare2469252016-02-04 10:54:34 +01003if !has('channel')
4 finish
5endif
6
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
8
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaard6a8d482016-02-10 20:32:20 +010011 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaare74e8e72016-02-16 22:01:30 +010015let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010016
Bram Moolenaard6a8d482016-02-10 20:32:20 +010017" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010018func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020019 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010020endfunc
21
Bram Moolenaar321efdd2016-07-15 17:09:11 +020022let g:Ch_responseMsg = ''
23func Ch_requestHandler(handle, msg)
24 let g:Ch_responseHandle = a:handle
25 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010026endfunc
27
Bram Moolenaar321efdd2016-07-15 17:09:11 +020028func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010029 " Avoid dropping messages, since we don't use a callback here.
30 let s:chopt.drop = 'never'
Bram Moolenaard6a8d482016-02-10 20:32:20 +010031 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010032 unlet s:chopt.drop
Bram Moolenaar77073442016-02-13 23:23:53 +010033 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +010034 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010035 return
36 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010037 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010038 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010039 call assert_equal('no process', string(ch_getjob(handle)))
40 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010041 let dict = ch_info(handle)
42 call assert_true(dict.id != 0)
43 call assert_equal('open', dict.status)
44 call assert_equal(a:port, string(dict.port))
45 call assert_equal('open', dict.sock_status)
46 call assert_equal('socket', dict.sock_io)
47
Bram Moolenaard7ece102016-02-02 23:23:02 +010048 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010049 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010050
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010051 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010052 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
53 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
54 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
55
56 " split command should work
57 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020058 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010059 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010060
Bram Moolenaarf1f07922016-08-26 17:58:53 +020061 " string with ][ should work
62 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
63
Bram Moolenaar4b785f62016-11-29 21:54:44 +010064 " nothing to read now
65 call assert_equal(0, ch_canread(handle))
66
Bram Moolenaarf1f07922016-08-26 17:58:53 +020067 " sending three messages quickly then reading should work
68 for i in range(3)
69 call ch_sendexpr(handle, 'echo hello ' . i)
70 endfor
71 call assert_equal('hello 0', ch_read(handle)[1])
72 call assert_equal('hello 1', ch_read(handle)[1])
73 call assert_equal('hello 2', ch_read(handle)[1])
74
Bram Moolenaard7ece102016-02-02 23:23:02 +010075 " Request that triggers sending two ex commands. These will usually be
76 " handled before getting the response, but it's not guaranteed, thus wait a
77 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010078 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020079 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010080 call assert_equal('added1', getline(line('$') - 1))
81 call assert_equal('added2', getline('$'))
82
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010083 " Request command "foo bar", which fails silently.
84 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020085 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020086 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010087
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010088 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020089 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010090 call assert_equal('added more', getline('$'))
91
Bram Moolenaara07fec92016-02-05 21:04:08 +010092 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020093 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
94 call WaitFor('exists("g:Ch_responseHandle")')
95 if !exists('g:Ch_responseHandle')
96 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010097 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020098 call assert_equal(handle, g:Ch_responseHandle)
99 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100100 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200101 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100102
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200103 let g:Ch_responseMsg = ''
104 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
105 call WaitFor('exists("g:Ch_responseHandle")')
106 if !exists('g:Ch_responseHandle')
107 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100108 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200109 call assert_equal(handle, g:Ch_responseHandle)
110 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100111 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200112 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100113
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200114 " Using lambda.
115 let g:Ch_responseMsg = ''
116 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
117 call WaitFor('exists("g:Ch_responseHandle")')
118 if !exists('g:Ch_responseHandle')
119 call assert_false(1, 'g:Ch_responseHandle was not set')
120 else
121 call assert_equal(handle, g:Ch_responseHandle)
122 unlet g:Ch_responseHandle
123 endif
124 call assert_equal('got it', g:Ch_responseMsg)
125
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100126 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200127 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100128
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100129 " check setting options (without testing the effect)
130 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100131 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100132 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100133 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100134 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100135 call ch_setoptions(handle, {'drop': 'never'})
136 call ch_setoptions(handle, {'drop': 'auto'})
137 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100138
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100139 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100140 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100141 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100142 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100143
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100144 " Send an eval request with special characters.
145 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
146 sleep 10m
147 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
148
149 " Send an eval request to get a line with special characters.
150 call setline(3, "a\nb\<CR>c\x01d\x7fe")
151 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
152 sleep 10m
153 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
154
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100155 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100156 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100157 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100158 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100159
Bram Moolenaar55fab432016-02-07 16:53:13 +0100160 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100161 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100162 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100163 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100164
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100165 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100166 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100167 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100168 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100169
Bram Moolenaarf4160862016-02-05 23:09:12 +0100170 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100171 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200172 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100173 call assert_equal('one', getline(line('$') - 2))
174 call assert_equal('two', getline(line('$') - 1))
175 call assert_equal('three', getline('$'))
176
177 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100178 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
179 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100180
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100181 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100182
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100183 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100184 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
185 let start = reltime()
186 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
187 let elapsed = reltime(start)
188 call assert_true(reltimefloat(elapsed) > 0.3)
189 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100190
191 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100192 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100193 let resp = ch_read(handle)
194 call assert_equal(type([]), type(resp))
195 call assert_equal(type(11), type(resp[0]))
196 call assert_equal('waited', resp[1])
197
Bram Moolenaard7ece102016-02-02 23:23:02 +0100198 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100199 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100200endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100201
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100202func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100203 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200204 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100205endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100206
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100207" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200208func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100209 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200210 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100211 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100212 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100213 return
214 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100215
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100216 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100217
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100218 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100219 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220 call assert_false(1, "Can't open second channel")
221 return
222 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100223 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
224 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100225
226 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100228
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100229 call ch_close(newhandle)
230endfunc
231
232func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200234 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100235endfunc
236
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100237" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200238func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100240 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100241 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100242 return
243 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100244
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100245 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100246
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100247 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248endfunc
249
250func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100251 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200252 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100253endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100254
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100255"""""""""
256
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200257func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100258 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200259 unlet g:Ch_reply
260 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100261endfunc
262
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200263func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100264 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100265 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100266 call assert_false(1, "Can't open channel")
267 return
268 endif
269
270 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100271 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200272 call WaitFor('"we called you" == g:Ch_reply')
273 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100274
275 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100276 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200277 call WaitFor('"we did call you" == g:Ch_reply')
278 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100279endfunc
280
281func Test_channel_handler()
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100282call ch_logfile('channellog', 'w')
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100283 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200284 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200285 let s:chopt.callback = 'Ch_handler'
286 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200287 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200288 let s:chopt.callback = function('Ch_handler')
289 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100290 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100291endfunc
292
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100293"""""""""
294
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200295let g:Ch_reply = ''
296func Ch_zeroHandler(chan, msg)
297 unlet g:Ch_reply
298 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100299endfunc
300
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200301let g:Ch_zero_reply = ''
302func Ch_oneHandler(chan, msg)
303 unlet g:Ch_zero_reply
304 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100305endfunc
306
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200307func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100308 let handle = ch_open('localhost:' . a:port, s:chopt)
309 if ch_status(handle) == "fail"
310 call assert_false(1, "Can't open channel")
311 return
312 endif
313
314 " Check that eval works.
315 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
316
317 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200318 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100319 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100320 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200321 call WaitFor('"zero index" == g:Ch_reply')
322 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100323 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100324 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200325 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 endif
327
328 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200329 let g:Ch_reply = ''
330 let g:Ch_zero_reply = ''
331 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
332 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100333 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200334 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100335 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200336 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200338 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100339endfunc
340
341func Test_zero_reply()
342 call ch_log('Test_zero_reply()')
343 " Run with channel handler
344 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200345 let s:chopt.callback = 'Ch_zeroHandler'
346 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347 unlet s:chopt.callback
348
349 " Run without channel handler
350 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100352endfunc
353
354"""""""""
355
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200356let g:Ch_reply1 = ""
357func Ch_handleRaw1(chan, msg)
358 unlet g:Ch_reply1
359 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100360endfunc
361
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200362let g:Ch_reply2 = ""
363func Ch_handleRaw2(chan, msg)
364 unlet g:Ch_reply2
365 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100366endfunc
367
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200368let g:Ch_reply3 = ""
369func Ch_handleRaw3(chan, msg)
370 unlet g:Ch_reply3
371 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100372endfunc
373
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200374func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100375 let handle = ch_open('localhost:' . a:port, s:chopt)
376 if ch_status(handle) == "fail"
377 call assert_false(1, "Can't open channel")
378 return
379 endif
380 call ch_setoptions(handle, {'mode': 'raw'})
381
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100382 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200383 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200384 call WaitFor('g:Ch_reply1 != ""')
385 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200386 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
387 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200388 call WaitFor('g:Ch_reply2 != ""')
389 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100390 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200391 call WaitFor('g:Ch_reply3 != ""')
392 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100393endfunc
394
395func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100396 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200397 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100398endfunc
399
400"""""""""
401
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100402" Test that trying to connect to a non-existing port fails quickly.
403func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100404 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100405 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100406 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100407 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100408 " Oops, port does exists.
409 call ch_close(handle)
410 else
411 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100412 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100413 endif
414
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100415 " We intend to use a socket that doesn't exist and wait for half a second
416 " before giving up. If the socket does exist it can fail in various ways.
417 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100418 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100419 try
420 let handle = ch_open('localhost:9867', {'waittime': 500})
421 if ch_status(handle) != "fail"
422 " Oops, port does exists.
423 call ch_close(handle)
424 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100425 " Failed connection should wait about 500 msec. Can be longer if the
426 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100427 let elapsed = reltime(start)
428 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100429 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100430 endif
431 catch
432 if v:exception !~ 'Connection reset by peer'
433 call assert_false(1, "Caught exception: " . v:exception)
434 endif
435 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100436endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100437
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100438"""""""""
439
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100440func Test_raw_pipe()
441 if !has('job')
442 return
443 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100444 call ch_log('Test_raw_pipe()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100445 " Add a dummy close callback to avoid that messages are dropped when calling
446 " ch_canread().
447 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100448 \ {'mode': 'raw', 'drop': 'never'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200449 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100450 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200451
452 call assert_equal("open", ch_status(job))
453 call assert_equal("open", ch_status(job), {"part": "out"})
454 call assert_equal("open", ch_status(job), {"part": "err"})
455 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
456 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
457
458 let dict = ch_info(job)
459 call assert_true(dict.id != 0)
460 call assert_equal('open', dict.status)
461 call assert_equal('open', dict.out_status)
462 call assert_equal('RAW', dict.out_mode)
463 call assert_equal('pipe', dict.out_io)
464 call assert_equal('open', dict.err_status)
465 call assert_equal('RAW', dict.err_mode)
466 call assert_equal('pipe', dict.err_io)
467
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100468 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100469 " For a change use the job where a channel is expected.
470 call ch_sendraw(job, "echo something\n")
471 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100472 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
473
Bram Moolenaar151f6562016-03-07 21:19:38 +0100474 call ch_sendraw(job, "double this\n")
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100475 let g:handle = job_getchannel(job)
476 call WaitFor('ch_canread(g:handle)')
477 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100478 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100479 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
480
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200481 let g:Ch_reply = ""
482 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
483 call WaitFor('"" != g:Ch_reply')
484 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
485
Bram Moolenaar151f6562016-03-07 21:19:38 +0100486 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100487 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
488 finally
489 call job_stop(job)
490 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100491
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200492 let g:Ch_job = job
493 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100494 let info = job_info(job)
495 call assert_equal("dead", info.status)
496 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100497endfunc
498
499func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100500 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100501 return
502 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100503 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100504 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100505 call assert_equal("run", job_status(job))
506 try
507 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100508 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100509 call assert_equal("something", ch_readraw(handle))
510
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100511 call ch_sendraw(handle, "echoerr wrong\n")
512 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
513
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100514 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100515 call assert_equal("this", ch_readraw(handle))
516 call assert_equal("AND this", ch_readraw(handle))
517
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200518 call ch_sendraw(handle, "split this line\n")
519 call assert_equal("this linethis linethis line", ch_readraw(handle))
520
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100521 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100522 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100523 finally
524 call job_stop(job)
525 endtry
526endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100527
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100528func Test_nl_err_to_out_pipe()
529 if !has('job')
530 return
531 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100532 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100533 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100534 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100535 call assert_equal("run", job_status(job))
536 try
537 let handle = job_getchannel(job)
538 call ch_sendraw(handle, "echo something\n")
539 call assert_equal("something", ch_readraw(handle))
540
541 call ch_sendraw(handle, "echoerr wrong\n")
542 call assert_equal("wrong", ch_readraw(handle))
543 finally
544 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100545 call ch_logfile('')
546 let loglines = readfile('Xlog')
547 call assert_true(len(loglines) > 10)
548 let found_test = 0
549 let found_send = 0
550 let found_recv = 0
551 let found_stop = 0
552 for l in loglines
553 if l =~ 'Test_nl_err_to_out_pipe'
554 let found_test = 1
555 endif
556 if l =~ 'SEND on.*echo something'
557 let found_send = 1
558 endif
559 if l =~ 'RECV on.*something'
560 let found_recv = 1
561 endif
562 if l =~ 'Stopping job with'
563 let found_stop = 1
564 endif
565 endfor
566 call assert_equal(1, found_test)
567 call assert_equal(1, found_send)
568 call assert_equal(1, found_recv)
569 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200570 " On MS-Windows need to sleep for a moment to be able to delete the file.
571 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100572 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100573 endtry
574endfunc
575
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200576func Stop_g_job()
577 call job_stop(g:job)
578 if has('win32')
579 " On MS-Windows the server must close the file handle before we are able
580 " to delete the file.
581 call WaitFor('job_status(g:job) == "dead"')
582 sleep 10m
583 endif
584endfunc
585
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100586func Test_nl_read_file()
587 if !has('job')
588 return
589 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100590 call ch_log('Test_nl_read_file()')
591 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200592 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100593 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200594 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100595 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200596 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100597 call assert_equal("something", ch_readraw(handle))
598 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
599 call assert_equal("this", ch_readraw(handle))
600 call assert_equal("AND this", ch_readraw(handle))
601 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200602 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100603 call delete('Xinput')
604 endtry
605endfunc
606
Bram Moolenaare98d1212016-03-08 15:37:41 +0100607func Test_nl_write_out_file()
608 if !has('job')
609 return
610 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100611 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200612 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100613 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200614 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100615 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200616 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100617 call ch_sendraw(handle, "echo line one\n")
618 call ch_sendraw(handle, "echo line two\n")
619 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200620 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100621 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
622 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200623 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call delete('Xoutput')
625 endtry
626endfunc
627
628func Test_nl_write_err_file()
629 if !has('job')
630 return
631 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100632 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200633 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100634 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200635 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100636 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200637 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100638 call ch_sendraw(handle, "echoerr line one\n")
639 call ch_sendraw(handle, "echoerr line two\n")
640 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200641 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100642 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
643 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200644 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call delete('Xoutput')
646 endtry
647endfunc
648
649func Test_nl_write_both_file()
650 if !has('job')
651 return
652 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100653 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200654 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100655 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200656 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100657 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200658 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100659 call ch_sendraw(handle, "echoerr line one\n")
660 call ch_sendraw(handle, "echo line two\n")
661 call ch_sendraw(handle, "double this\n")
662 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200663 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100664 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
665 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200666 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100667 call delete('Xoutput')
668 endtry
669endfunc
670
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200671func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200672 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200673endfunc
674
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200675func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100676 if !has('job')
677 return
678 endif
679 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200680 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200681 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200682 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100684 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200685 if a:do_msg
686 let expected[0] = 'Reading from channel output...'
687 else
688 let options['out_msg'] = 0
689 call remove(expected, 0)
690 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100691 else
692 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100693 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100694 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200695 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100696 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200697 if a:nomod
698 let options['out_modifiable'] = 0
699 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100700 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100701 call assert_equal("run", job_status(job))
702 try
703 let handle = job_getchannel(job)
704 call ch_sendraw(handle, "echo line one\n")
705 call ch_sendraw(handle, "echo line two\n")
706 call ch_sendraw(handle, "double this\n")
707 call ch_sendraw(handle, "quit\n")
708 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200709 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200710 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200711 if a:nomod
712 call assert_equal(0, &modifiable)
713 else
714 call assert_equal(1, &modifiable)
715 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200716 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100717 bwipe!
718 finally
719 call job_stop(job)
720 endtry
721endfunc
722
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100723func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200724 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100725endfunc
726
727func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200728 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100729endfunc
730
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200731func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200732 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200733endfunc
734
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200735func Test_pipe_to_buffer_name_nomsg()
736 call Run_test_pipe_to_buffer(1, 0, 1)
737endfunc
738
739func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100740 if !has('job')
741 return
742 endif
743 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100744 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200745 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100746 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100747 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200748 if a:do_msg
749 let expected[0] = 'Reading from channel error...'
750 else
751 let options['err_msg'] = 0
752 call remove(expected, 0)
753 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100754 else
755 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100756 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100757 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200758 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100759 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200760 if a:nomod
761 let options['err_modifiable'] = 0
762 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100763 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100764 call assert_equal("run", job_status(job))
765 try
766 let handle = job_getchannel(job)
767 call ch_sendraw(handle, "echoerr line one\n")
768 call ch_sendraw(handle, "echoerr line two\n")
769 call ch_sendraw(handle, "doubleerr this\n")
770 call ch_sendraw(handle, "quit\n")
771 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200772 call WaitFor('line("$") >= 5')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200773 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200774 if a:nomod
775 call assert_equal(0, &modifiable)
776 else
777 call assert_equal(1, &modifiable)
778 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100779 bwipe!
780 finally
781 call job_stop(job)
782 endtry
783endfunc
784
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100785func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200786 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100787endfunc
788
789func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200790 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200791endfunc
792
793func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200794 call Run_test_pipe_err_to_buffer(1, 1, 1)
795endfunc
796
797func Test_pipe_err_to_buffer_name_nomsg()
798 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100799endfunc
800
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100801func Test_pipe_both_to_buffer()
802 if !has('job')
803 return
804 endif
805 call ch_log('Test_pipe_both_to_buffer()')
806 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100807 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100808 call assert_equal("run", job_status(job))
809 try
810 let handle = job_getchannel(job)
811 call ch_sendraw(handle, "echo line one\n")
812 call ch_sendraw(handle, "echoerr line two\n")
813 call ch_sendraw(handle, "double this\n")
814 call ch_sendraw(handle, "doubleerr that\n")
815 call ch_sendraw(handle, "quit\n")
816 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200817 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100818 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
819 bwipe!
820 finally
821 call job_stop(job)
822 endtry
823endfunc
824
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100825func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100826 if !has('job')
827 return
828 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100829 call ch_log('Test_pipe_from_buffer()')
830
831 sp pipe-input
832 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200833 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100835 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100836 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100837 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100838 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100839
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100840 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100841 call assert_equal("run", job_status(job))
842 try
843 let handle = job_getchannel(job)
844 call assert_equal('one', ch_read(handle))
845 call assert_equal('two', ch_read(handle))
846 call assert_equal('three', ch_read(handle))
847 bwipe!
848 finally
849 call job_stop(job)
850 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100851endfunc
852
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100853func Test_pipe_from_buffer_name()
854 call Run_test_pipe_from_buffer(1)
855endfunc
856
857func Test_pipe_from_buffer_nr()
858 call Run_test_pipe_from_buffer(0)
859endfunc
860
Bram Moolenaar0874a832016-09-01 15:11:51 +0200861func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200862 if !executable('sort') || !has('job')
863 return
864 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200865 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
866 if a:use_buffer
867 split sortin
868 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
869 let options.in_io = 'buffer'
870 let options.in_name = 'sortin'
871 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200872 if !a:all
873 let options.in_top = 2
874 let options.in_bot = 4
875 endif
876 let g:job = job_start('sort', options)
877 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200878
879 if !a:use_buffer
880 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
881 call ch_close_in(g:job)
882 endif
883
Bram Moolenaard8b55492016-09-01 14:35:22 +0200884 call WaitFor('job_status(g:job) == "dead"')
885 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200886
Bram Moolenaard8b55492016-09-01 14:35:22 +0200887 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200888 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200889 call assert_equal('Reading from channel output...', getline(1))
890 if a:all
891 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
892 else
893 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
894 endif
895
896 call job_stop(g:job)
897 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200898 if a:use_buffer
899 bwipe! sortin
900 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200901 bwipe! sortout
902endfunc
903
904func Test_pipe_through_sort_all()
905 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200906 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200907endfunc
908
909func Test_pipe_through_sort_some()
910 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200911 call Run_pipe_through_sort(0, 1)
912endfunc
913
914func Test_pipe_through_sort_feed()
915 call ch_log('Test_pipe_through_sort_feed()')
916 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200917endfunc
918
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100919func Test_pipe_to_nameless_buffer()
920 if !has('job')
921 return
922 endif
923 call ch_log('Test_pipe_to_nameless_buffer()')
924 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100925 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100926 call assert_equal("run", job_status(job))
927 try
928 let handle = job_getchannel(job)
929 call ch_sendraw(handle, "echo line one\n")
930 call ch_sendraw(handle, "echo line two\n")
931 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200932 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100933 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
934 bwipe!
935 finally
936 call job_stop(job)
937 endtry
938endfunc
939
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100940func Test_pipe_to_buffer_json()
941 if !has('job')
942 return
943 endif
944 call ch_log('Test_pipe_to_buffer_json()')
945 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100946 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100947 call assert_equal("run", job_status(job))
948 try
949 let handle = job_getchannel(job)
950 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
951 call ch_sendraw(handle, "echo [-2, 12.34]\n")
952 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200953 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100954 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
955 bwipe!
956 finally
957 call job_stop(job)
958 endtry
959endfunc
960
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100961" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100962func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100963 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100964 if getline(line('$') - a:offset) == a:line
965 break
966 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100967 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100968 endfor
969endfunc
970
971func Test_pipe_io_two_buffers()
972 if !has('job')
973 return
974 endif
975 call ch_log('Test_pipe_io_two_buffers()')
976
977 " Create two buffers, one to read from and one to write to.
978 split pipe-output
979 set buftype=nofile
980 split pipe-input
981 set buftype=nofile
982
983 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100984 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200985 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
986 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100987 call assert_equal("run", job_status(job))
988 try
989 exe "normal Gaecho hello\<CR>"
990 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100991 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100992 call assert_equal('hello', getline('$'))
993
994 exe bufwinnr('pipe-input') . "wincmd w"
995 exe "normal Gadouble this\<CR>"
996 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100997 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100998 call assert_equal('this', getline(line('$') - 1))
999 call assert_equal('AND this', getline('$'))
1000
1001 bwipe!
1002 exe bufwinnr('pipe-input') . "wincmd w"
1003 bwipe!
1004 finally
1005 call job_stop(job)
1006 endtry
1007endfunc
1008
1009func Test_pipe_io_one_buffer()
1010 if !has('job')
1011 return
1012 endif
1013 call ch_log('Test_pipe_io_one_buffer()')
1014
1015 " Create one buffer to read from and to write to.
1016 split pipe-io
1017 set buftype=nofile
1018
1019 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001020 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001021 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1022 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001023 call assert_equal("run", job_status(job))
1024 try
1025 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001026 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001027 call assert_equal('hello', getline(line('$') - 1))
1028
1029 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001030 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001031 call assert_equal('this', getline(line('$') - 2))
1032 call assert_equal('AND this', getline(line('$') - 1))
1033
1034 bwipe!
1035 finally
1036 call job_stop(job)
1037 endtry
1038endfunc
1039
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001040func Test_pipe_null()
1041 if !has('job')
1042 return
1043 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001044 call ch_log('Test_pipe_null()')
1045
1046 " We cannot check that no I/O works, we only check that the job starts
1047 " properly.
1048 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001049 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001050 call assert_equal("run", job_status(job))
1051 try
1052 call assert_equal('something', ch_read(job))
1053 finally
1054 call job_stop(job)
1055 endtry
1056
1057 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001058 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001059 call assert_equal("run", job_status(job))
1060 try
1061 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1062 finally
1063 call job_stop(job)
1064 endtry
1065
1066 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001067 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001068 call assert_equal("run", job_status(job))
1069 try
1070 call assert_equal('something', ch_read(job))
1071 finally
1072 call job_stop(job)
1073 endtry
1074
1075 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001076 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001077 call assert_equal("run", job_status(job))
1078 call job_stop(job)
1079
1080 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001081 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001082 call assert_equal("run", job_status(job))
1083 call assert_equal('channel fail', string(job_getchannel(job)))
1084 call assert_equal('fail', ch_status(job))
1085 call job_stop(job)
1086endfunc
1087
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001088func Test_pipe_to_buffer_raw()
1089 if !has('job')
1090 return
1091 endif
1092 call ch_log('Test_raw_pipe_to_buffer()')
1093 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1094 split testout
1095 let job = job_start([s:python, '-c',
1096 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1097 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001098 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001099 try
1100 for line in getline(2, '$')
1101 let line = substitute(line, '^\.*', '', '')
1102 call assert_equal('', line)
1103 endfor
1104 finally
1105 call job_stop(job)
1106 bwipe!
1107 endtry
1108endfunc
1109
Bram Moolenaarde279892016-03-11 22:19:44 +01001110func Test_reuse_channel()
1111 if !has('job')
1112 return
1113 endif
1114 call ch_log('Test_reuse_channel()')
1115
1116 let job = job_start(s:python . " test_channel_pipe.py")
1117 call assert_equal("run", job_status(job))
1118 let handle = job_getchannel(job)
1119 try
1120 call ch_sendraw(handle, "echo something\n")
1121 call assert_equal("something", ch_readraw(handle))
1122 finally
1123 call job_stop(job)
1124 endtry
1125
1126 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1127 call assert_equal("run", job_status(job))
1128 let handle = job_getchannel(job)
1129 try
1130 call ch_sendraw(handle, "echo again\n")
1131 call assert_equal("again", ch_readraw(handle))
1132 finally
1133 call job_stop(job)
1134 endtry
1135endfunc
1136
Bram Moolenaar75f72652016-03-20 22:16:56 +01001137func Test_out_cb()
1138 if !has('job')
1139 return
1140 endif
1141 call ch_log('Test_out_cb()')
1142
1143 let dict = {'thisis': 'dict: '}
1144 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001145 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001146 endfunc
1147 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001148 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001149 endfunc
1150 let job = job_start(s:python . " test_channel_pipe.py",
1151 \ {'out_cb': dict.outHandler,
1152 \ 'out_mode': 'json',
1153 \ 'err_cb': dict.errHandler,
1154 \ 'err_mode': 'json'})
1155 call assert_equal("run", job_status(job))
1156 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001157 let g:Ch_outmsg = ''
1158 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001159 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1160 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001161 call WaitFor('g:Ch_outmsg != ""')
1162 call assert_equal("dict: hello", g:Ch_outmsg)
1163 call WaitFor('g:Ch_errmsg != ""')
1164 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001165 finally
1166 call job_stop(job)
1167 endtry
1168endfunc
1169
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001170func Test_out_close_cb()
1171 if !has('job')
1172 return
1173 endif
1174 call ch_log('Test_out_close_cb()')
1175
1176 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001177 let g:Ch_msg1 = ''
1178 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001179 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001180 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001181 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001182 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001183 let s:counter += 1
1184 endfunc
1185 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001186 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001187 let s:counter += 1
1188 endfunc
1189 let job = job_start(s:python . " test_channel_pipe.py quit now",
1190 \ {'out_cb': 'OutHandler',
1191 \ 'close_cb': 'CloseHandler'})
1192 call assert_equal("run", job_status(job))
1193 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001194 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1195 call assert_equal('quit', g:Ch_msg1)
1196 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001197 finally
1198 call job_stop(job)
1199 delfunc OutHandler
1200 delfunc CloseHandler
1201 endtry
1202endfunc
1203
Bram Moolenaar437905c2016-04-26 19:01:05 +02001204func Test_read_in_close_cb()
1205 if !has('job')
1206 return
1207 endif
1208 call ch_log('Test_read_in_close_cb()')
1209
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001210 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001211 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001212 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001213 endfunc
1214 let job = job_start(s:python . " test_channel_pipe.py quit now",
1215 \ {'close_cb': 'CloseHandler'})
1216 call assert_equal("run", job_status(job))
1217 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001218 call WaitFor('g:Ch_received != ""')
1219 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001220 finally
1221 call job_stop(job)
1222 delfunc CloseHandler
1223 endtry
1224endfunc
1225
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001226func Test_out_cb_lambda()
1227 if !has('job')
1228 return
1229 endif
1230 call ch_log('Test_out_cb_lambda()')
1231
1232 let job = job_start(s:python . " test_channel_pipe.py",
1233 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1234 \ 'out_mode': 'json',
1235 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1236 \ 'err_mode': 'json'})
1237 call assert_equal("run", job_status(job))
1238 try
1239 let g:Ch_outmsg = ''
1240 let g:Ch_errmsg = ''
1241 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1242 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1243 call WaitFor('g:Ch_outmsg != ""')
1244 call assert_equal("lambda: hello", g:Ch_outmsg)
1245 call WaitFor('g:Ch_errmsg != ""')
1246 call assert_equal("lambda: there", g:Ch_errmsg)
1247 finally
1248 call job_stop(job)
1249 endtry
1250endfunc
1251
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001252func Test_close_and_exit_cb()
1253 if !has('job')
1254 return
1255 endif
1256 call ch_log('Test_close_and_exit_cb')
1257
1258 let dict = {'ret': {}}
1259 func dict.close_cb(ch) dict
1260 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1261 endfunc
1262 func dict.exit_cb(job, status) dict
1263 let self.ret['exit_cb'] = job_status(a:job)
1264 endfunc
1265
1266 let g:job = job_start('echo', {
1267 \ 'close_cb': dict.close_cb,
1268 \ 'exit_cb': dict.exit_cb,
1269 \ })
1270 call assert_equal('run', job_status(g:job))
1271 unlet g:job
1272 call WaitFor('len(dict.ret) >= 2')
1273 call assert_equal(2, len(dict.ret))
1274 call assert_match('^\%(dead\|run\)', dict.ret['close_cb'])
1275 call assert_equal('dead', dict.ret['exit_cb'])
1276endfunc
1277
Bram Moolenaard46ae142016-02-16 13:33:52 +01001278""""""""""
1279
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001280let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001281func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001282 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001283 unlet s:channelfd
1284endfunc
1285
1286" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001287func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001288 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001289 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001290 call WaitFor('"what?" == g:Ch_unletResponse')
1291 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001292endfunc
1293
1294func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001295 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001296 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001297endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001298
Bram Moolenaard46ae142016-02-16 13:33:52 +01001299""""""""""
1300
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001301let g:Ch_unletResponse = ''
1302func Ch_CloseHandler(handle, msg)
1303 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001304 call ch_close(s:channelfd)
1305endfunc
1306
1307" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001308func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001309 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001310 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1311 call WaitFor('"what?" == g:Ch_unletResponse')
1312 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001313endfunc
1314
1315func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001316 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001317 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001318endfunc
1319
1320""""""""""
1321
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001322func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001323 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001324 silent! let ch = ch_open("noserver")
1325 echo ch
1326 let d = ch
1327endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001328
1329""""""""""
1330
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001331func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001332 " Wait up to a second for the port to open.
1333 let s:chopt.waittime = 1000
1334 let channel = ch_open('localhost:' . a:port, s:chopt)
1335 unlet s:chopt.waittime
1336 if ch_status(channel) == "fail"
1337 call assert_false(1, "Can't open channel")
1338 return
1339 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001340 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001341 call ch_close(channel)
1342endfunc
1343
1344func Test_open_delay()
1345 call ch_log('Test_open_delay()')
1346 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001347 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001348endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001349
1350"""""""""
1351
1352function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001353 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001354endfunc
1355
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001356function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001357 let handle = ch_open('localhost:' . a:port, s:chopt)
1358 if ch_status(handle) == "fail"
1359 call assert_false(1, "Can't open channel")
1360 return
1361 endif
1362
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001363 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001364 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001365 call WaitFor('len(g:Ch_call_ret) > 0')
1366 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001367endfunc
1368
1369func Test_call()
1370 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001371 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001372endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001373
1374"""""""""
1375
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001376let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001377function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001378 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001379endfunc
1380
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001381function Ch_test_exit_callback(port)
1382 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1383 let g:Ch_exit_job = g:currentJob
1384 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001385endfunc
1386
1387func Test_exit_callback()
1388 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001389 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001390 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001391
Bram Moolenaar9730f742016-02-28 19:50:51 +01001392 " wait up to a second for the job to exit
1393 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001394 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001395 break
1396 endif
1397 sleep 10m
1398 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001399 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001400 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001401
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001402 call assert_equal('done', g:Ch_job_exit_ret)
1403 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1404 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001405 endif
1406endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001407
Bram Moolenaar97792de2016-10-15 18:36:49 +02001408function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001409 if job_info(a:job).process == g:exit_cb_val.process
1410 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1411 endif
1412 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001413endfunction
1414
1415func Test_exit_callback_interval()
1416 if !has('job')
1417 return
1418 endif
1419
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001420 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001421 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001422 let g:exit_cb_val.process = job_info(job).process
1423 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1424 let elapsed = reltimefloat(g:exit_cb_val.end)
1425 call assert_true(elapsed > 0.5)
1426 call assert_true(elapsed < 1.0)
1427
1428 " case: unreferenced job, using timer
1429 if !has('timers')
1430 return
1431 endif
1432
1433 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1434 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1435 let g:exit_cb_val.process = job_info(g:job).process
1436 unlet g:job
1437 call Standby(1000)
1438 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1439 let elapsed = reltimefloat(g:exit_cb_val.end)
1440 else
1441 let elapsed = 1.0
1442 endif
1443 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001444 call assert_true(elapsed < 1.0)
1445endfunc
1446
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001447"""""""""
1448
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001449let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001450function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001451 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001452endfunc
1453
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001454function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001455 let handle = ch_open('localhost:' . a:port, s:chopt)
1456 if ch_status(handle) == "fail"
1457 call assert_false(1, "Can't open channel")
1458 return
1459 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001460 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001461
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001462 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001463 call WaitFor('"closed" == g:Ch_close_ret')
1464 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001465endfunc
1466
1467func Test_close_callback()
1468 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001469 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001470endfunc
1471
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001472function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001473 let handle = ch_open('localhost:' . a:port, s:chopt)
1474 if ch_status(handle) == "fail"
1475 call assert_false(1, "Can't open channel")
1476 return
1477 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001478 let g:Ch_d = {}
1479 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001480 let self.close_ret = 'closed'
1481 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001482 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001483
1484 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001485 call WaitFor('"closed" == g:Ch_d.close_ret')
1486 call assert_equal('closed', g:Ch_d.close_ret)
1487 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001488endfunc
1489
1490func Test_close_partial()
1491 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001492 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001493endfunc
1494
Bram Moolenaar80385682016-03-27 19:13:35 +02001495func Test_job_start_invalid()
1496 call assert_fails('call job_start($x)', 'E474:')
1497 call assert_fails('call job_start("")', 'E474:')
1498endfunc
1499
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001500func Test_job_stop_immediately()
1501 if !has('job')
1502 return
1503 endif
1504
1505 let job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
1506 try
1507 call job_stop(job)
1508 call WaitFor('"dead" == job_status(job)')
1509 call assert_equal('dead', job_status(job))
1510 finally
1511 call job_stop(job, 'kill')
1512 endtry
1513endfunc
1514
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001515" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001516func Test_partial_in_channel_cycle()
1517 let d = {}
1518 let d.a = function('string', [d])
1519 try
1520 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1521 catch
1522 call assert_exception('E901:')
1523 endtry
1524 unlet d
1525endfunc
1526
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001527func Test_using_freed_memory()
1528 let g:a = job_start(['ls'])
1529 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001530 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001531endfunc
1532
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001533func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001534 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001535 return
1536 endif
1537 sp test_channel.vim
1538 let g:linecount = line('$')
1539 close
1540 split testout
1541 1,$delete
1542 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001543 call WaitFor('line("$") > g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001544 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001545 bwipe!
1546endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001547
Bram Moolenaar82117982016-08-27 19:21:48 +02001548func Test_raw_passes_nul()
1549 if !executable('cat') || !has('job')
1550 return
1551 endif
1552
1553 " Test lines from the job containing NUL are stored correctly in a buffer.
1554 new
1555 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1556 w! Xtestread
1557 bwipe!
1558 split testout
1559 1,$delete
1560 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1561 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001562 call assert_equal("asdf\nasdf", getline(1))
1563 call assert_equal("xxx\n", getline(2))
1564 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001565
1566 call delete('Xtestread')
1567 bwipe!
1568
1569 " Test lines from a buffer with NUL bytes are written correctly to the job.
1570 new mybuffer
1571 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1572 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1573 call WaitFor('"dead" == job_status(g:Ch_job)')
1574 bwipe!
1575 split Xtestwrite
1576 call assert_equal("asdf\nasdf", getline(1))
1577 call assert_equal("xxx\n", getline(2))
1578 call assert_equal("\nyyy", getline(3))
1579
1580 call delete('Xtestwrite')
1581 bwipe!
1582endfunc
1583
Bram Moolenaarec68a992016-10-03 21:37:41 +02001584func MyLineCountCb(ch, msg)
1585 let g:linecount += 1
1586endfunc
1587
1588func Test_read_nonl_line()
1589 if !has('job')
1590 return
1591 endif
1592
1593 let g:linecount = 0
1594 if has('win32')
1595 " workaround: 'shellescape' does improper escaping double quotes
1596 let arg = 'import sys;sys.stdout.write(\"1\n2\n3\")'
1597 else
1598 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1599 endif
1600 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1601 call WaitFor('3 <= g:linecount')
1602 call assert_equal(3, g:linecount)
1603endfunc
1604
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001605func Test_read_from_terminated_job()
1606 if !has('job')
1607 return
1608 endif
1609
1610 let g:linecount = 0
1611 if has('win32')
1612 " workaround: 'shellescape' does improper escaping double quotes
1613 let arg = 'import os,sys;os.close(1);sys.stderr.write(\"test\n\")'
1614 else
1615 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
1616 endif
1617 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1618 call WaitFor('1 <= g:linecount')
1619 call assert_equal(1, g:linecount)
1620endfunc
1621
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001622function Ch_test_close_lambda(port)
1623 let handle = ch_open('localhost:' . a:port, s:chopt)
1624 if ch_status(handle) == "fail"
1625 call assert_false(1, "Can't open channel")
1626 return
1627 endif
1628 let g:Ch_close_ret = ''
1629 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1630
1631 call assert_equal('', ch_evalexpr(handle, 'close me'))
1632 call WaitFor('"closed" == g:Ch_close_ret')
1633 call assert_equal('closed', g:Ch_close_ret)
1634endfunc
1635
1636func Test_close_lambda()
1637 call ch_log('Test_close_lambda()')
1638 call s:run_server('Ch_test_close_lambda')
1639endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001640
Bram Moolenaar9730f742016-02-28 19:50:51 +01001641" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaara58c58b2016-07-23 14:01:15 +02001642" call ch_logfile('channellog', 'w')