blob: a2d41252c3270f0bfdca10c8490f610ccf1ebc4d [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')
123 " check that no job is handled correctly
124 call assert_equal('no process', string(ch_getjob(handle)))
125 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +0100126
127 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100128 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100129
130 " Request that triggers sending two ex commands. These will usually be
131 " handled before getting the response, but it's not guaranteed, thus wait a
132 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100133 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100134 call s:waitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100135 call assert_equal('added1', getline(line('$') - 1))
136 call assert_equal('added2', getline('$'))
137
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100138 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100139 call s:waitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100140 call assert_equal('added more', getline('$'))
141
Bram Moolenaara07fec92016-02-05 21:04:08 +0100142 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100143 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100144 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100145 if !exists('s:responseHandle')
146 call assert_false(1, 's:responseHandle was not set')
147 else
148 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100149 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100150 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100151 call assert_equal('got it', s:responseMsg)
152
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100153 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100154 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100155 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100156 if !exists('s:responseHandle')
157 call assert_false(1, 's:responseHandle was not set')
158 else
159 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100160 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100162 call assert_equal('got it', s:responseMsg)
163
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100164 " Collect garbage, tests that our handle isn't collected.
165 call garbagecollect()
166
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100167 " check setting options (without testing the effect)
168 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100169 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100170 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100171 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100172 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100173
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100174 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100175 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100176 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100177 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100178
179 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100181 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100182 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100183
Bram Moolenaar55fab432016-02-07 16:53:13 +0100184 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100186 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100188
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100189 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100191 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100192 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100193
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100195 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100196 call s:waitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100197 call assert_equal('one', getline(line('$') - 2))
198 call assert_equal('two', getline(line('$') - 1))
199 call assert_equal('three', getline('$'))
200
201 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100202 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
203 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100204
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100205 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100206
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100207 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100208 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
209 let start = reltime()
210 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
211 let elapsed = reltime(start)
212 call assert_true(reltimefloat(elapsed) > 0.3)
213 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100214
215 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100216 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100217 let resp = ch_read(handle)
218 call assert_equal(type([]), type(resp))
219 call assert_equal(type(11), type(resp[0]))
220 call assert_equal('waited', resp[1])
221
Bram Moolenaard7ece102016-02-02 23:23:02 +0100222 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100223 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100224endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100225
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100227 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100228 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100229endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100230
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100231" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100232func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100233 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100234 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100235 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100236 return
237 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100238
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100240
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100241 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100242 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100243 call assert_false(1, "Can't open second channel")
244 return
245 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100246 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
247 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100248
249 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100250 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100251
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100252 call ch_close(newhandle)
253endfunc
254
255func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100256 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100257 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100258endfunc
259
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100260" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100261func s:server_crash(port)
262 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100263 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100264 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100265 return
266 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100267
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100268 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100269
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100270 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100271endfunc
272
273func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100274 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100275 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100276endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100277
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100278"""""""""
279
Bram Moolenaarf6157282016-02-10 21:07:14 +0100280let s:reply = ""
281func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100282 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100283 let s:reply = a:msg
284endfunc
285
286func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100287 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100288 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100289 call assert_false(1, "Can't open channel")
290 return
291 endif
292
293 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100294 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100295 call s:waitFor('"we called you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296 call assert_equal('we called you', s:reply)
297
298 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100299 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100300 call s:waitFor('"we did call you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100301 call assert_equal('we did call you', s:reply)
302endfunc
303
304func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100305 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100306 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100307 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100308 let s:chopt.callback = function('s:Handler')
309 call s:run_server('s:channel_handler')
310 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100311endfunc
312
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100313"""""""""
314
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100315let s:ch_reply = ''
316func s:ChHandler(chan, msg)
317 unlet s:ch_reply
318 let s:ch_reply = a:msg
319endfunc
320
321let s:zero_reply = ''
322func s:OneHandler(chan, msg)
323 unlet s:zero_reply
324 let s:zero_reply = a:msg
325endfunc
326
327func s:channel_zero(port)
328 let handle = ch_open('localhost:' . a:port, s:chopt)
329 if ch_status(handle) == "fail"
330 call assert_false(1, "Can't open channel")
331 return
332 endif
333
334 " Check that eval works.
335 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
336
337 " Check that eval works if a zero id message is sent back.
338 let s:ch_reply = ''
339 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100340 if s:has_handler
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100341 call s:waitFor('"zero index" == s:ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100342 call assert_equal('zero index', s:ch_reply)
343 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100344 sleep 20m
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100345 call assert_equal('', s:ch_reply)
346 endif
347
348 " Check that handler works if a zero id message is sent back.
349 let s:ch_reply = ''
350 let s:zero_reply = ''
351 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100352 call s:waitFor('"sent zero" == s:zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100353 if s:has_handler
354 call assert_equal('zero index', s:ch_reply)
355 else
356 call assert_equal('', s:ch_reply)
357 endif
358 call assert_equal('sent zero', s:zero_reply)
359endfunc
360
361func Test_zero_reply()
362 call ch_log('Test_zero_reply()')
363 " Run with channel handler
364 let s:has_handler = 1
365 let s:chopt.callback = 's:ChHandler'
366 call s:run_server('s:channel_zero')
367 unlet s:chopt.callback
368
369 " Run without channel handler
370 let s:has_handler = 0
371 call s:run_server('s:channel_zero')
372endfunc
373
374"""""""""
375
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100376let s:reply1 = ""
377func s:HandleRaw1(chan, msg)
378 unlet s:reply1
379 let s:reply1 = a:msg
380endfunc
381
382let s:reply2 = ""
383func s:HandleRaw2(chan, msg)
384 unlet s:reply2
385 let s:reply2 = a:msg
386endfunc
387
388let s:reply3 = ""
389func s:HandleRaw3(chan, msg)
390 unlet s:reply3
391 let s:reply3 = a:msg
392endfunc
393
394func s:raw_one_time_callback(port)
395 let handle = ch_open('localhost:' . a:port, s:chopt)
396 if ch_status(handle) == "fail"
397 call assert_false(1, "Can't open channel")
398 return
399 endif
400 call ch_setoptions(handle, {'mode': 'raw'})
401
402 " The message are sent raw, we do our own JSON strings here.
403 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100404 call s:waitFor('s:reply1 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100405 call assert_equal("[1, \"got it\"]", s:reply1)
406 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
407 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100408 call s:waitFor('s:reply2 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100409 call assert_equal("[2, \"something\"]", s:reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100410 " wait for the 200 msec delayed reply
411 call s:waitFor('s:reply3 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100412 call assert_equal("[3, \"waited\"]", s:reply3)
413endfunc
414
415func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100416 call ch_log('Test_raw_one_time_callback()')
417 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100418endfunc
419
420"""""""""
421
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100422" Test that trying to connect to a non-existing port fails quickly.
423func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100424 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100425 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100426 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100427 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100428 " Oops, port does exists.
429 call ch_close(handle)
430 else
431 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100432 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100433 endif
434
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100435 " We intend to use a socket that doesn't exist and wait for half a second
436 " before giving up. If the socket does exist it can fail in various ways.
437 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100438 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100439 try
440 let handle = ch_open('localhost:9867', {'waittime': 500})
441 if ch_status(handle) != "fail"
442 " Oops, port does exists.
443 call ch_close(handle)
444 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100445 " Failed connection should wait about 500 msec. Can be longer if the
446 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100447 let elapsed = reltime(start)
448 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100449 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100450 endif
451 catch
452 if v:exception !~ 'Connection reset by peer'
453 call assert_false(1, "Caught exception: " . v:exception)
454 endif
455 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100456endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100457
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100458"""""""""
459
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100460func Test_raw_pipe()
461 if !has('job')
462 return
463 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100464 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100465 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
466 call assert_equal("run", job_status(job))
467 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100468 " For a change use the job where a channel is expected.
469 call ch_sendraw(job, "echo something\n")
470 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100471 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
472
Bram Moolenaar151f6562016-03-07 21:19:38 +0100473 call ch_sendraw(job, "double this\n")
474 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100475 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
476
Bram Moolenaar151f6562016-03-07 21:19:38 +0100477 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100478 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
479 finally
480 call job_stop(job)
481 endtry
482endfunc
483
484func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100485 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100486 return
487 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100488 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100489 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100490 call assert_equal("run", job_status(job))
491 try
492 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100493 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494 call assert_equal("something", ch_readraw(handle))
495
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100496 call ch_sendraw(handle, "echoerr wrong\n")
497 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
498
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100499 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100500 call assert_equal("this", ch_readraw(handle))
501 call assert_equal("AND this", ch_readraw(handle))
502
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100503 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100504 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100505 finally
506 call job_stop(job)
507 endtry
508endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100509
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100510func Test_nl_err_to_out_pipe()
511 if !has('job')
512 return
513 endif
514 call ch_log('Test_nl_err_to_out_pipe()')
515 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
516 call assert_equal("run", job_status(job))
517 try
518 let handle = job_getchannel(job)
519 call ch_sendraw(handle, "echo something\n")
520 call assert_equal("something", ch_readraw(handle))
521
522 call ch_sendraw(handle, "echoerr wrong\n")
523 call assert_equal("wrong", ch_readraw(handle))
524 finally
525 call job_stop(job)
526 endtry
527endfunc
528
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100529func Test_nl_read_file()
530 if !has('job')
531 return
532 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100533 call ch_log('Test_nl_read_file()')
534 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
535 let job = job_start(s:python . " test_channel_pipe.py",
536 \ {'in-io': 'file', 'in-name': 'Xinput'})
537 call assert_equal("run", job_status(job))
538 try
539 let handle = job_getchannel(job)
540 call assert_equal("something", ch_readraw(handle))
541 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
542 call assert_equal("this", ch_readraw(handle))
543 call assert_equal("AND this", ch_readraw(handle))
544 finally
545 call job_stop(job)
546 call delete('Xinput')
547 endtry
548endfunc
549
Bram Moolenaare98d1212016-03-08 15:37:41 +0100550func Test_nl_write_out_file()
551 if !has('job')
552 return
553 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100554 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
Bram Moolenaare98d1212016-03-08 15:37:41 +0100575 call ch_log('Test_nl_write_err_file()')
576 let job = job_start(s:python . " test_channel_pipe.py",
577 \ {'err-io': 'file', 'err-name': 'Xoutput'})
578 call assert_equal("run", job_status(job))
579 try
580 let handle = job_getchannel(job)
581 call ch_sendraw(handle, "echoerr line one\n")
582 call ch_sendraw(handle, "echoerr line two\n")
583 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100584 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100585 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
586 finally
587 call job_stop(job)
588 call delete('Xoutput')
589 endtry
590endfunc
591
592func Test_nl_write_both_file()
593 if !has('job')
594 return
595 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100596 call ch_log('Test_nl_write_both_file()')
597 let job = job_start(s:python . " test_channel_pipe.py",
598 \ {'out-io': 'file', 'out-name': 'Xoutput', 'err-io': 'out'})
599 call assert_equal("run", job_status(job))
600 try
601 let handle = job_getchannel(job)
602 call ch_sendraw(handle, "echoerr line one\n")
603 call ch_sendraw(handle, "echo line two\n")
604 call ch_sendraw(handle, "double this\n")
605 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100606 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100607 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
608 finally
609 call job_stop(job)
610 call delete('Xoutput')
611 endtry
612endfunc
613
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100614func Run_test_pipe_to_buffer(use_name)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100615 if !has('job')
616 return
617 endif
618 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100619 let options = {'out-io': 'buffer'}
620 if a:use_name
621 let options['out-name'] = 'pipe-output'
622 let firstline = 'Reading from channel output...'
623 else
624 sp pipe-output
625 let options['out-buf'] = bufnr('%')
626 quit
627 let firstline = ''
628 endif
629 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100630 call assert_equal("run", job_status(job))
631 try
632 let handle = job_getchannel(job)
633 call ch_sendraw(handle, "echo line one\n")
634 call ch_sendraw(handle, "echo line two\n")
635 call ch_sendraw(handle, "double this\n")
636 call ch_sendraw(handle, "quit\n")
637 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100638 call s:waitFor('line("$") >= 6')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100639 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100640 bwipe!
641 finally
642 call job_stop(job)
643 endtry
644endfunc
645
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100646func Test_pipe_to_buffer_name()
647 call Run_test_pipe_to_buffer(1)
648endfunc
649
650func Test_pipe_to_buffer_nr()
651 call Run_test_pipe_to_buffer(0)
652endfunc
653
654func Run_test_pipe_err_to_buffer(use_name)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100655 if !has('job')
656 return
657 endif
658 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100659 let options = {'err-io': 'buffer'}
660 if a:use_name
661 let options['err-name'] = 'pipe-err'
662 let firstline = 'Reading from channel error...'
663 else
664 sp pipe-err
665 let options['err-buf'] = bufnr('%')
666 quit
667 let firstline = ''
668 endif
669 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100670 call assert_equal("run", job_status(job))
671 try
672 let handle = job_getchannel(job)
673 call ch_sendraw(handle, "echoerr line one\n")
674 call ch_sendraw(handle, "echoerr line two\n")
675 call ch_sendraw(handle, "doubleerr this\n")
676 call ch_sendraw(handle, "quit\n")
677 sp pipe-err
678 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100679 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100680 bwipe!
681 finally
682 call job_stop(job)
683 endtry
684endfunc
685
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100686func Test_pipe_err_to_buffer_name()
687 call Run_test_pipe_err_to_buffer(1)
688endfunc
689
690func Test_pipe_err_to_buffer_nr()
691 call Run_test_pipe_err_to_buffer(0)
692endfunc
693
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100694func Test_pipe_both_to_buffer()
695 if !has('job')
696 return
697 endif
698 call ch_log('Test_pipe_both_to_buffer()')
699 let job = job_start(s:python . " test_channel_pipe.py",
700 \ {'out-io': 'buffer', 'out-name': 'pipe-err', 'err-io': 'out'})
701 call assert_equal("run", job_status(job))
702 try
703 let handle = job_getchannel(job)
704 call ch_sendraw(handle, "echo line one\n")
705 call ch_sendraw(handle, "echoerr line two\n")
706 call ch_sendraw(handle, "double this\n")
707 call ch_sendraw(handle, "doubleerr that\n")
708 call ch_sendraw(handle, "quit\n")
709 sp pipe-err
710 call s:waitFor('line("$") >= 7')
711 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
712 bwipe!
713 finally
714 call job_stop(job)
715 endtry
716endfunc
717
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100718func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100719 if !has('job')
720 return
721 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100722 call ch_log('Test_pipe_from_buffer()')
723
724 sp pipe-input
725 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100726 let options = {'in-io': 'buffer'}
727 if a:use_name
728 let options['in-name'] = 'pipe-input'
729 else
730 let options['in-buf'] = bufnr('%')
731 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100732
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100733 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100734 call assert_equal("run", job_status(job))
735 try
736 let handle = job_getchannel(job)
737 call assert_equal('one', ch_read(handle))
738 call assert_equal('two', ch_read(handle))
739 call assert_equal('three', ch_read(handle))
740 bwipe!
741 finally
742 call job_stop(job)
743 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100744endfunc
745
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100746func Test_pipe_from_buffer_name()
747 call Run_test_pipe_from_buffer(1)
748endfunc
749
750func Test_pipe_from_buffer_nr()
751 call Run_test_pipe_from_buffer(0)
752endfunc
753
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100754func Test_pipe_to_nameless_buffer()
755 if !has('job')
756 return
757 endif
758 call ch_log('Test_pipe_to_nameless_buffer()')
759 let job = job_start(s:python . " test_channel_pipe.py",
760 \ {'out-io': 'buffer'})
761 call assert_equal("run", job_status(job))
762 try
763 let handle = job_getchannel(job)
764 call ch_sendraw(handle, "echo line one\n")
765 call ch_sendraw(handle, "echo line two\n")
766 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100767 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100768 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
769 bwipe!
770 finally
771 call job_stop(job)
772 endtry
773endfunc
774
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100775func Test_pipe_to_buffer_json()
776 if !has('job')
777 return
778 endif
779 call ch_log('Test_pipe_to_buffer_json()')
780 let job = job_start(s:python . " test_channel_pipe.py",
781 \ {'out-io': 'buffer', 'out-mode': 'json'})
782 call assert_equal("run", job_status(job))
783 try
784 let handle = job_getchannel(job)
785 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
786 call ch_sendraw(handle, "echo [-2, 12.34]\n")
787 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100788 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100789 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
790 bwipe!
791 finally
792 call job_stop(job)
793 endtry
794endfunc
795
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100796" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100797func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100798 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100799 if getline(line('$') - a:offset) == a:line
800 break
801 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100802 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100803 endfor
804endfunc
805
806func Test_pipe_io_two_buffers()
807 if !has('job')
808 return
809 endif
810 call ch_log('Test_pipe_io_two_buffers()')
811
812 " Create two buffers, one to read from and one to write to.
813 split pipe-output
814 set buftype=nofile
815 split pipe-input
816 set buftype=nofile
817
818 let job = job_start(s:python . " test_channel_pipe.py",
819 \ {'in-io': 'buffer', 'in-name': 'pipe-input', 'in-top': 0,
820 \ 'out-io': 'buffer', 'out-name': 'pipe-output'})
821 call assert_equal("run", job_status(job))
822 try
823 exe "normal Gaecho hello\<CR>"
824 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100825 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100826 call assert_equal('hello', getline('$'))
827
828 exe bufwinnr('pipe-input') . "wincmd w"
829 exe "normal Gadouble this\<CR>"
830 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100831 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100832 call assert_equal('this', getline(line('$') - 1))
833 call assert_equal('AND this', getline('$'))
834
835 bwipe!
836 exe bufwinnr('pipe-input') . "wincmd w"
837 bwipe!
838 finally
839 call job_stop(job)
840 endtry
841endfunc
842
843func Test_pipe_io_one_buffer()
844 if !has('job')
845 return
846 endif
847 call ch_log('Test_pipe_io_one_buffer()')
848
849 " Create one buffer to read from and to write to.
850 split pipe-io
851 set buftype=nofile
852
853 let job = job_start(s:python . " test_channel_pipe.py",
854 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0,
855 \ 'out-io': 'buffer', 'out-name': 'pipe-io'})
856 call assert_equal("run", job_status(job))
857 try
858 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100859 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100860 call assert_equal('hello', getline(line('$') - 1))
861
862 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100863 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100864 call assert_equal('this', getline(line('$') - 2))
865 call assert_equal('AND this', getline(line('$') - 1))
866
867 bwipe!
868 finally
869 call job_stop(job)
870 endtry
871endfunc
872
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100873func Test_pipe_null()
874 if !has('job')
875 return
876 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100877 call ch_log('Test_pipe_null()')
878
879 " We cannot check that no I/O works, we only check that the job starts
880 " properly.
881 let job = job_start(s:python . " test_channel_pipe.py something",
882 \ {'in-io': 'null'})
883 call assert_equal("run", job_status(job))
884 try
885 call assert_equal('something', ch_read(job))
886 finally
887 call job_stop(job)
888 endtry
889
890 let job = job_start(s:python . " test_channel_pipe.py err-out",
891 \ {'out-io': 'null'})
892 call assert_equal("run", job_status(job))
893 try
894 call assert_equal('err-out', ch_read(job, {"part": "err"}))
895 finally
896 call job_stop(job)
897 endtry
898
899 let job = job_start(s:python . " test_channel_pipe.py something",
900 \ {'err-io': 'null'})
901 call assert_equal("run", job_status(job))
902 try
903 call assert_equal('something', ch_read(job))
904 finally
905 call job_stop(job)
906 endtry
907
908 let job = job_start(s:python . " test_channel_pipe.py something",
909 \ {'out-io': 'null', 'err-io': 'out'})
910 call assert_equal("run", job_status(job))
911 call job_stop(job)
912
913 let job = job_start(s:python . " test_channel_pipe.py something",
914 \ {'in-io': 'null', 'out-io': 'null', 'err-io': 'null'})
915 call assert_equal("run", job_status(job))
916 call assert_equal('channel fail', string(job_getchannel(job)))
917 call assert_equal('fail', ch_status(job))
918 call job_stop(job)
919endfunc
920
Bram Moolenaarde279892016-03-11 22:19:44 +0100921func Test_reuse_channel()
922 if !has('job')
923 return
924 endif
925 call ch_log('Test_reuse_channel()')
926
927 let job = job_start(s:python . " test_channel_pipe.py")
928 call assert_equal("run", job_status(job))
929 let handle = job_getchannel(job)
930 try
931 call ch_sendraw(handle, "echo something\n")
932 call assert_equal("something", ch_readraw(handle))
933 finally
934 call job_stop(job)
935 endtry
936
937 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
938 call assert_equal("run", job_status(job))
939 let handle = job_getchannel(job)
940 try
941 call ch_sendraw(handle, "echo again\n")
942 call assert_equal("again", ch_readraw(handle))
943 finally
944 call job_stop(job)
945 endtry
946endfunc
947
Bram Moolenaard46ae142016-02-16 13:33:52 +0100948""""""""""
949
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100950let s:unletResponse = ''
951func s:UnletHandler(handle, msg)
952 let s:unletResponse = a:msg
953 unlet s:channelfd
954endfunc
955
956" Test that "unlet handle" in a handler doesn't crash Vim.
957func s:unlet_handle(port)
958 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100959 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100960 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100961 call assert_equal('what?', s:unletResponse)
962endfunc
963
964func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100965 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100966 call s:run_server('s:unlet_handle')
967endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100968
Bram Moolenaard46ae142016-02-16 13:33:52 +0100969""""""""""
970
971let s:unletResponse = ''
972func s:CloseHandler(handle, msg)
973 let s:unletResponse = a:msg
974 call ch_close(s:channelfd)
975endfunc
976
977" Test that "unlet handle" in a handler doesn't crash Vim.
978func s:close_handle(port)
979 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100980 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100981 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100982 call assert_equal('what?', s:unletResponse)
983endfunc
984
985func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100986 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100987 call s:run_server('s:close_handle')
988endfunc
989
990""""""""""
991
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100992func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100993 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100994 silent! let ch = ch_open("noserver")
995 echo ch
996 let d = ch
997endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100998
999""""""""""
1000
1001func s:open_delay(port)
1002 " Wait up to a second for the port to open.
1003 let s:chopt.waittime = 1000
1004 let channel = ch_open('localhost:' . a:port, s:chopt)
1005 unlet s:chopt.waittime
1006 if ch_status(channel) == "fail"
1007 call assert_false(1, "Can't open channel")
1008 return
1009 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001010 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001011 call ch_close(channel)
1012endfunc
1013
1014func Test_open_delay()
1015 call ch_log('Test_open_delay()')
1016 " The server will wait half a second before creating the port.
1017 call s:run_server('s:open_delay', 'delay')
1018endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001019
1020"""""""""
1021
1022function MyFunction(a,b,c)
1023 let s:call_ret = [a:a, a:b, a:c]
1024endfunc
1025
1026function s:test_call(port)
1027 let handle = ch_open('localhost:' . a:port, s:chopt)
1028 if ch_status(handle) == "fail"
1029 call assert_false(1, "Can't open channel")
1030 return
1031 endif
1032
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001033 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001034 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001035 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001036 call assert_equal([1, 2, 3], s:call_ret)
1037endfunc
1038
1039func Test_call()
1040 call ch_log('Test_call()')
1041 call s:run_server('s:test_call')
1042endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001043
1044"""""""""
1045
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001046let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001047function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001048 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001049endfunc
1050
1051function s:test_exit_callback(port)
1052 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
1053 let s:exit_job = s:job
1054endfunc
1055
1056func Test_exit_callback()
1057 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001058 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001059 call s:run_server('s:test_exit_callback')
1060
Bram Moolenaar9730f742016-02-28 19:50:51 +01001061 " wait up to a second for the job to exit
1062 for i in range(100)
1063 if s:job_exit_ret == 'done'
1064 break
1065 endif
1066 sleep 10m
1067 " calling job_status() triggers the callback
1068 call job_status(s:exit_job)
1069 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001070
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001071 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001072 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001073 endif
1074endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001075
1076"""""""""
1077
1078let s:ch_close_ret = 'alive'
1079function MyCloseCb(ch)
1080 let s:ch_close_ret = 'closed'
1081endfunc
1082
1083function s:test_close_callback(port)
1084 let handle = ch_open('localhost:' . a:port, s:chopt)
1085 if ch_status(handle) == "fail"
1086 call assert_false(1, "Can't open channel")
1087 return
1088 endif
1089 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
1090
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001091 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001092 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001093 call assert_equal('closed', s:ch_close_ret)
1094endfunc
1095
1096func Test_close_callback()
1097 call ch_log('Test_close_callback()')
1098 call s:run_server('s:test_close_callback')
1099endfunc
1100
Bram Moolenaar9730f742016-02-28 19:50:51 +01001101" Uncomment this to see what happens, output is in src/testdir/channellog.
1102" call ch_logfile('channellog', 'w')