blob: 0fd7de9dd139e35ed5ce329378166c9236051f06 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
Bram Moolenaare2469252016-02-04 10:54:34 +01004if !has('channel')
5 finish
6endif
7
8" This test requires the Python command to run the test server.
Bram Moolenaara8343c12016-02-04 22:09:48 +01009" This most likely only works on Unix and Windows.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010010if has('unix')
Bram Moolenaar835dc632016-02-07 14:27:38 +010011 " We also need the job feature or the pkill command to make sure the server
12 " can be stopped.
13 if !(executable('python') && (has('job') || executable('pkill')))
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010014 finish
15 endif
Bram Moolenaarb6a77372016-02-15 22:55:28 +010016 let s:python = 'python'
Bram Moolenaara8343c12016-02-04 22:09:48 +010017elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010018 " Use Python Launcher for Windows (py.exe) if available.
19 if executable('py.exe')
20 let s:python = 'py.exe'
21 elseif executable('python.exe')
22 let s:python = 'python.exe'
23 else
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010024 finish
25 endif
26else
Bram Moolenaard6a8d482016-02-10 20:32:20 +010027 " Can't run this test.
Bram Moolenaard7ece102016-02-02 23:23:02 +010028 finish
29endif
30
Bram Moolenaare74e8e72016-02-16 22:01:30 +010031let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010032
Bram Moolenaard6a8d482016-02-10 20:32:20 +010033" Run "testfunc" after sarting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010034func s:run_server(testfunc, ...)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010035 " The Python program writes the port number in Xportnr.
36 call delete("Xportnr")
37
Bram Moolenaar81661fb2016-02-18 22:23:34 +010038 if a:0 == 1
39 let arg = ' ' . a:1
40 else
41 let arg = ''
42 endif
43 let cmd = s:python . " test_channel.py" . arg
44
Bram Moolenaard6a8d482016-02-10 20:32:20 +010045 try
46 if has('job')
Bram Moolenaar65edff82016-02-21 16:40:11 +010047 let s:job = job_start(cmd, {"stoponexit": "hup"})
48 call job_setoptions(s:job, {"stoponexit": "kill"})
Bram Moolenaard6a8d482016-02-10 20:32:20 +010049 elseif has('win32')
Bram Moolenaar81661fb2016-02-18 22:23:34 +010050 exe 'silent !start cmd /c start "test_channel" ' . cmd
Bram Moolenaard6a8d482016-02-10 20:32:20 +010051 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010052 exe 'silent !' . cmd . '&'
Bram Moolenaard7ece102016-02-02 23:23:02 +010053 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010054
Bram Moolenaard6a8d482016-02-10 20:32:20 +010055 " Wait for up to 2 seconds for the port number to be there.
Bram Moolenaard6a8d482016-02-10 20:32:20 +010056 let l = []
Bram Moolenaar9fe885e2016-03-08 16:06:55 +010057 for i in range(200)
Bram Moolenaard6a8d482016-02-10 20:32:20 +010058 try
59 let l = readfile("Xportnr")
60 catch
61 endtry
62 if len(l) >= 1
63 break
64 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +010065 sleep 10m
66 endfor
Bram Moolenaard6a8d482016-02-10 20:32:20 +010067 call delete("Xportnr")
68
69 if len(l) == 0
70 " Can't make the connection, give up.
71 call assert_false(1, "Can't start test_channel.py")
72 return -1
73 endif
74 let port = l[0]
75
76 call call(function(a:testfunc), [port])
77 catch
78 call assert_false(1, "Caught exception: " . v:exception)
79 finally
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010080 call s:kill_server()
Bram Moolenaard6a8d482016-02-10 20:32:20 +010081 endtry
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010082endfunc
83
84func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010085 if has('job')
Bram Moolenaard6a8d482016-02-10 20:32:20 +010086 if exists('s:job')
87 call job_stop(s:job)
88 unlet s:job
89 endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010090 elseif has('win32')
Bram Moolenaarb6a77372016-02-15 22:55:28 +010091 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010092 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010093 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010094 endif
95endfunc
96
Bram Moolenaara07fec92016-02-05 21:04:08 +010097let s:responseMsg = ''
98func s:RequestHandler(handle, msg)
99 let s:responseHandle = a:handle
100 let s:responseMsg = a:msg
101endfunc
102
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100103" Wait for up to a second for "expr" to become true.
104func s:waitFor(expr)
105 for i in range(100)
Bram Moolenaard9d473e2016-03-08 19:07:22 +0100106 try
107 if eval(a:expr)
108 return
109 endif
110 catch
111 endtry
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100112 sleep 10m
113 endfor
114endfunc
115
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100116func s:communicate(port)
117 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100118 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100119 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100120 return
121 endif
Bram Moolenaar839fd112016-03-06 21:34:03 +0100122 if has('job')
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100123 " check that getjob without a job is handled correctly
Bram Moolenaar839fd112016-03-06 21:34:03 +0100124 call assert_equal('no process', string(ch_getjob(handle)))
125 endif
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100126 let dict = ch_info(handle)
127 call assert_true(dict.id != 0)
128 call assert_equal('open', dict.status)
129 call assert_equal(a:port, string(dict.port))
130 call assert_equal('open', dict.sock_status)
131 call assert_equal('socket', dict.sock_io)
132
Bram Moolenaard7ece102016-02-02 23:23:02 +0100133 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100134 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100135
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100136 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100137 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
138 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
139 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
140
141 " split command should work
142 call assert_equal('ok', ch_evalexpr(handle, 'split'))
143 call s:waitFor('exists("g:split")')
144 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100145
Bram Moolenaard7ece102016-02-02 23:23:02 +0100146 " Request that triggers sending two ex commands. These will usually be
147 " handled before getting the response, but it's not guaranteed, thus wait a
148 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100149 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100150 call s:waitFor('"added2" == getline("$")')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100151 call assert_equal('added1', getline(line('$') - 1))
152 call assert_equal('added2', getline('$'))
153
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100154 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100155 call s:waitFor('"added more" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100156 call assert_equal('added more', getline('$'))
157
Bram Moolenaara07fec92016-02-05 21:04:08 +0100158 " Send a request with a specific handler.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100159 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100160 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 if !exists('s:responseHandle')
162 call assert_false(1, 's:responseHandle was not set')
163 else
164 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100165 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 endif
Bram Moolenaara07fec92016-02-05 21:04:08 +0100167 call assert_equal('got it', s:responseMsg)
168
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100169 let s:responseMsg = ''
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100170 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100171 call s:waitFor('exists("s:responseHandle")')
Bram Moolenaar77073442016-02-13 23:23:53 +0100172 if !exists('s:responseHandle')
173 call assert_false(1, 's:responseHandle was not set')
174 else
175 call assert_equal(handle, s:responseHandle)
Bram Moolenaar9186a272016-02-23 19:34:01 +0100176 unlet s:responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100177 endif
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100178 call assert_equal('got it', s:responseMsg)
179
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100180 " Collect garbage, tests that our handle isn't collected.
181 call garbagecollect()
182
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100183 " check setting options (without testing the effect)
184 call ch_setoptions(handle, {'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100185 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100186 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100187 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100188 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100189
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100190 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100191 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100192 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100193 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100194
195 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100196 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100197 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100198 call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100199
Bram Moolenaar55fab432016-02-07 16:53:13 +0100200 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100202 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100203 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100204
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100205 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100206 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100207 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100208 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100209
Bram Moolenaarf4160862016-02-05 23:09:12 +0100210 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100211 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100212 call s:waitFor('"three" == getline("$")')
Bram Moolenaarf4160862016-02-05 23:09:12 +0100213 call assert_equal('one', getline(line('$') - 2))
214 call assert_equal('two', getline(line('$') - 1))
215 call assert_equal('three', getline('$'))
216
217 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100218 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
219 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100220
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100221 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100222
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100223 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100224 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
225 let start = reltime()
226 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
227 let elapsed = reltime(start)
228 call assert_true(reltimefloat(elapsed) > 0.3)
229 call assert_true(reltimefloat(elapsed) < 0.6)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100230
231 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100232 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100233 let resp = ch_read(handle)
234 call assert_equal(type([]), type(resp))
235 call assert_equal(type(11), type(resp[0]))
236 call assert_equal('waited', resp[1])
237
Bram Moolenaard7ece102016-02-02 23:23:02 +0100238 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100239 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100240endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100241
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100242func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100243 call ch_log('Test_communicate()')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100244 call s:run_server('s:communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100245endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100246
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100247" Test that we can open two channels.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100248func s:two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100249 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100250 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100251 call assert_false(1, "Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100252 return
253 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100254
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100255 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100256
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100257 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if ch_status(newhandle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100259 call assert_false(1, "Can't open second channel")
260 return
261 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100262 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
263 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100264
265 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100266 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100267
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100268 call ch_close(newhandle)
269endfunc
270
271func Test_two_channels()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100272 call ch_log('Test_two_channels()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100273 call s:run_server('s:two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100274endfunc
275
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100276" Test that a server crash is handled gracefully.
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100277func s:server_crash(port)
278 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100279 if ch_status(handle) == "fail"
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100280 call assert_false(1, "Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100281 return
282 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100283
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100284 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100285
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100286 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100287endfunc
288
289func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100290 call ch_log('Test_server_crash()')
Bram Moolenaarbfa1ffc2016-02-13 18:40:30 +0100291 call s:run_server('s:server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100292endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100293
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100294"""""""""
295
Bram Moolenaarf6157282016-02-10 21:07:14 +0100296let s:reply = ""
297func s:Handler(chan, msg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100298 unlet s:reply
Bram Moolenaarf6157282016-02-10 21:07:14 +0100299 let s:reply = a:msg
300endfunc
301
302func s:channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100303 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100304 if ch_status(handle) == "fail"
Bram Moolenaarf6157282016-02-10 21:07:14 +0100305 call assert_false(1, "Can't open channel")
306 return
307 endif
308
309 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100310 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100311 call s:waitFor('"we called you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100312 call assert_equal('we called you', s:reply)
313
314 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100315 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100316 call s:waitFor('"we did call you" == s:reply')
Bram Moolenaarf6157282016-02-10 21:07:14 +0100317 call assert_equal('we did call you', s:reply)
318endfunc
319
320func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100321 call ch_log('Test_channel_handler()')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100322 let s:chopt.callback = 's:Handler'
Bram Moolenaarf6157282016-02-10 21:07:14 +0100323 call s:run_server('s:channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100324 let s:chopt.callback = function('s:Handler')
325 call s:run_server('s:channel_handler')
326 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100327endfunc
328
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100329"""""""""
330
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100331let s:ch_reply = ''
332func s:ChHandler(chan, msg)
333 unlet s:ch_reply
334 let s:ch_reply = a:msg
335endfunc
336
337let s:zero_reply = ''
338func s:OneHandler(chan, msg)
339 unlet s:zero_reply
340 let s:zero_reply = a:msg
341endfunc
342
343func s:channel_zero(port)
344 let handle = ch_open('localhost:' . a:port, s:chopt)
345 if ch_status(handle) == "fail"
346 call assert_false(1, "Can't open channel")
347 return
348 endif
349
350 " Check that eval works.
351 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
352
353 " Check that eval works if a zero id message is sent back.
354 let s:ch_reply = ''
355 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100356 if s:has_handler
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100357 call s:waitFor('"zero index" == s:ch_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100358 call assert_equal('zero index', s:ch_reply)
359 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100360 sleep 20m
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100361 call assert_equal('', s:ch_reply)
362 endif
363
364 " Check that handler works if a zero id message is sent back.
365 let s:ch_reply = ''
366 let s:zero_reply = ''
367 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100368 call s:waitFor('"sent zero" == s:zero_reply')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100369 if s:has_handler
370 call assert_equal('zero index', s:ch_reply)
371 else
372 call assert_equal('', s:ch_reply)
373 endif
374 call assert_equal('sent zero', s:zero_reply)
375endfunc
376
377func Test_zero_reply()
378 call ch_log('Test_zero_reply()')
379 " Run with channel handler
380 let s:has_handler = 1
381 let s:chopt.callback = 's:ChHandler'
382 call s:run_server('s:channel_zero')
383 unlet s:chopt.callback
384
385 " Run without channel handler
386 let s:has_handler = 0
387 call s:run_server('s:channel_zero')
388endfunc
389
390"""""""""
391
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100392let s:reply1 = ""
393func s:HandleRaw1(chan, msg)
394 unlet s:reply1
395 let s:reply1 = a:msg
396endfunc
397
398let s:reply2 = ""
399func s:HandleRaw2(chan, msg)
400 unlet s:reply2
401 let s:reply2 = a:msg
402endfunc
403
404let s:reply3 = ""
405func s:HandleRaw3(chan, msg)
406 unlet s:reply3
407 let s:reply3 = a:msg
408endfunc
409
410func s:raw_one_time_callback(port)
411 let handle = ch_open('localhost:' . a:port, s:chopt)
412 if ch_status(handle) == "fail"
413 call assert_false(1, "Can't open channel")
414 return
415 endif
416 call ch_setoptions(handle, {'mode': 'raw'})
417
418 " The message are sent raw, we do our own JSON strings here.
419 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100420 call s:waitFor('s:reply1 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100421 call assert_equal("[1, \"got it\"]", s:reply1)
422 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
423 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100424 call s:waitFor('s:reply2 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100425 call assert_equal("[2, \"something\"]", s:reply2)
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100426 " wait for the 200 msec delayed reply
427 call s:waitFor('s:reply3 != ""')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100428 call assert_equal("[3, \"waited\"]", s:reply3)
429endfunc
430
431func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100432 call ch_log('Test_raw_one_time_callback()')
433 call s:run_server('s:raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100434endfunc
435
436"""""""""
437
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100438" Test that trying to connect to a non-existing port fails quickly.
439func Test_connect_waittime()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100440 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100441 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100442 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100443 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100444 " Oops, port does exists.
445 call ch_close(handle)
446 else
447 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100448 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100449 endif
450
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100451 " We intend to use a socket that doesn't exist and wait for half a second
452 " before giving up. If the socket does exist it can fail in various ways.
453 " Check for "Connection reset by peer" to avoid flakyness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100454 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100455 try
456 let handle = ch_open('localhost:9867', {'waittime': 500})
457 if ch_status(handle) != "fail"
458 " Oops, port does exists.
459 call ch_close(handle)
460 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100461 " Failed connection should wait about 500 msec. Can be longer if the
462 " computer is busy with other things.
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100463 let elapsed = reltime(start)
464 call assert_true(reltimefloat(elapsed) > 0.3)
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100465 call assert_true(reltimefloat(elapsed) < 1.5)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100466 endif
467 catch
468 if v:exception !~ 'Connection reset by peer'
469 call assert_false(1, "Caught exception: " . v:exception)
470 endif
471 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100472endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100473
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100474"""""""""
475
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100476func Test_raw_pipe()
477 if !has('job')
478 return
479 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100480 call ch_log('Test_raw_pipe()')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100481 let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
482 call assert_equal("run", job_status(job))
483 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100484 " For a change use the job where a channel is expected.
485 call ch_sendraw(job, "echo something\n")
486 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100487 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
488
Bram Moolenaar151f6562016-03-07 21:19:38 +0100489 call ch_sendraw(job, "double this\n")
490 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100491 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
492
Bram Moolenaar151f6562016-03-07 21:19:38 +0100493 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100494 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
495 finally
496 call job_stop(job)
497 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100498
499 let s:job = job
500 call s:waitFor('"dead" == job_status(s:job)')
501 let info = job_info(job)
502 call assert_equal("dead", info.status)
503 call assert_equal("term", info.stoponexit)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100504endfunc
505
506func Test_nl_pipe()
Bram Moolenaard8070362016-02-15 21:56:54 +0100507 if !has('job')
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508 return
509 endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100510 call ch_log('Test_nl_pipe()')
Bram Moolenaar1adda342016-03-12 15:39:40 +0100511 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100512 call assert_equal("run", job_status(job))
513 try
514 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100515 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100516 call assert_equal("something", ch_readraw(handle))
517
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100518 call ch_sendraw(handle, "echoerr wrong\n")
519 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
520
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100521 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100522 call assert_equal("this", ch_readraw(handle))
523 call assert_equal("AND this", ch_readraw(handle))
524
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100525 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100526 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100527 finally
528 call job_stop(job)
529 endtry
530endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100531
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100532func Test_nl_err_to_out_pipe()
533 if !has('job')
534 return
535 endif
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100536 call ch_logfile('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100537 call ch_log('Test_nl_err_to_out_pipe()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100538 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100539 call assert_equal("run", job_status(job))
540 try
541 let handle = job_getchannel(job)
542 call ch_sendraw(handle, "echo something\n")
543 call assert_equal("something", ch_readraw(handle))
544
545 call ch_sendraw(handle, "echoerr wrong\n")
546 call assert_equal("wrong", ch_readraw(handle))
547 finally
548 call job_stop(job)
Bram Moolenaar5a6ec522016-03-12 15:51:44 +0100549 call ch_logfile('')
550 let loglines = readfile('Xlog')
551 call assert_true(len(loglines) > 10)
552 let found_test = 0
553 let found_send = 0
554 let found_recv = 0
555 let found_stop = 0
556 for l in loglines
557 if l =~ 'Test_nl_err_to_out_pipe'
558 let found_test = 1
559 endif
560 if l =~ 'SEND on.*echo something'
561 let found_send = 1
562 endif
563 if l =~ 'RECV on.*something'
564 let found_recv = 1
565 endif
566 if l =~ 'Stopping job with'
567 let found_stop = 1
568 endif
569 endfor
570 call assert_equal(1, found_test)
571 call assert_equal(1, found_send)
572 call assert_equal(1, found_recv)
573 call assert_equal(1, found_stop)
574 call delete('Xlog')
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100575 endtry
576endfunc
577
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100578func Test_nl_read_file()
579 if !has('job')
580 return
581 endif
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100582 call ch_log('Test_nl_read_file()')
583 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
584 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100585 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100586 call assert_equal("run", job_status(job))
587 try
588 let handle = job_getchannel(job)
589 call assert_equal("something", ch_readraw(handle))
590 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
591 call assert_equal("this", ch_readraw(handle))
592 call assert_equal("AND this", ch_readraw(handle))
593 finally
594 call job_stop(job)
595 call delete('Xinput')
596 endtry
597endfunc
598
Bram Moolenaare98d1212016-03-08 15:37:41 +0100599func Test_nl_write_out_file()
600 if !has('job')
601 return
602 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100603 call ch_log('Test_nl_write_out_file()')
604 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100605 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100606 call assert_equal("run", job_status(job))
607 try
608 let handle = job_getchannel(job)
609 call ch_sendraw(handle, "echo line one\n")
610 call ch_sendraw(handle, "echo line two\n")
611 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100612 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100613 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
614 finally
615 call job_stop(job)
616 call delete('Xoutput')
617 endtry
618endfunc
619
620func Test_nl_write_err_file()
621 if !has('job')
622 return
623 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100624 call ch_log('Test_nl_write_err_file()')
625 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100626 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100627 call assert_equal("run", job_status(job))
628 try
629 let handle = job_getchannel(job)
630 call ch_sendraw(handle, "echoerr line one\n")
631 call ch_sendraw(handle, "echoerr line two\n")
632 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100633 call s:waitFor('len(readfile("Xoutput")) > 2')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100634 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
635 finally
636 call job_stop(job)
637 call delete('Xoutput')
638 endtry
639endfunc
640
641func Test_nl_write_both_file()
642 if !has('job')
643 return
644 endif
Bram Moolenaare98d1212016-03-08 15:37:41 +0100645 call ch_log('Test_nl_write_both_file()')
646 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100647 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100648 call assert_equal("run", job_status(job))
649 try
650 let handle = job_getchannel(job)
651 call ch_sendraw(handle, "echoerr line one\n")
652 call ch_sendraw(handle, "echo line two\n")
653 call ch_sendraw(handle, "double this\n")
654 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100655 call s:waitFor('len(readfile("Xoutput")) > 5')
Bram Moolenaare98d1212016-03-08 15:37:41 +0100656 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
657 finally
658 call job_stop(job)
659 call delete('Xoutput')
660 endtry
661endfunc
662
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100663func Run_test_pipe_to_buffer(use_name)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100664 if !has('job')
665 return
666 endif
667 call ch_log('Test_pipe_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100668 let options = {'out_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100669 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100670 let options['out_name'] = 'pipe-output'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100671 let firstline = 'Reading from channel output...'
672 else
673 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100674 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100675 quit
676 let firstline = ''
677 endif
678 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100679 call assert_equal("run", job_status(job))
680 try
681 let handle = job_getchannel(job)
682 call ch_sendraw(handle, "echo line one\n")
683 call ch_sendraw(handle, "echo line two\n")
684 call ch_sendraw(handle, "double this\n")
685 call ch_sendraw(handle, "quit\n")
686 sp pipe-output
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100687 call s:waitFor('line("$") >= 6')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100688 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100689 bwipe!
690 finally
691 call job_stop(job)
692 endtry
693endfunc
694
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695func Test_pipe_to_buffer_name()
696 call Run_test_pipe_to_buffer(1)
697endfunc
698
699func Test_pipe_to_buffer_nr()
700 call Run_test_pipe_to_buffer(0)
701endfunc
702
703func Run_test_pipe_err_to_buffer(use_name)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100704 if !has('job')
705 return
706 endif
707 call ch_log('Test_pipe_err_to_buffer()')
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100708 let options = {'err_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100709 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100710 let options['err_name'] = 'pipe-err'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100711 let firstline = 'Reading from channel error...'
712 else
713 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100714 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100715 quit
716 let firstline = ''
717 endif
718 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100719 call assert_equal("run", job_status(job))
720 try
721 let handle = job_getchannel(job)
722 call ch_sendraw(handle, "echoerr line one\n")
723 call ch_sendraw(handle, "echoerr line two\n")
724 call ch_sendraw(handle, "doubleerr this\n")
725 call ch_sendraw(handle, "quit\n")
726 sp pipe-err
727 call s:waitFor('line("$") >= 5')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100728 call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100729 bwipe!
730 finally
731 call job_stop(job)
732 endtry
733endfunc
734
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100735func Test_pipe_err_to_buffer_name()
736 call Run_test_pipe_err_to_buffer(1)
737endfunc
738
739func Test_pipe_err_to_buffer_nr()
740 call Run_test_pipe_err_to_buffer(0)
741endfunc
742
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100743func Test_pipe_both_to_buffer()
744 if !has('job')
745 return
746 endif
747 call ch_log('Test_pipe_both_to_buffer()')
748 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100749 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100750 call assert_equal("run", job_status(job))
751 try
752 let handle = job_getchannel(job)
753 call ch_sendraw(handle, "echo line one\n")
754 call ch_sendraw(handle, "echoerr line two\n")
755 call ch_sendraw(handle, "double this\n")
756 call ch_sendraw(handle, "doubleerr that\n")
757 call ch_sendraw(handle, "quit\n")
758 sp pipe-err
759 call s:waitFor('line("$") >= 7')
760 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
761 bwipe!
762 finally
763 call job_stop(job)
764 endtry
765endfunc
766
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100767func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100768 if !has('job')
769 return
770 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100771 call ch_log('Test_pipe_from_buffer()')
772
773 sp pipe-input
774 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100775 let options = {'in_io': 'buffer'}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100776 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100777 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100778 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100779 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100780 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100781
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100782 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100783 call assert_equal("run", job_status(job))
784 try
785 let handle = job_getchannel(job)
786 call assert_equal('one', ch_read(handle))
787 call assert_equal('two', ch_read(handle))
788 call assert_equal('three', ch_read(handle))
789 bwipe!
790 finally
791 call job_stop(job)
792 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100793endfunc
794
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100795func Test_pipe_from_buffer_name()
796 call Run_test_pipe_from_buffer(1)
797endfunc
798
799func Test_pipe_from_buffer_nr()
800 call Run_test_pipe_from_buffer(0)
801endfunc
802
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100803func Test_pipe_to_nameless_buffer()
804 if !has('job')
805 return
806 endif
807 call ch_log('Test_pipe_to_nameless_buffer()')
808 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100809 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100810 call assert_equal("run", job_status(job))
811 try
812 let handle = job_getchannel(job)
813 call ch_sendraw(handle, "echo line one\n")
814 call ch_sendraw(handle, "echo line two\n")
815 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100816 call s:waitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100817 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
818 bwipe!
819 finally
820 call job_stop(job)
821 endtry
822endfunc
823
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100824func Test_pipe_to_buffer_json()
825 if !has('job')
826 return
827 endif
828 call ch_log('Test_pipe_to_buffer_json()')
829 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100830 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100831 call assert_equal("run", job_status(job))
832 try
833 let handle = job_getchannel(job)
834 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
835 call ch_sendraw(handle, "echo [-2, 12.34]\n")
836 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100837 call s:waitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100838 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
839 bwipe!
840 finally
841 call job_stop(job)
842 endtry
843endfunc
844
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100845" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100846func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100847 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100848 if getline(line('$') - a:offset) == a:line
849 break
850 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100851 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100852 endfor
853endfunc
854
855func Test_pipe_io_two_buffers()
856 if !has('job')
857 return
858 endif
859 call ch_log('Test_pipe_io_two_buffers()')
860
861 " Create two buffers, one to read from and one to write to.
862 split pipe-output
863 set buftype=nofile
864 split pipe-input
865 set buftype=nofile
866
867 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100868 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
869 \ 'out_io': 'buffer', 'out_name': 'pipe-output'})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100870 call assert_equal("run", job_status(job))
871 try
872 exe "normal Gaecho hello\<CR>"
873 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100874 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100875 call assert_equal('hello', getline('$'))
876
877 exe bufwinnr('pipe-input') . "wincmd w"
878 exe "normal Gadouble this\<CR>"
879 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100880 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100881 call assert_equal('this', getline(line('$') - 1))
882 call assert_equal('AND this', getline('$'))
883
884 bwipe!
885 exe bufwinnr('pipe-input') . "wincmd w"
886 bwipe!
887 finally
888 call job_stop(job)
889 endtry
890endfunc
891
892func Test_pipe_io_one_buffer()
893 if !has('job')
894 return
895 endif
896 call ch_log('Test_pipe_io_one_buffer()')
897
898 " Create one buffer to read from and to write to.
899 split pipe-io
900 set buftype=nofile
901
902 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100903 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
904 \ 'out_io': 'buffer', 'out_name': 'pipe-io'})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100905 call assert_equal("run", job_status(job))
906 try
907 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100908 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100909 call assert_equal('hello', getline(line('$') - 1))
910
911 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100912 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100913 call assert_equal('this', getline(line('$') - 2))
914 call assert_equal('AND this', getline(line('$') - 1))
915
916 bwipe!
917 finally
918 call job_stop(job)
919 endtry
920endfunc
921
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100922func Test_pipe_null()
923 if !has('job')
924 return
925 endif
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100926 call ch_log('Test_pipe_null()')
927
928 " We cannot check that no I/O works, we only check that the job starts
929 " properly.
930 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100931 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100932 call assert_equal("run", job_status(job))
933 try
934 call assert_equal('something', ch_read(job))
935 finally
936 call job_stop(job)
937 endtry
938
939 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100940 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100941 call assert_equal("run", job_status(job))
942 try
943 call assert_equal('err-out', ch_read(job, {"part": "err"}))
944 finally
945 call job_stop(job)
946 endtry
947
948 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100949 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100950 call assert_equal("run", job_status(job))
951 try
952 call assert_equal('something', ch_read(job))
953 finally
954 call job_stop(job)
955 endtry
956
957 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100958 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100959 call assert_equal("run", job_status(job))
960 call job_stop(job)
961
962 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100963 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +0100964 call assert_equal("run", job_status(job))
965 call assert_equal('channel fail', string(job_getchannel(job)))
966 call assert_equal('fail', ch_status(job))
967 call job_stop(job)
968endfunc
969
Bram Moolenaarde279892016-03-11 22:19:44 +0100970func Test_reuse_channel()
971 if !has('job')
972 return
973 endif
974 call ch_log('Test_reuse_channel()')
975
976 let job = job_start(s:python . " test_channel_pipe.py")
977 call assert_equal("run", job_status(job))
978 let handle = job_getchannel(job)
979 try
980 call ch_sendraw(handle, "echo something\n")
981 call assert_equal("something", ch_readraw(handle))
982 finally
983 call job_stop(job)
984 endtry
985
986 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
987 call assert_equal("run", job_status(job))
988 let handle = job_getchannel(job)
989 try
990 call ch_sendraw(handle, "echo again\n")
991 call assert_equal("again", ch_readraw(handle))
992 finally
993 call job_stop(job)
994 endtry
995endfunc
996
Bram Moolenaard46ae142016-02-16 13:33:52 +0100997""""""""""
998
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100999let s:unletResponse = ''
1000func s:UnletHandler(handle, msg)
1001 let s:unletResponse = a:msg
1002 unlet s:channelfd
1003endfunc
1004
1005" Test that "unlet handle" in a handler doesn't crash Vim.
1006func s:unlet_handle(port)
1007 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001008 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001009 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001010 call assert_equal('what?', s:unletResponse)
1011endfunc
1012
1013func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001014 call ch_log('Test_unlet_handle()')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001015 call s:run_server('s:unlet_handle')
1016endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001017
Bram Moolenaard46ae142016-02-16 13:33:52 +01001018""""""""""
1019
1020let s:unletResponse = ''
1021func s:CloseHandler(handle, msg)
1022 let s:unletResponse = a:msg
1023 call ch_close(s:channelfd)
1024endfunc
1025
1026" Test that "unlet handle" in a handler doesn't crash Vim.
1027func s:close_handle(port)
1028 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001029 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001030 call s:waitFor('"what?" == s:unletResponse')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001031 call assert_equal('what?', s:unletResponse)
1032endfunc
1033
1034func Test_close_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001035 call ch_log('Test_close_handle()')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001036 call s:run_server('s:close_handle')
1037endfunc
1038
1039""""""""""
1040
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001041func Test_open_fail()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001042 call ch_log('Test_open_fail()')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001043 silent! let ch = ch_open("noserver")
1044 echo ch
1045 let d = ch
1046endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001047
1048""""""""""
1049
1050func s:open_delay(port)
1051 " Wait up to a second for the port to open.
1052 let s:chopt.waittime = 1000
1053 let channel = ch_open('localhost:' . a:port, s:chopt)
1054 unlet s:chopt.waittime
1055 if ch_status(channel) == "fail"
1056 call assert_false(1, "Can't open channel")
1057 return
1058 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001059 call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001060 call ch_close(channel)
1061endfunc
1062
1063func Test_open_delay()
1064 call ch_log('Test_open_delay()')
1065 " The server will wait half a second before creating the port.
1066 call s:run_server('s:open_delay', 'delay')
1067endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001068
1069"""""""""
1070
1071function MyFunction(a,b,c)
1072 let s:call_ret = [a:a, a:b, a:c]
1073endfunc
1074
1075function s:test_call(port)
1076 let handle = ch_open('localhost:' . a:port, s:chopt)
1077 if ch_status(handle) == "fail"
1078 call assert_false(1, "Can't open channel")
1079 return
1080 endif
1081
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001082 let s:call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001083 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001084 call s:waitFor('len(s:call_ret) > 0')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001085 call assert_equal([1, 2, 3], s:call_ret)
1086endfunc
1087
1088func Test_call()
1089 call ch_log('Test_call()')
1090 call s:run_server('s:test_call')
1091endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001092
1093"""""""""
1094
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001095let s:job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001096function MyExitCb(job, status)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001097 let s:job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001098endfunc
1099
1100function s:test_exit_callback(port)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001101 call job_setoptions(s:job, {'exit_cb': 'MyExitCb'})
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001102 let s:exit_job = s:job
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001103 call assert_equal('MyExitCb', job_info(s:job)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001104endfunc
1105
1106func Test_exit_callback()
1107 if has('job')
Bram Moolenaar9730f742016-02-28 19:50:51 +01001108 call ch_log('Test_exit_callback()')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001109 call s:run_server('s:test_exit_callback')
1110
Bram Moolenaar9730f742016-02-28 19:50:51 +01001111 " wait up to a second for the job to exit
1112 for i in range(100)
1113 if s:job_exit_ret == 'done'
1114 break
1115 endif
1116 sleep 10m
1117 " calling job_status() triggers the callback
1118 call job_status(s:exit_job)
1119 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001120
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001121 call assert_equal('done', s:job_exit_ret)
Bram Moolenaar8950a562016-03-12 15:22:55 +01001122 call assert_equal('dead', job_info(s:exit_job).status)
Bram Moolenaar9730f742016-02-28 19:50:51 +01001123 unlet s:exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001124 endif
1125endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001126
1127"""""""""
1128
1129let s:ch_close_ret = 'alive'
1130function MyCloseCb(ch)
1131 let s:ch_close_ret = 'closed'
1132endfunc
1133
1134function s:test_close_callback(port)
1135 let handle = ch_open('localhost:' . a:port, s:chopt)
1136 if ch_status(handle) == "fail"
1137 call assert_false(1, "Can't open channel")
1138 return
1139 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001140 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001141
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001142 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001143 call s:waitFor('"closed" == s:ch_close_ret')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001144 call assert_equal('closed', s:ch_close_ret)
1145endfunc
1146
1147func Test_close_callback()
1148 call ch_log('Test_close_callback()')
1149 call s:run_server('s:test_close_callback')
1150endfunc
1151
Bram Moolenaar9730f742016-02-28 19:50:51 +01001152" Uncomment this to see what happens, output is in src/testdir/channellog.
1153" call ch_logfile('channellog', 'w')