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