blob: 363b5cd9e1f290ded6c0ca59e1402a42cdc09da1 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
Bram Moolenaare2469252016-02-04 10:54:34 +01004if !has('channel')
5 finish
6endif
7
8" This test requires the Python command to run the test server.
Bram Moolenaara8343c12016-02-04 22:09:48 +01009" This most likely only works on Unix and Windows.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010010if has('unix')
Bram Moolenaar835dc632016-02-07 14:27:38 +010011 " We also need the job feature or the pkill command to make sure the server
12 " can be stopped.
13 if !(executable('python') && (has('job') || executable('pkill')))
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010014 finish
15 endif
Bram Moolenaarb6a77372016-02-15 22:55:28 +010016 let s:python = 'python'
Bram Moolenaara8343c12016-02-04 22:09:48 +010017elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010018 " Use Python Launcher for Windows (py.exe) if available.
19 if executable('py.exe')
20 let s:python = 'py.exe'
21 elseif executable('python.exe')
22 let s:python = 'python.exe'
23 else
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010024 finish
25 endif
26else
Bram Moolenaard6a8d482016-02-10 20:32:20 +010027 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010028 finish
29endif
30
Bram Moolenaare74e8e72016-02-16 22:01:30 +010031let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010032
Bram Moolenaard6a8d482016-02-10 20:32:20 +010033" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010034func s:run_server(testfunc, ...)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010035 " The Python program writes the port number in Xportnr.
36 call delete("Xportnr")
37
Bram Moolenaar81661fb2016-02-18 22:23:34 +010038 if a:0 == 1
39 let arg = ' ' . a:1
40 else
41 let arg = ''
42 endif
43 let cmd = s:python . " test_channel.py" . arg
44
Bram Moolenaard6a8d482016-02-10 20:32:20 +010045 try
46 if has('job')
Bram Moolenaar65edff82016-02-21 16:40:11 +010047 let s:job = job_start(cmd, {"stoponexit": "hup"})
48 call job_setoptions(s:job, {"stoponexit": "kill"})
Bram Moolenaard6a8d482016-02-10 20:32:20 +010049 elseif has('win32')
Bram Moolenaar81661fb2016-02-18 22:23:34 +010050 exe 'silent !start cmd /c start "test_channel" ' . cmd
Bram Moolenaard6a8d482016-02-10 20:32:20 +010051 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010052 exe 'silent !' . cmd . '&'
Bram Moolenaard7ece102016-02-02 23:23:02 +010053 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010054
Bram Moolenaard6a8d482016-02-10 20:32:20 +010055 " Wait for up to 2 seconds for the port number to be there.
Bram Moolenaard6a8d482016-02-10 20:32:20 +010056 let l = []
Bram Moolenaar9fe885e2016-03-08 16:06:55 +010057 for i in range(200)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010058 try
59 let l = readfile("Xportnr")
60 catch
61 endtry
62 if len(l) >= 1
63 break
64 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +010065 sleep 10m
66 endfor
Bram Moolenaard6a8d482016-02-10 20:32:20 +010067 call delete("Xportnr")
68
69 if len(l) == 0
70 " Can't make the connection, give up.
71 call assert_false(1, "Can't start test_channel.py")
72 return -1
73 endif
74 let port = l[0]
75
76 call call(function(a:testfunc), [port])
77 catch
78 call assert_false(1, "Caught exception: " . v:exception)
79 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010080 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010081 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010082endfunc
83
84func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010085 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010086 if exists('s:job')
87 call job_stop(s:job)
88 unlet s:job
89 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010090 elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010091 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010092 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010093 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010094 endif
95endfunc
96
Bram Moolenaara07fec92016-02-05 21:04:08 +010097let s:responseMsg = ''
98func s:RequestHandler(handle, msg)
99 let s:responseHandle = a:handle
100 let s:responseMsg = a:msg
101endfunc
102
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100103" Wait for up to a second for "expr" to become true.
104func s:waitFor(expr)
105 for i in range(100)
Bram Moolenaard9d473e2016-03-08 19:07:22 +0100106 try
107 if eval(a:expr)
108 return
109 endif
110 catch
111 endtry
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100112 sleep 10m
113 endfor
114endfunc
115
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100116func s:communicate(port)
117 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100118 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100119 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100120 return
121 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +0100122 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100123 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +0100124 call assert_equal('no process', string(ch_getjob(handle)))
125 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100126 let dict = ch_info(handle)
127 call assert_true(dict.id != 0)
128 call assert_equal('open', dict.status)
129 call assert_equal(a:port, string(dict.port))
130 call assert_equal('open', dict.sock_status)
131 call assert_equal('socket', dict.sock_io)
132
Bram Moolenaard7ece102016-02-02 23:23:02 +0100133 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100134 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100135
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100136 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100137 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
138 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
139 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
140
141 " split command should work
142 call assert_equal('ok', ch_evalexpr(handle, 'split'))
143 call s:waitFor('exists("g:split")')
144 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100145
Bram Moolenaard7ece102016-02-02 23:23:02 +0100146 " Request that triggers sending two ex commands. These will usually be
147 " handled before getting the response, but it's not guaranteed, thus wait a
148 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100149 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100150 call s:waitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100151 call assert_equal('added1', getline(line('$') - 1))
152 call assert_equal('added2', getline('$'))
153
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100154 " Request command "foo bar", which fails silently.
155 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
156 call s:waitFor('v:errmsg =~ "E492"')
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200157 call assert_match('E492:.*foo bar', v:errmsg)
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100158
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100159 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100160 call s:waitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100161 call assert_equal('added more', getline('$'))
162
Bram Moolenaara07fec92016-02-05 21:04:08 +0100163 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100164 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100165 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 if !exists('s:responseHandle')
167 call assert_false(1, 's:responseHandle was not set')
168 else
169 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100170 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100171 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100172 call assert_equal('got it', s:responseMsg)
173
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100174 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100175 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100176 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100177 if !exists('s:responseHandle')
178 call assert_false(1, 's:responseHandle was not set')
179 else
180 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100181 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100182 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100183 call assert_equal('got it', s:responseMsg)
184
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100185 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200186 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100187
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100188 " check setting options (without testing the effect)
189 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100190 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100191 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100192 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100193 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100194
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100195 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100197 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100199
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100200 " Send an eval request with special characters.
201 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
202 sleep 10m
203 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
204
205 " Send an eval request to get a line with special characters.
206 call setline(3, "a\nb\<CR>c\x01d\x7fe")
207 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
208 sleep 10m
209 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
210
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100211 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100212 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100213 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100214 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100215
Bram Moolenaar55fab432016-02-07 16:53:13 +0100216 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100217 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100218 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100219 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100220
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100221 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100222 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100223 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100224 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100225
Bram Moolenaarf4160862016-02-05 23:09:12 +0100226 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100228 call s:waitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100229 call assert_equal('one', getline(line('$') - 2))
230 call assert_equal('two', getline(line('$') - 1))
231 call assert_equal('three', getline('$'))
232
233 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100234 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
235 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100236
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100237 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100238
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100239 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100240 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
241 let start = reltime()
242 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
243 let elapsed = reltime(start)
244 call assert_true(reltimefloat(elapsed) > 0.3)
245 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100246
247 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100248 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100249 let resp = ch_read(handle)
250 call assert_equal(type([]), type(resp))
251 call assert_equal(type(11), type(resp[0]))
252 call assert_equal('waited', resp[1])
253
Bram Moolenaard7ece102016-02-02 23:23:02 +0100254 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100255 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100256endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100257
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100258func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100259 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100260 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100261endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100262
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100263" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100264func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100265 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100266 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100267 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100268 return
269 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100270
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100271 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100272
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100273 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100274 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100275 call assert_false(1, "Can't open second channel")
276 return
277 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100278 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
279 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100280
281 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100282 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100283
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100284 call ch_close(newhandle)
285endfunc
286
287func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100288 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100289 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100290endfunc
291
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100292" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100293func s:server_crash(port)
294 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100295 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100296 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100297 return
298 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100299
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100300 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100301
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100302 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100303endfunc
304
305func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100306 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100307 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100308endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100309
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100310"""""""""
311
Bram Moolenaarf6157282016-02-10 21:07:14 +0100312let s:reply = ""
313func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100314 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100315 let s:reply = a:msg
316endfunc
317
318func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100319 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100320 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100321 call assert_false(1, "Can't open channel")
322 return
323 endif
324
325 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100326 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100327 call s:waitFor('"we called you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100328 call assert_equal('we called you', s:reply)
329
330 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100331 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100332 call s:waitFor('"we did call you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100333 call assert_equal('we did call you', s:reply)
334endfunc
335
336func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100337 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100338 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100339 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100340 let s:chopt.callback = function('s:Handler')
341 call s:run_server('s:channel_handler')
342 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100343endfunc
344
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100345"""""""""
346
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347let s:ch_reply = ''
348func s:ChHandler(chan, msg)
349 unlet s:ch_reply
350 let s:ch_reply = a:msg
351endfunc
352
353let s:zero_reply = ''
354func s:OneHandler(chan, msg)
355 unlet s:zero_reply
356 let s:zero_reply = a:msg
357endfunc
358
359func s:channel_zero(port)
360 let handle = ch_open('localhost:' . a:port, s:chopt)
361 if ch_status(handle) == "fail"
362 call assert_false(1, "Can't open channel")
363 return
364 endif
365
366 " Check that eval works.
367 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
368
369 " Check that eval works if a zero id message is sent back.
370 let s:ch_reply = ''
371 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100372 if s:has_handler
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100373 call s:waitFor('"zero index" == s:ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100374 call assert_equal('zero index', s:ch_reply)
375 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100376 sleep 20m
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100377 call assert_equal('', s:ch_reply)
378 endif
379
380 " Check that handler works if a zero id message is sent back.
381 let s:ch_reply = ''
382 let s:zero_reply = ''
383 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100384 call s:waitFor('"sent zero" == s:zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100385 if s:has_handler
386 call assert_equal('zero index', s:ch_reply)
387 else
388 call assert_equal('', s:ch_reply)
389 endif
390 call assert_equal('sent zero', s:zero_reply)
391endfunc
392
393func Test_zero_reply()
394 call ch_log('Test_zero_reply()')
395 " Run with channel handler
396 let s:has_handler = 1
397 let s:chopt.callback = 's:ChHandler'
398 call s:run_server('s:channel_zero')
399 unlet s:chopt.callback
400
401 " Run without channel handler
402 let s:has_handler = 0
403 call s:run_server('s:channel_zero')
404endfunc
405
406"""""""""
407
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100408let s:reply1 = ""
409func s:HandleRaw1(chan, msg)
410 unlet s:reply1
411 let s:reply1 = a:msg
412endfunc
413
414let s:reply2 = ""
415func s:HandleRaw2(chan, msg)
416 unlet s:reply2
417 let s:reply2 = a:msg
418endfunc
419
420let s:reply3 = ""
421func s:HandleRaw3(chan, msg)
422 unlet s:reply3
423 let s:reply3 = a:msg
424endfunc
425
426func s:raw_one_time_callback(port)
427 let handle = ch_open('localhost:' . a:port, s:chopt)
428 if ch_status(handle) == "fail"
429 call assert_false(1, "Can't open channel")
430 return
431 endif
432 call ch_setoptions(handle, {'mode': 'raw'})
433
434 " The message are sent raw, we do our own JSON strings here.
435 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100436 call s:waitFor('s:reply1 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100437 call assert_equal("[1, \"got it\"]", s:reply1)
438 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
439 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100440 call s:waitFor('s:reply2 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100441 call assert_equal("[2, \"something\"]", s:reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100442 " wait for the 200 msec delayed reply
443 call s:waitFor('s:reply3 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100444 call assert_equal("[3, \"waited\"]", s:reply3)
445endfunc
446
447func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100448 call ch_log('Test_raw_one_time_callback()')
449 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100450endfunc
451
452"""""""""
453
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100454" Test that trying to connect to a non-existing port fails quickly.
455func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100456 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100457 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100458 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100459 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100460 " Oops, port does exists.
461 call ch_close(handle)
462 else
463 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100464 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100465 endif
466
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100467 " We intend to use a socket that doesn't exist and wait for half a second
468 " before giving up. If the socket does exist it can fail in various ways.
469 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100470 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100471 try
472 let handle = ch_open('localhost:9867', {'waittime': 500})
473 if ch_status(handle) != "fail"
474 " Oops, port does exists.
475 call ch_close(handle)
476 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100477 " Failed connection should wait about 500 msec. Can be longer if the
478 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100479 let elapsed = reltime(start)
480 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100481 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100482 endif
483 catch
484 if v:exception !~ 'Connection reset by peer'
485 call assert_false(1, "Caught exception: " . v:exception)
486 endif
487 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100488endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100489
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100490"""""""""
491
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100492func Test_raw_pipe()
493 if !has('job')
494 return
495 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100496 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100497 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
498 call assert_equal("run", job_status(job))
499 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100500 " For a change use the job where a channel is expected.
501 call ch_sendraw(job, "echo something\n")
502 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100503 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
504
Bram Moolenaar151f6562016-03-07 21:19:38 +0100505 call ch_sendraw(job, "double this\n")
506 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100507 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
508
Bram Moolenaar151f6562016-03-07 21:19:38 +0100509 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100510 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
511 finally
512 call job_stop(job)
513 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100514
515 let s:job = job
516 call s:waitFor('"dead" == job_status(s:job)')
517 let info = job_info(job)
518 call assert_equal("dead", info.status)
519 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100520endfunc
521
522func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100523 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100524 return
525 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100526 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100527 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100528 call assert_equal("run", job_status(job))
529 try
530 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100531 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100532 call assert_equal("something", ch_readraw(handle))
533
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100534 call ch_sendraw(handle, "echoerr wrong\n")
535 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
536
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100537 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100538 call assert_equal("this", ch_readraw(handle))
539 call assert_equal("AND this", ch_readraw(handle))
540
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100541 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100542 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100543 finally
544 call job_stop(job)
545 endtry
546endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100547
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100548func Test_nl_err_to_out_pipe()
549 if !has('job')
550 return
551 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100552 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100553 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100554 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100555 call assert_equal("run", job_status(job))
556 try
557 let handle = job_getchannel(job)
558 call ch_sendraw(handle, "echo something\n")
559 call assert_equal("something", ch_readraw(handle))
560
561 call ch_sendraw(handle, "echoerr wrong\n")
562 call assert_equal("wrong", ch_readraw(handle))
563 finally
564 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100565 call ch_logfile('')
566 let loglines = readfile('Xlog')
567 call assert_true(len(loglines) > 10)
568 let found_test = 0
569 let found_send = 0
570 let found_recv = 0
571 let found_stop = 0
572 for l in loglines
573 if l =~ 'Test_nl_err_to_out_pipe'
574 let found_test = 1
575 endif
576 if l =~ 'SEND on.*echo something'
577 let found_send = 1
578 endif
579 if l =~ 'RECV on.*something'
580 let found_recv = 1
581 endif
582 if l =~ 'Stopping job with'
583 let found_stop = 1
584 endif
585 endfor
586 call assert_equal(1, found_test)
587 call assert_equal(1, found_send)
588 call assert_equal(1, found_recv)
589 call assert_equal(1, found_stop)
590 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100591 endtry
592endfunc
593
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100594func Test_nl_read_file()
595 if !has('job')
596 return
597 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100598 call ch_log('Test_nl_read_file()')
599 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
600 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100601 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100602 call assert_equal("run", job_status(job))
603 try
604 let handle = job_getchannel(job)
605 call assert_equal("something", ch_readraw(handle))
606 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
607 call assert_equal("this", ch_readraw(handle))
608 call assert_equal("AND this", ch_readraw(handle))
609 finally
610 call job_stop(job)
611 call delete('Xinput')
612 endtry
613endfunc
614
Bram Moolenaare98d1212016-03-08 15:37:41 +0100615func Test_nl_write_out_file()
616 if !has('job')
617 return
618 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100619 call ch_log('Test_nl_write_out_file()')
620 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100621 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100622 call assert_equal("run", job_status(job))
623 try
624 let handle = job_getchannel(job)
625 call ch_sendraw(handle, "echo line one\n")
626 call ch_sendraw(handle, "echo line two\n")
627 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100628 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100629 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
630 finally
631 call job_stop(job)
632 call delete('Xoutput')
633 endtry
634endfunc
635
636func Test_nl_write_err_file()
637 if !has('job')
638 return
639 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100640 call ch_log('Test_nl_write_err_file()')
641 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100642 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100643 call assert_equal("run", job_status(job))
644 try
645 let handle = job_getchannel(job)
646 call ch_sendraw(handle, "echoerr line one\n")
647 call ch_sendraw(handle, "echoerr line two\n")
648 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100649 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100650 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
651 finally
652 call job_stop(job)
653 call delete('Xoutput')
654 endtry
655endfunc
656
657func Test_nl_write_both_file()
658 if !has('job')
659 return
660 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100661 call ch_log('Test_nl_write_both_file()')
662 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100663 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100664 call assert_equal("run", job_status(job))
665 try
666 let handle = job_getchannel(job)
667 call ch_sendraw(handle, "echoerr line one\n")
668 call ch_sendraw(handle, "echo line two\n")
669 call ch_sendraw(handle, "double this\n")
670 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100671 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100672 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
673 finally
674 call job_stop(job)
675 call delete('Xoutput')
676 endtry
677endfunc
678
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100679func Run_test_pipe_to_buffer(use_name)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100680 if !has('job')
681 return
682 endif
683 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100684 let options = {'out_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100685 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100686 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100687 let firstline = 'Reading from channel output...'
688 else
689 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100690 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100691 quit
692 let firstline = ''
693 endif
694 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100695 call assert_equal("run", job_status(job))
696 try
697 let handle = job_getchannel(job)
698 call ch_sendraw(handle, "echo line one\n")
699 call ch_sendraw(handle, "echo line two\n")
700 call ch_sendraw(handle, "double this\n")
701 call ch_sendraw(handle, "quit\n")
702 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100703 call s:waitFor('line("$") >= 6')
Bram Moolenaar6a063632016-03-21 23:18:54 +0100704 if getline('$') == 'DETACH'
705 $del
706 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100707 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100708 bwipe!
709 finally
710 call job_stop(job)
711 endtry
712endfunc
713
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100714func Test_pipe_to_buffer_name()
715 call Run_test_pipe_to_buffer(1)
716endfunc
717
718func Test_pipe_to_buffer_nr()
719 call Run_test_pipe_to_buffer(0)
720endfunc
721
722func Run_test_pipe_err_to_buffer(use_name)
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 Moolenaar29fd0382016-03-09 23:14:07 +0100728 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100729 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100730 let firstline = 'Reading from channel error...'
731 else
732 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100733 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100734 quit
735 let firstline = ''
736 endif
737 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100738 call assert_equal("run", job_status(job))
739 try
740 let handle = job_getchannel(job)
741 call ch_sendraw(handle, "echoerr line one\n")
742 call ch_sendraw(handle, "echoerr line two\n")
743 call ch_sendraw(handle, "doubleerr this\n")
744 call ch_sendraw(handle, "quit\n")
745 sp pipe-err
746 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100747 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100748 bwipe!
749 finally
750 call job_stop(job)
751 endtry
752endfunc
753
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100754func Test_pipe_err_to_buffer_name()
755 call Run_test_pipe_err_to_buffer(1)
756endfunc
757
758func Test_pipe_err_to_buffer_nr()
759 call Run_test_pipe_err_to_buffer(0)
760endfunc
761
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100762func Test_pipe_both_to_buffer()
763 if !has('job')
764 return
765 endif
766 call ch_log('Test_pipe_both_to_buffer()')
767 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100768 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100769 call assert_equal("run", job_status(job))
770 try
771 let handle = job_getchannel(job)
772 call ch_sendraw(handle, "echo line one\n")
773 call ch_sendraw(handle, "echoerr line two\n")
774 call ch_sendraw(handle, "double this\n")
775 call ch_sendraw(handle, "doubleerr that\n")
776 call ch_sendraw(handle, "quit\n")
777 sp pipe-err
778 call s:waitFor('line("$") >= 7')
779 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
780 bwipe!
781 finally
782 call job_stop(job)
783 endtry
784endfunc
785
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100786func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100787 if !has('job')
788 return
789 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100790 call ch_log('Test_pipe_from_buffer()')
791
792 sp pipe-input
793 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200794 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100795 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100796 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100797 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100798 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100799 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100800
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100801 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100802 call assert_equal("run", job_status(job))
803 try
804 let handle = job_getchannel(job)
805 call assert_equal('one', ch_read(handle))
806 call assert_equal('two', ch_read(handle))
807 call assert_equal('three', ch_read(handle))
808 bwipe!
809 finally
810 call job_stop(job)
811 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100812endfunc
813
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100814func Test_pipe_from_buffer_name()
815 call Run_test_pipe_from_buffer(1)
816endfunc
817
818func Test_pipe_from_buffer_nr()
819 call Run_test_pipe_from_buffer(0)
820endfunc
821
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100822func Test_pipe_to_nameless_buffer()
823 if !has('job')
824 return
825 endif
826 call ch_log('Test_pipe_to_nameless_buffer()')
827 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100828 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100829 call assert_equal("run", job_status(job))
830 try
831 let handle = job_getchannel(job)
832 call ch_sendraw(handle, "echo line one\n")
833 call ch_sendraw(handle, "echo line two\n")
834 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100835 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100836 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
837 bwipe!
838 finally
839 call job_stop(job)
840 endtry
841endfunc
842
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100843func Test_pipe_to_buffer_json()
844 if !has('job')
845 return
846 endif
847 call ch_log('Test_pipe_to_buffer_json()')
848 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100849 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100850 call assert_equal("run", job_status(job))
851 try
852 let handle = job_getchannel(job)
853 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
854 call ch_sendraw(handle, "echo [-2, 12.34]\n")
855 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100856 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100857 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
858 bwipe!
859 finally
860 call job_stop(job)
861 endtry
862endfunc
863
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100864" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100865func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100866 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100867 if getline(line('$') - a:offset) == a:line
868 break
869 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100870 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100871 endfor
872endfunc
873
874func Test_pipe_io_two_buffers()
875 if !has('job')
876 return
877 endif
878 call ch_log('Test_pipe_io_two_buffers()')
879
880 " Create two buffers, one to read from and one to write to.
881 split pipe-output
882 set buftype=nofile
883 split pipe-input
884 set buftype=nofile
885
886 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100887 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200888 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
889 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100890 call assert_equal("run", job_status(job))
891 try
892 exe "normal Gaecho hello\<CR>"
893 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100894 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100895 call assert_equal('hello', getline('$'))
896
897 exe bufwinnr('pipe-input') . "wincmd w"
898 exe "normal Gadouble this\<CR>"
899 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100900 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100901 call assert_equal('this', getline(line('$') - 1))
902 call assert_equal('AND this', getline('$'))
903
904 bwipe!
905 exe bufwinnr('pipe-input') . "wincmd w"
906 bwipe!
907 finally
908 call job_stop(job)
909 endtry
910endfunc
911
912func Test_pipe_io_one_buffer()
913 if !has('job')
914 return
915 endif
916 call ch_log('Test_pipe_io_one_buffer()')
917
918 " Create one buffer to read from and to write to.
919 split pipe-io
920 set buftype=nofile
921
922 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100923 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200924 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
925 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100926 call assert_equal("run", job_status(job))
927 try
928 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100929 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100930 call assert_equal('hello', getline(line('$') - 1))
931
932 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100933 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100934 call assert_equal('this', getline(line('$') - 2))
935 call assert_equal('AND this', getline(line('$') - 1))
936
937 bwipe!
938 finally
939 call job_stop(job)
940 endtry
941endfunc
942
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100943func Test_pipe_null()
944 if !has('job')
945 return
946 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100947 call ch_log('Test_pipe_null()')
948
949 " We cannot check that no I/O works, we only check that the job starts
950 " properly.
951 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100952 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100953 call assert_equal("run", job_status(job))
954 try
955 call assert_equal('something', ch_read(job))
956 finally
957 call job_stop(job)
958 endtry
959
960 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100961 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100962 call assert_equal("run", job_status(job))
963 try
964 call assert_equal('err-out', ch_read(job, {"part": "err"}))
965 finally
966 call job_stop(job)
967 endtry
968
969 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100970 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100971 call assert_equal("run", job_status(job))
972 try
973 call assert_equal('something', ch_read(job))
974 finally
975 call job_stop(job)
976 endtry
977
978 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100979 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100980 call assert_equal("run", job_status(job))
981 call job_stop(job)
982
983 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100984 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100985 call assert_equal("run", job_status(job))
986 call assert_equal('channel fail', string(job_getchannel(job)))
987 call assert_equal('fail', ch_status(job))
988 call job_stop(job)
989endfunc
990
Bram Moolenaarde279892016-03-11 22:19:44 +0100991func Test_reuse_channel()
992 if !has('job')
993 return
994 endif
995 call ch_log('Test_reuse_channel()')
996
997 let job = job_start(s:python . " test_channel_pipe.py")
998 call assert_equal("run", job_status(job))
999 let handle = job_getchannel(job)
1000 try
1001 call ch_sendraw(handle, "echo something\n")
1002 call assert_equal("something", ch_readraw(handle))
1003 finally
1004 call job_stop(job)
1005 endtry
1006
1007 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1008 call assert_equal("run", job_status(job))
1009 let handle = job_getchannel(job)
1010 try
1011 call ch_sendraw(handle, "echo again\n")
1012 call assert_equal("again", ch_readraw(handle))
1013 finally
1014 call job_stop(job)
1015 endtry
1016endfunc
1017
Bram Moolenaar75f72652016-03-20 22:16:56 +01001018func Test_out_cb()
1019 if !has('job')
1020 return
1021 endif
1022 call ch_log('Test_out_cb()')
1023
1024 let dict = {'thisis': 'dict: '}
1025 func dict.outHandler(chan, msg) dict
1026 let s:outmsg = self.thisis . a:msg
1027 endfunc
1028 func dict.errHandler(chan, msg) dict
1029 let s:errmsg = self.thisis . a:msg
1030 endfunc
1031 let job = job_start(s:python . " test_channel_pipe.py",
1032 \ {'out_cb': dict.outHandler,
1033 \ 'out_mode': 'json',
1034 \ 'err_cb': dict.errHandler,
1035 \ 'err_mode': 'json'})
1036 call assert_equal("run", job_status(job))
1037 try
1038 let s:outmsg = ''
1039 let s:errmsg = ''
1040 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1041 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1042 call s:waitFor('s:outmsg != ""')
1043 call assert_equal("dict: hello", s:outmsg)
1044 call s:waitFor('s:errmsg != ""')
1045 call assert_equal("dict: there", s:errmsg)
1046 finally
1047 call job_stop(job)
1048 endtry
1049endfunc
1050
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001051func Test_out_close_cb()
1052 if !has('job')
1053 return
1054 endif
1055 call ch_log('Test_out_close_cb()')
1056
1057 let s:counter = 1
Bram Moolenaard75263c2016-04-30 16:07:23 +02001058 let s:msg1 = ''
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001059 let s:closemsg = 0
1060 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001061 if s:counter == 1
1062 let s:msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001063 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001064 let s:counter += 1
1065 endfunc
1066 func! CloseHandler(chan)
1067 let s:closemsg = s:counter
1068 let s:counter += 1
1069 endfunc
1070 let job = job_start(s:python . " test_channel_pipe.py quit now",
1071 \ {'out_cb': 'OutHandler',
1072 \ 'close_cb': 'CloseHandler'})
1073 call assert_equal("run", job_status(job))
1074 try
Bram Moolenaar715d2852016-04-30 17:06:31 +02001075 call s:waitFor('s:closemsg != 0 && s:msg1 != ""')
Bram Moolenaard75263c2016-04-30 16:07:23 +02001076 call assert_equal('quit', s:msg1)
Bram Moolenaar715d2852016-04-30 17:06:31 +02001077 call assert_equal(2, s:closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001078 finally
1079 call job_stop(job)
1080 delfunc OutHandler
1081 delfunc CloseHandler
1082 endtry
1083endfunc
1084
Bram Moolenaar437905c2016-04-26 19:01:05 +02001085func Test_read_in_close_cb()
1086 if !has('job')
1087 return
1088 endif
1089 call ch_log('Test_read_in_close_cb()')
1090
1091 let s:received = ''
1092 func! CloseHandler(chan)
1093 let s:received = ch_read(a:chan)
1094 endfunc
1095 let job = job_start(s:python . " test_channel_pipe.py quit now",
1096 \ {'close_cb': 'CloseHandler'})
1097 call assert_equal("run", job_status(job))
1098 try
1099 call s:waitFor('s:received != ""')
1100 call assert_equal('quit', s:received)
1101 finally
1102 call job_stop(job)
1103 delfunc CloseHandler
1104 endtry
1105endfunc
1106
Bram Moolenaard46ae142016-02-16 13:33:52 +01001107""""""""""
1108
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001109let s:unletResponse = ''
1110func s:UnletHandler(handle, msg)
1111 let s:unletResponse = a:msg
1112 unlet s:channelfd
1113endfunc
1114
1115" Test that "unlet handle" in a handler doesn't crash Vim.
1116func s:unlet_handle(port)
1117 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001118 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001119 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001120 call assert_equal('what?', s:unletResponse)
1121endfunc
1122
1123func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001124 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001125 call s:run_server('s:unlet_handle')
1126endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001127
Bram Moolenaard46ae142016-02-16 13:33:52 +01001128""""""""""
1129
1130let s:unletResponse = ''
1131func s:CloseHandler(handle, msg)
1132 let s:unletResponse = a:msg
1133 call ch_close(s:channelfd)
1134endfunc
1135
1136" Test that "unlet handle" in a handler doesn't crash Vim.
1137func s:close_handle(port)
1138 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001139 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001140 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001141 call assert_equal('what?', s:unletResponse)
1142endfunc
1143
1144func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001145 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001146 call s:run_server('s:close_handle')
1147endfunc
1148
1149""""""""""
1150
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001151func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001152 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001153 silent! let ch = ch_open("noserver")
1154 echo ch
1155 let d = ch
1156endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001157
1158""""""""""
1159
1160func s:open_delay(port)
1161 " Wait up to a second for the port to open.
1162 let s:chopt.waittime = 1000
1163 let channel = ch_open('localhost:' . a:port, s:chopt)
1164 unlet s:chopt.waittime
1165 if ch_status(channel) == "fail"
1166 call assert_false(1, "Can't open channel")
1167 return
1168 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001169 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001170 call ch_close(channel)
1171endfunc
1172
1173func Test_open_delay()
1174 call ch_log('Test_open_delay()')
1175 " The server will wait half a second before creating the port.
1176 call s:run_server('s:open_delay', 'delay')
1177endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001178
1179"""""""""
1180
1181function MyFunction(a,b,c)
1182 let s:call_ret = [a:a, a:b, a:c]
1183endfunc
1184
1185function s:test_call(port)
1186 let handle = ch_open('localhost:' . a:port, s:chopt)
1187 if ch_status(handle) == "fail"
1188 call assert_false(1, "Can't open channel")
1189 return
1190 endif
1191
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001192 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001193 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001194 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001195 call assert_equal([1, 2, 3], s:call_ret)
1196endfunc
1197
1198func Test_call()
1199 call ch_log('Test_call()')
1200 call s:run_server('s:test_call')
1201endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001202
1203"""""""""
1204
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001205let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001206function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001207 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001208endfunc
1209
1210function s:test_exit_callback(port)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001211 call job_setoptions(s:job, {'exit_cb': 'MyExitCb'})
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001212 let s:exit_job = s:job
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001213 call assert_equal('MyExitCb', job_info(s:job)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001214endfunc
1215
1216func Test_exit_callback()
1217 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001218 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001219 call s:run_server('s:test_exit_callback')
1220
Bram Moolenaar9730f742016-02-28 19:50:51 +01001221 " wait up to a second for the job to exit
1222 for i in range(100)
1223 if s:job_exit_ret == 'done'
1224 break
1225 endif
1226 sleep 10m
1227 " calling job_status() triggers the callback
1228 call job_status(s:exit_job)
1229 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001230
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001231 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar8950a562016-03-12 15:22:55 +01001232 call assert_equal('dead', job_info(s:exit_job).status)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001233 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001234 endif
1235endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001236
1237"""""""""
1238
1239let s:ch_close_ret = 'alive'
1240function MyCloseCb(ch)
1241 let s:ch_close_ret = 'closed'
1242endfunc
1243
1244function s:test_close_callback(port)
1245 let handle = ch_open('localhost:' . a:port, s:chopt)
1246 if ch_status(handle) == "fail"
1247 call assert_false(1, "Can't open channel")
1248 return
1249 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001250 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001251
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001252 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001253 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001254 call assert_equal('closed', s:ch_close_ret)
1255endfunc
1256
1257func Test_close_callback()
1258 call ch_log('Test_close_callback()')
1259 call s:run_server('s:test_close_callback')
1260endfunc
1261
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001262function s:test_close_partial(port)
1263 let handle = ch_open('localhost:' . a:port, s:chopt)
1264 if ch_status(handle) == "fail"
1265 call assert_false(1, "Can't open channel")
1266 return
1267 endif
1268 let s:d = {}
1269 func s:d.closeCb(ch) dict
1270 let self.close_ret = 'closed'
1271 endfunc
1272 call ch_setoptions(handle, {'close_cb': s:d.closeCb})
1273
1274 call assert_equal('', ch_evalexpr(handle, 'close me'))
1275 call s:waitFor('"closed" == s:d.close_ret')
1276 call assert_equal('closed', s:d.close_ret)
1277 unlet s:d
1278endfunc
1279
1280func Test_close_partial()
1281 call ch_log('Test_close_partial()')
1282 call s:run_server('s:test_close_partial')
1283endfunc
1284
Bram Moolenaar80385682016-03-27 19:13:35 +02001285func Test_job_start_invalid()
1286 call assert_fails('call job_start($x)', 'E474:')
1287 call assert_fails('call job_start("")', 'E474:')
1288endfunc
1289
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001290" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001291func Test_partial_in_channel_cycle()
1292 let d = {}
1293 let d.a = function('string', [d])
1294 try
1295 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1296 catch
1297 call assert_exception('E901:')
1298 endtry
1299 unlet d
1300endfunc
1301
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001302func Test_using_freed_memory()
1303 let g:a = job_start(['ls'])
1304 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001305 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001306endfunc
1307
1308
1309
Bram Moolenaar9730f742016-02-28 19:50:51 +01001310" Uncomment this to see what happens, output is in src/testdir/channellog.
1311" call ch_logfile('channellog', 'w')