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 | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 31 | let s:chopt = {} |
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. |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 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 | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 38 | if a:0 == 1 |
| 39 | let arg = ' ' . a:1 |
| 40 | else |
| 41 | let arg = '' |
| 42 | endif |
| 43 | let cmd = s:python . " test_channel.py" . arg |
| 44 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 45 | try |
| 46 | if has('job') |
Bram Moolenaar | 65edff8 | 2016-02-21 16:40:11 +0100 | [diff] [blame] | 47 | let s:job = job_start(cmd, {"stoponexit": "hup"}) |
| 48 | call job_setoptions(s:job, {"stoponexit": "kill"}) |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 49 | elseif has('win32') |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 50 | exe 'silent !start cmd /c start "test_channel" ' . cmd |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 51 | else |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 52 | exe 'silent !' . cmd . '&' |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 53 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 54 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 55 | " Wait for up to 2 seconds for the port number to be there. |
| 56 | let cnt = 20 |
| 57 | let l = [] |
| 58 | while cnt > 0 |
| 59 | try |
| 60 | let l = readfile("Xportnr") |
| 61 | catch |
| 62 | endtry |
| 63 | if len(l) >= 1 |
| 64 | break |
| 65 | endif |
| 66 | sleep 100m |
| 67 | let cnt -= 1 |
| 68 | endwhile |
| 69 | call delete("Xportnr") |
| 70 | |
| 71 | if len(l) == 0 |
| 72 | " Can't make the connection, give up. |
| 73 | call assert_false(1, "Can't start test_channel.py") |
| 74 | return -1 |
| 75 | endif |
| 76 | let port = l[0] |
| 77 | |
| 78 | call call(function(a:testfunc), [port]) |
| 79 | catch |
| 80 | call assert_false(1, "Caught exception: " . v:exception) |
| 81 | finally |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 82 | call s:kill_server() |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 83 | endtry |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 84 | endfunc |
| 85 | |
| 86 | func s:kill_server() |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 87 | if has('job') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 88 | if exists('s:job') |
| 89 | call job_stop(s:job) |
| 90 | unlet s:job |
| 91 | endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 92 | elseif has('win32') |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 93 | 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] | 94 | else |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 95 | call system("pkill -f test_channel.py") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 96 | endif |
| 97 | endfunc |
| 98 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 99 | let s:responseMsg = '' |
| 100 | func s:RequestHandler(handle, msg) |
| 101 | let s:responseHandle = a:handle |
| 102 | let s:responseMsg = a:msg |
| 103 | endfunc |
| 104 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 105 | func s:communicate(port) |
| 106 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 107 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 108 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 109 | return |
| 110 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 111 | |
| 112 | " Simple string request and reply. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 113 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 114 | |
| 115 | " Request that triggers sending two ex commands. These will usually be |
| 116 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 117 | " tiny bit for the commands to get executed. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 118 | call assert_equal('ok', ch_evalexpr(handle, 'make change')) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 119 | sleep 10m |
| 120 | call assert_equal('added1', getline(line('$') - 1)) |
| 121 | call assert_equal('added2', getline('$')) |
| 122 | |
Bram Moolenaar | da94fdf | 2016-03-03 18:09:10 +0100 | [diff] [blame] | 123 | call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100})) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 124 | sleep 10m |
| 125 | call assert_equal('added more', getline('$')) |
| 126 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 127 | " Send a request with a specific handler. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 128 | call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'}) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 129 | sleep 10m |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 130 | if !exists('s:responseHandle') |
| 131 | call assert_false(1, 's:responseHandle was not set') |
| 132 | else |
| 133 | call assert_equal(handle, s:responseHandle) |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 134 | unlet s:responseHandle |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 135 | endif |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 136 | call assert_equal('got it', s:responseMsg) |
| 137 | |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 138 | let s:responseMsg = '' |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 139 | call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')}) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 140 | sleep 10m |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 141 | if !exists('s:responseHandle') |
| 142 | call assert_false(1, 's:responseHandle was not set') |
| 143 | else |
| 144 | call assert_equal(handle, s:responseHandle) |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 145 | unlet s:responseHandle |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 146 | endif |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 147 | call assert_equal('got it', s:responseMsg) |
| 148 | |
Bram Moolenaar | 38fd4bb | 2016-03-06 16:38:28 +0100 | [diff] [blame] | 149 | " Collect garbage, tests that our handle isn't collected. |
| 150 | call garbagecollect() |
| 151 | |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 152 | " check setting options (without testing the effect) |
| 153 | call ch_setoptions(handle, {'callback': 's:NotUsed'}) |
Bram Moolenaar | 1f6ef66 | 2016-02-19 22:59:44 +0100 | [diff] [blame] | 154 | call ch_setoptions(handle, {'timeout': 1111}) |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 155 | call ch_setoptions(handle, {'mode': 'json'}) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 156 | call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475") |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 157 | call ch_setoptions(handle, {'callback': ''}) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 158 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 159 | " Send an eval request that works. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 160 | call assert_equal('ok', ch_evalexpr(handle, 'eval-works')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 161 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 162 | call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 163 | |
| 164 | " Send an eval request that fails. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 165 | call assert_equal('ok', ch_evalexpr(handle, 'eval-fails')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 166 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 167 | call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 168 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 169 | " Send an eval request that works but can't be encoded. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 170 | call assert_equal('ok', ch_evalexpr(handle, 'eval-error')) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 171 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 172 | call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 173 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 174 | " Send a bad eval request. There will be no response. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 175 | call assert_equal('ok', ch_evalexpr(handle, 'eval-bad')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 176 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 177 | call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 178 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 179 | " Send an expr request |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 180 | call assert_equal('ok', ch_evalexpr(handle, 'an expr')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 181 | sleep 10m |
| 182 | call assert_equal('one', getline(line('$') - 2)) |
| 183 | call assert_equal('two', getline(line('$') - 1)) |
| 184 | call assert_equal('three', getline('$')) |
| 185 | |
| 186 | " Request a redraw, we don't check for the effect. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 187 | call assert_equal('ok', ch_evalexpr(handle, 'redraw')) |
| 188 | call assert_equal('ok', ch_evalexpr(handle, 'redraw!')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 189 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 190 | call assert_equal('ok', ch_evalexpr(handle, 'empty-request')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 191 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 192 | " Reading while there is nothing available. |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 193 | call assert_equal(v:none, ch_read(handle, {'timeout': 0})) |
| 194 | let start = reltime() |
| 195 | call assert_equal(v:none, ch_read(handle, {'timeout': 333})) |
| 196 | let elapsed = reltime(start) |
| 197 | call assert_true(reltimefloat(elapsed) > 0.3) |
| 198 | call assert_true(reltimefloat(elapsed) < 0.6) |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 199 | |
| 200 | " Send without waiting for a response, then wait for a response. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 201 | call ch_sendexpr(handle, 'wait a bit') |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 202 | let resp = ch_read(handle) |
| 203 | call assert_equal(type([]), type(resp)) |
| 204 | call assert_equal(type(11), type(resp[0])) |
| 205 | call assert_equal('waited', resp[1]) |
| 206 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 207 | " make the server quit, can't check if this works, should not hang. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 208 | call ch_sendexpr(handle, '!quit!') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 209 | endfunc |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 210 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 211 | func Test_communicate() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 212 | call ch_log('Test_communicate()') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 213 | call s:run_server('s:communicate') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 214 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 216 | " Test that we can open two channels. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 217 | func s:two_channels(port) |
Bram Moolenaar | 39b2127 | 2016-02-10 23:28:21 +0100 | [diff] [blame] | 218 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 219 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 220 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 221 | return |
| 222 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 224 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 225 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 226 | let newhandle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 227 | if ch_status(newhandle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 228 | call assert_false(1, "Can't open second channel") |
| 229 | return |
| 230 | endif |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 231 | call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) |
| 232 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 233 | |
| 234 | call ch_close(handle) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 235 | call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 236 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 237 | call ch_close(newhandle) |
| 238 | endfunc |
| 239 | |
| 240 | func Test_two_channels() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 241 | call ch_log('Test_two_channels()') |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 242 | call s:run_server('s:two_channels') |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 243 | endfunc |
| 244 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 245 | " Test that a server crash is handled gracefully. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 246 | func s:server_crash(port) |
| 247 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 248 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 249 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 250 | return |
| 251 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 252 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 253 | call ch_evalexpr(handle, '!crash!') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 254 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 255 | sleep 10m |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 256 | endfunc |
| 257 | |
| 258 | func Test_server_crash() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 259 | call ch_log('Test_server_crash()') |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 260 | call s:run_server('s:server_crash') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 261 | endfunc |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 262 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 263 | """"""""" |
| 264 | |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 265 | let s:reply = "" |
| 266 | func s:Handler(chan, msg) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 267 | unlet s:reply |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 268 | let s:reply = a:msg |
| 269 | endfunc |
| 270 | |
| 271 | func s:channel_handler(port) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 272 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 273 | if ch_status(handle) == "fail" |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 274 | call assert_false(1, "Can't open channel") |
| 275 | return |
| 276 | endif |
| 277 | |
| 278 | " Test that it works while waiting on a numbered message. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 279 | call assert_equal('ok', ch_evalexpr(handle, 'call me')) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 280 | sleep 10m |
| 281 | call assert_equal('we called you', s:reply) |
| 282 | |
| 283 | " Test that it works while not waiting on a numbered message. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 284 | call ch_sendexpr(handle, 'call me again') |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 285 | sleep 10m |
| 286 | call assert_equal('we did call you', s:reply) |
| 287 | endfunc |
| 288 | |
| 289 | func Test_channel_handler() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 290 | call ch_log('Test_channel_handler()') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 291 | let s:chopt.callback = 's:Handler' |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 292 | call s:run_server('s:channel_handler') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 293 | let s:chopt.callback = function('s:Handler') |
| 294 | call s:run_server('s:channel_handler') |
| 295 | unlet s:chopt.callback |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 296 | endfunc |
| 297 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 298 | """"""""" |
| 299 | |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 300 | let s:ch_reply = '' |
| 301 | func s:ChHandler(chan, msg) |
| 302 | unlet s:ch_reply |
| 303 | let s:ch_reply = a:msg |
| 304 | endfunc |
| 305 | |
| 306 | let s:zero_reply = '' |
| 307 | func s:OneHandler(chan, msg) |
| 308 | unlet s:zero_reply |
| 309 | let s:zero_reply = a:msg |
| 310 | endfunc |
| 311 | |
| 312 | func s:channel_zero(port) |
| 313 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 314 | if ch_status(handle) == "fail" |
| 315 | call assert_false(1, "Can't open channel") |
| 316 | return |
| 317 | endif |
| 318 | |
| 319 | " Check that eval works. |
| 320 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
| 321 | |
| 322 | " Check that eval works if a zero id message is sent back. |
| 323 | let s:ch_reply = '' |
| 324 | call assert_equal('sent zero', ch_evalexpr(handle, 'send zero')) |
| 325 | sleep 10m |
| 326 | if s:has_handler |
| 327 | call assert_equal('zero index', s:ch_reply) |
| 328 | else |
| 329 | call assert_equal('', s:ch_reply) |
| 330 | endif |
| 331 | |
| 332 | " Check that handler works if a zero id message is sent back. |
| 333 | let s:ch_reply = '' |
| 334 | let s:zero_reply = '' |
| 335 | call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'}) |
| 336 | " Somehow the second message takes a bit of time. |
| 337 | for i in range(50) |
| 338 | if s:zero_reply == 'sent zero' |
| 339 | break |
| 340 | endif |
| 341 | sleep 10m |
| 342 | endfor |
| 343 | if s:has_handler |
| 344 | call assert_equal('zero index', s:ch_reply) |
| 345 | else |
| 346 | call assert_equal('', s:ch_reply) |
| 347 | endif |
| 348 | call assert_equal('sent zero', s:zero_reply) |
| 349 | endfunc |
| 350 | |
| 351 | func Test_zero_reply() |
| 352 | call ch_log('Test_zero_reply()') |
| 353 | " Run with channel handler |
| 354 | let s:has_handler = 1 |
| 355 | let s:chopt.callback = 's:ChHandler' |
| 356 | call s:run_server('s:channel_zero') |
| 357 | unlet s:chopt.callback |
| 358 | |
| 359 | " Run without channel handler |
| 360 | let s:has_handler = 0 |
| 361 | call s:run_server('s:channel_zero') |
| 362 | endfunc |
| 363 | |
| 364 | """"""""" |
| 365 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 366 | let s:reply1 = "" |
| 367 | func s:HandleRaw1(chan, msg) |
| 368 | unlet s:reply1 |
| 369 | let s:reply1 = a:msg |
| 370 | endfunc |
| 371 | |
| 372 | let s:reply2 = "" |
| 373 | func s:HandleRaw2(chan, msg) |
| 374 | unlet s:reply2 |
| 375 | let s:reply2 = a:msg |
| 376 | endfunc |
| 377 | |
| 378 | let s:reply3 = "" |
| 379 | func s:HandleRaw3(chan, msg) |
| 380 | unlet s:reply3 |
| 381 | let s:reply3 = a:msg |
| 382 | endfunc |
| 383 | |
| 384 | func s:raw_one_time_callback(port) |
| 385 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 386 | if ch_status(handle) == "fail" |
| 387 | call assert_false(1, "Can't open channel") |
| 388 | return |
| 389 | endif |
| 390 | call ch_setoptions(handle, {'mode': 'raw'}) |
| 391 | |
| 392 | " The message are sent raw, we do our own JSON strings here. |
| 393 | call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'}) |
| 394 | sleep 10m |
| 395 | call assert_equal("[1, \"got it\"]", s:reply1) |
| 396 | call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'}) |
| 397 | call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'}) |
| 398 | sleep 10m |
| 399 | call assert_equal("[2, \"something\"]", s:reply2) |
| 400 | " wait for up to 500 msec for the 200 msec delayed reply |
| 401 | for i in range(50) |
| 402 | sleep 10m |
| 403 | if s:reply3 != '' |
| 404 | break |
| 405 | endif |
| 406 | endfor |
| 407 | call assert_equal("[3, \"waited\"]", s:reply3) |
| 408 | endfunc |
| 409 | |
| 410 | func Test_raw_one_time_callback() |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 411 | call ch_log('Test_raw_one_time_callback()') |
| 412 | call s:run_server('s:raw_one_time_callback') |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 413 | endfunc |
| 414 | |
| 415 | """"""""" |
| 416 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 417 | " Test that trying to connect to a non-existing port fails quickly. |
| 418 | func Test_connect_waittime() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 419 | call ch_log('Test_connect_waittime()') |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 420 | let start = reltime() |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame] | 421 | let handle = ch_open('localhost:9876', s:chopt) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 422 | if ch_status(handle) != "fail" |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 423 | " Oops, port does exists. |
| 424 | call ch_close(handle) |
| 425 | else |
| 426 | let elapsed = reltime(start) |
Bram Moolenaar | 74f5e65 | 2016-02-07 21:44:49 +0100 | [diff] [blame] | 427 | call assert_true(reltimefloat(elapsed) < 1.0) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 428 | endif |
| 429 | |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 430 | " We intend to use a socket that doesn't exist and wait for half a second |
| 431 | " before giving up. If the socket does exist it can fail in various ways. |
| 432 | " Check for "Connection reset by peer" to avoid flakyness. |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 433 | let start = reltime() |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 434 | try |
| 435 | let handle = ch_open('localhost:9867', {'waittime': 500}) |
| 436 | if ch_status(handle) != "fail" |
| 437 | " Oops, port does exists. |
| 438 | call ch_close(handle) |
| 439 | else |
| 440 | " Failed connection should wait about 500 msec. |
| 441 | let elapsed = reltime(start) |
| 442 | call assert_true(reltimefloat(elapsed) > 0.3) |
| 443 | call assert_true(reltimefloat(elapsed) < 1.0) |
| 444 | endif |
| 445 | catch |
| 446 | if v:exception !~ 'Connection reset by peer' |
| 447 | call assert_false(1, "Caught exception: " . v:exception) |
| 448 | endif |
| 449 | endtry |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 450 | endfunc |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 451 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 452 | """"""""" |
| 453 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 454 | func Test_raw_pipe() |
| 455 | if !has('job') |
| 456 | return |
| 457 | endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 458 | call ch_log('Test_raw_pipe()') |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 459 | let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'}) |
| 460 | call assert_equal("run", job_status(job)) |
| 461 | try |
| 462 | let handle = job_getchannel(job) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 463 | call ch_sendraw(handle, "echo something\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 464 | let msg = ch_readraw(handle) |
| 465 | call assert_equal("something\n", substitute(msg, "\r", "", 'g')) |
| 466 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 467 | call ch_sendraw(handle, "double this\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 468 | let msg = ch_readraw(handle) |
| 469 | call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g')) |
| 470 | |
Bram Moolenaar | da94fdf | 2016-03-03 18:09:10 +0100 | [diff] [blame] | 471 | let reply = ch_evalraw(handle, "quit\n", {'timeout': 100}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 472 | call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g')) |
| 473 | finally |
| 474 | call job_stop(job) |
| 475 | endtry |
| 476 | endfunc |
| 477 | |
| 478 | func Test_nl_pipe() |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 479 | if !has('job') |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 480 | return |
| 481 | endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 482 | call ch_log('Test_nl_pipe()') |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 483 | let job = job_start(s:python . " test_channel_pipe.py") |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 484 | call assert_equal("run", job_status(job)) |
| 485 | try |
| 486 | let handle = job_getchannel(job) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 487 | call ch_sendraw(handle, "echo something\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 488 | call assert_equal("something", ch_readraw(handle)) |
| 489 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 490 | call ch_sendraw(handle, "echoerr wrong\n") |
| 491 | call assert_equal("wrong", ch_readraw(handle, {'part': 'err'})) |
| 492 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 493 | call ch_sendraw(handle, "double this\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 494 | call assert_equal("this", ch_readraw(handle)) |
| 495 | call assert_equal("AND this", ch_readraw(handle)) |
| 496 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 497 | let reply = ch_evalraw(handle, "quit\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 498 | call assert_equal("Goodbye!", reply) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 499 | finally |
| 500 | call job_stop(job) |
| 501 | endtry |
| 502 | endfunc |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 503 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 504 | func Test_nl_err_to_out_pipe() |
| 505 | if !has('job') |
| 506 | return |
| 507 | endif |
| 508 | call ch_log('Test_nl_err_to_out_pipe()') |
| 509 | let job = job_start(s:python . " test_channel_pipe.py", {'err-io': 'out'}) |
| 510 | call assert_equal("run", job_status(job)) |
| 511 | try |
| 512 | let handle = job_getchannel(job) |
| 513 | call ch_sendraw(handle, "echo something\n") |
| 514 | call assert_equal("something", ch_readraw(handle)) |
| 515 | |
| 516 | call ch_sendraw(handle, "echoerr wrong\n") |
| 517 | call assert_equal("wrong", ch_readraw(handle)) |
| 518 | finally |
| 519 | call job_stop(job) |
| 520 | endtry |
| 521 | endfunc |
| 522 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 523 | func Test_pipe_to_buffer() |
| 524 | if !has('job') |
| 525 | return |
| 526 | endif |
| 527 | call ch_log('Test_pipe_to_buffer()') |
| 528 | let job = job_start(s:python . " test_channel_pipe.py", |
| 529 | \ {'out-io': 'buffer', 'out-name': 'pipe-output'}) |
| 530 | call assert_equal("run", job_status(job)) |
| 531 | try |
| 532 | let handle = job_getchannel(job) |
| 533 | call ch_sendraw(handle, "echo line one\n") |
| 534 | call ch_sendraw(handle, "echo line two\n") |
| 535 | call ch_sendraw(handle, "double this\n") |
| 536 | call ch_sendraw(handle, "quit\n") |
| 537 | sp pipe-output |
| 538 | for i in range(100) |
| 539 | sleep 10m |
| 540 | if line('$') >= 6 |
| 541 | break |
| 542 | endif |
| 543 | endfor |
| 544 | call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$')) |
| 545 | bwipe! |
| 546 | finally |
| 547 | call job_stop(job) |
| 548 | endtry |
| 549 | endfunc |
| 550 | |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 551 | func Test_pipe_from_buffer() |
| 552 | if !has('job') |
| 553 | return |
| 554 | endif |
| 555 | call ch_logfile('channellog', 'w') |
| 556 | call ch_log('Test_pipe_from_buffer()') |
| 557 | |
| 558 | sp pipe-input |
| 559 | call setline(1, ['echo one', 'echo two', 'echo three']) |
| 560 | |
| 561 | let job = job_start(s:python . " test_channel_pipe.py", |
| 562 | \ {'in-io': 'buffer', 'in-name': 'pipe-input'}) |
| 563 | call assert_equal("run", job_status(job)) |
| 564 | try |
| 565 | let handle = job_getchannel(job) |
| 566 | call assert_equal('one', ch_read(handle)) |
| 567 | call assert_equal('two', ch_read(handle)) |
| 568 | call assert_equal('three', ch_read(handle)) |
| 569 | bwipe! |
| 570 | finally |
| 571 | call job_stop(job) |
| 572 | endtry |
| 573 | call ch_logfile('') |
| 574 | endfunc |
| 575 | |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 576 | func Test_pipe_to_nameless_buffer() |
| 577 | if !has('job') |
| 578 | return |
| 579 | endif |
| 580 | call ch_log('Test_pipe_to_nameless_buffer()') |
| 581 | let job = job_start(s:python . " test_channel_pipe.py", |
| 582 | \ {'out-io': 'buffer'}) |
| 583 | call assert_equal("run", job_status(job)) |
| 584 | try |
| 585 | let handle = job_getchannel(job) |
| 586 | call ch_sendraw(handle, "echo line one\n") |
| 587 | call ch_sendraw(handle, "echo line two\n") |
| 588 | exe ch_getbufnr(handle, "out") . 'sbuf' |
| 589 | for i in range(100) |
| 590 | sleep 10m |
| 591 | if line('$') >= 3 |
| 592 | break |
| 593 | endif |
| 594 | endfor |
| 595 | call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$')) |
| 596 | bwipe! |
| 597 | finally |
| 598 | call job_stop(job) |
| 599 | endtry |
| 600 | endfunc |
| 601 | |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 602 | func Test_pipe_to_buffer_json() |
| 603 | if !has('job') |
| 604 | return |
| 605 | endif |
| 606 | call ch_log('Test_pipe_to_buffer_json()') |
| 607 | let job = job_start(s:python . " test_channel_pipe.py", |
| 608 | \ {'out-io': 'buffer', 'out-mode': 'json'}) |
| 609 | call assert_equal("run", job_status(job)) |
| 610 | try |
| 611 | let handle = job_getchannel(job) |
| 612 | call ch_sendraw(handle, "echo [0, \"hello\"]\n") |
| 613 | call ch_sendraw(handle, "echo [-2, 12.34]\n") |
| 614 | exe ch_getbufnr(handle, "out") . 'sbuf' |
| 615 | for i in range(100) |
| 616 | sleep 10m |
| 617 | if line('$') >= 3 |
| 618 | break |
| 619 | endif |
| 620 | endfor |
| 621 | call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$')) |
| 622 | bwipe! |
| 623 | finally |
| 624 | call job_stop(job) |
| 625 | endtry |
| 626 | endfunc |
| 627 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 628 | """""""""" |
| 629 | |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 630 | let s:unletResponse = '' |
| 631 | func s:UnletHandler(handle, msg) |
| 632 | let s:unletResponse = a:msg |
| 633 | unlet s:channelfd |
| 634 | endfunc |
| 635 | |
| 636 | " Test that "unlet handle" in a handler doesn't crash Vim. |
| 637 | func s:unlet_handle(port) |
| 638 | let s:channelfd = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 639 | call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')}) |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 640 | sleep 10m |
| 641 | call assert_equal('what?', s:unletResponse) |
| 642 | endfunc |
| 643 | |
| 644 | func Test_unlet_handle() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 645 | call ch_log('Test_unlet_handle()') |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 646 | call s:run_server('s:unlet_handle') |
| 647 | endfunc |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 648 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 649 | """""""""" |
| 650 | |
| 651 | let s:unletResponse = '' |
| 652 | func s:CloseHandler(handle, msg) |
| 653 | let s:unletResponse = a:msg |
| 654 | call ch_close(s:channelfd) |
| 655 | endfunc |
| 656 | |
| 657 | " Test that "unlet handle" in a handler doesn't crash Vim. |
| 658 | func s:close_handle(port) |
| 659 | let s:channelfd = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 660 | call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')}) |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 661 | sleep 10m |
| 662 | call assert_equal('what?', s:unletResponse) |
| 663 | endfunc |
| 664 | |
| 665 | func Test_close_handle() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 666 | call ch_log('Test_close_handle()') |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 667 | call s:run_server('s:close_handle') |
| 668 | endfunc |
| 669 | |
| 670 | """""""""" |
| 671 | |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 672 | func Test_open_fail() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 673 | call ch_log('Test_open_fail()') |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 674 | silent! let ch = ch_open("noserver") |
| 675 | echo ch |
| 676 | let d = ch |
| 677 | endfunc |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 678 | |
| 679 | """""""""" |
| 680 | |
| 681 | func s:open_delay(port) |
| 682 | " Wait up to a second for the port to open. |
| 683 | let s:chopt.waittime = 1000 |
| 684 | let channel = ch_open('localhost:' . a:port, s:chopt) |
| 685 | unlet s:chopt.waittime |
| 686 | if ch_status(channel) == "fail" |
| 687 | call assert_false(1, "Can't open channel") |
| 688 | return |
| 689 | endif |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 690 | call assert_equal('got it', ch_evalexpr(channel, 'hello!')) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 691 | call ch_close(channel) |
| 692 | endfunc |
| 693 | |
| 694 | func Test_open_delay() |
| 695 | call ch_log('Test_open_delay()') |
| 696 | " The server will wait half a second before creating the port. |
| 697 | call s:run_server('s:open_delay', 'delay') |
| 698 | endfunc |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 699 | |
| 700 | """"""""" |
| 701 | |
| 702 | function MyFunction(a,b,c) |
| 703 | let s:call_ret = [a:a, a:b, a:c] |
| 704 | endfunc |
| 705 | |
| 706 | function s:test_call(port) |
| 707 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 708 | if ch_status(handle) == "fail" |
| 709 | call assert_false(1, "Can't open channel") |
| 710 | return |
| 711 | endif |
| 712 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 713 | call assert_equal('ok', ch_evalexpr(handle, 'call-func')) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 714 | sleep 20m |
| 715 | call assert_equal([1, 2, 3], s:call_ret) |
| 716 | endfunc |
| 717 | |
| 718 | func Test_call() |
| 719 | call ch_log('Test_call()') |
| 720 | call s:run_server('s:test_call') |
| 721 | endfunc |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 722 | |
| 723 | """"""""" |
| 724 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 725 | let s:job_exit_ret = 'not yet' |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 726 | function MyExitCb(job, status) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 727 | let s:job_exit_ret = 'done' |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 728 | endfunc |
| 729 | |
| 730 | function s:test_exit_callback(port) |
| 731 | call job_setoptions(s:job, {'exit-cb': 'MyExitCb'}) |
| 732 | let s:exit_job = s:job |
| 733 | endfunc |
| 734 | |
| 735 | func Test_exit_callback() |
| 736 | if has('job') |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 737 | call ch_log('Test_exit_callback()') |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 738 | call s:run_server('s:test_exit_callback') |
| 739 | |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 740 | " wait up to a second for the job to exit |
| 741 | for i in range(100) |
| 742 | if s:job_exit_ret == 'done' |
| 743 | break |
| 744 | endif |
| 745 | sleep 10m |
| 746 | " calling job_status() triggers the callback |
| 747 | call job_status(s:exit_job) |
| 748 | endfor |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 749 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 750 | call assert_equal('done', s:job_exit_ret) |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 751 | unlet s:exit_job |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 752 | endif |
| 753 | endfunc |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 754 | |
| 755 | """"""""" |
| 756 | |
| 757 | let s:ch_close_ret = 'alive' |
| 758 | function MyCloseCb(ch) |
| 759 | let s:ch_close_ret = 'closed' |
| 760 | endfunc |
| 761 | |
| 762 | function s:test_close_callback(port) |
| 763 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 764 | if ch_status(handle) == "fail" |
| 765 | call assert_false(1, "Can't open channel") |
| 766 | return |
| 767 | endif |
| 768 | call ch_setoptions(handle, {'close-cb': 'MyCloseCb'}) |
| 769 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 770 | call assert_equal('', ch_evalexpr(handle, 'close me')) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 771 | sleep 20m |
| 772 | call assert_equal('closed', s:ch_close_ret) |
| 773 | endfunc |
| 774 | |
| 775 | func Test_close_callback() |
| 776 | call ch_log('Test_close_callback()') |
| 777 | call s:run_server('s:test_close_callback') |
| 778 | endfunc |
| 779 | |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 780 | " Uncomment this to see what happens, output is in src/testdir/channellog. |
| 781 | " call ch_logfile('channellog', 'w') |