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 | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 22 | finish |
| 23 | endif |
| 24 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 25 | let s:port = -1 |
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 | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 28 | func s:start_server() |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 29 | " The Python program writes the port number in Xportnr. |
| 30 | call delete("Xportnr") |
| 31 | |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 32 | if has('job') |
| 33 | let s:job = job_start("python test_channel.py") |
| 34 | elseif has('win32') |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 35 | silent !start cmd /c start "test_channel" py test_channel.py |
| 36 | else |
Bram Moolenaar | f92591f | 2016-02-03 20:22:32 +0100 | [diff] [blame] | 37 | silent !python test_channel.py& |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 38 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 39 | |
| 40 | " Wait for up to 2 seconds for the port number to be there. |
| 41 | let cnt = 20 |
| 42 | let l = [] |
| 43 | while cnt > 0 |
| 44 | try |
| 45 | let l = readfile("Xportnr") |
| 46 | catch |
| 47 | endtry |
| 48 | if len(l) >= 1 |
| 49 | break |
| 50 | endif |
| 51 | sleep 100m |
| 52 | let cnt -= 1 |
| 53 | endwhile |
| 54 | call delete("Xportnr") |
| 55 | |
| 56 | if len(l) == 0 |
| 57 | " Can't make the connection, give up. |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 58 | call s:kill_server() |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 59 | call assert_false(1, "Can't start test_channel.py") |
| 60 | return -1 |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 61 | endif |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 62 | let s:port = l[0] |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 63 | |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame^] | 64 | let handle = ch_open('localhost:' . s:port, s:chopt) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 65 | return handle |
| 66 | endfunc |
| 67 | |
| 68 | func s:kill_server() |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 69 | if has('job') |
| 70 | call job_stop(s:job) |
| 71 | elseif has('win32') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 72 | call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"') |
| 73 | else |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 74 | call system("pkill -f test_channel.py") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 75 | endif |
| 76 | endfunc |
| 77 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 78 | let s:responseHandle = -1 |
| 79 | let s:responseMsg = '' |
| 80 | func s:RequestHandler(handle, msg) |
| 81 | let s:responseHandle = a:handle |
| 82 | let s:responseMsg = a:msg |
| 83 | endfunc |
| 84 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 85 | func Test_communicate() |
| 86 | let handle = s:start_server() |
| 87 | if handle < 0 |
| 88 | return |
| 89 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 90 | |
| 91 | " Simple string request and reply. |
| 92 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 93 | |
| 94 | " Request that triggers sending two ex commands. These will usually be |
| 95 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 96 | " tiny bit for the commands to get executed. |
| 97 | call assert_equal('ok', ch_sendexpr(handle, 'make change')) |
| 98 | sleep 10m |
| 99 | call assert_equal('added1', getline(line('$') - 1)) |
| 100 | call assert_equal('added2', getline('$')) |
| 101 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 102 | call assert_equal('ok', ch_sendexpr(handle, 'do normal')) |
| 103 | sleep 10m |
| 104 | call assert_equal('added more', getline('$')) |
| 105 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 106 | " Send a request with a specific handler. |
| 107 | call ch_sendexpr(handle, 'hello!', 's:RequestHandler') |
| 108 | sleep 10m |
| 109 | call assert_equal(handle, s:responseHandle) |
| 110 | call assert_equal('got it', s:responseMsg) |
| 111 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 112 | " Send an eval request that works. |
| 113 | call assert_equal('ok', ch_sendexpr(handle, 'eval-works')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 114 | sleep 10m |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 115 | call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result')) |
| 116 | |
| 117 | " Send an eval request that fails. |
| 118 | call assert_equal('ok', ch_sendexpr(handle, 'eval-fails')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 119 | sleep 10m |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 120 | call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 121 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 122 | " Send an eval request that works but can't be encoded. |
| 123 | call assert_equal('ok', ch_sendexpr(handle, 'eval-error')) |
| 124 | sleep 10m |
| 125 | call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 126 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 127 | " Send a bad eval request. There will be no response. |
| 128 | call assert_equal('ok', ch_sendexpr(handle, 'eval-bad')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 129 | sleep 10m |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 130 | call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 131 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 132 | " Send an expr request |
| 133 | call assert_equal('ok', ch_sendexpr(handle, 'an expr')) |
| 134 | sleep 10m |
| 135 | call assert_equal('one', getline(line('$') - 2)) |
| 136 | call assert_equal('two', getline(line('$') - 1)) |
| 137 | call assert_equal('three', getline('$')) |
| 138 | |
| 139 | " Request a redraw, we don't check for the effect. |
| 140 | call assert_equal('ok', ch_sendexpr(handle, 'redraw')) |
| 141 | call assert_equal('ok', ch_sendexpr(handle, 'redraw!')) |
| 142 | |
| 143 | call assert_equal('ok', ch_sendexpr(handle, 'empty-request')) |
| 144 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 145 | " make the server quit, can't check if this works, should not hang. |
| 146 | call ch_sendexpr(handle, '!quit!', 0) |
| 147 | |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 148 | call s:kill_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 149 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 150 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 151 | " Test that we can open two channels. |
| 152 | func Test_two_channels() |
| 153 | let handle = s:start_server() |
| 154 | if handle < 0 |
| 155 | return |
| 156 | endif |
| 157 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 158 | |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame^] | 159 | let newhandle = ch_open('localhost:' . s:port, s:chopt) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 160 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 161 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 162 | |
| 163 | call ch_close(handle) |
| 164 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 165 | |
| 166 | call s:kill_server() |
| 167 | endfunc |
| 168 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 169 | " Test that a server crash is handled gracefully. |
| 170 | func Test_server_crash() |
| 171 | let handle = s:start_server() |
| 172 | if handle < 0 |
| 173 | return |
| 174 | endif |
| 175 | call ch_sendexpr(handle, '!crash!') |
| 176 | |
| 177 | " kill the server in case if failed to crash |
| 178 | sleep 10m |
| 179 | call s:kill_server() |
| 180 | endfunc |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 181 | |
| 182 | " Test that trying to connect to a non-existing port fails quickly. |
| 183 | func Test_connect_waittime() |
| 184 | let start = reltime() |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame^] | 185 | let handle = ch_open('localhost:9876', s:chopt) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 186 | if handle >= 0 |
| 187 | " Oops, port does exists. |
| 188 | call ch_close(handle) |
| 189 | else |
| 190 | let elapsed = reltime(start) |
Bram Moolenaar | 74f5e65 | 2016-02-07 21:44:49 +0100 | [diff] [blame] | 191 | call assert_true(reltimefloat(elapsed) < 1.0) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 192 | endif |
| 193 | |
| 194 | let start = reltime() |
| 195 | let handle = ch_open('localhost:9867', {'waittime': 2000}) |
| 196 | if handle >= 0 |
| 197 | " Oops, port does exists. |
| 198 | call ch_close(handle) |
| 199 | else |
Bram Moolenaar | 0fa98e7 | 2016-02-07 22:21:19 +0100 | [diff] [blame] | 200 | " Failed connection doesn't wait the full time on Unix. |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 201 | let elapsed = reltime(start) |
Bram Moolenaar | 0fa98e7 | 2016-02-07 22:21:19 +0100 | [diff] [blame] | 202 | call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0)) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 203 | endif |
| 204 | endfunc |