blob: fa494d31808cdfd035e570740fd6e0d757126bbb [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)
106 if eval(a:expr)
107 return
108 endif
109 sleep 10m
110 endfor
111endfunc
112
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100113func s:communicate(port)
114 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100115 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100116 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100117 return
118 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +0100119 if has('job')
120 " check that no job is handled correctly
121 call assert_equal('no process', string(ch_getjob(handle)))
122 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100123
124 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100125 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100126
127 " Request that triggers sending two ex commands. These will usually be
128 " handled before getting the response, but it's not guaranteed, thus wait a
129 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100130 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100131 call s:waitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100132 call assert_equal('added1', getline(line('$') - 1))
133 call assert_equal('added2', getline('$'))
134
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100135 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100136 call s:waitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100137 call assert_equal('added more', getline('$'))
138
Bram Moolenaara07fec92016-02-05 21:04:08 +0100139 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100140 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100141 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100142 if !exists('s:responseHandle')
143 call assert_false(1, 's:responseHandle was not set')
144 else
145 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100146 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100147 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100148 call assert_equal('got it', s:responseMsg)
149
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100150 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100151 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100152 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100153 if !exists('s:responseHandle')
154 call assert_false(1, 's:responseHandle was not set')
155 else
156 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100157 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100158 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100159 call assert_equal('got it', s:responseMsg)
160
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100161 " Collect garbage, tests that our handle isn't collected.
162 call garbagecollect()
163
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100164 " check setting options (without testing the effect)
165 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100166 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100167 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100168 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100169 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100170
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100171 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100172 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100173 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100174 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100175
176 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100178 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100179 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100180
Bram Moolenaar55fab432016-02-07 16:53:13 +0100181 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100183 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100184 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100185
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100186 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100188 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100189 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100190
Bram Moolenaarf4160862016-02-05 23:09:12 +0100191 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100192 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100193 call s:waitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194 call assert_equal('one', getline(line('$') - 2))
195 call assert_equal('two', getline(line('$') - 1))
196 call assert_equal('three', getline('$'))
197
198 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100199 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
200 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100201
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100202 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100203
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100204 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100205 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
206 let start = reltime()
207 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
208 let elapsed = reltime(start)
209 call assert_true(reltimefloat(elapsed) > 0.3)
210 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100211
212 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100213 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100214 let resp = ch_read(handle)
215 call assert_equal(type([]), type(resp))
216 call assert_equal(type(11), type(resp[0]))
217 call assert_equal('waited', resp[1])
218
Bram Moolenaard7ece102016-02-02 23:23:02 +0100219 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100221endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100222
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100223func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100224 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100225 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100226endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100227
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100228" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100229func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100230 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100233 return
234 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100236 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100237
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100238 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100239 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100240 call assert_false(1, "Can't open second channel")
241 return
242 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100243 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
244 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100245
246 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100247 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100248
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100249 call ch_close(newhandle)
250endfunc
251
252func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100253 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100254 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100255endfunc
256
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100257" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100258func s:server_crash(port)
259 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100260 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100261 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100262 return
263 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100264
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100265 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100266
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100267 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100268endfunc
269
270func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100271 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100272 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100273endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100274
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100275"""""""""
276
Bram Moolenaarf6157282016-02-10 21:07:14 +0100277let s:reply = ""
278func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100279 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280 let s:reply = a:msg
281endfunc
282
283func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100284 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100285 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100286 call assert_false(1, "Can't open channel")
287 return
288 endif
289
290 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100291 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100292 call s:waitFor('"we called you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100293 call assert_equal('we called you', s:reply)
294
295 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100296 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100297 call s:waitFor('"we did call you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100298 call assert_equal('we did call you', s:reply)
299endfunc
300
301func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100302 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100303 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100304 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100305 let s:chopt.callback = function('s:Handler')
306 call s:run_server('s:channel_handler')
307 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100308endfunc
309
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100310"""""""""
311
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100312let s:ch_reply = ''
313func s:ChHandler(chan, msg)
314 unlet s:ch_reply
315 let s:ch_reply = a:msg
316endfunc
317
318let s:zero_reply = ''
319func s:OneHandler(chan, msg)
320 unlet s:zero_reply
321 let s:zero_reply = a:msg
322endfunc
323
324func s:channel_zero(port)
325 let handle = ch_open('localhost:' . a:port, s:chopt)
326 if ch_status(handle) == "fail"
327 call assert_false(1, "Can't open channel")
328 return
329 endif
330
331 " Check that eval works.
332 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
333
334 " Check that eval works if a zero id message is sent back.
335 let s:ch_reply = ''
336 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100337 if s:has_handler
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100338 call s:waitFor('"zero index" == s:ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100339 call assert_equal('zero index', s:ch_reply)
340 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100341 sleep 20m
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100342 call assert_equal('', s:ch_reply)
343 endif
344
345 " Check that handler works if a zero id message is sent back.
346 let s:ch_reply = ''
347 let s:zero_reply = ''
348 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100349 call s:waitFor('"sent zero" == s:zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100350 if s:has_handler
351 call assert_equal('zero index', s:ch_reply)
352 else
353 call assert_equal('', s:ch_reply)
354 endif
355 call assert_equal('sent zero', s:zero_reply)
356endfunc
357
358func Test_zero_reply()
359 call ch_log('Test_zero_reply()')
360 " Run with channel handler
361 let s:has_handler = 1
362 let s:chopt.callback = 's:ChHandler'
363 call s:run_server('s:channel_zero')
364 unlet s:chopt.callback
365
366 " Run without channel handler
367 let s:has_handler = 0
368 call s:run_server('s:channel_zero')
369endfunc
370
371"""""""""
372
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100373let s:reply1 = ""
374func s:HandleRaw1(chan, msg)
375 unlet s:reply1
376 let s:reply1 = a:msg
377endfunc
378
379let s:reply2 = ""
380func s:HandleRaw2(chan, msg)
381 unlet s:reply2
382 let s:reply2 = a:msg
383endfunc
384
385let s:reply3 = ""
386func s:HandleRaw3(chan, msg)
387 unlet s:reply3
388 let s:reply3 = a:msg
389endfunc
390
391func s:raw_one_time_callback(port)
392 let handle = ch_open('localhost:' . a:port, s:chopt)
393 if ch_status(handle) == "fail"
394 call assert_false(1, "Can't open channel")
395 return
396 endif
397 call ch_setoptions(handle, {'mode': 'raw'})
398
399 " The message are sent raw, we do our own JSON strings here.
400 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100401 call s:waitFor('s:reply1 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100402 call assert_equal("[1, \"got it\"]", s:reply1)
403 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
404 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100405 call s:waitFor('s:reply2 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100406 call assert_equal("[2, \"something\"]", s:reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100407 " wait for the 200 msec delayed reply
408 call s:waitFor('s:reply3 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100409 call assert_equal("[3, \"waited\"]", s:reply3)
410endfunc
411
412func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100413 call ch_log('Test_raw_one_time_callback()')
414 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100415endfunc
416
417"""""""""
418
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100419" Test that trying to connect to a non-existing port fails quickly.
420func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100421 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100422 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100423 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100424 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100425 " Oops, port does exists.
426 call ch_close(handle)
427 else
428 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100429 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100430 endif
431
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100432 " We intend to use a socket that doesn't exist and wait for half a second
433 " before giving up. If the socket does exist it can fail in various ways.
434 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100435 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100436 try
437 let handle = ch_open('localhost:9867', {'waittime': 500})
438 if ch_status(handle) != "fail"
439 " Oops, port does exists.
440 call ch_close(handle)
441 else
442 " Failed connection should wait about 500 msec.
443 let elapsed = reltime(start)
444 call assert_true(reltimefloat(elapsed) > 0.3)
445 call assert_true(reltimefloat(elapsed) < 1.0)
446 endif
447 catch
448 if v:exception !~ 'Connection reset by peer'
449 call assert_false(1, "Caught exception: " . v:exception)
450 endif
451 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100452endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100453
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100454"""""""""
455
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100456func Test_raw_pipe()
457 if !has('job')
458 return
459 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100460 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100461 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
462 call assert_equal("run", job_status(job))
463 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100464 " For a change use the job where a channel is expected.
465 call ch_sendraw(job, "echo something\n")
466 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100467 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
468
Bram Moolenaar151f6562016-03-07 21:19:38 +0100469 call ch_sendraw(job, "double this\n")
470 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
472
Bram Moolenaar151f6562016-03-07 21:19:38 +0100473 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100474 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
475 finally
476 call job_stop(job)
477 endtry
478endfunc
479
480func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100481 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100482 return
483 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100484 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100485 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100486 call assert_equal("run", job_status(job))
487 try
488 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100489 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100490 call assert_equal("something", ch_readraw(handle))
491
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100492 call ch_sendraw(handle, "echoerr wrong\n")
493 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
494
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100495 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100496 call assert_equal("this", ch_readraw(handle))
497 call assert_equal("AND this", ch_readraw(handle))
498
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100499 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100500 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100501 finally
502 call job_stop(job)
503 endtry
504endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100505
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100506func Test_nl_err_to_out_pipe()
507 if !has('job')
508 return
509 endif
510 call ch_log('Test_nl_err_to_out_pipe()')
511 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
512 call assert_equal("run", job_status(job))
513 try
514 let handle = job_getchannel(job)
515 call ch_sendraw(handle, "echo something\n")
516 call assert_equal("something", ch_readraw(handle))
517
518 call ch_sendraw(handle, "echoerr wrong\n")
519 call assert_equal("wrong", ch_readraw(handle))
520 finally
521 call job_stop(job)
522 endtry
523endfunc
524
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100525func Test_nl_read_file()
526 if !has('job')
527 return
528 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100529 call ch_log('Test_nl_read_file()')
530 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
531 let job = job_start(s:python . " test_channel_pipe.py",
532 \ {'in-io': 'file', 'in-name': 'Xinput'})
533 call assert_equal("run", job_status(job))
534 try
535 let handle = job_getchannel(job)
536 call assert_equal("something", ch_readraw(handle))
537 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
538 call assert_equal("this", ch_readraw(handle))
539 call assert_equal("AND this", ch_readraw(handle))
540 finally
541 call job_stop(job)
542 call delete('Xinput')
543 endtry
544endfunc
545
Bram Moolenaare98d1212016-03-08 15:37:41 +0100546func Test_nl_write_out_file()
547 if !has('job')
548 return
549 endif
550 " TODO: make this work for MS-Windows
551 if !has('unix')
552 return
553 endif
554 call ch_log('Test_nl_write_out_file()')
555 let job = job_start(s:python . " test_channel_pipe.py",
556 \ {'out-io': 'file', 'out-name': 'Xoutput'})
557 call assert_equal("run", job_status(job))
558 try
559 let handle = job_getchannel(job)
560 call ch_sendraw(handle, "echo line one\n")
561 call ch_sendraw(handle, "echo line two\n")
562 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100563 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100564 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
565 finally
566 call job_stop(job)
567 call delete('Xoutput')
568 endtry
569endfunc
570
571func Test_nl_write_err_file()
572 if !has('job')
573 return
574 endif
575 " TODO: make this work for MS-Windows
576 if !has('unix')
577 return
578 endif
579 call ch_log('Test_nl_write_err_file()')
580 let job = job_start(s:python . " test_channel_pipe.py",
581 \ {'err-io': 'file', 'err-name': 'Xoutput'})
582 call assert_equal("run", job_status(job))
583 try
584 let handle = job_getchannel(job)
585 call ch_sendraw(handle, "echoerr line one\n")
586 call ch_sendraw(handle, "echoerr line two\n")
587 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100588 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100589 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
590 finally
591 call job_stop(job)
592 call delete('Xoutput')
593 endtry
594endfunc
595
596func Test_nl_write_both_file()
597 if !has('job')
598 return
599 endif
600 " TODO: make this work for MS-Windows
601 if !has('unix')
602 return
603 endif
604 call ch_log('Test_nl_write_both_file()')
605 let job = job_start(s:python . " test_channel_pipe.py",
606 \ {'out-io': 'file', 'out-name': 'Xoutput', 'err-io': 'out'})
607 call assert_equal("run", job_status(job))
608 try
609 let handle = job_getchannel(job)
610 call ch_sendraw(handle, "echoerr line one\n")
611 call ch_sendraw(handle, "echo line two\n")
612 call ch_sendraw(handle, "double this\n")
613 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100614 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100615 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
616 finally
617 call job_stop(job)
618 call delete('Xoutput')
619 endtry
620endfunc
621
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100622func Test_pipe_to_buffer()
623 if !has('job')
624 return
625 endif
626 call ch_log('Test_pipe_to_buffer()')
627 let job = job_start(s:python . " test_channel_pipe.py",
628 \ {'out-io': 'buffer', 'out-name': 'pipe-output'})
629 call assert_equal("run", job_status(job))
630 try
631 let handle = job_getchannel(job)
632 call ch_sendraw(handle, "echo line one\n")
633 call ch_sendraw(handle, "echo line two\n")
634 call ch_sendraw(handle, "double this\n")
635 call ch_sendraw(handle, "quit\n")
636 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100637 call s:waitFor('line("$") >= 6')
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100638 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
639 bwipe!
640 finally
641 call job_stop(job)
642 endtry
643endfunc
644
Bram Moolenaar014069a2016-03-03 22:51:40 +0100645func Test_pipe_from_buffer()
646 if !has('job')
647 return
648 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100649 call ch_log('Test_pipe_from_buffer()')
650
651 sp pipe-input
652 call setline(1, ['echo one', 'echo two', 'echo three'])
653
654 let job = job_start(s:python . " test_channel_pipe.py",
655 \ {'in-io': 'buffer', 'in-name': 'pipe-input'})
656 call assert_equal("run", job_status(job))
657 try
658 let handle = job_getchannel(job)
659 call assert_equal('one', ch_read(handle))
660 call assert_equal('two', ch_read(handle))
661 call assert_equal('three', ch_read(handle))
662 bwipe!
663 finally
664 call job_stop(job)
665 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100666endfunc
667
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100668func Test_pipe_to_nameless_buffer()
669 if !has('job')
670 return
671 endif
672 call ch_log('Test_pipe_to_nameless_buffer()')
673 let job = job_start(s:python . " test_channel_pipe.py",
674 \ {'out-io': 'buffer'})
675 call assert_equal("run", job_status(job))
676 try
677 let handle = job_getchannel(job)
678 call ch_sendraw(handle, "echo line one\n")
679 call ch_sendraw(handle, "echo line two\n")
680 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100681 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100682 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
683 bwipe!
684 finally
685 call job_stop(job)
686 endtry
687endfunc
688
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100689func Test_pipe_to_buffer_json()
690 if !has('job')
691 return
692 endif
693 call ch_log('Test_pipe_to_buffer_json()')
694 let job = job_start(s:python . " test_channel_pipe.py",
695 \ {'out-io': 'buffer', 'out-mode': 'json'})
696 call assert_equal("run", job_status(job))
697 try
698 let handle = job_getchannel(job)
699 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
700 call ch_sendraw(handle, "echo [-2, 12.34]\n")
701 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100702 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100703 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
704 bwipe!
705 finally
706 call job_stop(job)
707 endtry
708endfunc
709
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100710" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100711func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100712 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100713 if getline(line('$') - a:offset) == a:line
714 break
715 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100716 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100717 endfor
718endfunc
719
720func Test_pipe_io_two_buffers()
721 if !has('job')
722 return
723 endif
724 call ch_log('Test_pipe_io_two_buffers()')
725
726 " Create two buffers, one to read from and one to write to.
727 split pipe-output
728 set buftype=nofile
729 split pipe-input
730 set buftype=nofile
731
732 let job = job_start(s:python . " test_channel_pipe.py",
733 \ {'in-io': 'buffer', 'in-name': 'pipe-input', 'in-top': 0,
734 \ 'out-io': 'buffer', 'out-name': 'pipe-output'})
735 call assert_equal("run", job_status(job))
736 try
737 exe "normal Gaecho hello\<CR>"
738 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100739 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100740 call assert_equal('hello', getline('$'))
741
742 exe bufwinnr('pipe-input') . "wincmd w"
743 exe "normal Gadouble this\<CR>"
744 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100745 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100746 call assert_equal('this', getline(line('$') - 1))
747 call assert_equal('AND this', getline('$'))
748
749 bwipe!
750 exe bufwinnr('pipe-input') . "wincmd w"
751 bwipe!
752 finally
753 call job_stop(job)
754 endtry
755endfunc
756
757func Test_pipe_io_one_buffer()
758 if !has('job')
759 return
760 endif
761 call ch_log('Test_pipe_io_one_buffer()')
762
763 " Create one buffer to read from and to write to.
764 split pipe-io
765 set buftype=nofile
766
767 let job = job_start(s:python . " test_channel_pipe.py",
768 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0,
769 \ 'out-io': 'buffer', 'out-name': 'pipe-io'})
770 call assert_equal("run", job_status(job))
771 try
772 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100773 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100774 call assert_equal('hello', getline(line('$') - 1))
775
776 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100777 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100778 call assert_equal('this', getline(line('$') - 2))
779 call assert_equal('AND this', getline(line('$') - 1))
780
781 bwipe!
782 finally
783 call job_stop(job)
784 endtry
785endfunc
786
Bram Moolenaard46ae142016-02-16 13:33:52 +0100787""""""""""
788
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100789let s:unletResponse = ''
790func s:UnletHandler(handle, msg)
791 let s:unletResponse = a:msg
792 unlet s:channelfd
793endfunc
794
795" Test that "unlet handle" in a handler doesn't crash Vim.
796func s:unlet_handle(port)
797 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100798 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100799 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100800 call assert_equal('what?', s:unletResponse)
801endfunc
802
803func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100805 call s:run_server('s:unlet_handle')
806endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100807
Bram Moolenaard46ae142016-02-16 13:33:52 +0100808""""""""""
809
810let s:unletResponse = ''
811func s:CloseHandler(handle, msg)
812 let s:unletResponse = a:msg
813 call ch_close(s:channelfd)
814endfunc
815
816" Test that "unlet handle" in a handler doesn't crash Vim.
817func s:close_handle(port)
818 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100819 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100820 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100821 call assert_equal('what?', s:unletResponse)
822endfunc
823
824func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100825 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100826 call s:run_server('s:close_handle')
827endfunc
828
829""""""""""
830
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100831func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100832 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100833 silent! let ch = ch_open("noserver")
834 echo ch
835 let d = ch
836endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100837
838""""""""""
839
840func s:open_delay(port)
841 " Wait up to a second for the port to open.
842 let s:chopt.waittime = 1000
843 let channel = ch_open('localhost:' . a:port, s:chopt)
844 unlet s:chopt.waittime
845 if ch_status(channel) == "fail"
846 call assert_false(1, "Can't open channel")
847 return
848 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100849 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100850 call ch_close(channel)
851endfunc
852
853func Test_open_delay()
854 call ch_log('Test_open_delay()')
855 " The server will wait half a second before creating the port.
856 call s:run_server('s:open_delay', 'delay')
857endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +0100858
859"""""""""
860
861function MyFunction(a,b,c)
862 let s:call_ret = [a:a, a:b, a:c]
863endfunc
864
865function s:test_call(port)
866 let handle = ch_open('localhost:' . a:port, s:chopt)
867 if ch_status(handle) == "fail"
868 call assert_false(1, "Can't open channel")
869 return
870 endif
871
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100872 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100873 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100874 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +0100875 call assert_equal([1, 2, 3], s:call_ret)
876endfunc
877
878func Test_call()
879 call ch_log('Test_call()')
880 call s:run_server('s:test_call')
881endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100882
883"""""""""
884
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100885let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100886function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100887 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100888endfunc
889
890function s:test_exit_callback(port)
891 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
892 let s:exit_job = s:job
893endfunc
894
895func Test_exit_callback()
896 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +0100897 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100898 call s:run_server('s:test_exit_callback')
899
Bram Moolenaar9730f742016-02-28 19:50:51 +0100900 " wait up to a second for the job to exit
901 for i in range(100)
902 if s:job_exit_ret == 'done'
903 break
904 endif
905 sleep 10m
906 " calling job_status() triggers the callback
907 call job_status(s:exit_job)
908 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100909
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100910 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +0100911 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +0100912 endif
913endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100914
915"""""""""
916
917let s:ch_close_ret = 'alive'
918function MyCloseCb(ch)
919 let s:ch_close_ret = 'closed'
920endfunc
921
922function s:test_close_callback(port)
923 let handle = ch_open('localhost:' . a:port, s:chopt)
924 if ch_status(handle) == "fail"
925 call assert_false(1, "Can't open channel")
926 return
927 endif
928 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
929
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100930 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100931 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100932 call assert_equal('closed', s:ch_close_ret)
933endfunc
934
935func Test_close_callback()
936 call ch_log('Test_close_callback()')
937 call s:run_server('s:test_close_callback')
938endfunc
939
Bram Moolenaar9730f742016-02-28 19:50:51 +0100940" Uncomment this to see what happens, output is in src/testdir/channellog.
941" call ch_logfile('channellog', 'w')