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. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 56 | let l = [] |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 57 | for i in range(200) |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 58 | try |
| 59 | let l = readfile("Xportnr") |
| 60 | catch |
| 61 | endtry |
| 62 | if len(l) >= 1 |
| 63 | break |
| 64 | endif |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 65 | sleep 10m |
| 66 | endfor |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 67 | call delete("Xportnr") |
| 68 | |
| 69 | if len(l) == 0 |
| 70 | " Can't make the connection, give up. |
| 71 | call assert_false(1, "Can't start test_channel.py") |
| 72 | return -1 |
| 73 | endif |
| 74 | let port = l[0] |
| 75 | |
| 76 | call call(function(a:testfunc), [port]) |
| 77 | catch |
| 78 | call assert_false(1, "Caught exception: " . v:exception) |
| 79 | finally |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 80 | call s:kill_server() |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 81 | endtry |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 82 | endfunc |
| 83 | |
| 84 | func s:kill_server() |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 85 | if has('job') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 86 | if exists('s:job') |
| 87 | call job_stop(s:job) |
| 88 | unlet s:job |
| 89 | endif |
Bram Moolenaar | 835dc63 | 2016-02-07 14:27:38 +0100 | [diff] [blame] | 90 | elseif has('win32') |
Bram Moolenaar | b6a7737 | 2016-02-15 22:55:28 +0100 | [diff] [blame] | 91 | 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] | 92 | else |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 93 | call system("pkill -f test_channel.py") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 94 | endif |
| 95 | endfunc |
| 96 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 97 | let s:responseMsg = '' |
| 98 | func s:RequestHandler(handle, msg) |
| 99 | let s:responseHandle = a:handle |
| 100 | let s:responseMsg = a:msg |
| 101 | endfunc |
| 102 | |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 103 | " Wait for up to a second for "expr" to become true. |
| 104 | func s:waitFor(expr) |
| 105 | for i in range(100) |
Bram Moolenaar | d9d473e | 2016-03-08 19:07:22 +0100 | [diff] [blame] | 106 | try |
| 107 | if eval(a:expr) |
| 108 | return |
| 109 | endif |
| 110 | catch |
| 111 | endtry |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 112 | sleep 10m |
| 113 | endfor |
| 114 | endfunc |
| 115 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 116 | func s:communicate(port) |
| 117 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 118 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 119 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 120 | return |
| 121 | endif |
Bram Moolenaar | 839fd11 | 2016-03-06 21:34:03 +0100 | [diff] [blame] | 122 | if has('job') |
| 123 | " check that no job is handled correctly |
| 124 | call assert_equal('no process', string(ch_getjob(handle))) |
| 125 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 126 | " Simple string request and reply. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 127 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 129 | " Malformed command should be ignored. |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame^] | 130 | call assert_equal('ok', ch_evalexpr(handle, 'malformed1')) |
| 131 | call assert_equal('ok', ch_evalexpr(handle, 'malformed2')) |
| 132 | call assert_equal('ok', ch_evalexpr(handle, 'malformed3')) |
| 133 | |
| 134 | " split command should work |
| 135 | call assert_equal('ok', ch_evalexpr(handle, 'split')) |
| 136 | call s:waitFor('exists("g:split")') |
| 137 | call assert_equal(123, g:split) |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 138 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 139 | " Request that triggers sending two ex commands. These will usually be |
| 140 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 141 | " tiny bit for the commands to get executed. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 142 | call assert_equal('ok', ch_evalexpr(handle, 'make change')) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 143 | call s:waitFor('"added2" == getline("$")') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 144 | call assert_equal('added1', getline(line('$') - 1)) |
| 145 | call assert_equal('added2', getline('$')) |
| 146 | |
Bram Moolenaar | da94fdf | 2016-03-03 18:09:10 +0100 | [diff] [blame] | 147 | call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100})) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 148 | call s:waitFor('"added more" == getline("$")') |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 149 | call assert_equal('added more', getline('$')) |
| 150 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 151 | " Send a request with a specific handler. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 152 | call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 153 | call s:waitFor('exists("s:responseHandle")') |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 154 | if !exists('s:responseHandle') |
| 155 | call assert_false(1, 's:responseHandle was not set') |
| 156 | else |
| 157 | call assert_equal(handle, s:responseHandle) |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 158 | unlet s:responseHandle |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 159 | endif |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 160 | call assert_equal('got it', s:responseMsg) |
| 161 | |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 162 | let s:responseMsg = '' |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 163 | call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 164 | call s:waitFor('exists("s:responseHandle")') |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 165 | if !exists('s:responseHandle') |
| 166 | call assert_false(1, 's:responseHandle was not set') |
| 167 | else |
| 168 | call assert_equal(handle, s:responseHandle) |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 169 | unlet s:responseHandle |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 170 | endif |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 171 | call assert_equal('got it', s:responseMsg) |
| 172 | |
Bram Moolenaar | 38fd4bb | 2016-03-06 16:38:28 +0100 | [diff] [blame] | 173 | " Collect garbage, tests that our handle isn't collected. |
| 174 | call garbagecollect() |
| 175 | |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 176 | " check setting options (without testing the effect) |
| 177 | call ch_setoptions(handle, {'callback': 's:NotUsed'}) |
Bram Moolenaar | 1f6ef66 | 2016-02-19 22:59:44 +0100 | [diff] [blame] | 178 | call ch_setoptions(handle, {'timeout': 1111}) |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 179 | call ch_setoptions(handle, {'mode': 'json'}) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 180 | call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475") |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 181 | call ch_setoptions(handle, {'callback': ''}) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 182 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 183 | " Send an eval request that works. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 184 | call assert_equal('ok', ch_evalexpr(handle, 'eval-works')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 185 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 186 | call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 187 | |
| 188 | " Send an eval request that fails. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 189 | call assert_equal('ok', ch_evalexpr(handle, 'eval-fails')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 190 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 191 | call assert_equal([-2, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 192 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 193 | " Send an eval request that works but can't be encoded. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 194 | call assert_equal('ok', ch_evalexpr(handle, 'eval-error')) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 195 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 196 | call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 197 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 198 | " Send a bad eval request. There will be no response. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 199 | call assert_equal('ok', ch_evalexpr(handle, 'eval-bad')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 200 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 201 | call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 202 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 203 | " Send an expr request |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 204 | call assert_equal('ok', ch_evalexpr(handle, 'an expr')) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 205 | call s:waitFor('"three" == getline("$")') |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 206 | call assert_equal('one', getline(line('$') - 2)) |
| 207 | call assert_equal('two', getline(line('$') - 1)) |
| 208 | call assert_equal('three', getline('$')) |
| 209 | |
| 210 | " Request a redraw, we don't check for the effect. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 211 | call assert_equal('ok', ch_evalexpr(handle, 'redraw')) |
| 212 | call assert_equal('ok', ch_evalexpr(handle, 'redraw!')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 213 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 214 | call assert_equal('ok', ch_evalexpr(handle, 'empty-request')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 216 | " Reading while there is nothing available. |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 217 | call assert_equal(v:none, ch_read(handle, {'timeout': 0})) |
| 218 | let start = reltime() |
| 219 | call assert_equal(v:none, ch_read(handle, {'timeout': 333})) |
| 220 | let elapsed = reltime(start) |
| 221 | call assert_true(reltimefloat(elapsed) > 0.3) |
| 222 | call assert_true(reltimefloat(elapsed) < 0.6) |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 223 | |
| 224 | " Send without waiting for a response, then wait for a response. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 225 | call ch_sendexpr(handle, 'wait a bit') |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 226 | let resp = ch_read(handle) |
| 227 | call assert_equal(type([]), type(resp)) |
| 228 | call assert_equal(type(11), type(resp[0])) |
| 229 | call assert_equal('waited', resp[1]) |
| 230 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 231 | " 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] | 232 | call ch_sendexpr(handle, '!quit!') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 233 | endfunc |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 234 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 235 | func Test_communicate() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 236 | call ch_log('Test_communicate()') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 237 | call s:run_server('s:communicate') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 238 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 239 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 240 | " Test that we can open two channels. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 241 | func s:two_channels(port) |
Bram Moolenaar | 39b2127 | 2016-02-10 23:28:21 +0100 | [diff] [blame] | 242 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 243 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 244 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 245 | return |
| 246 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 247 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 248 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 249 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 250 | let newhandle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 251 | if ch_status(newhandle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 252 | call assert_false(1, "Can't open second channel") |
| 253 | return |
| 254 | endif |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 255 | call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) |
| 256 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 257 | |
| 258 | call ch_close(handle) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 259 | call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 260 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 261 | call ch_close(newhandle) |
| 262 | endfunc |
| 263 | |
| 264 | func Test_two_channels() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 265 | call ch_log('Test_two_channels()') |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 266 | call s:run_server('s:two_channels') |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 267 | endfunc |
| 268 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 269 | " Test that a server crash is handled gracefully. |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 270 | func s:server_crash(port) |
| 271 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 272 | if ch_status(handle) == "fail" |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 273 | call assert_false(1, "Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 274 | return |
| 275 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 276 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 277 | call ch_evalexpr(handle, '!crash!') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 278 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 279 | sleep 10m |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 280 | endfunc |
| 281 | |
| 282 | func Test_server_crash() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 283 | call ch_log('Test_server_crash()') |
Bram Moolenaar | bfa1ffc | 2016-02-13 18:40:30 +0100 | [diff] [blame] | 284 | call s:run_server('s:server_crash') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 285 | endfunc |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 286 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 287 | """"""""" |
| 288 | |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 289 | let s:reply = "" |
| 290 | func s:Handler(chan, msg) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 291 | unlet s:reply |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 292 | let s:reply = a:msg |
| 293 | endfunc |
| 294 | |
| 295 | func s:channel_handler(port) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 296 | let handle = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 297 | if ch_status(handle) == "fail" |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 298 | call assert_false(1, "Can't open channel") |
| 299 | return |
| 300 | endif |
| 301 | |
| 302 | " Test that it works while waiting on a numbered message. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 303 | call assert_equal('ok', ch_evalexpr(handle, 'call me')) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 304 | call s:waitFor('"we called you" == s:reply') |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 305 | call assert_equal('we called you', s:reply) |
| 306 | |
| 307 | " Test that it works while not waiting on a numbered message. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 308 | call ch_sendexpr(handle, 'call me again') |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 309 | call s:waitFor('"we did call you" == s:reply') |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 310 | call assert_equal('we did call you', s:reply) |
| 311 | endfunc |
| 312 | |
| 313 | func Test_channel_handler() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 314 | call ch_log('Test_channel_handler()') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 315 | let s:chopt.callback = 's:Handler' |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 316 | call s:run_server('s:channel_handler') |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 317 | let s:chopt.callback = function('s:Handler') |
| 318 | call s:run_server('s:channel_handler') |
| 319 | unlet s:chopt.callback |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 320 | endfunc |
| 321 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 322 | """"""""" |
| 323 | |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 324 | let s:ch_reply = '' |
| 325 | func s:ChHandler(chan, msg) |
| 326 | unlet s:ch_reply |
| 327 | let s:ch_reply = a:msg |
| 328 | endfunc |
| 329 | |
| 330 | let s:zero_reply = '' |
| 331 | func s:OneHandler(chan, msg) |
| 332 | unlet s:zero_reply |
| 333 | let s:zero_reply = a:msg |
| 334 | endfunc |
| 335 | |
| 336 | func s:channel_zero(port) |
| 337 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 338 | if ch_status(handle) == "fail" |
| 339 | call assert_false(1, "Can't open channel") |
| 340 | return |
| 341 | endif |
| 342 | |
| 343 | " Check that eval works. |
| 344 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
| 345 | |
| 346 | " Check that eval works if a zero id message is sent back. |
| 347 | let s:ch_reply = '' |
| 348 | call assert_equal('sent zero', ch_evalexpr(handle, 'send zero')) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 349 | if s:has_handler |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 350 | call s:waitFor('"zero index" == s:ch_reply') |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 351 | call assert_equal('zero index', s:ch_reply) |
| 352 | else |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 353 | sleep 20m |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 354 | call assert_equal('', s:ch_reply) |
| 355 | endif |
| 356 | |
| 357 | " Check that handler works if a zero id message is sent back. |
| 358 | let s:ch_reply = '' |
| 359 | let s:zero_reply = '' |
| 360 | call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 361 | call s:waitFor('"sent zero" == s:zero_reply') |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 362 | if s:has_handler |
| 363 | call assert_equal('zero index', s:ch_reply) |
| 364 | else |
| 365 | call assert_equal('', s:ch_reply) |
| 366 | endif |
| 367 | call assert_equal('sent zero', s:zero_reply) |
| 368 | endfunc |
| 369 | |
| 370 | func Test_zero_reply() |
| 371 | call ch_log('Test_zero_reply()') |
| 372 | " Run with channel handler |
| 373 | let s:has_handler = 1 |
| 374 | let s:chopt.callback = 's:ChHandler' |
| 375 | call s:run_server('s:channel_zero') |
| 376 | unlet s:chopt.callback |
| 377 | |
| 378 | " Run without channel handler |
| 379 | let s:has_handler = 0 |
| 380 | call s:run_server('s:channel_zero') |
| 381 | endfunc |
| 382 | |
| 383 | """"""""" |
| 384 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 385 | let s:reply1 = "" |
| 386 | func s:HandleRaw1(chan, msg) |
| 387 | unlet s:reply1 |
| 388 | let s:reply1 = a:msg |
| 389 | endfunc |
| 390 | |
| 391 | let s:reply2 = "" |
| 392 | func s:HandleRaw2(chan, msg) |
| 393 | unlet s:reply2 |
| 394 | let s:reply2 = a:msg |
| 395 | endfunc |
| 396 | |
| 397 | let s:reply3 = "" |
| 398 | func s:HandleRaw3(chan, msg) |
| 399 | unlet s:reply3 |
| 400 | let s:reply3 = a:msg |
| 401 | endfunc |
| 402 | |
| 403 | func s:raw_one_time_callback(port) |
| 404 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 405 | if ch_status(handle) == "fail" |
| 406 | call assert_false(1, "Can't open channel") |
| 407 | return |
| 408 | endif |
| 409 | call ch_setoptions(handle, {'mode': 'raw'}) |
| 410 | |
| 411 | " The message are sent raw, we do our own JSON strings here. |
| 412 | call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 413 | call s:waitFor('s:reply1 != ""') |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 414 | call assert_equal("[1, \"got it\"]", s:reply1) |
| 415 | call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'}) |
| 416 | call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 417 | call s:waitFor('s:reply2 != ""') |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 418 | call assert_equal("[2, \"something\"]", s:reply2) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 419 | " wait for the 200 msec delayed reply |
| 420 | call s:waitFor('s:reply3 != ""') |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | ac42afd | 2016-03-12 13:48:49 +0100 | [diff] [blame] | 454 | " Failed connection should wait about 500 msec. Can be longer if the |
| 455 | " computer is busy with other things. |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 456 | let elapsed = reltime(start) |
| 457 | call assert_true(reltimefloat(elapsed) > 0.3) |
Bram Moolenaar | ac42afd | 2016-03-12 13:48:49 +0100 | [diff] [blame] | 458 | call assert_true(reltimefloat(elapsed) < 1.5) |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 459 | endif |
| 460 | catch |
| 461 | if v:exception !~ 'Connection reset by peer' |
| 462 | call assert_false(1, "Caught exception: " . v:exception) |
| 463 | endif |
| 464 | endtry |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 465 | endfunc |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 466 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 467 | """"""""" |
| 468 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 469 | func Test_raw_pipe() |
| 470 | if !has('job') |
| 471 | return |
| 472 | endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 473 | call ch_log('Test_raw_pipe()') |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 474 | let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'}) |
| 475 | call assert_equal("run", job_status(job)) |
| 476 | try |
Bram Moolenaar | 151f656 | 2016-03-07 21:19:38 +0100 | [diff] [blame] | 477 | " For a change use the job where a channel is expected. |
| 478 | call ch_sendraw(job, "echo something\n") |
| 479 | let msg = ch_readraw(job) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 480 | call assert_equal("something\n", substitute(msg, "\r", "", 'g')) |
| 481 | |
Bram Moolenaar | 151f656 | 2016-03-07 21:19:38 +0100 | [diff] [blame] | 482 | call ch_sendraw(job, "double this\n") |
| 483 | let msg = ch_readraw(job) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 484 | call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g')) |
| 485 | |
Bram Moolenaar | 151f656 | 2016-03-07 21:19:38 +0100 | [diff] [blame] | 486 | let reply = ch_evalraw(job, "quit\n", {'timeout': 100}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 487 | call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g')) |
| 488 | finally |
| 489 | call job_stop(job) |
| 490 | endtry |
Bram Moolenaar | 8950a56 | 2016-03-12 15:22:55 +0100 | [diff] [blame] | 491 | |
| 492 | let s:job = job |
| 493 | call s:waitFor('"dead" == job_status(s:job)') |
| 494 | let info = job_info(job) |
| 495 | call assert_equal("dead", info.status) |
| 496 | call assert_equal("term", info.stoponexit) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 497 | endfunc |
| 498 | |
| 499 | func Test_nl_pipe() |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 500 | if !has('job') |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 501 | return |
| 502 | endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 503 | call ch_log('Test_nl_pipe()') |
Bram Moolenaar | 1adda34 | 2016-03-12 15:39:40 +0100 | [diff] [blame] | 504 | let job = job_start([s:python, "test_channel_pipe.py"]) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 505 | call assert_equal("run", job_status(job)) |
| 506 | try |
| 507 | let handle = job_getchannel(job) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 508 | call ch_sendraw(handle, "echo something\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 509 | call assert_equal("something", ch_readraw(handle)) |
| 510 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 511 | call ch_sendraw(handle, "echoerr wrong\n") |
| 512 | call assert_equal("wrong", ch_readraw(handle, {'part': 'err'})) |
| 513 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 514 | call ch_sendraw(handle, "double this\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 515 | call assert_equal("this", ch_readraw(handle)) |
| 516 | call assert_equal("AND this", ch_readraw(handle)) |
| 517 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 518 | let reply = ch_evalraw(handle, "quit\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 519 | call assert_equal("Goodbye!", reply) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 520 | finally |
| 521 | call job_stop(job) |
| 522 | endtry |
| 523 | endfunc |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 524 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 525 | func Test_nl_err_to_out_pipe() |
| 526 | if !has('job') |
| 527 | return |
| 528 | endif |
Bram Moolenaar | 5a6ec52 | 2016-03-12 15:51:44 +0100 | [diff] [blame] | 529 | call ch_logfile('Xlog') |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 530 | call ch_log('Test_nl_err_to_out_pipe()') |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 531 | let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'}) |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 532 | call assert_equal("run", job_status(job)) |
| 533 | try |
| 534 | let handle = job_getchannel(job) |
| 535 | call ch_sendraw(handle, "echo something\n") |
| 536 | call assert_equal("something", ch_readraw(handle)) |
| 537 | |
| 538 | call ch_sendraw(handle, "echoerr wrong\n") |
| 539 | call assert_equal("wrong", ch_readraw(handle)) |
| 540 | finally |
| 541 | call job_stop(job) |
Bram Moolenaar | 5a6ec52 | 2016-03-12 15:51:44 +0100 | [diff] [blame] | 542 | call ch_logfile('') |
| 543 | let loglines = readfile('Xlog') |
| 544 | call assert_true(len(loglines) > 10) |
| 545 | let found_test = 0 |
| 546 | let found_send = 0 |
| 547 | let found_recv = 0 |
| 548 | let found_stop = 0 |
| 549 | for l in loglines |
| 550 | if l =~ 'Test_nl_err_to_out_pipe' |
| 551 | let found_test = 1 |
| 552 | endif |
| 553 | if l =~ 'SEND on.*echo something' |
| 554 | let found_send = 1 |
| 555 | endif |
| 556 | if l =~ 'RECV on.*something' |
| 557 | let found_recv = 1 |
| 558 | endif |
| 559 | if l =~ 'Stopping job with' |
| 560 | let found_stop = 1 |
| 561 | endif |
| 562 | endfor |
| 563 | call assert_equal(1, found_test) |
| 564 | call assert_equal(1, found_send) |
| 565 | call assert_equal(1, found_recv) |
| 566 | call assert_equal(1, found_stop) |
| 567 | call delete('Xlog') |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 568 | endtry |
| 569 | endfunc |
| 570 | |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 571 | func Test_nl_read_file() |
| 572 | if !has('job') |
| 573 | return |
| 574 | endif |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 575 | call ch_log('Test_nl_read_file()') |
| 576 | call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput') |
| 577 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 578 | \ {'in_io': 'file', 'in_name': 'Xinput'}) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 579 | call assert_equal("run", job_status(job)) |
| 580 | try |
| 581 | let handle = job_getchannel(job) |
| 582 | call assert_equal("something", ch_readraw(handle)) |
| 583 | call assert_equal("wrong", ch_readraw(handle, {'part': 'err'})) |
| 584 | call assert_equal("this", ch_readraw(handle)) |
| 585 | call assert_equal("AND this", ch_readraw(handle)) |
| 586 | finally |
| 587 | call job_stop(job) |
| 588 | call delete('Xinput') |
| 589 | endtry |
| 590 | endfunc |
| 591 | |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 592 | func Test_nl_write_out_file() |
| 593 | if !has('job') |
| 594 | return |
| 595 | endif |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 596 | call ch_log('Test_nl_write_out_file()') |
| 597 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 598 | \ {'out_io': 'file', 'out_name': 'Xoutput'}) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 599 | call assert_equal("run", job_status(job)) |
| 600 | try |
| 601 | let handle = job_getchannel(job) |
| 602 | call ch_sendraw(handle, "echo line one\n") |
| 603 | call ch_sendraw(handle, "echo line two\n") |
| 604 | call ch_sendraw(handle, "double this\n") |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 605 | call s:waitFor('len(readfile("Xoutput")) > 2') |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 606 | call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput')) |
| 607 | finally |
| 608 | call job_stop(job) |
| 609 | call delete('Xoutput') |
| 610 | endtry |
| 611 | endfunc |
| 612 | |
| 613 | func Test_nl_write_err_file() |
| 614 | if !has('job') |
| 615 | return |
| 616 | endif |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 617 | call ch_log('Test_nl_write_err_file()') |
| 618 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 619 | \ {'err_io': 'file', 'err_name': 'Xoutput'}) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 620 | call assert_equal("run", job_status(job)) |
| 621 | try |
| 622 | let handle = job_getchannel(job) |
| 623 | call ch_sendraw(handle, "echoerr line one\n") |
| 624 | call ch_sendraw(handle, "echoerr line two\n") |
| 625 | call ch_sendraw(handle, "doubleerr this\n") |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 626 | call s:waitFor('len(readfile("Xoutput")) > 2') |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 627 | call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput')) |
| 628 | finally |
| 629 | call job_stop(job) |
| 630 | call delete('Xoutput') |
| 631 | endtry |
| 632 | endfunc |
| 633 | |
| 634 | func Test_nl_write_both_file() |
| 635 | if !has('job') |
| 636 | return |
| 637 | endif |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 638 | call ch_log('Test_nl_write_both_file()') |
| 639 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 640 | \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'}) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 641 | call assert_equal("run", job_status(job)) |
| 642 | try |
| 643 | let handle = job_getchannel(job) |
| 644 | call ch_sendraw(handle, "echoerr line one\n") |
| 645 | call ch_sendraw(handle, "echo line two\n") |
| 646 | call ch_sendraw(handle, "double this\n") |
| 647 | call ch_sendraw(handle, "doubleerr that\n") |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 648 | call s:waitFor('len(readfile("Xoutput")) > 5') |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 649 | call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput')) |
| 650 | finally |
| 651 | call job_stop(job) |
| 652 | call delete('Xoutput') |
| 653 | endtry |
| 654 | endfunc |
| 655 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 656 | func Run_test_pipe_to_buffer(use_name) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 657 | if !has('job') |
| 658 | return |
| 659 | endif |
| 660 | call ch_log('Test_pipe_to_buffer()') |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 661 | let options = {'out_io': 'buffer'} |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 662 | if a:use_name |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 663 | let options['out_name'] = 'pipe-output' |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 664 | let firstline = 'Reading from channel output...' |
| 665 | else |
| 666 | sp pipe-output |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 667 | let options['out_buf'] = bufnr('%') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 668 | quit |
| 669 | let firstline = '' |
| 670 | endif |
| 671 | let job = job_start(s:python . " test_channel_pipe.py", options) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 672 | call assert_equal("run", job_status(job)) |
| 673 | try |
| 674 | let handle = job_getchannel(job) |
| 675 | call ch_sendraw(handle, "echo line one\n") |
| 676 | call ch_sendraw(handle, "echo line two\n") |
| 677 | call ch_sendraw(handle, "double this\n") |
| 678 | call ch_sendraw(handle, "quit\n") |
| 679 | sp pipe-output |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 680 | call s:waitFor('line("$") >= 6') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 681 | call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$')) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 682 | bwipe! |
| 683 | finally |
| 684 | call job_stop(job) |
| 685 | endtry |
| 686 | endfunc |
| 687 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 688 | func Test_pipe_to_buffer_name() |
| 689 | call Run_test_pipe_to_buffer(1) |
| 690 | endfunc |
| 691 | |
| 692 | func Test_pipe_to_buffer_nr() |
| 693 | call Run_test_pipe_to_buffer(0) |
| 694 | endfunc |
| 695 | |
| 696 | func Run_test_pipe_err_to_buffer(use_name) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 697 | if !has('job') |
| 698 | return |
| 699 | endif |
| 700 | call ch_log('Test_pipe_err_to_buffer()') |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 701 | let options = {'err_io': 'buffer'} |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 702 | if a:use_name |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 703 | let options['err_name'] = 'pipe-err' |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 704 | let firstline = 'Reading from channel error...' |
| 705 | else |
| 706 | sp pipe-err |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 707 | let options['err_buf'] = bufnr('%') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 708 | quit |
| 709 | let firstline = '' |
| 710 | endif |
| 711 | let job = job_start(s:python . " test_channel_pipe.py", options) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 712 | call assert_equal("run", job_status(job)) |
| 713 | try |
| 714 | let handle = job_getchannel(job) |
| 715 | call ch_sendraw(handle, "echoerr line one\n") |
| 716 | call ch_sendraw(handle, "echoerr line two\n") |
| 717 | call ch_sendraw(handle, "doubleerr this\n") |
| 718 | call ch_sendraw(handle, "quit\n") |
| 719 | sp pipe-err |
| 720 | call s:waitFor('line("$") >= 5') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 721 | call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$')) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 722 | bwipe! |
| 723 | finally |
| 724 | call job_stop(job) |
| 725 | endtry |
| 726 | endfunc |
| 727 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 728 | func Test_pipe_err_to_buffer_name() |
| 729 | call Run_test_pipe_err_to_buffer(1) |
| 730 | endfunc |
| 731 | |
| 732 | func Test_pipe_err_to_buffer_nr() |
| 733 | call Run_test_pipe_err_to_buffer(0) |
| 734 | endfunc |
| 735 | |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 736 | func Test_pipe_both_to_buffer() |
| 737 | if !has('job') |
| 738 | return |
| 739 | endif |
| 740 | call ch_log('Test_pipe_both_to_buffer()') |
| 741 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 742 | \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'}) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 743 | call assert_equal("run", job_status(job)) |
| 744 | try |
| 745 | let handle = job_getchannel(job) |
| 746 | call ch_sendraw(handle, "echo line one\n") |
| 747 | call ch_sendraw(handle, "echoerr line two\n") |
| 748 | call ch_sendraw(handle, "double this\n") |
| 749 | call ch_sendraw(handle, "doubleerr that\n") |
| 750 | call ch_sendraw(handle, "quit\n") |
| 751 | sp pipe-err |
| 752 | call s:waitFor('line("$") >= 7') |
| 753 | call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$')) |
| 754 | bwipe! |
| 755 | finally |
| 756 | call job_stop(job) |
| 757 | endtry |
| 758 | endfunc |
| 759 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 760 | func Run_test_pipe_from_buffer(use_name) |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 761 | if !has('job') |
| 762 | return |
| 763 | endif |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 764 | call ch_log('Test_pipe_from_buffer()') |
| 765 | |
| 766 | sp pipe-input |
| 767 | call setline(1, ['echo one', 'echo two', 'echo three']) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 768 | let options = {'in_io': 'buffer'} |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 769 | if a:use_name |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 770 | let options['in_name'] = 'pipe-input' |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 771 | else |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 772 | let options['in_buf'] = bufnr('%') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 773 | endif |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 774 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 775 | let job = job_start(s:python . " test_channel_pipe.py", options) |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 776 | call assert_equal("run", job_status(job)) |
| 777 | try |
| 778 | let handle = job_getchannel(job) |
| 779 | call assert_equal('one', ch_read(handle)) |
| 780 | call assert_equal('two', ch_read(handle)) |
| 781 | call assert_equal('three', ch_read(handle)) |
| 782 | bwipe! |
| 783 | finally |
| 784 | call job_stop(job) |
| 785 | endtry |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 786 | endfunc |
| 787 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 788 | func Test_pipe_from_buffer_name() |
| 789 | call Run_test_pipe_from_buffer(1) |
| 790 | endfunc |
| 791 | |
| 792 | func Test_pipe_from_buffer_nr() |
| 793 | call Run_test_pipe_from_buffer(0) |
| 794 | endfunc |
| 795 | |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 796 | func Test_pipe_to_nameless_buffer() |
| 797 | if !has('job') |
| 798 | return |
| 799 | endif |
| 800 | call ch_log('Test_pipe_to_nameless_buffer()') |
| 801 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 802 | \ {'out_io': 'buffer'}) |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 803 | call assert_equal("run", job_status(job)) |
| 804 | try |
| 805 | let handle = job_getchannel(job) |
| 806 | call ch_sendraw(handle, "echo line one\n") |
| 807 | call ch_sendraw(handle, "echo line two\n") |
| 808 | exe ch_getbufnr(handle, "out") . 'sbuf' |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 809 | call s:waitFor('line("$") >= 3') |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 810 | call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$')) |
| 811 | bwipe! |
| 812 | finally |
| 813 | call job_stop(job) |
| 814 | endtry |
| 815 | endfunc |
| 816 | |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 817 | func Test_pipe_to_buffer_json() |
| 818 | if !has('job') |
| 819 | return |
| 820 | endif |
| 821 | call ch_log('Test_pipe_to_buffer_json()') |
| 822 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 823 | \ {'out_io': 'buffer', 'out_mode': 'json'}) |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 824 | call assert_equal("run", job_status(job)) |
| 825 | try |
| 826 | let handle = job_getchannel(job) |
| 827 | call ch_sendraw(handle, "echo [0, \"hello\"]\n") |
| 828 | call ch_sendraw(handle, "echo [-2, 12.34]\n") |
| 829 | exe ch_getbufnr(handle, "out") . 'sbuf' |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 830 | call s:waitFor('line("$") >= 3') |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 831 | call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$')) |
| 832 | bwipe! |
| 833 | finally |
| 834 | call job_stop(job) |
| 835 | endtry |
| 836 | endfunc |
| 837 | |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 838 | " Wait a little while for the last line, minus "offset", to equal "line". |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 839 | func s:wait_for_last_line(line, offset) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 840 | for i in range(100) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 841 | if getline(line('$') - a:offset) == a:line |
| 842 | break |
| 843 | endif |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 844 | sleep 10m |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 845 | endfor |
| 846 | endfunc |
| 847 | |
| 848 | func Test_pipe_io_two_buffers() |
| 849 | if !has('job') |
| 850 | return |
| 851 | endif |
| 852 | call ch_log('Test_pipe_io_two_buffers()') |
| 853 | |
| 854 | " Create two buffers, one to read from and one to write to. |
| 855 | split pipe-output |
| 856 | set buftype=nofile |
| 857 | split pipe-input |
| 858 | set buftype=nofile |
| 859 | |
| 860 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 861 | \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0, |
| 862 | \ 'out_io': 'buffer', 'out_name': 'pipe-output'}) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 863 | call assert_equal("run", job_status(job)) |
| 864 | try |
| 865 | exe "normal Gaecho hello\<CR>" |
| 866 | exe bufwinnr('pipe-output') . "wincmd w" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 867 | call s:wait_for_last_line('hello', 0) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 868 | call assert_equal('hello', getline('$')) |
| 869 | |
| 870 | exe bufwinnr('pipe-input') . "wincmd w" |
| 871 | exe "normal Gadouble this\<CR>" |
| 872 | exe bufwinnr('pipe-output') . "wincmd w" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 873 | call s:wait_for_last_line('AND this', 0) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 874 | call assert_equal('this', getline(line('$') - 1)) |
| 875 | call assert_equal('AND this', getline('$')) |
| 876 | |
| 877 | bwipe! |
| 878 | exe bufwinnr('pipe-input') . "wincmd w" |
| 879 | bwipe! |
| 880 | finally |
| 881 | call job_stop(job) |
| 882 | endtry |
| 883 | endfunc |
| 884 | |
| 885 | func Test_pipe_io_one_buffer() |
| 886 | if !has('job') |
| 887 | return |
| 888 | endif |
| 889 | call ch_log('Test_pipe_io_one_buffer()') |
| 890 | |
| 891 | " Create one buffer to read from and to write to. |
| 892 | split pipe-io |
| 893 | set buftype=nofile |
| 894 | |
| 895 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 896 | \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0, |
| 897 | \ 'out_io': 'buffer', 'out_name': 'pipe-io'}) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 898 | call assert_equal("run", job_status(job)) |
| 899 | try |
| 900 | exe "normal Goecho hello\<CR>" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 901 | call s:wait_for_last_line('hello', 1) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 902 | call assert_equal('hello', getline(line('$') - 1)) |
| 903 | |
| 904 | exe "normal Gadouble this\<CR>" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 905 | call s:wait_for_last_line('AND this', 1) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 906 | call assert_equal('this', getline(line('$') - 2)) |
| 907 | call assert_equal('AND this', getline(line('$') - 1)) |
| 908 | |
| 909 | bwipe! |
| 910 | finally |
| 911 | call job_stop(job) |
| 912 | endtry |
| 913 | endfunc |
| 914 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 915 | func Test_pipe_null() |
| 916 | if !has('job') |
| 917 | return |
| 918 | endif |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 919 | call ch_log('Test_pipe_null()') |
| 920 | |
| 921 | " We cannot check that no I/O works, we only check that the job starts |
| 922 | " properly. |
| 923 | let job = job_start(s:python . " test_channel_pipe.py something", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 924 | \ {'in_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 925 | call assert_equal("run", job_status(job)) |
| 926 | try |
| 927 | call assert_equal('something', ch_read(job)) |
| 928 | finally |
| 929 | call job_stop(job) |
| 930 | endtry |
| 931 | |
| 932 | let job = job_start(s:python . " test_channel_pipe.py err-out", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 933 | \ {'out_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 934 | call assert_equal("run", job_status(job)) |
| 935 | try |
| 936 | call assert_equal('err-out', ch_read(job, {"part": "err"})) |
| 937 | finally |
| 938 | call job_stop(job) |
| 939 | endtry |
| 940 | |
| 941 | let job = job_start(s:python . " test_channel_pipe.py something", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 942 | \ {'err_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 943 | call assert_equal("run", job_status(job)) |
| 944 | try |
| 945 | call assert_equal('something', ch_read(job)) |
| 946 | finally |
| 947 | call job_stop(job) |
| 948 | endtry |
| 949 | |
| 950 | let job = job_start(s:python . " test_channel_pipe.py something", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 951 | \ {'out_io': 'null', 'err_io': 'out'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 952 | call assert_equal("run", job_status(job)) |
| 953 | call job_stop(job) |
| 954 | |
| 955 | let job = job_start(s:python . " test_channel_pipe.py something", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 956 | \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 957 | call assert_equal("run", job_status(job)) |
| 958 | call assert_equal('channel fail', string(job_getchannel(job))) |
| 959 | call assert_equal('fail', ch_status(job)) |
| 960 | call job_stop(job) |
| 961 | endfunc |
| 962 | |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 963 | func Test_reuse_channel() |
| 964 | if !has('job') |
| 965 | return |
| 966 | endif |
| 967 | call ch_log('Test_reuse_channel()') |
| 968 | |
| 969 | let job = job_start(s:python . " test_channel_pipe.py") |
| 970 | call assert_equal("run", job_status(job)) |
| 971 | let handle = job_getchannel(job) |
| 972 | try |
| 973 | call ch_sendraw(handle, "echo something\n") |
| 974 | call assert_equal("something", ch_readraw(handle)) |
| 975 | finally |
| 976 | call job_stop(job) |
| 977 | endtry |
| 978 | |
| 979 | let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle}) |
| 980 | call assert_equal("run", job_status(job)) |
| 981 | let handle = job_getchannel(job) |
| 982 | try |
| 983 | call ch_sendraw(handle, "echo again\n") |
| 984 | call assert_equal("again", ch_readraw(handle)) |
| 985 | finally |
| 986 | call job_stop(job) |
| 987 | endtry |
| 988 | endfunc |
| 989 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 990 | """""""""" |
| 991 | |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 992 | let s:unletResponse = '' |
| 993 | func s:UnletHandler(handle, msg) |
| 994 | let s:unletResponse = a:msg |
| 995 | unlet s:channelfd |
| 996 | endfunc |
| 997 | |
| 998 | " Test that "unlet handle" in a handler doesn't crash Vim. |
| 999 | func s:unlet_handle(port) |
| 1000 | let s:channelfd = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 1001 | call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1002 | call s:waitFor('"what?" == s:unletResponse') |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1003 | call assert_equal('what?', s:unletResponse) |
| 1004 | endfunc |
| 1005 | |
| 1006 | func Test_unlet_handle() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1007 | call ch_log('Test_unlet_handle()') |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1008 | call s:run_server('s:unlet_handle') |
| 1009 | endfunc |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1010 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1011 | """""""""" |
| 1012 | |
| 1013 | let s:unletResponse = '' |
| 1014 | func s:CloseHandler(handle, msg) |
| 1015 | let s:unletResponse = a:msg |
| 1016 | call ch_close(s:channelfd) |
| 1017 | endfunc |
| 1018 | |
| 1019 | " Test that "unlet handle" in a handler doesn't crash Vim. |
| 1020 | func s:close_handle(port) |
| 1021 | let s:channelfd = ch_open('localhost:' . a:port, s:chopt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 1022 | call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1023 | call s:waitFor('"what?" == s:unletResponse') |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1024 | call assert_equal('what?', s:unletResponse) |
| 1025 | endfunc |
| 1026 | |
| 1027 | func Test_close_handle() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1028 | call ch_log('Test_close_handle()') |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1029 | call s:run_server('s:close_handle') |
| 1030 | endfunc |
| 1031 | |
| 1032 | """""""""" |
| 1033 | |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1034 | func Test_open_fail() |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1035 | call ch_log('Test_open_fail()') |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1036 | silent! let ch = ch_open("noserver") |
| 1037 | echo ch |
| 1038 | let d = ch |
| 1039 | endfunc |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1040 | |
| 1041 | """""""""" |
| 1042 | |
| 1043 | func s:open_delay(port) |
| 1044 | " Wait up to a second for the port to open. |
| 1045 | let s:chopt.waittime = 1000 |
| 1046 | let channel = ch_open('localhost:' . a:port, s:chopt) |
| 1047 | unlet s:chopt.waittime |
| 1048 | if ch_status(channel) == "fail" |
| 1049 | call assert_false(1, "Can't open channel") |
| 1050 | return |
| 1051 | endif |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 1052 | call assert_equal('got it', ch_evalexpr(channel, 'hello!')) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1053 | call ch_close(channel) |
| 1054 | endfunc |
| 1055 | |
| 1056 | func Test_open_delay() |
| 1057 | call ch_log('Test_open_delay()') |
| 1058 | " The server will wait half a second before creating the port. |
| 1059 | call s:run_server('s:open_delay', 'delay') |
| 1060 | endfunc |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1061 | |
| 1062 | """"""""" |
| 1063 | |
| 1064 | function MyFunction(a,b,c) |
| 1065 | let s:call_ret = [a:a, a:b, a:c] |
| 1066 | endfunc |
| 1067 | |
| 1068 | function s:test_call(port) |
| 1069 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 1070 | if ch_status(handle) == "fail" |
| 1071 | call assert_false(1, "Can't open channel") |
| 1072 | return |
| 1073 | endif |
| 1074 | |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1075 | let s:call_ret = [] |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 1076 | call assert_equal('ok', ch_evalexpr(handle, 'call-func')) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1077 | call s:waitFor('len(s:call_ret) > 0') |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1078 | call assert_equal([1, 2, 3], s:call_ret) |
| 1079 | endfunc |
| 1080 | |
| 1081 | func Test_call() |
| 1082 | call ch_log('Test_call()') |
| 1083 | call s:run_server('s:test_call') |
| 1084 | endfunc |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1085 | |
| 1086 | """"""""" |
| 1087 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1088 | let s:job_exit_ret = 'not yet' |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1089 | function MyExitCb(job, status) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1090 | let s:job_exit_ret = 'done' |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1091 | endfunc |
| 1092 | |
| 1093 | function s:test_exit_callback(port) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1094 | call job_setoptions(s:job, {'exit_cb': 'MyExitCb'}) |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1095 | let s:exit_job = s:job |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1096 | call assert_equal('MyExitCb', job_info(s:job)['exit_cb']) |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1097 | endfunc |
| 1098 | |
| 1099 | func Test_exit_callback() |
| 1100 | if has('job') |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 1101 | call ch_log('Test_exit_callback()') |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1102 | call s:run_server('s:test_exit_callback') |
| 1103 | |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 1104 | " wait up to a second for the job to exit |
| 1105 | for i in range(100) |
| 1106 | if s:job_exit_ret == 'done' |
| 1107 | break |
| 1108 | endif |
| 1109 | sleep 10m |
| 1110 | " calling job_status() triggers the callback |
| 1111 | call job_status(s:exit_job) |
| 1112 | endfor |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1113 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1114 | call assert_equal('done', s:job_exit_ret) |
Bram Moolenaar | 8950a56 | 2016-03-12 15:22:55 +0100 | [diff] [blame] | 1115 | call assert_equal('dead', job_info(s:exit_job).status) |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 1116 | unlet s:exit_job |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1117 | endif |
| 1118 | endfunc |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1119 | |
| 1120 | """"""""" |
| 1121 | |
| 1122 | let s:ch_close_ret = 'alive' |
| 1123 | function MyCloseCb(ch) |
| 1124 | let s:ch_close_ret = 'closed' |
| 1125 | endfunc |
| 1126 | |
| 1127 | function s:test_close_callback(port) |
| 1128 | let handle = ch_open('localhost:' . a:port, s:chopt) |
| 1129 | if ch_status(handle) == "fail" |
| 1130 | call assert_false(1, "Can't open channel") |
| 1131 | return |
| 1132 | endif |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1133 | call ch_setoptions(handle, {'close_cb': 'MyCloseCb'}) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1134 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 1135 | call assert_equal('', ch_evalexpr(handle, 'close me')) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1136 | call s:waitFor('"closed" == s:ch_close_ret') |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1137 | call assert_equal('closed', s:ch_close_ret) |
| 1138 | endfunc |
| 1139 | |
| 1140 | func Test_close_callback() |
| 1141 | call ch_log('Test_close_callback()') |
| 1142 | call s:run_server('s:test_close_callback') |
| 1143 | endfunc |
| 1144 | |
Bram Moolenaar | 9730f74 | 2016-02-28 19:50:51 +0100 | [diff] [blame] | 1145 | " Uncomment this to see what happens, output is in src/testdir/channellog. |
| 1146 | " call ch_logfile('channellog', 'w') |