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