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