blob: 5f2b97f70fe4df723e0e0b199885618f5ee0fadb [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
445 " Failed connection should wait about 500 msec.
446 let elapsed = reltime(start)
447 call assert_true(reltimefloat(elapsed) > 0.3)
448 call assert_true(reltimefloat(elapsed) < 1.0)
449 endif
450 catch
451 if v:exception !~ 'Connection reset by peer'
452 call assert_false(1, "Caught exception: " . v:exception)
453 endif
454 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100455endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100456
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100457"""""""""
458
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100459func Test_raw_pipe()
460 if !has('job')
461 return
462 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100463 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100464 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
465 call assert_equal("run", job_status(job))
466 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100467 " For a change use the job where a channel is expected.
468 call ch_sendraw(job, "echo something\n")
469 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100470 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
471
Bram Moolenaar151f6562016-03-07 21:19:38 +0100472 call ch_sendraw(job, "double this\n")
473 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100474 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
475
Bram Moolenaar151f6562016-03-07 21:19:38 +0100476 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100477 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
478 finally
479 call job_stop(job)
480 endtry
481endfunc
482
483func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100484 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100485 return
486 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100487 call ch_log('Test_nl_pipe()')
Bram Moolenaarb6a77372016-02-15 22:55:28 +0100488 let job = job_start(s:python . " test_channel_pipe.py")
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100489 call assert_equal("run", job_status(job))
490 try
491 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100492 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100493 call assert_equal("something", ch_readraw(handle))
494
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100495 call ch_sendraw(handle, "echoerr wrong\n")
496 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
497
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100498 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100499 call assert_equal("this", ch_readraw(handle))
500 call assert_equal("AND this", ch_readraw(handle))
501
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100502 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100503 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100504 finally
505 call job_stop(job)
506 endtry
507endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100508
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100509func Test_nl_err_to_out_pipe()
510 if !has('job')
511 return
512 endif
513 call ch_log('Test_nl_err_to_out_pipe()')
514 let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'})
515 call assert_equal("run", job_status(job))
516 try
517 let handle = job_getchannel(job)
518 call ch_sendraw(handle, "echo something\n")
519 call assert_equal("something", ch_readraw(handle))
520
521 call ch_sendraw(handle, "echoerr wrong\n")
522 call assert_equal("wrong", ch_readraw(handle))
523 finally
524 call job_stop(job)
525 endtry
526endfunc
527
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100528func Test_nl_read_file()
529 if !has('job')
530 return
531 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100532 call ch_log('Test_nl_read_file()')
533 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
534 let job = job_start(s:python . " test_channel_pipe.py",
535 \ {'in-io': 'file', 'in-name': 'Xinput'})
536 call assert_equal("run", job_status(job))
537 try
538 let handle = job_getchannel(job)
539 call assert_equal("something", ch_readraw(handle))
540 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
541 call assert_equal("this", ch_readraw(handle))
542 call assert_equal("AND this", ch_readraw(handle))
543 finally
544 call job_stop(job)
545 call delete('Xinput')
546 endtry
547endfunc
548
Bram Moolenaare98d1212016-03-08 15:37:41 +0100549func Test_nl_write_out_file()
550 if !has('job')
551 return
552 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100553 call ch_log('Test_nl_write_out_file()')
554 let job = job_start(s:python . " test_channel_pipe.py",
555 \ {'out-io': 'file', 'out-name': 'Xoutput'})
556 call assert_equal("run", job_status(job))
557 try
558 let handle = job_getchannel(job)
559 call ch_sendraw(handle, "echo line one\n")
560 call ch_sendraw(handle, "echo line two\n")
561 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100562 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100563 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
564 finally
565 call job_stop(job)
566 call delete('Xoutput')
567 endtry
568endfunc
569
570func Test_nl_write_err_file()
571 if !has('job')
572 return
573 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100574 call ch_log('Test_nl_write_err_file()')
575 let job = job_start(s:python . " test_channel_pipe.py",
576 \ {'err-io': 'file', 'err-name': 'Xoutput'})
577 call assert_equal("run", job_status(job))
578 try
579 let handle = job_getchannel(job)
580 call ch_sendraw(handle, "echoerr line one\n")
581 call ch_sendraw(handle, "echoerr line two\n")
582 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100583 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100584 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
585 finally
586 call job_stop(job)
587 call delete('Xoutput')
588 endtry
589endfunc
590
591func Test_nl_write_both_file()
592 if !has('job')
593 return
594 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100595 call ch_log('Test_nl_write_both_file()')
596 let job = job_start(s:python . " test_channel_pipe.py",
597 \ {'out-io': 'file', 'out-name': 'Xoutput', 'err-io': 'out'})
598 call assert_equal("run", job_status(job))
599 try
600 let handle = job_getchannel(job)
601 call ch_sendraw(handle, "echoerr line one\n")
602 call ch_sendraw(handle, "echo line two\n")
603 call ch_sendraw(handle, "double this\n")
604 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100605 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100606 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
607 finally
608 call job_stop(job)
609 call delete('Xoutput')
610 endtry
611endfunc
612
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100613func Run_test_pipe_to_buffer(use_name)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100614 if !has('job')
615 return
616 endif
617 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100618 let options = {'out-io': 'buffer'}
619 if a:use_name
620 let options['out-name'] = 'pipe-output'
621 let firstline = 'Reading from channel output...'
622 else
623 sp pipe-output
624 let options['out-buf'] = bufnr('%')
625 quit
626 let firstline = ''
627 endif
628 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100629 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 Moolenaar29fd0382016-03-09 23:14:07 +0100638 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100639 bwipe!
640 finally
641 call job_stop(job)
642 endtry
643endfunc
644
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100645func Test_pipe_to_buffer_name()
646 call Run_test_pipe_to_buffer(1)
647endfunc
648
649func Test_pipe_to_buffer_nr()
650 call Run_test_pipe_to_buffer(0)
651endfunc
652
653func Run_test_pipe_err_to_buffer(use_name)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100654 if !has('job')
655 return
656 endif
657 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100658 let options = {'err-io': 'buffer'}
659 if a:use_name
660 let options['err-name'] = 'pipe-err'
661 let firstline = 'Reading from channel error...'
662 else
663 sp pipe-err
664 let options['err-buf'] = bufnr('%')
665 quit
666 let firstline = ''
667 endif
668 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100669 call assert_equal("run", job_status(job))
670 try
671 let handle = job_getchannel(job)
672 call ch_sendraw(handle, "echoerr line one\n")
673 call ch_sendraw(handle, "echoerr line two\n")
674 call ch_sendraw(handle, "doubleerr this\n")
675 call ch_sendraw(handle, "quit\n")
676 sp pipe-err
677 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100678 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100679 bwipe!
680 finally
681 call job_stop(job)
682 endtry
683endfunc
684
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100685func Test_pipe_err_to_buffer_name()
686 call Run_test_pipe_err_to_buffer(1)
687endfunc
688
689func Test_pipe_err_to_buffer_nr()
690 call Run_test_pipe_err_to_buffer(0)
691endfunc
692
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100693func Test_pipe_both_to_buffer()
694 if !has('job')
695 return
696 endif
697 call ch_log('Test_pipe_both_to_buffer()')
698 let job = job_start(s:python . " test_channel_pipe.py",
699 \ {'out-io': 'buffer', 'out-name': 'pipe-err', 'err-io': 'out'})
700 call assert_equal("run", job_status(job))
701 try
702 let handle = job_getchannel(job)
703 call ch_sendraw(handle, "echo line one\n")
704 call ch_sendraw(handle, "echoerr line two\n")
705 call ch_sendraw(handle, "double this\n")
706 call ch_sendraw(handle, "doubleerr that\n")
707 call ch_sendraw(handle, "quit\n")
708 sp pipe-err
709 call s:waitFor('line("$") >= 7')
710 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
711 bwipe!
712 finally
713 call job_stop(job)
714 endtry
715endfunc
716
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100717func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100718 if !has('job')
719 return
720 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100721 call ch_log('Test_pipe_from_buffer()')
722
723 sp pipe-input
724 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100725 let options = {'in-io': 'buffer'}
726 if a:use_name
727 let options['in-name'] = 'pipe-input'
728 else
729 let options['in-buf'] = bufnr('%')
730 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100731
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100732 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100733 call assert_equal("run", job_status(job))
734 try
735 let handle = job_getchannel(job)
736 call assert_equal('one', ch_read(handle))
737 call assert_equal('two', ch_read(handle))
738 call assert_equal('three', ch_read(handle))
739 bwipe!
740 finally
741 call job_stop(job)
742 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100743endfunc
744
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100745func Test_pipe_from_buffer_name()
746 call Run_test_pipe_from_buffer(1)
747endfunc
748
749func Test_pipe_from_buffer_nr()
750 call Run_test_pipe_from_buffer(0)
751endfunc
752
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100753func Test_pipe_to_nameless_buffer()
754 if !has('job')
755 return
756 endif
757 call ch_log('Test_pipe_to_nameless_buffer()')
758 let job = job_start(s:python . " test_channel_pipe.py",
759 \ {'out-io': 'buffer'})
760 call assert_equal("run", job_status(job))
761 try
762 let handle = job_getchannel(job)
763 call ch_sendraw(handle, "echo line one\n")
764 call ch_sendraw(handle, "echo line two\n")
765 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100766 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100767 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
768 bwipe!
769 finally
770 call job_stop(job)
771 endtry
772endfunc
773
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100774func Test_pipe_to_buffer_json()
775 if !has('job')
776 return
777 endif
778 call ch_log('Test_pipe_to_buffer_json()')
779 let job = job_start(s:python . " test_channel_pipe.py",
780 \ {'out-io': 'buffer', 'out-mode': 'json'})
781 call assert_equal("run", job_status(job))
782 try
783 let handle = job_getchannel(job)
784 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
785 call ch_sendraw(handle, "echo [-2, 12.34]\n")
786 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100787 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100788 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
789 bwipe!
790 finally
791 call job_stop(job)
792 endtry
793endfunc
794
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100795" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100796func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100797 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100798 if getline(line('$') - a:offset) == a:line
799 break
800 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100801 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100802 endfor
803endfunc
804
805func Test_pipe_io_two_buffers()
806 if !has('job')
807 return
808 endif
809 call ch_log('Test_pipe_io_two_buffers()')
810
811 " Create two buffers, one to read from and one to write to.
812 split pipe-output
813 set buftype=nofile
814 split pipe-input
815 set buftype=nofile
816
817 let job = job_start(s:python . " test_channel_pipe.py",
818 \ {'in-io': 'buffer', 'in-name': 'pipe-input', 'in-top': 0,
819 \ 'out-io': 'buffer', 'out-name': 'pipe-output'})
820 call assert_equal("run", job_status(job))
821 try
822 exe "normal Gaecho hello\<CR>"
823 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100824 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100825 call assert_equal('hello', getline('$'))
826
827 exe bufwinnr('pipe-input') . "wincmd w"
828 exe "normal Gadouble this\<CR>"
829 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100830 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100831 call assert_equal('this', getline(line('$') - 1))
832 call assert_equal('AND this', getline('$'))
833
834 bwipe!
835 exe bufwinnr('pipe-input') . "wincmd w"
836 bwipe!
837 finally
838 call job_stop(job)
839 endtry
840endfunc
841
842func Test_pipe_io_one_buffer()
843 if !has('job')
844 return
845 endif
846 call ch_log('Test_pipe_io_one_buffer()')
847
848 " Create one buffer to read from and to write to.
849 split pipe-io
850 set buftype=nofile
851
852 let job = job_start(s:python . " test_channel_pipe.py",
853 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0,
854 \ 'out-io': 'buffer', 'out-name': 'pipe-io'})
855 call assert_equal("run", job_status(job))
856 try
857 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100858 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100859 call assert_equal('hello', getline(line('$') - 1))
860
861 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100862 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100863 call assert_equal('this', getline(line('$') - 2))
864 call assert_equal('AND this', getline(line('$') - 1))
865
866 bwipe!
867 finally
868 call job_stop(job)
869 endtry
870endfunc
871
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100872func Test_pipe_null()
873 if !has('job')
874 return
875 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100876 call ch_log('Test_pipe_null()')
877
878 " We cannot check that no I/O works, we only check that the job starts
879 " properly.
880 let job = job_start(s:python . " test_channel_pipe.py something",
881 \ {'in-io': 'null'})
882 call assert_equal("run", job_status(job))
883 try
884 call assert_equal('something', ch_read(job))
885 finally
886 call job_stop(job)
887 endtry
888
889 let job = job_start(s:python . " test_channel_pipe.py err-out",
890 \ {'out-io': 'null'})
891 call assert_equal("run", job_status(job))
892 try
893 call assert_equal('err-out', ch_read(job, {"part": "err"}))
894 finally
895 call job_stop(job)
896 endtry
897
898 let job = job_start(s:python . " test_channel_pipe.py something",
899 \ {'err-io': 'null'})
900 call assert_equal("run", job_status(job))
901 try
902 call assert_equal('something', ch_read(job))
903 finally
904 call job_stop(job)
905 endtry
906
907 let job = job_start(s:python . " test_channel_pipe.py something",
908 \ {'out-io': 'null', 'err-io': 'out'})
909 call assert_equal("run", job_status(job))
910 call job_stop(job)
911
912 let job = job_start(s:python . " test_channel_pipe.py something",
913 \ {'in-io': 'null', 'out-io': 'null', 'err-io': 'null'})
914 call assert_equal("run", job_status(job))
915 call assert_equal('channel fail', string(job_getchannel(job)))
916 call assert_equal('fail', ch_status(job))
917 call job_stop(job)
918endfunc
919
Bram Moolenaarde279892016-03-11 22:19:44 +0100920func Test_reuse_channel()
921 if !has('job')
922 return
923 endif
924 call ch_log('Test_reuse_channel()')
925
926 let job = job_start(s:python . " test_channel_pipe.py")
927 call assert_equal("run", job_status(job))
928 let handle = job_getchannel(job)
929 try
930 call ch_sendraw(handle, "echo something\n")
931 call assert_equal("something", ch_readraw(handle))
932 finally
933 call job_stop(job)
934 endtry
935
936 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
937 call assert_equal("run", job_status(job))
938 let handle = job_getchannel(job)
939 try
940 call ch_sendraw(handle, "echo again\n")
941 call assert_equal("again", ch_readraw(handle))
942 finally
943 call job_stop(job)
944 endtry
945endfunc
946
Bram Moolenaard46ae142016-02-16 13:33:52 +0100947""""""""""
948
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100949let s:unletResponse = ''
950func s:UnletHandler(handle, msg)
951 let s:unletResponse = a:msg
952 unlet s:channelfd
953endfunc
954
955" Test that "unlet handle" in a handler doesn't crash Vim.
956func s:unlet_handle(port)
957 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100958 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100959 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100960 call assert_equal('what?', s:unletResponse)
961endfunc
962
963func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100964 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100965 call s:run_server('s:unlet_handle')
966endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100967
Bram Moolenaard46ae142016-02-16 13:33:52 +0100968""""""""""
969
970let s:unletResponse = ''
971func s:CloseHandler(handle, msg)
972 let s:unletResponse = a:msg
973 call ch_close(s:channelfd)
974endfunc
975
976" Test that "unlet handle" in a handler doesn't crash Vim.
977func s:close_handle(port)
978 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100979 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100980 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100981 call assert_equal('what?', s:unletResponse)
982endfunc
983
984func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100985 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +0100986 call s:run_server('s:close_handle')
987endfunc
988
989""""""""""
990
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100991func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100992 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +0100993 silent! let ch = ch_open("noserver")
994 echo ch
995 let d = ch
996endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100997
998""""""""""
999
1000func s:open_delay(port)
1001 " Wait up to a second for the port to open.
1002 let s:chopt.waittime = 1000
1003 let channel = ch_open('localhost:' . a:port, s:chopt)
1004 unlet s:chopt.waittime
1005 if ch_status(channel) == "fail"
1006 call assert_false(1, "Can't open channel")
1007 return
1008 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001009 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001010 call ch_close(channel)
1011endfunc
1012
1013func Test_open_delay()
1014 call ch_log('Test_open_delay()')
1015 " The server will wait half a second before creating the port.
1016 call s:run_server('s:open_delay', 'delay')
1017endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001018
1019"""""""""
1020
1021function MyFunction(a,b,c)
1022 let s:call_ret = [a:a, a:b, a:c]
1023endfunc
1024
1025function s:test_call(port)
1026 let handle = ch_open('localhost:' . a:port, s:chopt)
1027 if ch_status(handle) == "fail"
1028 call assert_false(1, "Can't open channel")
1029 return
1030 endif
1031
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001032 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001033 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001034 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001035 call assert_equal([1, 2, 3], s:call_ret)
1036endfunc
1037
1038func Test_call()
1039 call ch_log('Test_call()')
1040 call s:run_server('s:test_call')
1041endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001042
1043"""""""""
1044
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001045let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001046function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001047 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001048endfunc
1049
1050function s:test_exit_callback(port)
1051 call job_setoptions(s:job, {'exit-cb': 'MyExitCb'})
1052 let s:exit_job = s:job
1053endfunc
1054
1055func Test_exit_callback()
1056 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001057 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001058 call s:run_server('s:test_exit_callback')
1059
Bram Moolenaar9730f742016-02-28 19:50:51 +01001060 " wait up to a second for the job to exit
1061 for i in range(100)
1062 if s:job_exit_ret == 'done'
1063 break
1064 endif
1065 sleep 10m
1066 " calling job_status() triggers the callback
1067 call job_status(s:exit_job)
1068 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001069
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001070 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001071 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001072 endif
1073endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001074
1075"""""""""
1076
1077let s:ch_close_ret = 'alive'
1078function MyCloseCb(ch)
1079 let s:ch_close_ret = 'closed'
1080endfunc
1081
1082function s:test_close_callback(port)
1083 let handle = ch_open('localhost:' . a:port, s:chopt)
1084 if ch_status(handle) == "fail"
1085 call assert_false(1, "Can't open channel")
1086 return
1087 endif
1088 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
1089
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001090 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001091 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001092 call assert_equal('closed', s:ch_close_ret)
1093endfunc
1094
1095func Test_close_callback()
1096 call ch_log('Test_close_callback()')
1097 call s:run_server('s:test_close_callback')
1098endfunc
1099
Bram Moolenaar9730f742016-02-28 19:50:51 +01001100" Uncomment this to see what happens, output is in src/testdir/channellog.
1101" call ch_logfile('channellog', 'w')