blob: 42e0e04b8176f3623d70c7db2411c8713b6c4f0b [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaare2469252016-02-04 10:54:34 +01003if !has('channel')
4 finish
5endif
6
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
8
9let s:python = PythonProg()
10if s:python == ''
Bram Moolenaard6a8d482016-02-10 20:32:20 +010011 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010012 finish
13endif
14
Bram Moolenaare74e8e72016-02-16 22:01:30 +010015let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010016
Bram Moolenaard6a8d482016-02-10 20:32:20 +010017" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010018func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020019 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010020endfunc
21
Bram Moolenaar321efdd2016-07-15 17:09:11 +020022let g:Ch_responseMsg = ''
23func Ch_requestHandler(handle, msg)
24 let g:Ch_responseHandle = a:handle
25 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010026endfunc
27
Bram Moolenaar321efdd2016-07-15 17:09:11 +020028func Ch_communicate(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010029 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +010030 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +010031 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010032 return
33 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +010034 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +010035 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +010036 call assert_equal('no process', string(ch_getjob(handle)))
37 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +010038 let dict = ch_info(handle)
39 call assert_true(dict.id != 0)
40 call assert_equal('open', dict.status)
41 call assert_equal(a:port, string(dict.port))
42 call assert_equal('open', dict.sock_status)
43 call assert_equal('socket', dict.sock_io)
44
Bram Moolenaard7ece102016-02-02 23:23:02 +010045 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010046 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010047
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010048 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010049 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
50 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
51 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
52
53 " split command should work
54 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020055 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010056 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010057
Bram Moolenaarf1f07922016-08-26 17:58:53 +020058 " string with ][ should work
59 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
60
61 " sending three messages quickly then reading should work
62 for i in range(3)
63 call ch_sendexpr(handle, 'echo hello ' . i)
64 endfor
65 call assert_equal('hello 0', ch_read(handle)[1])
66 call assert_equal('hello 1', ch_read(handle)[1])
67 call assert_equal('hello 2', ch_read(handle)[1])
68
Bram Moolenaard7ece102016-02-02 23:23:02 +010069 " Request that triggers sending two ex commands. These will usually be
70 " handled before getting the response, but it's not guaranteed, thus wait a
71 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010072 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020073 call WaitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +010074 call assert_equal('added1', getline(line('$') - 1))
75 call assert_equal('added2', getline('$'))
76
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010077 " Request command "foo bar", which fails silently.
78 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020079 call WaitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +020080 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +010081
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010082 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020083 call WaitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +010084 call assert_equal('added more', getline('$'))
85
Bram Moolenaara07fec92016-02-05 21:04:08 +010086 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +020087 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
88 call WaitFor('exists("g:Ch_responseHandle")')
89 if !exists('g:Ch_responseHandle')
90 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +010091 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +020092 call assert_equal(handle, g:Ch_responseHandle)
93 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +010094 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +020095 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +010096
Bram Moolenaar321efdd2016-07-15 17:09:11 +020097 let g:Ch_responseMsg = ''
98 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
99 call WaitFor('exists("g:Ch_responseHandle")')
100 if !exists('g:Ch_responseHandle')
101 call assert_false(1, 'g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100102 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200103 call assert_equal(handle, g:Ch_responseHandle)
104 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100105 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200106 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100107
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200108 " Using lambda.
109 let g:Ch_responseMsg = ''
110 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
111 call WaitFor('exists("g:Ch_responseHandle")')
112 if !exists('g:Ch_responseHandle')
113 call assert_false(1, 'g:Ch_responseHandle was not set')
114 else
115 call assert_equal(handle, g:Ch_responseHandle)
116 unlet g:Ch_responseHandle
117 endif
118 call assert_equal('got it', g:Ch_responseMsg)
119
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100120 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200121 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100122
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100123 " check setting options (without testing the effect)
124 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100125 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100126 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100127 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100128 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100129
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100130 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100131 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100132 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100133 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100134
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100135 " Send an eval request with special characters.
136 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
137 sleep 10m
138 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
139
140 " Send an eval request to get a line with special characters.
141 call setline(3, "a\nb\<CR>c\x01d\x7fe")
142 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
143 sleep 10m
144 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
145
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100146 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100147 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100148 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100149 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100150
Bram Moolenaar55fab432016-02-07 16:53:13 +0100151 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100152 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100153 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100154 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100155
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100156 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100157 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100158 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100159 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100160
Bram Moolenaarf4160862016-02-05 23:09:12 +0100161 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100162 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200163 call WaitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100164 call assert_equal('one', getline(line('$') - 2))
165 call assert_equal('two', getline(line('$') - 1))
166 call assert_equal('three', getline('$'))
167
168 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100169 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
170 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100171
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100173
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100174 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100175 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
176 let start = reltime()
177 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
178 let elapsed = reltime(start)
179 call assert_true(reltimefloat(elapsed) > 0.3)
180 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100181
182 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100183 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100184 let resp = ch_read(handle)
185 call assert_equal(type([]), type(resp))
186 call assert_equal(type(11), type(resp[0]))
187 call assert_equal('waited', resp[1])
188
Bram Moolenaard7ece102016-02-02 23:23:02 +0100189 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100191endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100192
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100193func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200195 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100196endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100197
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100198" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200199func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100200 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200201 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar77073442016-02-13 23:23:53 +0100202 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100203 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100204 return
205 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100206
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100207 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100208
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100209 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100210 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100211 call assert_false(1, "Can't open second channel")
212 return
213 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100214 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
215 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100216
217 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100219
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100220 call ch_close(newhandle)
221endfunc
222
223func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100224 call ch_log('Test_two_channels()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200225 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100226endfunc
227
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100228" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200229func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100230 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100233 return
234 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100236 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100237
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100238 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239endfunc
240
241func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100242 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200243 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100244endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100245
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100246"""""""""
247
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200248func Ch_handler(chan, msg)
249 unlet g:Ch_reply
250 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100251endfunc
252
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200253func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100254 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100255 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100256 call assert_false(1, "Can't open channel")
257 return
258 endif
259
260 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100261 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200262 call WaitFor('"we called you" == g:Ch_reply')
263 call assert_equal('we called you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100264
265 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100266 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200267 call WaitFor('"we did call you" == g:Ch_reply')
268 call assert_equal('we did call you', g:Ch_reply)
Bram Moolenaarf6157282016-02-10 21:07:14 +0100269endfunc
270
271func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100272 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200273 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200274 let s:chopt.callback = 'Ch_handler'
275 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200276 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200277 let s:chopt.callback = function('Ch_handler')
278 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100279 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280endfunc
281
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100282"""""""""
283
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200284let g:Ch_reply = ''
285func Ch_zeroHandler(chan, msg)
286 unlet g:Ch_reply
287 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100288endfunc
289
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200290let g:Ch_zero_reply = ''
291func Ch_oneHandler(chan, msg)
292 unlet g:Ch_zero_reply
293 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100294endfunc
295
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200296func Ch_channel_zero(port)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100297 let handle = ch_open('localhost:' . a:port, s:chopt)
298 if ch_status(handle) == "fail"
299 call assert_false(1, "Can't open channel")
300 return
301 endif
302
303 " Check that eval works.
304 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
305
306 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200307 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100308 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100309 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310 call WaitFor('"zero index" == g:Ch_reply')
311 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100312 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100313 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200314 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100315 endif
316
317 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200318 let g:Ch_reply = ''
319 let g:Ch_zero_reply = ''
320 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
321 call WaitFor('"sent zero" == g:Ch_zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100322 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200323 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100324 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200325 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100326 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200327 call assert_equal('sent zero', g:Ch_zero_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100328endfunc
329
330func Test_zero_reply()
331 call ch_log('Test_zero_reply()')
332 " Run with channel handler
333 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200334 let s:chopt.callback = 'Ch_zeroHandler'
335 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100336 unlet s:chopt.callback
337
338 " Run without channel handler
339 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200340 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100341endfunc
342
343"""""""""
344
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200345let g:Ch_reply1 = ""
346func Ch_handleRaw1(chan, msg)
347 unlet g:Ch_reply1
348 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100349endfunc
350
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200351let g:Ch_reply2 = ""
352func Ch_handleRaw2(chan, msg)
353 unlet g:Ch_reply2
354 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100355endfunc
356
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200357let g:Ch_reply3 = ""
358func Ch_handleRaw3(chan, msg)
359 unlet g:Ch_reply3
360 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100361endfunc
362
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200363func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100364 let handle = ch_open('localhost:' . a:port, s:chopt)
365 if ch_status(handle) == "fail"
366 call assert_false(1, "Can't open channel")
367 return
368 endif
369 call ch_setoptions(handle, {'mode': 'raw'})
370
371 " The message are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200372 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200373 call WaitFor('g:Ch_reply1 != ""')
374 call assert_equal("[1, \"got it\"]", g:Ch_reply1)
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200375 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
376 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200377 call WaitFor('g:Ch_reply2 != ""')
378 call assert_equal("[2, \"something\"]", g:Ch_reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100379 " wait for the 200 msec delayed reply
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200380 call WaitFor('g:Ch_reply3 != ""')
381 call assert_equal("[3, \"waited\"]", g:Ch_reply3)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100382endfunc
383
384func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100385 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200386 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100387endfunc
388
389"""""""""
390
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100391" Test that trying to connect to a non-existing port fails quickly.
392func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100393 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100394 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100395 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100396 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100397 " Oops, port does exists.
398 call ch_close(handle)
399 else
400 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100401 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100402 endif
403
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100404 " We intend to use a socket that doesn't exist and wait for half a second
405 " before giving up. If the socket does exist it can fail in various ways.
406 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100407 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100408 try
409 let handle = ch_open('localhost:9867', {'waittime': 500})
410 if ch_status(handle) != "fail"
411 " Oops, port does exists.
412 call ch_close(handle)
413 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100414 " Failed connection should wait about 500 msec. Can be longer if the
415 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100416 let elapsed = reltime(start)
417 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100418 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100419 endif
420 catch
421 if v:exception !~ 'Connection reset by peer'
422 call assert_false(1, "Caught exception: " . v:exception)
423 endif
424 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100425endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100426
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100427"""""""""
428
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100429func Test_raw_pipe()
430 if !has('job')
431 return
432 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100433 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100434 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200435 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100436 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200437
438 call assert_equal("open", ch_status(job))
439 call assert_equal("open", ch_status(job), {"part": "out"})
440 call assert_equal("open", ch_status(job), {"part": "err"})
441 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
442 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
443
444 let dict = ch_info(job)
445 call assert_true(dict.id != 0)
446 call assert_equal('open', dict.status)
447 call assert_equal('open', dict.out_status)
448 call assert_equal('RAW', dict.out_mode)
449 call assert_equal('pipe', dict.out_io)
450 call assert_equal('open', dict.err_status)
451 call assert_equal('RAW', dict.err_mode)
452 call assert_equal('pipe', dict.err_io)
453
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100454 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100455 " For a change use the job where a channel is expected.
456 call ch_sendraw(job, "echo something\n")
457 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100458 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
459
Bram Moolenaar151f6562016-03-07 21:19:38 +0100460 call ch_sendraw(job, "double this\n")
461 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100462 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
463
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200464 let g:Ch_reply = ""
465 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
466 call WaitFor('"" != g:Ch_reply')
467 call assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))
468
Bram Moolenaar151f6562016-03-07 21:19:38 +0100469 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100470 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
471 finally
472 call job_stop(job)
473 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100474
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200475 let g:Ch_job = job
476 call WaitFor('"dead" == job_status(g:Ch_job)')
Bram Moolenaar8950a562016-03-12 15:22:55 +0100477 let info = job_info(job)
478 call assert_equal("dead", info.status)
479 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100480endfunc
481
482func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100483 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100484 return
485 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100486 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100487 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100488 call assert_equal("run", job_status(job))
489 try
490 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100491 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100492 call assert_equal("something", ch_readraw(handle))
493
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100494 call ch_sendraw(handle, "echoerr wrong\n")
495 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
496
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100497 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100498 call assert_equal("this", ch_readraw(handle))
499 call assert_equal("AND this", ch_readraw(handle))
500
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200501 call ch_sendraw(handle, "split this line\n")
502 call assert_equal("this linethis linethis line", ch_readraw(handle))
503
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100504 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100505 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100506 finally
507 call job_stop(job)
508 endtry
509endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100510
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100511func Test_nl_err_to_out_pipe()
512 if !has('job')
513 return
514 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100515 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100516 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100517 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100518 call assert_equal("run", job_status(job))
519 try
520 let handle = job_getchannel(job)
521 call ch_sendraw(handle, "echo something\n")
522 call assert_equal("something", ch_readraw(handle))
523
524 call ch_sendraw(handle, "echoerr wrong\n")
525 call assert_equal("wrong", ch_readraw(handle))
526 finally
527 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100528 call ch_logfile('')
529 let loglines = readfile('Xlog')
530 call assert_true(len(loglines) > 10)
531 let found_test = 0
532 let found_send = 0
533 let found_recv = 0
534 let found_stop = 0
535 for l in loglines
536 if l =~ 'Test_nl_err_to_out_pipe'
537 let found_test = 1
538 endif
539 if l =~ 'SEND on.*echo something'
540 let found_send = 1
541 endif
542 if l =~ 'RECV on.*something'
543 let found_recv = 1
544 endif
545 if l =~ 'Stopping job with'
546 let found_stop = 1
547 endif
548 endfor
549 call assert_equal(1, found_test)
550 call assert_equal(1, found_send)
551 call assert_equal(1, found_recv)
552 call assert_equal(1, found_stop)
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200553 " On MS-Windows need to sleep for a moment to be able to delete the file.
554 sleep 10m
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100555 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100556 endtry
557endfunc
558
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200559func Stop_g_job()
560 call job_stop(g:job)
561 if has('win32')
562 " On MS-Windows the server must close the file handle before we are able
563 " to delete the file.
564 call WaitFor('job_status(g:job) == "dead"')
565 sleep 10m
566 endif
567endfunc
568
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100569func Test_nl_read_file()
570 if !has('job')
571 return
572 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100573 call ch_log('Test_nl_read_file()')
574 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200575 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100576 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200577 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100578 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200579 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100580 call assert_equal("something", ch_readraw(handle))
581 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
582 call assert_equal("this", ch_readraw(handle))
583 call assert_equal("AND this", ch_readraw(handle))
584 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200585 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100586 call delete('Xinput')
587 endtry
588endfunc
589
Bram Moolenaare98d1212016-03-08 15:37:41 +0100590func Test_nl_write_out_file()
591 if !has('job')
592 return
593 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100594 call ch_log('Test_nl_write_out_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200595 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100596 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200597 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100598 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200599 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100600 call ch_sendraw(handle, "echo line one\n")
601 call ch_sendraw(handle, "echo line two\n")
602 call ch_sendraw(handle, "double this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200603 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100604 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
605 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200606 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100607 call delete('Xoutput')
608 endtry
609endfunc
610
611func Test_nl_write_err_file()
612 if !has('job')
613 return
614 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100615 call ch_log('Test_nl_write_err_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200616 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100617 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200618 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100619 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200620 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100621 call ch_sendraw(handle, "echoerr line one\n")
622 call ch_sendraw(handle, "echoerr line two\n")
623 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200624 call WaitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100625 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
626 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200627 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100628 call delete('Xoutput')
629 endtry
630endfunc
631
632func Test_nl_write_both_file()
633 if !has('job')
634 return
635 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100636 call ch_log('Test_nl_write_both_file()')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200637 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100638 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200639 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100640 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200641 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100642 call ch_sendraw(handle, "echoerr line one\n")
643 call ch_sendraw(handle, "echo line two\n")
644 call ch_sendraw(handle, "double this\n")
645 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200646 call WaitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100647 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
648 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200649 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100650 call delete('Xoutput')
651 endtry
652endfunc
653
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200654func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200655 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200656endfunc
657
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200658func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100659 if !has('job')
660 return
661 endif
662 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200663 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200664 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200665 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100666 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100667 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200668 if a:do_msg
669 let expected[0] = 'Reading from channel output...'
670 else
671 let options['out_msg'] = 0
672 call remove(expected, 0)
673 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100674 else
675 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100676 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100677 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200678 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100679 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200680 if a:nomod
681 let options['out_modifiable'] = 0
682 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100683 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100684 call assert_equal("run", job_status(job))
685 try
686 let handle = job_getchannel(job)
687 call ch_sendraw(handle, "echo line one\n")
688 call ch_sendraw(handle, "echo line two\n")
689 call ch_sendraw(handle, "double this\n")
690 call ch_sendraw(handle, "quit\n")
691 sp pipe-output
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200692 call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200693 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200694 if a:nomod
695 call assert_equal(0, &modifiable)
696 else
697 call assert_equal(1, &modifiable)
698 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200699 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100700 bwipe!
701 finally
702 call job_stop(job)
703 endtry
704endfunc
705
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100706func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200707 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100708endfunc
709
710func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200711 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100712endfunc
713
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200714func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200715 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200716endfunc
717
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200718func Test_pipe_to_buffer_name_nomsg()
719 call Run_test_pipe_to_buffer(1, 0, 1)
720endfunc
721
722func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100723 if !has('job')
724 return
725 endif
726 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100727 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200728 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100729 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100730 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200731 if a:do_msg
732 let expected[0] = 'Reading from channel error...'
733 else
734 let options['err_msg'] = 0
735 call remove(expected, 0)
736 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100737 else
738 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100739 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100740 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200741 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100742 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200743 if a:nomod
744 let options['err_modifiable'] = 0
745 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100746 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100747 call assert_equal("run", job_status(job))
748 try
749 let handle = job_getchannel(job)
750 call ch_sendraw(handle, "echoerr line one\n")
751 call ch_sendraw(handle, "echoerr line two\n")
752 call ch_sendraw(handle, "doubleerr this\n")
753 call ch_sendraw(handle, "quit\n")
754 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200755 call WaitFor('line("$") >= 5')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200756 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200757 if a:nomod
758 call assert_equal(0, &modifiable)
759 else
760 call assert_equal(1, &modifiable)
761 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100762 bwipe!
763 finally
764 call job_stop(job)
765 endtry
766endfunc
767
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100768func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200769 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100770endfunc
771
772func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200773 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200774endfunc
775
776func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200777 call Run_test_pipe_err_to_buffer(1, 1, 1)
778endfunc
779
780func Test_pipe_err_to_buffer_name_nomsg()
781 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100782endfunc
783
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100784func Test_pipe_both_to_buffer()
785 if !has('job')
786 return
787 endif
788 call ch_log('Test_pipe_both_to_buffer()')
789 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100790 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100791 call assert_equal("run", job_status(job))
792 try
793 let handle = job_getchannel(job)
794 call ch_sendraw(handle, "echo line one\n")
795 call ch_sendraw(handle, "echoerr line two\n")
796 call ch_sendraw(handle, "double this\n")
797 call ch_sendraw(handle, "doubleerr that\n")
798 call ch_sendraw(handle, "quit\n")
799 sp pipe-err
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200800 call WaitFor('line("$") >= 7')
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100801 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
802 bwipe!
803 finally
804 call job_stop(job)
805 endtry
806endfunc
807
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100808func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100809 if !has('job')
810 return
811 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100812 call ch_log('Test_pipe_from_buffer()')
813
814 sp pipe-input
815 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200816 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100817 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100818 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100819 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100820 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100821 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100822
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100823 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100824 call assert_equal("run", job_status(job))
825 try
826 let handle = job_getchannel(job)
827 call assert_equal('one', ch_read(handle))
828 call assert_equal('two', ch_read(handle))
829 call assert_equal('three', ch_read(handle))
830 bwipe!
831 finally
832 call job_stop(job)
833 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100834endfunc
835
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100836func Test_pipe_from_buffer_name()
837 call Run_test_pipe_from_buffer(1)
838endfunc
839
840func Test_pipe_from_buffer_nr()
841 call Run_test_pipe_from_buffer(0)
842endfunc
843
Bram Moolenaar0874a832016-09-01 15:11:51 +0200844func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200845 if !executable('sort') || !has('job')
846 return
847 endif
Bram Moolenaar0874a832016-09-01 15:11:51 +0200848 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
849 if a:use_buffer
850 split sortin
851 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
852 let options.in_io = 'buffer'
853 let options.in_name = 'sortin'
854 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200855 if !a:all
856 let options.in_top = 2
857 let options.in_bot = 4
858 endif
859 let g:job = job_start('sort', options)
860 call assert_equal("run", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200861
862 if !a:use_buffer
863 call ch_sendraw(g:job, "ccc\naaa\nddd\nbbb\neee\n")
864 call ch_close_in(g:job)
865 endif
866
Bram Moolenaard8b55492016-09-01 14:35:22 +0200867 call WaitFor('job_status(g:job) == "dead"')
868 call assert_equal("dead", job_status(g:job))
Bram Moolenaar0874a832016-09-01 15:11:51 +0200869
Bram Moolenaard8b55492016-09-01 14:35:22 +0200870 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200871 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200872 call assert_equal('Reading from channel output...', getline(1))
873 if a:all
874 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
875 else
876 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
877 endif
878
879 call job_stop(g:job)
880 unlet g:job
Bram Moolenaar0874a832016-09-01 15:11:51 +0200881 if a:use_buffer
882 bwipe! sortin
883 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200884 bwipe! sortout
885endfunc
886
887func Test_pipe_through_sort_all()
888 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200889 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200890endfunc
891
892func Test_pipe_through_sort_some()
893 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200894 call Run_pipe_through_sort(0, 1)
895endfunc
896
897func Test_pipe_through_sort_feed()
898 call ch_log('Test_pipe_through_sort_feed()')
899 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200900endfunc
901
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100902func Test_pipe_to_nameless_buffer()
903 if !has('job')
904 return
905 endif
906 call ch_log('Test_pipe_to_nameless_buffer()')
907 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100908 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100909 call assert_equal("run", job_status(job))
910 try
911 let handle = job_getchannel(job)
912 call ch_sendraw(handle, "echo line one\n")
913 call ch_sendraw(handle, "echo line two\n")
914 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200915 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100916 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
917 bwipe!
918 finally
919 call job_stop(job)
920 endtry
921endfunc
922
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100923func Test_pipe_to_buffer_json()
924 if !has('job')
925 return
926 endif
927 call ch_log('Test_pipe_to_buffer_json()')
928 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100929 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100930 call assert_equal("run", job_status(job))
931 try
932 let handle = job_getchannel(job)
933 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
934 call ch_sendraw(handle, "echo [-2, 12.34]\n")
935 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200936 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100937 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
938 bwipe!
939 finally
940 call job_stop(job)
941 endtry
942endfunc
943
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100944" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100945func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100946 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100947 if getline(line('$') - a:offset) == a:line
948 break
949 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100950 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100951 endfor
952endfunc
953
954func Test_pipe_io_two_buffers()
955 if !has('job')
956 return
957 endif
958 call ch_log('Test_pipe_io_two_buffers()')
959
960 " Create two buffers, one to read from and one to write to.
961 split pipe-output
962 set buftype=nofile
963 split pipe-input
964 set buftype=nofile
965
966 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100967 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200968 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
969 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100970 call assert_equal("run", job_status(job))
971 try
972 exe "normal Gaecho hello\<CR>"
973 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100974 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100975 call assert_equal('hello', getline('$'))
976
977 exe bufwinnr('pipe-input') . "wincmd w"
978 exe "normal Gadouble this\<CR>"
979 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100980 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100981 call assert_equal('this', getline(line('$') - 1))
982 call assert_equal('AND this', getline('$'))
983
984 bwipe!
985 exe bufwinnr('pipe-input') . "wincmd w"
986 bwipe!
987 finally
988 call job_stop(job)
989 endtry
990endfunc
991
992func Test_pipe_io_one_buffer()
993 if !has('job')
994 return
995 endif
996 call ch_log('Test_pipe_io_one_buffer()')
997
998 " Create one buffer to read from and to write to.
999 split pipe-io
1000 set buftype=nofile
1001
1002 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001003 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001004 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1005 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001006 call assert_equal("run", job_status(job))
1007 try
1008 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001009 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001010 call assert_equal('hello', getline(line('$') - 1))
1011
1012 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001013 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001014 call assert_equal('this', getline(line('$') - 2))
1015 call assert_equal('AND this', getline(line('$') - 1))
1016
1017 bwipe!
1018 finally
1019 call job_stop(job)
1020 endtry
1021endfunc
1022
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001023func Test_pipe_null()
1024 if !has('job')
1025 return
1026 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001027 call ch_log('Test_pipe_null()')
1028
1029 " We cannot check that no I/O works, we only check that the job starts
1030 " properly.
1031 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001032 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001033 call assert_equal("run", job_status(job))
1034 try
1035 call assert_equal('something', ch_read(job))
1036 finally
1037 call job_stop(job)
1038 endtry
1039
1040 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001041 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001042 call assert_equal("run", job_status(job))
1043 try
1044 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1045 finally
1046 call job_stop(job)
1047 endtry
1048
1049 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001050 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001051 call assert_equal("run", job_status(job))
1052 try
1053 call assert_equal('something', ch_read(job))
1054 finally
1055 call job_stop(job)
1056 endtry
1057
1058 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001059 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001060 call assert_equal("run", job_status(job))
1061 call job_stop(job)
1062
1063 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001064 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001065 call assert_equal("run", job_status(job))
1066 call assert_equal('channel fail', string(job_getchannel(job)))
1067 call assert_equal('fail', ch_status(job))
1068 call job_stop(job)
1069endfunc
1070
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001071func Test_pipe_to_buffer_raw()
1072 if !has('job')
1073 return
1074 endif
1075 call ch_log('Test_raw_pipe_to_buffer()')
1076 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1077 split testout
1078 let job = job_start([s:python, '-c',
1079 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
1080 call assert_equal("run", job_status(job))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001081 call WaitFor('len(join(getline(2,line("$")),"") >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001082 try
1083 for line in getline(2, '$')
1084 let line = substitute(line, '^\.*', '', '')
1085 call assert_equal('', line)
1086 endfor
1087 finally
1088 call job_stop(job)
1089 bwipe!
1090 endtry
1091endfunc
1092
Bram Moolenaarde279892016-03-11 22:19:44 +01001093func Test_reuse_channel()
1094 if !has('job')
1095 return
1096 endif
1097 call ch_log('Test_reuse_channel()')
1098
1099 let job = job_start(s:python . " test_channel_pipe.py")
1100 call assert_equal("run", job_status(job))
1101 let handle = job_getchannel(job)
1102 try
1103 call ch_sendraw(handle, "echo something\n")
1104 call assert_equal("something", ch_readraw(handle))
1105 finally
1106 call job_stop(job)
1107 endtry
1108
1109 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1110 call assert_equal("run", job_status(job))
1111 let handle = job_getchannel(job)
1112 try
1113 call ch_sendraw(handle, "echo again\n")
1114 call assert_equal("again", ch_readraw(handle))
1115 finally
1116 call job_stop(job)
1117 endtry
1118endfunc
1119
Bram Moolenaar75f72652016-03-20 22:16:56 +01001120func Test_out_cb()
1121 if !has('job')
1122 return
1123 endif
1124 call ch_log('Test_out_cb()')
1125
1126 let dict = {'thisis': 'dict: '}
1127 func dict.outHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001128 let g:Ch_outmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001129 endfunc
1130 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001131 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001132 endfunc
1133 let job = job_start(s:python . " test_channel_pipe.py",
1134 \ {'out_cb': dict.outHandler,
1135 \ 'out_mode': 'json',
1136 \ 'err_cb': dict.errHandler,
1137 \ 'err_mode': 'json'})
1138 call assert_equal("run", job_status(job))
1139 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001140 let g:Ch_outmsg = ''
1141 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001142 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1143 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001144 call WaitFor('g:Ch_outmsg != ""')
1145 call assert_equal("dict: hello", g:Ch_outmsg)
1146 call WaitFor('g:Ch_errmsg != ""')
1147 call assert_equal("dict: there", g:Ch_errmsg)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001148 finally
1149 call job_stop(job)
1150 endtry
1151endfunc
1152
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001153func Test_out_close_cb()
1154 if !has('job')
1155 return
1156 endif
1157 call ch_log('Test_out_close_cb()')
1158
1159 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001160 let g:Ch_msg1 = ''
1161 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001162 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001163 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001164 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001165 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001166 let s:counter += 1
1167 endfunc
1168 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001169 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001170 let s:counter += 1
1171 endfunc
1172 let job = job_start(s:python . " test_channel_pipe.py quit now",
1173 \ {'out_cb': 'OutHandler',
1174 \ 'close_cb': 'CloseHandler'})
1175 call assert_equal("run", job_status(job))
1176 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001177 call WaitFor('g:Ch_closemsg != 0 && g:Ch_msg1 != ""')
1178 call assert_equal('quit', g:Ch_msg1)
1179 call assert_equal(2, g:Ch_closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001180 finally
1181 call job_stop(job)
1182 delfunc OutHandler
1183 delfunc CloseHandler
1184 endtry
1185endfunc
1186
Bram Moolenaar437905c2016-04-26 19:01:05 +02001187func Test_read_in_close_cb()
1188 if !has('job')
1189 return
1190 endif
1191 call ch_log('Test_read_in_close_cb()')
1192
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001193 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001194 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001195 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001196 endfunc
1197 let job = job_start(s:python . " test_channel_pipe.py quit now",
1198 \ {'close_cb': 'CloseHandler'})
1199 call assert_equal("run", job_status(job))
1200 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001201 call WaitFor('g:Ch_received != ""')
1202 call assert_equal('quit', g:Ch_received)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001203 finally
1204 call job_stop(job)
1205 delfunc CloseHandler
1206 endtry
1207endfunc
1208
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001209func Test_out_cb_lambda()
1210 if !has('job')
1211 return
1212 endif
1213 call ch_log('Test_out_cb_lambda()')
1214
1215 let job = job_start(s:python . " test_channel_pipe.py",
1216 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1217 \ 'out_mode': 'json',
1218 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1219 \ 'err_mode': 'json'})
1220 call assert_equal("run", job_status(job))
1221 try
1222 let g:Ch_outmsg = ''
1223 let g:Ch_errmsg = ''
1224 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1225 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1226 call WaitFor('g:Ch_outmsg != ""')
1227 call assert_equal("lambda: hello", g:Ch_outmsg)
1228 call WaitFor('g:Ch_errmsg != ""')
1229 call assert_equal("lambda: there", g:Ch_errmsg)
1230 finally
1231 call job_stop(job)
1232 endtry
1233endfunc
1234
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001235func Test_close_and_exit_cb()
1236 if !has('job')
1237 return
1238 endif
1239 call ch_log('Test_close_and_exit_cb')
1240
1241 let dict = {'ret': {}}
1242 func dict.close_cb(ch) dict
1243 let self.ret['close_cb'] = job_status(ch_getjob(a:ch))
1244 endfunc
1245 func dict.exit_cb(job, status) dict
1246 let self.ret['exit_cb'] = job_status(a:job)
1247 endfunc
1248
1249 let g:job = job_start('echo', {
1250 \ 'close_cb': dict.close_cb,
1251 \ 'exit_cb': dict.exit_cb,
1252 \ })
1253 call assert_equal('run', job_status(g:job))
1254 unlet g:job
1255 call WaitFor('len(dict.ret) >= 2')
1256 call assert_equal(2, len(dict.ret))
1257 call assert_match('^\%(dead\|run\)', dict.ret['close_cb'])
1258 call assert_equal('dead', dict.ret['exit_cb'])
1259endfunc
1260
Bram Moolenaard46ae142016-02-16 13:33:52 +01001261""""""""""
1262
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001263let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001264func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001265 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001266 unlet s:channelfd
1267endfunc
1268
1269" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001270func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001271 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001272 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001273 call WaitFor('"what?" == g:Ch_unletResponse')
1274 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001275endfunc
1276
1277func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001278 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001279 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001280endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001281
Bram Moolenaard46ae142016-02-16 13:33:52 +01001282""""""""""
1283
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001284let g:Ch_unletResponse = ''
1285func Ch_CloseHandler(handle, msg)
1286 let g:Ch_unletResponse = a:msg
Bram Moolenaard46ae142016-02-16 13:33:52 +01001287 call ch_close(s:channelfd)
1288endfunc
1289
1290" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001291func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001292 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001293 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1294 call WaitFor('"what?" == g:Ch_unletResponse')
1295 call assert_equal('what?', g:Ch_unletResponse)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001296endfunc
1297
1298func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001299 call ch_log('Test_close_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001300 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001301endfunc
1302
1303""""""""""
1304
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001305func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001306 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001307 silent! let ch = ch_open("noserver")
1308 echo ch
1309 let d = ch
1310endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001311
1312""""""""""
1313
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001314func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001315 " Wait up to a second for the port to open.
1316 let s:chopt.waittime = 1000
1317 let channel = ch_open('localhost:' . a:port, s:chopt)
1318 unlet s:chopt.waittime
1319 if ch_status(channel) == "fail"
1320 call assert_false(1, "Can't open channel")
1321 return
1322 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001323 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001324 call ch_close(channel)
1325endfunc
1326
1327func Test_open_delay()
1328 call ch_log('Test_open_delay()')
1329 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001330 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001331endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001332
1333"""""""""
1334
1335function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001336 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001337endfunc
1338
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001339function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001340 let handle = ch_open('localhost:' . a:port, s:chopt)
1341 if ch_status(handle) == "fail"
1342 call assert_false(1, "Can't open channel")
1343 return
1344 endif
1345
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001346 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001347 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001348 call WaitFor('len(g:Ch_call_ret) > 0')
1349 call assert_equal([1, 2, 3], g:Ch_call_ret)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001350endfunc
1351
1352func Test_call()
1353 call ch_log('Test_call()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001354 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001355endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001356
1357"""""""""
1358
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001359let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001360function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001361 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001362endfunc
1363
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001364function Ch_test_exit_callback(port)
1365 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'})
1366 let g:Ch_exit_job = g:currentJob
1367 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001368endfunc
1369
1370func Test_exit_callback()
1371 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001372 call ch_log('Test_exit_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001373 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001374
Bram Moolenaar9730f742016-02-28 19:50:51 +01001375 " wait up to a second for the job to exit
1376 for i in range(100)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001377 if g:Ch_job_exit_ret == 'done'
Bram Moolenaar9730f742016-02-28 19:50:51 +01001378 break
1379 endif
1380 sleep 10m
1381 " calling job_status() triggers the callback
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001382 call job_status(g:Ch_exit_job)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001383 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001384
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001385 call assert_equal('done', g:Ch_job_exit_ret)
1386 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1387 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001388 endif
1389endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001390
Bram Moolenaar97792de2016-10-15 18:36:49 +02001391function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001392 if job_info(a:job).process == g:exit_cb_val.process
1393 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1394 endif
1395 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001396endfunction
1397
1398func Test_exit_callback_interval()
1399 if !has('job')
1400 return
1401 endif
1402
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001403 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar97792de2016-10-15 18:36:49 +02001404 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001405 let g:exit_cb_val.process = job_info(job).process
1406 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1407 let elapsed = reltimefloat(g:exit_cb_val.end)
1408 call assert_true(elapsed > 0.5)
1409 call assert_true(elapsed < 1.0)
1410
1411 " case: unreferenced job, using timer
1412 if !has('timers')
1413 return
1414 endif
1415
1416 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1417 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1418 let g:exit_cb_val.process = job_info(g:job).process
1419 unlet g:job
1420 call Standby(1000)
1421 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1422 let elapsed = reltimefloat(g:exit_cb_val.end)
1423 else
1424 let elapsed = 1.0
1425 endif
1426 call assert_true(elapsed > 0.5)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001427 call assert_true(elapsed < 1.0)
1428endfunc
1429
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001430"""""""""
1431
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001432let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001433function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001434 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001435endfunc
1436
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001437function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001438 let handle = ch_open('localhost:' . a:port, s:chopt)
1439 if ch_status(handle) == "fail"
1440 call assert_false(1, "Can't open channel")
1441 return
1442 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001443 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001444
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001445 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001446 call WaitFor('"closed" == g:Ch_close_ret')
1447 call assert_equal('closed', g:Ch_close_ret)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001448endfunc
1449
1450func Test_close_callback()
1451 call ch_log('Test_close_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001452 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001453endfunc
1454
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001455function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001456 let handle = ch_open('localhost:' . a:port, s:chopt)
1457 if ch_status(handle) == "fail"
1458 call assert_false(1, "Can't open channel")
1459 return
1460 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001461 let g:Ch_d = {}
1462 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001463 let self.close_ret = 'closed'
1464 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001465 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001466
1467 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001468 call WaitFor('"closed" == g:Ch_d.close_ret')
1469 call assert_equal('closed', g:Ch_d.close_ret)
1470 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001471endfunc
1472
1473func Test_close_partial()
1474 call ch_log('Test_close_partial()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001475 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001476endfunc
1477
Bram Moolenaar80385682016-03-27 19:13:35 +02001478func Test_job_start_invalid()
1479 call assert_fails('call job_start($x)', 'E474:')
1480 call assert_fails('call job_start("")', 'E474:')
1481endfunc
1482
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001483func Test_job_stop_immediately()
1484 if !has('job')
1485 return
1486 endif
1487
1488 let job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
1489 try
1490 call job_stop(job)
1491 call WaitFor('"dead" == job_status(job)')
1492 call assert_equal('dead', job_status(job))
1493 finally
1494 call job_stop(job, 'kill')
1495 endtry
1496endfunc
1497
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001498" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001499func Test_partial_in_channel_cycle()
1500 let d = {}
1501 let d.a = function('string', [d])
1502 try
1503 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1504 catch
1505 call assert_exception('E901:')
1506 endtry
1507 unlet d
1508endfunc
1509
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001510func Test_using_freed_memory()
1511 let g:a = job_start(['ls'])
1512 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001513 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001514endfunc
1515
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001516func Test_collapse_buffers()
Bram Moolenaar82117982016-08-27 19:21:48 +02001517 if !executable('cat') || !has('job')
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001518 return
1519 endif
1520 sp test_channel.vim
1521 let g:linecount = line('$')
1522 close
1523 split testout
1524 1,$delete
1525 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001526 call WaitFor('line("$") > g:linecount')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001527 call assert_inrange(g:linecount, g:linecount + 1, line('$'))
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001528 bwipe!
1529endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001530
Bram Moolenaar82117982016-08-27 19:21:48 +02001531func Test_raw_passes_nul()
1532 if !executable('cat') || !has('job')
1533 return
1534 endif
1535
1536 " Test lines from the job containing NUL are stored correctly in a buffer.
1537 new
1538 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1539 w! Xtestread
1540 bwipe!
1541 split testout
1542 1,$delete
1543 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1544 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001545 call assert_equal("asdf\nasdf", getline(1))
1546 call assert_equal("xxx\n", getline(2))
1547 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001548
1549 call delete('Xtestread')
1550 bwipe!
1551
1552 " Test lines from a buffer with NUL bytes are written correctly to the job.
1553 new mybuffer
1554 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1555 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
1556 call WaitFor('"dead" == job_status(g:Ch_job)')
1557 bwipe!
1558 split Xtestwrite
1559 call assert_equal("asdf\nasdf", getline(1))
1560 call assert_equal("xxx\n", getline(2))
1561 call assert_equal("\nyyy", getline(3))
1562
1563 call delete('Xtestwrite')
1564 bwipe!
1565endfunc
1566
Bram Moolenaarec68a992016-10-03 21:37:41 +02001567func MyLineCountCb(ch, msg)
1568 let g:linecount += 1
1569endfunc
1570
1571func Test_read_nonl_line()
1572 if !has('job')
1573 return
1574 endif
1575
1576 let g:linecount = 0
1577 if has('win32')
1578 " workaround: 'shellescape' does improper escaping double quotes
1579 let arg = 'import sys;sys.stdout.write(\"1\n2\n3\")'
1580 else
1581 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1582 endif
1583 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1584 call WaitFor('3 <= g:linecount')
1585 call assert_equal(3, g:linecount)
1586endfunc
1587
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001588func Test_read_from_terminated_job()
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 os,sys;os.close(1);sys.stderr.write(\"test\n\")'
1597 else
1598 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
1599 endif
1600 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
1601 call WaitFor('1 <= g:linecount')
1602 call assert_equal(1, g:linecount)
1603endfunc
1604
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001605function Ch_test_close_lambda(port)
1606 let handle = ch_open('localhost:' . a:port, s:chopt)
1607 if ch_status(handle) == "fail"
1608 call assert_false(1, "Can't open channel")
1609 return
1610 endif
1611 let g:Ch_close_ret = ''
1612 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1613
1614 call assert_equal('', ch_evalexpr(handle, 'close me'))
1615 call WaitFor('"closed" == g:Ch_close_ret')
1616 call assert_equal('closed', g:Ch_close_ret)
1617endfunc
1618
1619func Test_close_lambda()
1620 call ch_log('Test_close_lambda()')
1621 call s:run_server('Ch_test_close_lambda')
1622endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001623
Bram Moolenaar9730f742016-02-28 19:50:51 +01001624" Uncomment this to see what happens, output is in src/testdir/channellog.
Bram Moolenaara58c58b2016-07-23 14:01:15 +02001625" call ch_logfile('channellog', 'w')