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 | a8343c1 | 2016-02-04 22:09:48 +0100 | [diff] [blame] | 16 | elseif has('win32') |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 17 | " Use Python Launcher for Windows (py.exe). |
| 18 | if !executable('py') |
| 19 | finish |
| 20 | endif |
| 21 | else |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 22 | " Can't run this test. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 23 | finish |
| 24 | endif |
| 25 | |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame] | 26 | let s:chopt = has('macunix') ? {'waittime' : 1} : {} |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 27 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 28 | " Run "testfunc" after sarting the server and stop the server afterwards. |
| 29 | func s:run_server(testfunc) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 30 | " The Python program writes the port number in Xportnr. |
| 31 | call delete("Xportnr") |
| 32 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 33 | try |
| 34 | if has('job') |
| 35 | let s:job = job_start("python test_channel.py") |
| 36 | elseif has('win32') |
| 37 | silent !start cmd /c start "test_channel" py test_channel.py |
| 38 | else |
| 39 | silent !python test_channel.py& |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 40 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 41 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 42 | " Wait for up to 2 seconds for the port number to be there. |
| 43 | let cnt = 20 |
| 44 | let l = [] |
| 45 | while cnt > 0 |
| 46 | try |
| 47 | let l = readfile("Xportnr") |
| 48 | catch |
| 49 | endtry |
| 50 | if len(l) >= 1 |
| 51 | break |
| 52 | endif |
| 53 | sleep 100m |
| 54 | let cnt -= 1 |
| 55 | endwhile |
| 56 | call delete("Xportnr") |
| 57 | |
| 58 | if len(l) == 0 |
| 59 | " Can't make the connection, give up. |
| 60 | call assert_false(1, "Can't start test_channel.py") |
| 61 | return -1 |
| 62 | endif |
| 63 | let port = l[0] |
| 64 | |
| 65 | call call(function(a:testfunc), [port]) |
| 66 | catch |
| 67 | call assert_false(1, "Caught exception: " . v:exception) |
| 68 | finally |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 69 | call s:kill_server() |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 70 | endtry |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 71 | endfunc |
| 72 | |
| 73 | func s:kill_server() |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 74 | if has('job') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 75 | if exists('s:job') |
| 76 | call job_stop(s:job) |
| 77 | unlet s:job |
| 78 | endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 79 | elseif has('win32') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 80 | call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"') |
| 81 | else |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 82 | call system("pkill -f test_channel.py") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 83 | endif |
| 84 | endfunc |
| 85 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 86 | let s:responseMsg = '' |
| 87 | func s:RequestHandler(handle, msg) |
| 88 | let s:responseHandle = a:handle |
| 89 | let s:responseMsg = a:msg |
| 90 | endfunc |
| 91 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 92 | func s:communicate(port) |
| 93 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 94 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 95 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 96 | return |
| 97 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 98 | |
| 99 | " Simple string request and reply. |
| 100 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 101 | |
| 102 | " Request that triggers sending two ex commands. These will usually be |
| 103 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 104 | " tiny bit for the commands to get executed. |
| 105 | call assert_equal('ok', ch_sendexpr(handle, 'make change')) |
| 106 | sleep 10m |
| 107 | call assert_equal('added1', getline(line('$') - 1)) |
| 108 | call assert_equal('added2', getline('$')) |
| 109 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 110 | call assert_equal('ok', ch_sendexpr(handle, 'do normal')) |
| 111 | sleep 10m |
| 112 | call assert_equal('added more', getline('$')) |
| 113 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 114 | " Send a request with a specific handler. |
| 115 | call ch_sendexpr(handle, 'hello!', 's:RequestHandler') |
| 116 | sleep 10m |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 117 | if !exists('s:responseHandle') |
| 118 | call assert_false(1, 's:responseHandle was not set') |
| 119 | else |
| 120 | call assert_equal(handle, s:responseHandle) |
| 121 | endif |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 122 | call assert_equal('got it', s:responseMsg) |
| 123 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 124 | unlet s:responseHandle |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 125 | let s:responseMsg = '' |
| 126 | call ch_sendexpr(handle, 'hello!', function('s:RequestHandler')) |
| 127 | sleep 10m |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 128 | if !exists('s:responseHandle') |
| 129 | call assert_false(1, 's:responseHandle was not set') |
| 130 | else |
| 131 | call assert_equal(handle, s:responseHandle) |
| 132 | endif |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 133 | call assert_equal('got it', s:responseMsg) |
| 134 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 135 | " Send an eval request that works. |
| 136 | call assert_equal('ok', ch_sendexpr(handle, 'eval-works')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 137 | sleep 10m |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 138 | call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result')) |
| 139 | |
| 140 | " Send an eval request that fails. |
| 141 | call assert_equal('ok', ch_sendexpr(handle, 'eval-fails')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 142 | sleep 10m |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 143 | call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 144 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 145 | " Send an eval request that works but can't be encoded. |
| 146 | call assert_equal('ok', ch_sendexpr(handle, 'eval-error')) |
| 147 | sleep 10m |
| 148 | call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 149 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 150 | " Send a bad eval request. There will be no response. |
| 151 | call assert_equal('ok', ch_sendexpr(handle, 'eval-bad')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 152 | sleep 10m |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 153 | call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 154 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 155 | " Send an expr request |
| 156 | call assert_equal('ok', ch_sendexpr(handle, 'an expr')) |
| 157 | sleep 10m |
| 158 | call assert_equal('one', getline(line('$') - 2)) |
| 159 | call assert_equal('two', getline(line('$') - 1)) |
| 160 | call assert_equal('three', getline('$')) |
| 161 | |
| 162 | " Request a redraw, we don't check for the effect. |
| 163 | call assert_equal('ok', ch_sendexpr(handle, 'redraw')) |
| 164 | call assert_equal('ok', ch_sendexpr(handle, 'redraw!')) |
| 165 | |
| 166 | call assert_equal('ok', ch_sendexpr(handle, 'empty-request')) |
| 167 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 168 | " make the server quit, can't check if this works, should not hang. |
| 169 | call ch_sendexpr(handle, '!quit!', 0) |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 170 | endfunc |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 171 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 172 | func Test_communicate() |
| 173 | call s:run_server('s:communicate') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 174 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 175 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 176 | " Test that we can open two channels. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 177 | func s:two_channels(port) |
Bram Moolenaar | 39b2127 | 2016-02-10 23:28:21 +0100 | [diff] [blame] | 178 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 179 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 180 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 181 | return |
| 182 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 183 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 184 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 185 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 186 | let newhandle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 187 | if ch_status(newhandle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 188 | call assert_false(1, "Can't open second channel") |
| 189 | return |
| 190 | endif |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 191 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 192 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 193 | |
| 194 | call ch_close(handle) |
| 195 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 196 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 197 | call ch_close(newhandle) |
| 198 | endfunc |
| 199 | |
| 200 | func Test_two_channels() |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 201 | call s:run_server('s:two_channels') |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 202 | endfunc |
| 203 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 204 | " Test that a server crash is handled gracefully. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 205 | func s:server_crash(port) |
| 206 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 207 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 208 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 209 | return |
| 210 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 211 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 212 | call ch_sendexpr(handle, '!crash!') |
| 213 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 214 | sleep 10m |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 215 | endfunc |
| 216 | |
| 217 | func Test_server_crash() |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 218 | call s:run_server('s:server_crash') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 219 | endfunc |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 220 | |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 221 | let s:reply = "" |
| 222 | func s:Handler(chan, msg) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 223 | unlet s:reply |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 224 | let s:reply = a:msg |
| 225 | endfunc |
| 226 | |
| 227 | func s:channel_handler(port) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 228 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 229 | if ch_status(handle) == "fail" |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 230 | call assert_false(1, "Can't open channel") |
| 231 | return |
| 232 | endif |
| 233 | |
| 234 | " Test that it works while waiting on a numbered message. |
| 235 | call assert_equal('ok', ch_sendexpr(handle, 'call me')) |
| 236 | sleep 10m |
| 237 | call assert_equal('we called you', s:reply) |
| 238 | |
| 239 | " Test that it works while not waiting on a numbered message. |
| 240 | call ch_sendexpr(handle, 'call me again', 0) |
| 241 | sleep 10m |
| 242 | call assert_equal('we did call you', s:reply) |
| 243 | endfunc |
| 244 | |
| 245 | func Test_channel_handler() |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 246 | let s:chopt.callback = 's:Handler' |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 247 | call s:run_server('s:channel_handler') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 248 | let s:chopt.callback = function('s:Handler') |
| 249 | call s:run_server('s:channel_handler') |
| 250 | unlet s:chopt.callback |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 251 | endfunc |
| 252 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 253 | " Test that trying to connect to a non-existing port fails quickly. |
| 254 | func Test_connect_waittime() |
| 255 | let start = reltime() |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame] | 256 | let handle = ch_open('localhost:9876', s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 257 | if ch_status(handle) == "fail" |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 258 | " Oops, port does exists. |
| 259 | call ch_close(handle) |
| 260 | else |
| 261 | let elapsed = reltime(start) |
Bram Moolenaar | 74f5e65 | 2016-02-07 21:44:49 +0100 | [diff] [blame] | 262 | call assert_true(reltimefloat(elapsed) < 1.0) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 263 | endif |
| 264 | |
| 265 | let start = reltime() |
| 266 | let handle = ch_open('localhost:9867', {'waittime': 2000}) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 267 | if ch_status(handle) != "fail" |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 268 | " Oops, port does exists. |
| 269 | call ch_close(handle) |
| 270 | else |
Bram Moolenaar | 0fa98e7 | 2016-02-07 22:21:19 +0100 | [diff] [blame] | 271 | " Failed connection doesn't wait the full time on Unix. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 272 | " TODO: why is MS-Windows different? |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 273 | let elapsed = reltime(start) |
Bram Moolenaar | 0fa98e7 | 2016-02-07 22:21:19 +0100 | [diff] [blame] | 274 | call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0)) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 275 | endif |
| 276 | endfunc |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 277 | |
| 278 | func Test_pipe() |
| 279 | if !has('job') || !has('unix') |
| 280 | return |
| 281 | endif |
| 282 | let job = job_start("python test_channel_pipe.py") |
| 283 | call assert_equal("run", job_status(job)) |
| 284 | try |
| 285 | let handle = job_getchannel(job) |
| 286 | call ch_sendraw(handle, "echo something\n", 0) |
| 287 | call assert_equal("something\n", ch_readraw(handle)) |
| 288 | let reply = ch_sendraw(handle, "quit\n") |
| 289 | call assert_equal("Goodbye!\n", reply) |
| 290 | finally |
| 291 | call job_stop(job) |
| 292 | endtry |
| 293 | endfunc |