Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1 | " Test for channel functions. |
| 2 | scriptencoding utf-8 |
| 3 | |
Bram Moolenaar | e246925 | 2016-02-04 10:54:34 +0100 | [diff] [blame] | 4 | if !has('channel') |
| 5 | finish |
| 6 | endif |
| 7 | |
| 8 | " This test requires the Python command to run the test server. |
Bram Moolenaar | a8343c1 | 2016-02-04 22:09:48 +0100 | [diff] [blame] | 9 | " This most likely only works on Unix and Windows. |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 10 | if has('unix') |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 11 | " 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 Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 14 | finish |
| 15 | endif |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 16 | let s:python = 'python' |
Bram Moolenaar | a8343c1 | 2016-02-04 22:09:48 +0100 | [diff] [blame] | 17 | elseif has('win32') |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 18 | " 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 Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 24 | finish |
| 25 | endif |
| 26 | else |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 27 | " Can't run this test. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 28 | finish |
| 29 | endif |
| 30 | |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 31 | let s:chopt = {} |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 32 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 33 | " Run "testfunc" after sarting the server and stop the server afterwards. |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 34 | func s:run_server(testfunc, ...) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 35 | " The Python program writes the port number in Xportnr. |
| 36 | call delete("Xportnr") |
| 37 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 38 | 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 Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 45 | try |
| 46 | if has('job') |
Bram Moolenaar | 65edff8 | 2016-02-21 16:40:11 +0100 | [diff] [blame] | 47 | let s:job = job_start(cmd, {"stoponexit": "hup"}) |
| 48 | call job_setoptions(s:job, {"stoponexit": "kill"}) |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 49 | elseif has('win32') |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 50 | exe 'silent !start cmd /c start "test_channel" ' . cmd |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 51 | else |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 52 | exe 'silent !' . cmd . '&' |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 53 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 54 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 55 | " Wait for up to 2 seconds for the port number to be there. |
| 56 | let cnt = 20 |
| 57 | let l = [] |
| 58 | while cnt > 0 |
| 59 | try |
| 60 | let l = readfile("Xportnr") |
| 61 | catch |
| 62 | endtry |
| 63 | if len(l) >= 1 |
| 64 | break |
| 65 | endif |
| 66 | sleep 100m |
| 67 | let cnt -= 1 |
| 68 | endwhile |
| 69 | call delete("Xportnr") |
| 70 | |
| 71 | if len(l) == 0 |
| 72 | " Can't make the connection, give up. |
| 73 | call assert_false(1, "Can't start test_channel.py") |
| 74 | return -1 |
| 75 | endif |
| 76 | let port = l[0] |
| 77 | |
| 78 | call call(function(a:testfunc), [port]) |
| 79 | catch |
| 80 | call assert_false(1, "Caught exception: " . v:exception) |
| 81 | finally |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 82 | call s:kill_server() |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 83 | endtry |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 84 | endfunc |
| 85 | |
| 86 | func s:kill_server() |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 87 | if has('job') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 88 | if exists('s:job') |
| 89 | call job_stop(s:job) |
| 90 | unlet s:job |
| 91 | endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 92 | elseif has('win32') |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 93 | call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 94 | else |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 95 | call system("pkill -f test_channel.py") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 96 | endif |
| 97 | endfunc |
| 98 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 99 | let s:responseMsg = '' |
| 100 | func s:RequestHandler(handle, msg) |
| 101 | let s:responseHandle = a:handle |
| 102 | let s:responseMsg = a:msg |
| 103 | endfunc |
| 104 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 105 | func s:communicate(port) |
| 106 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 107 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 108 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 109 | return |
| 110 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 111 | |
| 112 | " Simple string request and reply. |
| 113 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 114 | |
| 115 | " Request that triggers sending two ex commands. These will usually be |
| 116 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 117 | " tiny bit for the commands to get executed. |
| 118 | call assert_equal('ok', ch_sendexpr(handle, 'make change')) |
| 119 | sleep 10m |
| 120 | call assert_equal('added1', getline(line('$') - 1)) |
| 121 | call assert_equal('added2', getline('$')) |
| 122 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 123 | call assert_equal('ok', ch_sendexpr(handle, 'do normal')) |
| 124 | sleep 10m |
| 125 | call assert_equal('added more', getline('$')) |
| 126 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 127 | " Send a request with a specific handler. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 128 | call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'}) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 129 | sleep 10m |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 130 | if !exists('s:responseHandle') |
| 131 | call assert_false(1, 's:responseHandle was not set') |
| 132 | else |
| 133 | call assert_equal(handle, s:responseHandle) |
| 134 | endif |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 135 | call assert_equal('got it', s:responseMsg) |
| 136 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 137 | unlet s:responseHandle |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 138 | let s:responseMsg = '' |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 139 | call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')}) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 140 | sleep 10m |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 141 | if !exists('s:responseHandle') |
| 142 | call assert_false(1, 's:responseHandle was not set') |
| 143 | else |
| 144 | call assert_equal(handle, s:responseHandle) |
| 145 | endif |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 146 | call assert_equal('got it', s:responseMsg) |
| 147 | |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 148 | " check setting options (without testing the effect) |
| 149 | call ch_setoptions(handle, {'callback': 's:NotUsed'}) |
Bram Moolenaar | 1f6ef66 | 2016-02-19 22:59:44 +0100 | [diff] [blame] | 150 | call ch_setoptions(handle, {'timeout': 1111}) |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 151 | call ch_setoptions(handle, {'mode': 'json'}) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 152 | call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475") |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 153 | call ch_setoptions(handle, {'callback': ''}) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 154 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 155 | " Send an eval request that works. |
| 156 | call assert_equal('ok', ch_sendexpr(handle, 'eval-works')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 157 | sleep 10m |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 158 | call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result')) |
| 159 | |
| 160 | " Send an eval request that fails. |
| 161 | call assert_equal('ok', ch_sendexpr(handle, 'eval-fails')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 162 | sleep 10m |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 163 | call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 164 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 165 | " Send an eval request that works but can't be encoded. |
| 166 | call assert_equal('ok', ch_sendexpr(handle, 'eval-error')) |
| 167 | sleep 10m |
| 168 | call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 169 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 170 | " Send a bad eval request. There will be no response. |
| 171 | call assert_equal('ok', ch_sendexpr(handle, 'eval-bad')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 172 | sleep 10m |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 173 | call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 174 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 175 | " Send an expr request |
| 176 | call assert_equal('ok', ch_sendexpr(handle, 'an expr')) |
| 177 | sleep 10m |
| 178 | call assert_equal('one', getline(line('$') - 2)) |
| 179 | call assert_equal('two', getline(line('$') - 1)) |
| 180 | call assert_equal('three', getline('$')) |
| 181 | |
| 182 | " Request a redraw, we don't check for the effect. |
| 183 | call assert_equal('ok', ch_sendexpr(handle, 'redraw')) |
| 184 | call assert_equal('ok', ch_sendexpr(handle, 'redraw!')) |
| 185 | |
| 186 | call assert_equal('ok', ch_sendexpr(handle, 'empty-request')) |
| 187 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 188 | " Reading while there is nothing available. |
Bram Moolenaar | af7559f | 2016-02-20 21:48:25 +0100 | [diff] [blame] | 189 | " TODO: make this work for MS-Windows |
| 190 | if has('unix') |
| 191 | call assert_equal(v:none, ch_read(handle, {'timeout': 0})) |
| 192 | let start = reltime() |
| 193 | call assert_equal(v:none, ch_read(handle, {'timeout': 333})) |
| 194 | let elapsed = reltime(start) |
| 195 | call assert_true(reltimefloat(elapsed) > 0.3) |
| 196 | call assert_true(reltimefloat(elapsed) < 0.6) |
| 197 | endif |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 198 | |
| 199 | " Send without waiting for a response, then wait for a response. |
| 200 | call ch_sendexpr(handle, 'wait a bit', {'callback': 0}) |
| 201 | let resp = ch_read(handle) |
| 202 | call assert_equal(type([]), type(resp)) |
| 203 | call assert_equal(type(11), type(resp[0])) |
| 204 | call assert_equal('waited', resp[1]) |
| 205 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 206 | " make the server quit, can't check if this works, should not hang. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 207 | call ch_sendexpr(handle, '!quit!', {'callback': 0}) |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 208 | endfunc |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 209 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 210 | func Test_communicate() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 211 | call ch_log('Test_communicate()') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 212 | call s:run_server('s:communicate') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 213 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 214 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 215 | " Test that we can open two channels. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 216 | func s:two_channels(port) |
Bram Moolenaar | 39b2127 | 2016-02-10 23:28:21 +0100 | [diff] [blame] | 217 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 218 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 219 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 220 | return |
| 221 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 222 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 223 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 224 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 225 | let newhandle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 226 | if ch_status(newhandle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 227 | call assert_false(1, "Can't open second channel") |
| 228 | return |
| 229 | endif |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 230 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 231 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 232 | |
| 233 | call ch_close(handle) |
| 234 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 235 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 236 | call ch_close(newhandle) |
| 237 | endfunc |
| 238 | |
| 239 | func Test_two_channels() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 240 | call ch_log('Test_two_channels()') |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 241 | call s:run_server('s:two_channels') |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 242 | endfunc |
| 243 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 244 | " Test that a server crash is handled gracefully. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 245 | func s:server_crash(port) |
| 246 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 247 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 248 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 249 | return |
| 250 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 251 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 252 | call ch_sendexpr(handle, '!crash!') |
| 253 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 254 | sleep 10m |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 255 | endfunc |
| 256 | |
| 257 | func Test_server_crash() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 258 | call ch_log('Test_server_crash()') |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 259 | call s:run_server('s:server_crash') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 260 | endfunc |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 261 | |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 262 | let s:reply = "" |
| 263 | func s:Handler(chan, msg) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 264 | unlet s:reply |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 265 | let s:reply = a:msg |
| 266 | endfunc |
| 267 | |
| 268 | func s:channel_handler(port) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 269 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 270 | if ch_status(handle) == "fail" |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 271 | call assert_false(1, "Can't open channel") |
| 272 | return |
| 273 | endif |
| 274 | |
| 275 | " Test that it works while waiting on a numbered message. |
| 276 | call assert_equal('ok', ch_sendexpr(handle, 'call me')) |
| 277 | sleep 10m |
| 278 | call assert_equal('we called you', s:reply) |
| 279 | |
| 280 | " Test that it works while not waiting on a numbered message. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 281 | call ch_sendexpr(handle, 'call me again', {'callback': 0}) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 282 | sleep 10m |
| 283 | call assert_equal('we did call you', s:reply) |
| 284 | endfunc |
| 285 | |
| 286 | func Test_channel_handler() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 287 | call ch_log('Test_channel_handler()') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 288 | let s:chopt.callback = 's:Handler' |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 289 | call s:run_server('s:channel_handler') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 290 | let s:chopt.callback = function('s:Handler') |
| 291 | call s:run_server('s:channel_handler') |
| 292 | unlet s:chopt.callback |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 293 | endfunc |
| 294 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 295 | " Test that trying to connect to a non-existing port fails quickly. |
| 296 | func Test_connect_waittime() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 297 | call ch_log('Test_connect_waittime()') |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 298 | let start = reltime() |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame] | 299 | let handle = ch_open('localhost:9876', s:chopt) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 300 | if ch_status(handle) != "fail" |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 301 | " Oops, port does exists. |
| 302 | call ch_close(handle) |
| 303 | else |
| 304 | let elapsed = reltime(start) |
Bram Moolenaar | 74f5e65 | 2016-02-07 21:44:49 +0100 | [diff] [blame] | 305 | call assert_true(reltimefloat(elapsed) < 1.0) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 306 | endif |
| 307 | |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 308 | " We intend to use a socket that doesn't exist and wait for half a second |
| 309 | " before giving up. If the socket does exist it can fail in various ways. |
| 310 | " Check for "Connection reset by peer" to avoid flakyness. |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 311 | let start = reltime() |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 312 | try |
| 313 | let handle = ch_open('localhost:9867', {'waittime': 500}) |
| 314 | if ch_status(handle) != "fail" |
| 315 | " Oops, port does exists. |
| 316 | call ch_close(handle) |
| 317 | else |
| 318 | " Failed connection should wait about 500 msec. |
| 319 | let elapsed = reltime(start) |
| 320 | call assert_true(reltimefloat(elapsed) > 0.3) |
| 321 | call assert_true(reltimefloat(elapsed) < 1.0) |
| 322 | endif |
| 323 | catch |
| 324 | if v:exception !~ 'Connection reset by peer' |
| 325 | call assert_false(1, "Caught exception: " . v:exception) |
| 326 | endif |
| 327 | endtry |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 328 | endfunc |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 329 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 330 | func Test_raw_pipe() |
| 331 | if !has('job') |
| 332 | return |
| 333 | endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 334 | call ch_log('Test_raw_pipe()') |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 335 | let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'}) |
| 336 | call assert_equal("run", job_status(job)) |
| 337 | try |
| 338 | let handle = job_getchannel(job) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 339 | call ch_sendraw(handle, "echo something\n", {'callback': 0}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 340 | let msg = ch_readraw(handle) |
| 341 | call assert_equal("something\n", substitute(msg, "\r", "", 'g')) |
| 342 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 343 | call ch_sendraw(handle, "double this\n", {'callback': 0}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 344 | let msg = ch_readraw(handle) |
| 345 | call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g')) |
| 346 | |
| 347 | let reply = ch_sendraw(handle, "quit\n") |
| 348 | call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g')) |
| 349 | finally |
| 350 | call job_stop(job) |
| 351 | endtry |
| 352 | endfunc |
| 353 | |
| 354 | func Test_nl_pipe() |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 355 | if !has('job') |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 356 | return |
| 357 | endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 358 | call ch_log('Test_nl_pipe()') |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 359 | let job = job_start(s:python . " test_channel_pipe.py") |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 360 | call assert_equal("run", job_status(job)) |
| 361 | try |
| 362 | let handle = job_getchannel(job) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 363 | call ch_sendraw(handle, "echo something\n", {'callback': 0}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 364 | call assert_equal("something", ch_readraw(handle)) |
| 365 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 366 | call ch_sendraw(handle, "double this\n", {'callback': 0}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 367 | call assert_equal("this", ch_readraw(handle)) |
| 368 | call assert_equal("AND this", ch_readraw(handle)) |
| 369 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 370 | let reply = ch_sendraw(handle, "quit\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 371 | call assert_equal("Goodbye!", reply) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 372 | finally |
| 373 | call job_stop(job) |
| 374 | endtry |
| 375 | endfunc |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 376 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 377 | """""""""" |
| 378 | |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 379 | let s:unletResponse = '' |
| 380 | func s:UnletHandler(handle, msg) |
| 381 | let s:unletResponse = a:msg |
| 382 | unlet s:channelfd |
| 383 | endfunc |
| 384 | |
| 385 | " Test that "unlet handle" in a handler doesn't crash Vim. |
| 386 | func s:unlet_handle(port) |
| 387 | let s:channelfd = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 388 | call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')}) |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 389 | sleep 10m |
| 390 | call assert_equal('what?', s:unletResponse) |
| 391 | endfunc |
| 392 | |
| 393 | func Test_unlet_handle() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 394 | call ch_log('Test_unlet_handle()') |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 395 | call s:run_server('s:unlet_handle') |
| 396 | endfunc |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 397 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 398 | """""""""" |
| 399 | |
| 400 | let s:unletResponse = '' |
| 401 | func s:CloseHandler(handle, msg) |
| 402 | let s:unletResponse = a:msg |
| 403 | call ch_close(s:channelfd) |
| 404 | endfunc |
| 405 | |
| 406 | " Test that "unlet handle" in a handler doesn't crash Vim. |
| 407 | func s:close_handle(port) |
| 408 | let s:channelfd = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 409 | call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')}) |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 410 | sleep 10m |
| 411 | call assert_equal('what?', s:unletResponse) |
| 412 | endfunc |
| 413 | |
| 414 | func Test_close_handle() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 415 | call ch_log('Test_close_handle()') |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 416 | call s:run_server('s:close_handle') |
| 417 | endfunc |
| 418 | |
| 419 | """""""""" |
| 420 | |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 421 | func Test_open_fail() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 422 | call ch_log('Test_open_fail()') |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 423 | silent! let ch = ch_open("noserver") |
| 424 | echo ch |
| 425 | let d = ch |
| 426 | endfunc |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 427 | |
| 428 | """""""""" |
| 429 | |
| 430 | func s:open_delay(port) |
| 431 | " Wait up to a second for the port to open. |
| 432 | let s:chopt.waittime = 1000 |
| 433 | let channel = ch_open('localhost:' . a:port, s:chopt) |
| 434 | unlet s:chopt.waittime |
| 435 | if ch_status(channel) == "fail" |
| 436 | call assert_false(1, "Can't open channel") |
| 437 | return |
| 438 | endif |
| 439 | call assert_equal('got it', ch_sendexpr(channel, 'hello!')) |
| 440 | call ch_close(channel) |
| 441 | endfunc |
| 442 | |
| 443 | func Test_open_delay() |
| 444 | call ch_log('Test_open_delay()') |
| 445 | " The server will wait half a second before creating the port. |
| 446 | call s:run_server('s:open_delay', 'delay') |
| 447 | endfunc |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 448 | |
| 449 | """"""""" |
| 450 | |
| 451 | function MyFunction(a,b,c) |
| 452 | let s:call_ret = [a:a, a:b, a:c] |
| 453 | endfunc |
| 454 | |
| 455 | function s:test_call(port) |
| 456 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 457 | if ch_status(handle) == "fail" |
| 458 | call assert_false(1, "Can't open channel") |
| 459 | return |
| 460 | endif |
| 461 | |
| 462 | call assert_equal('ok', ch_sendexpr(handle, 'call-func')) |
| 463 | sleep 20m |
| 464 | call assert_equal([1, 2, 3], s:call_ret) |
| 465 | endfunc |
| 466 | |
| 467 | func Test_call() |
| 468 | call ch_log('Test_call()') |
| 469 | call s:run_server('s:test_call') |
| 470 | endfunc |