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