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