blob: ed3fa6d4664d4c6a37b40f7f48922e06e0a71abb [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 Moolenaar9f5842e2016-05-29 16:17:08 +0200679func Run_test_pipe_to_buffer(use_name, nomod)
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
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200694 if a:nomod
695 let options['out_modifiable'] = 0
696 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100697 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100698 call assert_equal("run", job_status(job))
699 try
700 let handle = job_getchannel(job)
701 call ch_sendraw(handle, "echo line one\n")
702 call ch_sendraw(handle, "echo line two\n")
703 call ch_sendraw(handle, "double this\n")
704 call ch_sendraw(handle, "quit\n")
705 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100706 call s:waitFor('line("$") >= 6')
Bram Moolenaar6a063632016-03-21 23:18:54 +0100707 if getline('$') == 'DETACH'
708 $del
709 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100710 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200711 if a:nomod
712 call assert_equal(0, &modifiable)
713 else
714 call assert_equal(1, &modifiable)
715 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100716 bwipe!
717 finally
718 call job_stop(job)
719 endtry
720endfunc
721
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100722func Test_pipe_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200723 call Run_test_pipe_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100724endfunc
725
726func Test_pipe_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200727 call Run_test_pipe_to_buffer(0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100728endfunc
729
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200730func Test_pipe_to_buffer_name_nomod()
731 call Run_test_pipe_to_buffer(1, 1)
732endfunc
733
734func Run_test_pipe_err_to_buffer(use_name, nomod)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100735 if !has('job')
736 return
737 endif
738 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100739 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100740 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100741 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100742 let firstline = 'Reading from channel error...'
743 else
744 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100745 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100746 quit
747 let firstline = ''
748 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200749 if a:nomod
750 let options['err_modifiable'] = 0
751 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100752 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100753 call assert_equal("run", job_status(job))
754 try
755 let handle = job_getchannel(job)
756 call ch_sendraw(handle, "echoerr line one\n")
757 call ch_sendraw(handle, "echoerr line two\n")
758 call ch_sendraw(handle, "doubleerr this\n")
759 call ch_sendraw(handle, "quit\n")
760 sp pipe-err
761 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100762 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200763 if a:nomod
764 call assert_equal(0, &modifiable)
765 else
766 call assert_equal(1, &modifiable)
767 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100768 bwipe!
769 finally
770 call job_stop(job)
771 endtry
772endfunc
773
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100774func Test_pipe_err_to_buffer_name()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200775 call Run_test_pipe_err_to_buffer(1, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100776endfunc
777
778func Test_pipe_err_to_buffer_nr()
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200779 call Run_test_pipe_err_to_buffer(0, 0)
780endfunc
781
782func Test_pipe_err_to_buffer_name_nomod()
783 call Run_test_pipe_err_to_buffer(1, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100784endfunc
785
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100786func Test_pipe_both_to_buffer()
787 if !has('job')
788 return
789 endif
790 call ch_log('Test_pipe_both_to_buffer()')
791 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100792 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100793 call assert_equal("run", job_status(job))
794 try
795 let handle = job_getchannel(job)
796 call ch_sendraw(handle, "echo line one\n")
797 call ch_sendraw(handle, "echoerr line two\n")
798 call ch_sendraw(handle, "double this\n")
799 call ch_sendraw(handle, "doubleerr that\n")
800 call ch_sendraw(handle, "quit\n")
801 sp pipe-err
802 call s:waitFor('line("$") >= 7')
803 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
804 bwipe!
805 finally
806 call job_stop(job)
807 endtry
808endfunc
809
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100810func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100811 if !has('job')
812 return
813 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100814 call ch_log('Test_pipe_from_buffer()')
815
816 sp pipe-input
817 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200818 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100819 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100820 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100821 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100822 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100823 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100824
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100825 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100826 call assert_equal("run", job_status(job))
827 try
828 let handle = job_getchannel(job)
829 call assert_equal('one', ch_read(handle))
830 call assert_equal('two', ch_read(handle))
831 call assert_equal('three', ch_read(handle))
832 bwipe!
833 finally
834 call job_stop(job)
835 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100836endfunc
837
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100838func Test_pipe_from_buffer_name()
839 call Run_test_pipe_from_buffer(1)
840endfunc
841
842func Test_pipe_from_buffer_nr()
843 call Run_test_pipe_from_buffer(0)
844endfunc
845
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100846func Test_pipe_to_nameless_buffer()
847 if !has('job')
848 return
849 endif
850 call ch_log('Test_pipe_to_nameless_buffer()')
851 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100852 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100853 call assert_equal("run", job_status(job))
854 try
855 let handle = job_getchannel(job)
856 call ch_sendraw(handle, "echo line one\n")
857 call ch_sendraw(handle, "echo line two\n")
858 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100859 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100860 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
861 bwipe!
862 finally
863 call job_stop(job)
864 endtry
865endfunc
866
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100867func Test_pipe_to_buffer_json()
868 if !has('job')
869 return
870 endif
871 call ch_log('Test_pipe_to_buffer_json()')
872 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100873 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100874 call assert_equal("run", job_status(job))
875 try
876 let handle = job_getchannel(job)
877 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
878 call ch_sendraw(handle, "echo [-2, 12.34]\n")
879 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100880 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100881 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
882 bwipe!
883 finally
884 call job_stop(job)
885 endtry
886endfunc
887
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100888" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100889func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100890 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100891 if getline(line('$') - a:offset) == a:line
892 break
893 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100894 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100895 endfor
896endfunc
897
898func Test_pipe_io_two_buffers()
899 if !has('job')
900 return
901 endif
902 call ch_log('Test_pipe_io_two_buffers()')
903
904 " Create two buffers, one to read from and one to write to.
905 split pipe-output
906 set buftype=nofile
907 split pipe-input
908 set buftype=nofile
909
910 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100911 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200912 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
913 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100914 call assert_equal("run", job_status(job))
915 try
916 exe "normal Gaecho hello\<CR>"
917 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100918 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100919 call assert_equal('hello', getline('$'))
920
921 exe bufwinnr('pipe-input') . "wincmd w"
922 exe "normal Gadouble this\<CR>"
923 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100924 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100925 call assert_equal('this', getline(line('$') - 1))
926 call assert_equal('AND this', getline('$'))
927
928 bwipe!
929 exe bufwinnr('pipe-input') . "wincmd w"
930 bwipe!
931 finally
932 call job_stop(job)
933 endtry
934endfunc
935
936func Test_pipe_io_one_buffer()
937 if !has('job')
938 return
939 endif
940 call ch_log('Test_pipe_io_one_buffer()')
941
942 " Create one buffer to read from and to write to.
943 split pipe-io
944 set buftype=nofile
945
946 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100947 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200948 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
949 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100950 call assert_equal("run", job_status(job))
951 try
952 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100953 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100954 call assert_equal('hello', getline(line('$') - 1))
955
956 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100957 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100958 call assert_equal('this', getline(line('$') - 2))
959 call assert_equal('AND this', getline(line('$') - 1))
960
961 bwipe!
962 finally
963 call job_stop(job)
964 endtry
965endfunc
966
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100967func Test_pipe_null()
968 if !has('job')
969 return
970 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100971 call ch_log('Test_pipe_null()')
972
973 " We cannot check that no I/O works, we only check that the job starts
974 " properly.
975 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100976 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100977 call assert_equal("run", job_status(job))
978 try
979 call assert_equal('something', ch_read(job))
980 finally
981 call job_stop(job)
982 endtry
983
984 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100985 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100986 call assert_equal("run", job_status(job))
987 try
988 call assert_equal('err-out', ch_read(job, {"part": "err"}))
989 finally
990 call job_stop(job)
991 endtry
992
993 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100994 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100995 call assert_equal("run", job_status(job))
996 try
997 call assert_equal('something', ch_read(job))
998 finally
999 call job_stop(job)
1000 endtry
1001
1002 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001003 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001004 call assert_equal("run", job_status(job))
1005 call job_stop(job)
1006
1007 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001008 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001009 call assert_equal("run", job_status(job))
1010 call assert_equal('channel fail', string(job_getchannel(job)))
1011 call assert_equal('fail', ch_status(job))
1012 call job_stop(job)
1013endfunc
1014
Bram Moolenaarde279892016-03-11 22:19:44 +01001015func Test_reuse_channel()
1016 if !has('job')
1017 return
1018 endif
1019 call ch_log('Test_reuse_channel()')
1020
1021 let job = job_start(s:python . " test_channel_pipe.py")
1022 call assert_equal("run", job_status(job))
1023 let handle = job_getchannel(job)
1024 try
1025 call ch_sendraw(handle, "echo something\n")
1026 call assert_equal("something", ch_readraw(handle))
1027 finally
1028 call job_stop(job)
1029 endtry
1030
1031 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1032 call assert_equal("run", job_status(job))
1033 let handle = job_getchannel(job)
1034 try
1035 call ch_sendraw(handle, "echo again\n")
1036 call assert_equal("again", ch_readraw(handle))
1037 finally
1038 call job_stop(job)
1039 endtry
1040endfunc
1041
Bram Moolenaar75f72652016-03-20 22:16:56 +01001042func Test_out_cb()
1043 if !has('job')
1044 return
1045 endif
1046 call ch_log('Test_out_cb()')
1047
1048 let dict = {'thisis': 'dict: '}
1049 func dict.outHandler(chan, msg) dict
1050 let s:outmsg = self.thisis . a:msg
1051 endfunc
1052 func dict.errHandler(chan, msg) dict
1053 let s:errmsg = self.thisis . a:msg
1054 endfunc
1055 let job = job_start(s:python . " test_channel_pipe.py",
1056 \ {'out_cb': dict.outHandler,
1057 \ 'out_mode': 'json',
1058 \ 'err_cb': dict.errHandler,
1059 \ 'err_mode': 'json'})
1060 call assert_equal("run", job_status(job))
1061 try
1062 let s:outmsg = ''
1063 let s:errmsg = ''
1064 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1065 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
1066 call s:waitFor('s:outmsg != ""')
1067 call assert_equal("dict: hello", s:outmsg)
1068 call s:waitFor('s:errmsg != ""')
1069 call assert_equal("dict: there", s:errmsg)
1070 finally
1071 call job_stop(job)
1072 endtry
1073endfunc
1074
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001075func Test_out_close_cb()
1076 if !has('job')
1077 return
1078 endif
1079 call ch_log('Test_out_close_cb()')
1080
1081 let s:counter = 1
Bram Moolenaard75263c2016-04-30 16:07:23 +02001082 let s:msg1 = ''
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001083 let s:closemsg = 0
1084 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001085 if s:counter == 1
1086 let s:msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001087 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001088 let s:counter += 1
1089 endfunc
1090 func! CloseHandler(chan)
1091 let s:closemsg = s:counter
1092 let s:counter += 1
1093 endfunc
1094 let job = job_start(s:python . " test_channel_pipe.py quit now",
1095 \ {'out_cb': 'OutHandler',
1096 \ 'close_cb': 'CloseHandler'})
1097 call assert_equal("run", job_status(job))
1098 try
Bram Moolenaar715d2852016-04-30 17:06:31 +02001099 call s:waitFor('s:closemsg != 0 && s:msg1 != ""')
Bram Moolenaard75263c2016-04-30 16:07:23 +02001100 call assert_equal('quit', s:msg1)
Bram Moolenaar715d2852016-04-30 17:06:31 +02001101 call assert_equal(2, s:closemsg)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001102 finally
1103 call job_stop(job)
1104 delfunc OutHandler
1105 delfunc CloseHandler
1106 endtry
1107endfunc
1108
Bram Moolenaar437905c2016-04-26 19:01:05 +02001109func Test_read_in_close_cb()
1110 if !has('job')
1111 return
1112 endif
1113 call ch_log('Test_read_in_close_cb()')
1114
1115 let s:received = ''
1116 func! CloseHandler(chan)
1117 let s:received = ch_read(a:chan)
1118 endfunc
1119 let job = job_start(s:python . " test_channel_pipe.py quit now",
1120 \ {'close_cb': 'CloseHandler'})
1121 call assert_equal("run", job_status(job))
1122 try
1123 call s:waitFor('s:received != ""')
1124 call assert_equal('quit', s:received)
1125 finally
1126 call job_stop(job)
1127 delfunc CloseHandler
1128 endtry
1129endfunc
1130
Bram Moolenaard46ae142016-02-16 13:33:52 +01001131""""""""""
1132
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001133let s:unletResponse = ''
1134func s:UnletHandler(handle, msg)
1135 let s:unletResponse = a:msg
1136 unlet s:channelfd
1137endfunc
1138
1139" Test that "unlet handle" in a handler doesn't crash Vim.
1140func s:unlet_handle(port)
1141 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001142 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001143 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001144 call assert_equal('what?', s:unletResponse)
1145endfunc
1146
1147func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001148 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001149 call s:run_server('s:unlet_handle')
1150endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001151
Bram Moolenaard46ae142016-02-16 13:33:52 +01001152""""""""""
1153
1154let s:unletResponse = ''
1155func s:CloseHandler(handle, msg)
1156 let s:unletResponse = a:msg
1157 call ch_close(s:channelfd)
1158endfunc
1159
1160" Test that "unlet handle" in a handler doesn't crash Vim.
1161func s:close_handle(port)
1162 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001163 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001164 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001165 call assert_equal('what?', s:unletResponse)
1166endfunc
1167
1168func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001169 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001170 call s:run_server('s:close_handle')
1171endfunc
1172
1173""""""""""
1174
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001175func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001176 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001177 silent! let ch = ch_open("noserver")
1178 echo ch
1179 let d = ch
1180endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001181
1182""""""""""
1183
1184func s:open_delay(port)
1185 " Wait up to a second for the port to open.
1186 let s:chopt.waittime = 1000
1187 let channel = ch_open('localhost:' . a:port, s:chopt)
1188 unlet s:chopt.waittime
1189 if ch_status(channel) == "fail"
1190 call assert_false(1, "Can't open channel")
1191 return
1192 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001193 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001194 call ch_close(channel)
1195endfunc
1196
1197func Test_open_delay()
1198 call ch_log('Test_open_delay()')
1199 " The server will wait half a second before creating the port.
1200 call s:run_server('s:open_delay', 'delay')
1201endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001202
1203"""""""""
1204
1205function MyFunction(a,b,c)
1206 let s:call_ret = [a:a, a:b, a:c]
1207endfunc
1208
1209function s:test_call(port)
1210 let handle = ch_open('localhost:' . a:port, s:chopt)
1211 if ch_status(handle) == "fail"
1212 call assert_false(1, "Can't open channel")
1213 return
1214 endif
1215
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001216 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001217 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001218 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001219 call assert_equal([1, 2, 3], s:call_ret)
1220endfunc
1221
1222func Test_call()
1223 call ch_log('Test_call()')
1224 call s:run_server('s:test_call')
1225endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001226
1227"""""""""
1228
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001229let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001230function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001231 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001232endfunc
1233
1234function s:test_exit_callback(port)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001235 call job_setoptions(s:job, {'exit_cb': 'MyExitCb'})
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001236 let s:exit_job = s:job
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001237 call assert_equal('MyExitCb', job_info(s:job)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001238endfunc
1239
1240func Test_exit_callback()
1241 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001242 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001243 call s:run_server('s:test_exit_callback')
1244
Bram Moolenaar9730f742016-02-28 19:50:51 +01001245 " wait up to a second for the job to exit
1246 for i in range(100)
1247 if s:job_exit_ret == 'done'
1248 break
1249 endif
1250 sleep 10m
1251 " calling job_status() triggers the callback
1252 call job_status(s:exit_job)
1253 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001254
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001255 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar8950a562016-03-12 15:22:55 +01001256 call assert_equal('dead', job_info(s:exit_job).status)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001257 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001258 endif
1259endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001260
1261"""""""""
1262
1263let s:ch_close_ret = 'alive'
1264function MyCloseCb(ch)
1265 let s:ch_close_ret = 'closed'
1266endfunc
1267
1268function s:test_close_callback(port)
1269 let handle = ch_open('localhost:' . a:port, s:chopt)
1270 if ch_status(handle) == "fail"
1271 call assert_false(1, "Can't open channel")
1272 return
1273 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001274 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001275
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001276 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001277 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001278 call assert_equal('closed', s:ch_close_ret)
1279endfunc
1280
1281func Test_close_callback()
1282 call ch_log('Test_close_callback()')
1283 call s:run_server('s:test_close_callback')
1284endfunc
1285
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001286function s:test_close_partial(port)
1287 let handle = ch_open('localhost:' . a:port, s:chopt)
1288 if ch_status(handle) == "fail"
1289 call assert_false(1, "Can't open channel")
1290 return
1291 endif
1292 let s:d = {}
1293 func s:d.closeCb(ch) dict
1294 let self.close_ret = 'closed'
1295 endfunc
1296 call ch_setoptions(handle, {'close_cb': s:d.closeCb})
1297
1298 call assert_equal('', ch_evalexpr(handle, 'close me'))
1299 call s:waitFor('"closed" == s:d.close_ret')
1300 call assert_equal('closed', s:d.close_ret)
1301 unlet s:d
1302endfunc
1303
1304func Test_close_partial()
1305 call ch_log('Test_close_partial()')
1306 call s:run_server('s:test_close_partial')
1307endfunc
1308
Bram Moolenaar80385682016-03-27 19:13:35 +02001309func Test_job_start_invalid()
1310 call assert_fails('call job_start($x)', 'E474:')
1311 call assert_fails('call job_start("")', 'E474:')
1312endfunc
1313
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001314" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001315func Test_partial_in_channel_cycle()
1316 let d = {}
1317 let d.a = function('string', [d])
1318 try
1319 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1320 catch
1321 call assert_exception('E901:')
1322 endtry
1323 unlet d
1324endfunc
1325
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001326func Test_using_freed_memory()
1327 let g:a = job_start(['ls'])
1328 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001329 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001330endfunc
1331
1332
1333
Bram Moolenaar9730f742016-02-28 19:50:51 +01001334" Uncomment this to see what happens, output is in src/testdir/channellog.
1335" call ch_logfile('channellog', 'w')