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