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