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