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