Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 1 | " Test for channel and job functions. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2 | |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 3 | " When +channel is supported then +job is too, so we don't check for that. |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 4 | CheckFeature channel |
Bram Moolenaar | e246925 | 2016-02-04 10:54:34 +0100 | [diff] [blame] | 5 | |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 6 | source util/screendump.vim |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 7 | |
| 8 | let s:python = PythonProg() |
| 9 | if s:python == '' |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 10 | " Can't run this test without Python. |
Bram Moolenaar | 5d30ff1 | 2019-06-06 16:12:12 +0200 | [diff] [blame] | 11 | throw 'Skipped: Python command missing' |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 12 | endif |
| 13 | |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 14 | " Uncomment the next line to see what happens. Output is in |
| 15 | " src/testdir/channellog. |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 16 | " Add ch_log() calls where you want to see what happens. |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 17 | " call ch_logfile('channellog', 'w') |
| 18 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 19 | func SetUp() |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 20 | if g:testfunc =~ '_ipv6()$' |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 21 | let s:localhost = '[::1]:' |
| 22 | let s:testscript = 'test_channel_6.py' |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 23 | elseif g:testfunc =~ '_unix()$' |
| 24 | let s:localhost = 'unix:Xtestsocket' |
| 25 | let s:testscript = 'test_channel_unix.py' |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 26 | else |
| 27 | let s:localhost = 'localhost:' |
| 28 | let s:testscript = 'test_channel.py' |
| 29 | endif |
| 30 | let s:chopt = {} |
| 31 | call ch_log(g:testfunc) |
Bram Moolenaar | ec9b017 | 2020-06-19 19:10:59 +0200 | [diff] [blame] | 32 | |
| 33 | " Most tests use job_start(), which can be flaky |
| 34 | let g:test_is_flaky = 1 |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 35 | endfunc |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 36 | |
Bram Moolenaar | 4b96df5 | 2020-01-26 22:00:26 +0100 | [diff] [blame] | 37 | " Run "testfunc" after starting the server and stop the server afterwards. |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 38 | func s:run_server(testfunc, ...) |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 39 | call RunServer(s:testscript, a:testfunc, a:000) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 40 | endfunc |
| 41 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 42 | " Returns the address of the test server. |
| 43 | func s:address(port) |
| 44 | if s:localhost =~ '^unix:' |
| 45 | return s:localhost |
| 46 | else |
| 47 | return s:localhost . a:port |
| 48 | end |
| 49 | endfunc |
| 50 | |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 51 | " Return a list of open files. |
| 52 | " Can be used to make sure no resources leaked. |
| 53 | " Returns an empty list on systems where this is not supported. |
| 54 | func s:get_resources() |
| 55 | let pid = getpid() |
| 56 | |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 57 | if executable('lsof') |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 58 | return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''') |
| 59 | elseif isdirectory('/proc/' . pid . '/fd/') |
| 60 | return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''') |
| 61 | else |
| 62 | return [] |
| 63 | endif |
| 64 | endfunc |
| 65 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 66 | let g:Ch_responseMsg = '' |
| 67 | func Ch_requestHandler(handle, msg) |
| 68 | let g:Ch_responseHandle = a:handle |
| 69 | let g:Ch_responseMsg = a:msg |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 70 | endfunc |
| 71 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 72 | func Ch_communicate(port) |
Bram Moolenaar | 5643db8 | 2016-12-03 14:29:10 +0100 | [diff] [blame] | 73 | " Avoid dropping messages, since we don't use a callback here. |
| 74 | let s:chopt.drop = 'never' |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 75 | " Also add the noblock flag to try it out. |
| 76 | let s:chopt.noblock = 1 |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 77 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 78 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 79 | call assert_report("Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 80 | return |
| 81 | endif |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 82 | |
| 83 | " check that getjob without a job is handled correctly |
| 84 | call assert_equal('no process', string(ch_getjob(handle))) |
| 85 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 86 | let dict = handle->ch_info() |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 87 | call assert_true(dict.id != 0) |
| 88 | call assert_equal('open', dict.status) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 89 | if has_key(dict, 'port') |
| 90 | " Channels using Unix sockets have no 'port' entry. |
| 91 | call assert_equal(a:port, string(dict.port)) |
| 92 | end |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 93 | call assert_equal('open', dict.sock_status) |
| 94 | call assert_equal('socket', dict.sock_io) |
| 95 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 96 | " Simple string request and reply. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 97 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 98 | |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 99 | " Malformed command should be ignored. |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 100 | call assert_equal('ok', ch_evalexpr(handle, 'malformed1')) |
| 101 | call assert_equal('ok', ch_evalexpr(handle, 'malformed2')) |
| 102 | call assert_equal('ok', ch_evalexpr(handle, 'malformed3')) |
| 103 | |
| 104 | " split command should work |
| 105 | call assert_equal('ok', ch_evalexpr(handle, 'split')) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 106 | call WaitFor('exists("g:split")') |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 107 | call assert_equal(123, g:split) |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 108 | |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 109 | " string with ][ should work |
| 110 | call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that')) |
| 111 | |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 112 | " nothing to read now |
| 113 | call assert_equal(0, ch_canread(handle)) |
| 114 | |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 115 | " sending three messages quickly then reading should work |
| 116 | for i in range(3) |
| 117 | call ch_sendexpr(handle, 'echo hello ' . i) |
| 118 | endfor |
| 119 | call assert_equal('hello 0', ch_read(handle)[1]) |
| 120 | call assert_equal('hello 1', ch_read(handle)[1]) |
| 121 | call assert_equal('hello 2', ch_read(handle)[1]) |
| 122 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 123 | " Request that triggers sending two ex commands. These will usually be |
| 124 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 125 | " tiny bit for the commands to get executed. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 126 | call assert_equal('ok', ch_evalexpr(handle, 'make change')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 127 | call WaitForAssert({-> assert_equal("added2", getline("$"))}) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 128 | call assert_equal('added1', getline(line('$') - 1)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 129 | |
Bram Moolenaar | b836f63 | 2021-07-01 22:11:28 +0200 | [diff] [blame] | 130 | " Request command "echoerr 'this is an error'". |
| 131 | " This will throw an exception, catch it here. |
| 132 | let caught = 'no' |
| 133 | try |
| 134 | call assert_equal('ok', ch_evalexpr(handle, 'echoerr')) |
| 135 | catch /this is an error/ |
| 136 | let caught = 'yes' |
| 137 | endtry |
| 138 | if caught != 'yes' |
| 139 | call assert_report("Expected exception from error message") |
| 140 | endif |
| 141 | |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 142 | " Request command "foo bar", which fails silently. |
| 143 | call assert_equal('ok', ch_evalexpr(handle, 'bad command')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 144 | call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)}) |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 145 | |
Bram Moolenaar | da94fdf | 2016-03-03 18:09:10 +0100 | [diff] [blame] | 146 | call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100})) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 147 | call WaitForAssert({-> assert_equal('added more', getline('$'))}) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 148 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 149 | " Send a request with a specific handler. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 150 | call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'}) |
| 151 | call WaitFor('exists("g:Ch_responseHandle")') |
| 152 | if !exists('g:Ch_responseHandle') |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 153 | call assert_report('g:Ch_responseHandle was not set') |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 154 | else |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 155 | call assert_equal(handle, g:Ch_responseHandle) |
| 156 | unlet g:Ch_responseHandle |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 157 | endif |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 158 | call assert_equal('got it', g:Ch_responseMsg) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 159 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 160 | let g:Ch_responseMsg = '' |
| 161 | call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')}) |
| 162 | call WaitFor('exists("g:Ch_responseHandle")') |
| 163 | if !exists('g:Ch_responseHandle') |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 164 | call assert_report('g:Ch_responseHandle was not set') |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 165 | else |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 166 | call assert_equal(handle, g:Ch_responseHandle) |
| 167 | unlet g:Ch_responseHandle |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 168 | endif |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 169 | call assert_equal('got it', g:Ch_responseMsg) |
Bram Moolenaar | b6a4fee | 2016-02-11 20:48:34 +0100 | [diff] [blame] | 170 | |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 171 | " Using lambda. |
| 172 | let g:Ch_responseMsg = '' |
| 173 | call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}}) |
| 174 | call WaitFor('exists("g:Ch_responseHandle")') |
| 175 | if !exists('g:Ch_responseHandle') |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 176 | call assert_report('g:Ch_responseHandle was not set') |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 177 | else |
| 178 | call assert_equal(handle, g:Ch_responseHandle) |
| 179 | unlet g:Ch_responseHandle |
| 180 | endif |
| 181 | call assert_equal('got it', g:Ch_responseMsg) |
| 182 | |
Bram Moolenaar | 38fd4bb | 2016-03-06 16:38:28 +0100 | [diff] [blame] | 183 | " Collect garbage, tests that our handle isn't collected. |
Bram Moolenaar | 574860b | 2016-05-24 17:33:34 +0200 | [diff] [blame] | 184 | call test_garbagecollect_now() |
Bram Moolenaar | 38fd4bb | 2016-03-06 16:38:28 +0100 | [diff] [blame] | 185 | |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 186 | " check setting options (without testing the effect) |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 187 | eval handle->ch_setoptions({'callback': 's:NotUsed'}) |
Bram Moolenaar | 1f6ef66 | 2016-02-19 22:59:44 +0100 | [diff] [blame] | 188 | call ch_setoptions(handle, {'timeout': 1111}) |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 189 | call ch_setoptions(handle, {'mode': 'json'}) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 190 | call assert_fails("call ch_setoptions(handle, {'waittime': 111})", 'E475:') |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 191 | call ch_setoptions(handle, {'callback': ''}) |
Bram Moolenaar | 65e08ee | 2016-12-01 16:41:50 +0100 | [diff] [blame] | 192 | call ch_setoptions(handle, {'drop': 'never'}) |
| 193 | call ch_setoptions(handle, {'drop': 'auto'}) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 194 | call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", 'E475:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 195 | call assert_equal(0, ch_setoptions(handle, test_null_dict())) |
| 196 | call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'})) |
Bram Moolenaar | 40ea1da | 2016-02-19 22:33:35 +0100 | [diff] [blame] | 197 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 198 | " Send an eval request that works. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 199 | call assert_equal('ok', ch_evalexpr(handle, 'eval-works')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 200 | sleep 10m |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 201 | call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 202 | |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 203 | " Send an eval request with special characters. |
| 204 | call assert_equal('ok', ch_evalexpr(handle, 'eval-special')) |
| 205 | sleep 10m |
| 206 | call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result')) |
| 207 | |
| 208 | " Send an eval request to get a line with special characters. |
| 209 | call setline(3, "a\nb\<CR>c\x01d\x7fe") |
| 210 | call assert_equal('ok', ch_evalexpr(handle, 'eval-getline')) |
| 211 | sleep 10m |
| 212 | call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result')) |
| 213 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 214 | " Send an eval request that fails. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 215 | call assert_equal('ok', ch_evalexpr(handle, 'eval-fails')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 216 | sleep 10m |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 217 | call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 218 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 219 | " Send an eval request that works but can't be encoded. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 220 | call assert_equal('ok', ch_evalexpr(handle, 'eval-error')) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 221 | sleep 10m |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 222 | call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 224 | " Send a bad eval request. There will be no response. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 225 | call assert_equal('ok', ch_evalexpr(handle, 'eval-bad')) |
Bram Moolenaar | a02b321 | 2016-02-04 21:03:33 +0100 | [diff] [blame] | 226 | sleep 10m |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 227 | call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result')) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 228 | |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 229 | " Send an expr request |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 230 | call assert_equal('ok', ch_evalexpr(handle, 'an expr')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 231 | call WaitForAssert({-> assert_equal('three', getline('$'))}) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 232 | call assert_equal('one', getline(line('$') - 2)) |
| 233 | call assert_equal('two', getline(line('$') - 1)) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 234 | |
| 235 | " Request a redraw, we don't check for the effect. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 236 | call assert_equal('ok', ch_evalexpr(handle, 'redraw')) |
| 237 | call assert_equal('ok', ch_evalexpr(handle, 'redraw!')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 238 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 239 | call assert_equal('ok', ch_evalexpr(handle, 'empty-request')) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 241 | " Reading while there is nothing available. |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 242 | call assert_equal(v:none, ch_read(handle, {'timeout': 0})) |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 243 | if exists('*reltimefloat') |
| 244 | let start = reltime() |
| 245 | call assert_equal(v:none, ch_read(handle, {'timeout': 333})) |
| 246 | let elapsed = reltime(start) |
| 247 | call assert_inrange(0.3, 0.6, reltimefloat(reltime(start))) |
| 248 | endif |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 249 | |
| 250 | " Send without waiting for a response, then wait for a response. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 251 | call ch_sendexpr(handle, 'wait a bit') |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 252 | let resp = ch_read(handle) |
| 253 | call assert_equal(type([]), type(resp)) |
| 254 | call assert_equal(type(11), type(resp[0])) |
| 255 | call assert_equal('waited', resp[1]) |
| 256 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 257 | " 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] | 258 | call ch_sendexpr(handle, '!quit!') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 259 | endfunc |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 260 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 261 | func Test_communicate() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 262 | call s:run_server('Ch_communicate') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 263 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 264 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 265 | func Test_communicate_ipv6() |
| 266 | CheckIPv6 |
| 267 | call Test_communicate() |
| 268 | endfunc |
| 269 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 270 | func Test_communicate_unix() |
| 271 | CheckUnix |
| 272 | call Test_communicate() |
| 273 | call delete('Xtestsocket') |
| 274 | endfunc |
| 275 | |
| 276 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 277 | " Test that we can open two channels. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 278 | func Ch_two_channels(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 279 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 280 | call assert_equal(v:t_channel, type(handle)) |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 281 | if handle->ch_status() == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 282 | call assert_report("Can't open channel") |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 283 | return |
| 284 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 285 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 286 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 287 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 288 | let newhandle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 289 | if ch_status(newhandle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 290 | call assert_report("Can't open second channel") |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 291 | return |
| 292 | endif |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 293 | call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) |
| 294 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 295 | |
| 296 | call ch_close(handle) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 297 | call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 298 | |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 299 | call ch_close(newhandle) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 300 | call assert_fails("call ch_close(newhandle)", 'E906:') |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 301 | endfunc |
| 302 | |
| 303 | func Test_two_channels() |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 304 | eval 'Test_two_channels()'->ch_log() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 305 | call s:run_server('Ch_two_channels') |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 306 | endfunc |
| 307 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 308 | func Test_two_channels_ipv6() |
| 309 | CheckIPv6 |
| 310 | call Test_two_channels() |
| 311 | endfunc |
| 312 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 313 | func Test_two_channels_unix() |
| 314 | CheckUnix |
| 315 | call Test_two_channels() |
| 316 | call delete('Xtestsocket') |
| 317 | endfunc |
| 318 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 319 | " Test that a server crash is handled gracefully. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 320 | func Ch_server_crash(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 321 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 322 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 323 | call assert_report("Can't open channel") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 324 | return |
| 325 | endif |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 326 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 327 | call ch_evalexpr(handle, '!crash!') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 328 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 329 | sleep 10m |
Bram Moolenaar | d6a8d48 | 2016-02-10 20:32:20 +0100 | [diff] [blame] | 330 | endfunc |
| 331 | |
| 332 | func Test_server_crash() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 333 | call s:run_server('Ch_server_crash') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 334 | endfunc |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 335 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 336 | func Test_server_crash_ipv6() |
| 337 | CheckIPv6 |
| 338 | call Test_server_crash() |
| 339 | endfunc |
| 340 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 341 | func Test_server_crash_unix() |
| 342 | CheckUnix |
| 343 | call Test_server_crash() |
| 344 | call delete('Xtestsocket') |
| 345 | endfunc |
| 346 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 347 | """"""""" |
| 348 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 349 | func Ch_handler(chan, msg) |
Bram Moolenaar | 65e08ee | 2016-12-01 16:41:50 +0100 | [diff] [blame] | 350 | call ch_log('Ch_handler()') |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 351 | unlet g:Ch_reply |
| 352 | let g:Ch_reply = a:msg |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 353 | endfunc |
| 354 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 355 | func Ch_channel_handler(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 356 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 357 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 358 | call assert_report("Can't open channel") |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 359 | return |
| 360 | endif |
| 361 | |
| 362 | " Test that it works while waiting on a numbered message. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 363 | call assert_equal('ok', ch_evalexpr(handle, 'call me')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 364 | call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)}) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 365 | |
| 366 | " Test that it works while not waiting on a numbered message. |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 367 | call ch_sendexpr(handle, 'call me again') |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 368 | call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)}) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 369 | endfunc |
| 370 | |
| 371 | func Test_channel_handler() |
Bram Moolenaar | 6fc8227 | 2016-08-28 19:26:43 +0200 | [diff] [blame] | 372 | let g:Ch_reply = "" |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 373 | let s:chopt.callback = 'Ch_handler' |
| 374 | call s:run_server('Ch_channel_handler') |
Bram Moolenaar | 6fc8227 | 2016-08-28 19:26:43 +0200 | [diff] [blame] | 375 | let g:Ch_reply = "" |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 376 | let s:chopt.callback = function('Ch_handler') |
| 377 | call s:run_server('Ch_channel_handler') |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 378 | endfunc |
| 379 | |
| 380 | func Test_channel_handler_ipv6() |
| 381 | CheckIPv6 |
| 382 | call Test_channel_handler() |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 383 | endfunc |
| 384 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 385 | func Test_channel_handler_unix() |
| 386 | CheckUnix |
| 387 | call Test_channel_handler() |
| 388 | call delete('Xtestsocket') |
| 389 | endfunc |
| 390 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 391 | """"""""" |
| 392 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 393 | let g:Ch_reply = '' |
| 394 | func Ch_zeroHandler(chan, msg) |
| 395 | unlet g:Ch_reply |
| 396 | let g:Ch_reply = a:msg |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 397 | endfunc |
| 398 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 399 | let g:Ch_zero_reply = '' |
| 400 | func Ch_oneHandler(chan, msg) |
| 401 | unlet g:Ch_zero_reply |
| 402 | let g:Ch_zero_reply = a:msg |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 403 | endfunc |
| 404 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 405 | func Ch_channel_zero(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 406 | let handle = (s:address(a:port))->ch_open(s:chopt) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 407 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 408 | call assert_report("Can't open channel") |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 409 | return |
| 410 | endif |
| 411 | |
| 412 | " Check that eval works. |
| 413 | call assert_equal('got it', ch_evalexpr(handle, 'hello!')) |
| 414 | |
| 415 | " Check that eval works if a zero id message is sent back. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 416 | let g:Ch_reply = '' |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 417 | call assert_equal('sent zero', ch_evalexpr(handle, 'send zero')) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 418 | if s:has_handler |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 419 | call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)}) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 420 | else |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 421 | sleep 20m |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 422 | call assert_equal('', g:Ch_reply) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 423 | endif |
| 424 | |
| 425 | " Check that handler works if a zero id message is sent back. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 426 | let g:Ch_reply = '' |
| 427 | let g:Ch_zero_reply = '' |
| 428 | call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 429 | call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)}) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 430 | if s:has_handler |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 431 | call assert_equal('zero index', g:Ch_reply) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 432 | else |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 433 | call assert_equal('', g:Ch_reply) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 434 | endif |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 435 | endfunc |
| 436 | |
| 437 | func Test_zero_reply() |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 438 | " Run with channel handler |
| 439 | let s:has_handler = 1 |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 440 | let s:chopt.callback = 'Ch_zeroHandler' |
| 441 | call s:run_server('Ch_channel_zero') |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 442 | unlet s:chopt.callback |
| 443 | |
| 444 | " Run without channel handler |
| 445 | let s:has_handler = 0 |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 446 | call s:run_server('Ch_channel_zero') |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 447 | endfunc |
| 448 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 449 | func Test_zero_reply_ipv6() |
| 450 | CheckIPv6 |
| 451 | call Test_zero_reply() |
| 452 | endfunc |
| 453 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 454 | func Test_zero_reply_unix() |
| 455 | CheckUnix |
| 456 | call Test_zero_reply() |
| 457 | call delete('Xtestsocket') |
| 458 | endfunc |
| 459 | |
| 460 | |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 461 | """"""""" |
| 462 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 463 | let g:Ch_reply1 = "" |
| 464 | func Ch_handleRaw1(chan, msg) |
| 465 | unlet g:Ch_reply1 |
| 466 | let g:Ch_reply1 = a:msg |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 467 | endfunc |
| 468 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 469 | let g:Ch_reply2 = "" |
| 470 | func Ch_handleRaw2(chan, msg) |
| 471 | unlet g:Ch_reply2 |
| 472 | let g:Ch_reply2 = a:msg |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 473 | endfunc |
| 474 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 475 | let g:Ch_reply3 = "" |
| 476 | func Ch_handleRaw3(chan, msg) |
| 477 | unlet g:Ch_reply3 |
| 478 | let g:Ch_reply3 = a:msg |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 479 | endfunc |
| 480 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 481 | func Ch_raw_one_time_callback(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 482 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 483 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 484 | call assert_report("Can't open channel") |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 485 | return |
| 486 | endif |
| 487 | call ch_setoptions(handle, {'mode': 'raw'}) |
| 488 | |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 489 | " The messages are sent raw, we do our own JSON strings here. |
Bram Moolenaar | dd74ab9 | 2016-08-26 19:20:26 +0200 | [diff] [blame] | 490 | call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 491 | call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)}) |
Bram Moolenaar | dd74ab9 | 2016-08-26 19:20:26 +0200 | [diff] [blame] | 492 | call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'}) |
| 493 | call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 494 | call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)}) |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 495 | " wait for the 200 msec delayed reply |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 496 | call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)}) |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 497 | endfunc |
| 498 | |
| 499 | func Test_raw_one_time_callback() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 500 | call s:run_server('Ch_raw_one_time_callback') |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 501 | endfunc |
| 502 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 503 | func Test_raw_one_time_callback_ipv6() |
| 504 | CheckIPv6 |
| 505 | call Test_raw_one_time_callback() |
| 506 | endfunc |
| 507 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 508 | func Test_raw_one_time_callback_unix() |
| 509 | CheckUnix |
| 510 | call Test_raw_one_time_callback() |
| 511 | call delete('Xtestsocket') |
| 512 | endfunc |
| 513 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 514 | """"""""" |
| 515 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 516 | " Test that trying to connect to a non-existing port fails quickly. |
| 517 | func Test_connect_waittime() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 518 | CheckFunction reltimefloat |
Bram Moolenaar | 373a876 | 2020-03-19 19:44:32 +0100 | [diff] [blame] | 519 | " this is timing sensitive |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 520 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 521 | let start = reltime() |
Bram Moolenaar | a483326 | 2016-02-09 23:33:25 +0100 | [diff] [blame] | 522 | let handle = ch_open('localhost:9876', s:chopt) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 523 | if ch_status(handle) != "fail" |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 524 | " Oops, port exists. |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 525 | call ch_close(handle) |
| 526 | else |
| 527 | let elapsed = reltime(start) |
zeertzjq | cdc6a43 | 2022-06-19 11:45:46 +0100 | [diff] [blame] | 528 | call assert_inrange(0.0, 1.0, reltimefloat(elapsed)) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 529 | endif |
| 530 | |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 531 | " We intend to use a socket that doesn't exist and wait for half a second |
| 532 | " before giving up. If the socket does exist it can fail in various ways. |
Bram Moolenaar | 4b96df5 | 2020-01-26 22:00:26 +0100 | [diff] [blame] | 533 | " Check for "Connection reset by peer" to avoid flakiness. |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 534 | let start = reltime() |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 535 | try |
| 536 | let handle = ch_open('localhost:9867', {'waittime': 500}) |
| 537 | if ch_status(handle) != "fail" |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 538 | " Oops, port exists. |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 539 | call ch_close(handle) |
| 540 | else |
Bram Moolenaar | ac42afd | 2016-03-12 13:48:49 +0100 | [diff] [blame] | 541 | " Failed connection should wait about 500 msec. Can be longer if the |
| 542 | " computer is busy with other things. |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 543 | call assert_inrange(0.3, 1.5, reltimefloat(reltime(start))) |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 544 | endif |
| 545 | catch |
| 546 | if v:exception !~ 'Connection reset by peer' |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 547 | call assert_report("Caught exception: " . v:exception) |
Bram Moolenaar | 08298fa | 2016-02-21 13:01:53 +0100 | [diff] [blame] | 548 | endif |
| 549 | endtry |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 550 | endfunc |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 551 | |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 552 | """"""""" |
| 553 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 554 | func Test_raw_pipe() |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 555 | " Add a dummy close callback to avoid that messages are dropped when calling |
| 556 | " ch_canread(). |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 557 | " Also test the non-blocking option. |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 558 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 559 | \ {'mode': 'raw', 'drop': 'never', 'noblock': 1}) |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 560 | call assert_equal(v:t_job, type(job)) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 561 | call assert_equal("run", job_status(job)) |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 562 | |
| 563 | call assert_equal("open", ch_status(job)) |
| 564 | call assert_equal("open", ch_status(job), {"part": "out"}) |
| 565 | call assert_equal("open", ch_status(job), {"part": "err"}) |
| 566 | call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:') |
| 567 | call assert_fails('call ch_status(job, {"part": "in"})', 'E475:') |
| 568 | |
| 569 | let dict = ch_info(job) |
| 570 | call assert_true(dict.id != 0) |
| 571 | call assert_equal('open', dict.status) |
| 572 | call assert_equal('open', dict.out_status) |
| 573 | call assert_equal('RAW', dict.out_mode) |
| 574 | call assert_equal('pipe', dict.out_io) |
| 575 | call assert_equal('open', dict.err_status) |
| 576 | call assert_equal('RAW', dict.err_mode) |
| 577 | call assert_equal('pipe', dict.err_io) |
| 578 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 579 | try |
Bram Moolenaar | 151f656 | 2016-03-07 21:19:38 +0100 | [diff] [blame] | 580 | " For a change use the job where a channel is expected. |
| 581 | call ch_sendraw(job, "echo something\n") |
| 582 | let msg = ch_readraw(job) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 583 | call assert_equal("something\n", substitute(msg, "\r", "", 'g')) |
| 584 | |
Bram Moolenaar | 151f656 | 2016-03-07 21:19:38 +0100 | [diff] [blame] | 585 | call ch_sendraw(job, "double this\n") |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 586 | let g:handle = job->job_getchannel() |
| 587 | call WaitFor('g:handle->ch_canread()') |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 588 | unlet g:handle |
Bram Moolenaar | 151f656 | 2016-03-07 21:19:38 +0100 | [diff] [blame] | 589 | let msg = ch_readraw(job) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 590 | call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g')) |
| 591 | |
Bram Moolenaar | 6fc8227 | 2016-08-28 19:26:43 +0200 | [diff] [blame] | 592 | let g:Ch_reply = "" |
| 593 | call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 594 | call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))}) |
Bram Moolenaar | 6fc8227 | 2016-08-28 19:26:43 +0200 | [diff] [blame] | 595 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 596 | call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:') |
| 597 | call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:') |
| 598 | call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:') |
| 599 | call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:') |
| 600 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 601 | let reply = job->ch_evalraw("quit\n", {'timeout': 100}) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 602 | call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g')) |
| 603 | finally |
| 604 | call job_stop(job) |
| 605 | endtry |
Bram Moolenaar | 8950a56 | 2016-03-12 15:22:55 +0100 | [diff] [blame] | 606 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 607 | let g:Ch_job = job |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 608 | call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))}) |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 609 | let info = job->job_info() |
Bram Moolenaar | 8950a56 | 2016-03-12 15:22:55 +0100 | [diff] [blame] | 610 | call assert_equal("dead", info.status) |
| 611 | call assert_equal("term", info.stoponexit) |
Bram Moolenaar | e1fc515 | 2018-04-21 19:49:08 +0200 | [diff] [blame] | 612 | call assert_equal(2, len(info.cmd)) |
| 613 | call assert_equal("test_channel_pipe.py", info.cmd[1]) |
| 614 | |
| 615 | let found = 0 |
| 616 | for j in job_info() |
| 617 | if j == job |
| 618 | let found += 1 |
| 619 | endif |
| 620 | endfor |
| 621 | call assert_equal(1, found) |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 622 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 623 | call assert_fails("call job_stop('abc')", 'E475:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 624 | call assert_fails("call job_stop(job, [])", 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 625 | call assert_fails("call job_stop(test_null_job())", 'E916:') |
| 626 | |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 627 | " Try to use the job and channel where a number is expected. This is not |
| 628 | " related to testing the raw pipe. This test is here just to reuse the |
| 629 | " already created job/channel. |
| 630 | let ch = job_getchannel(job) |
| 631 | call assert_fails('let i = job + 1', 'E910:') |
| 632 | call assert_fails('let j = ch + 1', 'E913:') |
| 633 | call assert_fails('echo 2.0 == job', 'E911:') |
| 634 | call assert_fails('echo 2.0 == ch', 'E914:') |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 635 | endfunc |
| 636 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 637 | func Test_raw_pipe_blob() |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 638 | " Add a dummy close callback to avoid that messages are dropped when calling |
| 639 | " ch_canread(). |
| 640 | " Also test the non-blocking option. |
| 641 | let job = job_start(s:python . " test_channel_pipe.py", |
| 642 | \ {'mode': 'raw', 'drop': 'never', 'noblock': 1}) |
| 643 | call assert_equal(v:t_job, type(job)) |
| 644 | call assert_equal("run", job_status(job)) |
| 645 | |
| 646 | call assert_equal("open", ch_status(job)) |
| 647 | call assert_equal("open", ch_status(job), {"part": "out"}) |
| 648 | |
| 649 | try |
| 650 | " Create a blob with the echo command and write it. |
| 651 | let blob = 0z00 |
| 652 | let cmd = "echo something\n" |
| 653 | for i in range(0, len(cmd) - 1) |
| 654 | let blob[i] = char2nr(cmd[i]) |
| 655 | endfor |
| 656 | call assert_equal(len(cmd), len(blob)) |
| 657 | call ch_sendraw(job, blob) |
| 658 | |
| 659 | " Read a blob with the reply. |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 660 | let msg = job->ch_readblob() |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 661 | let expected = 'something' |
| 662 | for i in range(0, len(expected) - 1) |
| 663 | call assert_equal(char2nr(expected[i]), msg[i]) |
| 664 | endfor |
| 665 | |
| 666 | let reply = ch_evalraw(job, "quit\n", {'timeout': 100}) |
| 667 | call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g')) |
| 668 | finally |
| 669 | call job_stop(job) |
| 670 | endtry |
| 671 | |
| 672 | let g:Ch_job = job |
| 673 | call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))}) |
| 674 | let info = job_info(job) |
| 675 | call assert_equal("dead", info.status) |
| 676 | endfunc |
| 677 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 678 | func Test_nl_pipe() |
Bram Moolenaar | 1adda34 | 2016-03-12 15:39:40 +0100 | [diff] [blame] | 679 | let job = job_start([s:python, "test_channel_pipe.py"]) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 680 | call assert_equal("run", job_status(job)) |
| 681 | try |
| 682 | let handle = job_getchannel(job) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 683 | call ch_sendraw(handle, "echo something\n") |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 684 | call assert_equal("something", handle->ch_readraw()) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 685 | |
Bram Moolenaar | c25558b | 2016-03-03 21:02:23 +0100 | [diff] [blame] | 686 | call ch_sendraw(handle, "echoerr wrong\n") |
| 687 | call assert_equal("wrong", ch_readraw(handle, {'part': 'err'})) |
| 688 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 689 | call ch_sendraw(handle, "double this\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 690 | call assert_equal("this", ch_readraw(handle)) |
| 691 | call assert_equal("AND this", ch_readraw(handle)) |
| 692 | |
Bram Moolenaar | bbe8d91 | 2016-06-05 16:10:57 +0200 | [diff] [blame] | 693 | call ch_sendraw(handle, "split this line\n") |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 694 | call assert_equal("this linethis linethis line", handle->ch_read()) |
Bram Moolenaar | bbe8d91 | 2016-06-05 16:10:57 +0200 | [diff] [blame] | 695 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 696 | let reply = ch_evalraw(handle, "quit\n") |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 697 | call assert_equal("Goodbye!", reply) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 698 | finally |
| 699 | call job_stop(job) |
| 700 | endtry |
| 701 | endfunc |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 702 | |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 703 | func Stop_g_job() |
| 704 | call job_stop(g:job) |
| 705 | if has('win32') |
| 706 | " On MS-Windows the server must close the file handle before we are able |
| 707 | " to delete the file. |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 708 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 709 | sleep 10m |
| 710 | endif |
| 711 | endfunc |
| 712 | |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 713 | func Test_nl_read_file() |
Bram Moolenaar | 45bbaef | 2022-09-08 16:39:22 +0100 | [diff] [blame] | 714 | call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput', 'D') |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 715 | let g:job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 716 | \ {'in_io': 'file', 'in_name': 'Xinput'}) |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 717 | call assert_equal("run", job_status(g:job)) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 718 | try |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 719 | let handle = job_getchannel(g:job) |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 720 | call assert_equal("something", ch_readraw(handle)) |
| 721 | call assert_equal("wrong", ch_readraw(handle, {'part': 'err'})) |
| 722 | call assert_equal("this", ch_readraw(handle)) |
| 723 | call assert_equal("AND this", ch_readraw(handle)) |
| 724 | finally |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 725 | call Stop_g_job() |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 726 | endtry |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 727 | call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:') |
Bram Moolenaar | b69fccf | 2016-03-06 23:06:25 +0100 | [diff] [blame] | 728 | endfunc |
| 729 | |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 730 | func Test_nl_write_out_file() |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 731 | let g:job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 732 | \ {'out_io': 'file', 'out_name': 'Xoutput'}) |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 733 | call assert_equal("run", job_status(g:job)) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 734 | try |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 735 | let handle = job_getchannel(g:job) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 736 | call ch_sendraw(handle, "echo line one\n") |
| 737 | call ch_sendraw(handle, "echo line two\n") |
| 738 | call ch_sendraw(handle, "double this\n") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 739 | call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))}) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 740 | finally |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 741 | call Stop_g_job() |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 742 | call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$')) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 743 | call delete('Xoutput') |
| 744 | endtry |
| 745 | endfunc |
| 746 | |
| 747 | func Test_nl_write_err_file() |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 748 | let g:job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 749 | \ {'err_io': 'file', 'err_name': 'Xoutput'}) |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 750 | call assert_equal("run", job_status(g:job)) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 751 | try |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 752 | let handle = job_getchannel(g:job) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 753 | call ch_sendraw(handle, "echoerr line one\n") |
| 754 | call ch_sendraw(handle, "echoerr line two\n") |
| 755 | call ch_sendraw(handle, "doubleerr this\n") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 756 | call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))}) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 757 | finally |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 758 | call Stop_g_job() |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 759 | call delete('Xoutput') |
| 760 | endtry |
| 761 | endfunc |
| 762 | |
| 763 | func Test_nl_write_both_file() |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 764 | let g:job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 765 | \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'}) |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 766 | call assert_equal("run", job_status(g:job)) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 767 | try |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 768 | let handle = job_getchannel(g:job) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 769 | call ch_sendraw(handle, "echoerr line one\n") |
| 770 | call ch_sendraw(handle, "echo line two\n") |
| 771 | call ch_sendraw(handle, "double this\n") |
| 772 | call ch_sendraw(handle, "doubleerr that\n") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 773 | call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))}) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 774 | finally |
Bram Moolenaar | 641ad6c | 2016-09-01 18:32:11 +0200 | [diff] [blame] | 775 | call Stop_g_job() |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 776 | call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$')) |
Bram Moolenaar | e98d121 | 2016-03-08 15:37:41 +0100 | [diff] [blame] | 777 | call delete('Xoutput') |
| 778 | endtry |
| 779 | endfunc |
| 780 | |
Bram Moolenaar | 01d46e4 | 2016-06-02 19:06:25 +0200 | [diff] [blame] | 781 | func BufCloseCb(ch) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 782 | let g:Ch_bufClosed = 'yes' |
Bram Moolenaar | 01d46e4 | 2016-06-02 19:06:25 +0200 | [diff] [blame] | 783 | endfunc |
| 784 | |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 785 | func Run_test_pipe_to_buffer(use_name, nomod, do_msg) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 786 | let g:Ch_bufClosed = 'no' |
Bram Moolenaar | 01d46e4 | 2016-06-02 19:06:25 +0200 | [diff] [blame] | 787 | let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'} |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 788 | let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'] |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 789 | if a:use_name |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 790 | let options['out_name'] = 'pipe-output' |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 791 | if a:do_msg |
| 792 | let expected[0] = 'Reading from channel output...' |
| 793 | else |
| 794 | let options['out_msg'] = 0 |
| 795 | call remove(expected, 0) |
| 796 | endif |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 797 | else |
| 798 | sp pipe-output |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 799 | let options['out_buf'] = bufnr('%') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 800 | quit |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 801 | call remove(expected, 0) |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 802 | endif |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 803 | if a:nomod |
| 804 | let options['out_modifiable'] = 0 |
| 805 | endif |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 806 | let job = job_start(s:python . " test_channel_pipe.py", options) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 807 | call assert_equal("run", job_status(job)) |
| 808 | try |
| 809 | let handle = job_getchannel(job) |
| 810 | call ch_sendraw(handle, "echo line one\n") |
| 811 | call ch_sendraw(handle, "echo line two\n") |
| 812 | call ch_sendraw(handle, "double this\n") |
| 813 | call ch_sendraw(handle, "quit\n") |
| 814 | sp pipe-output |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 815 | call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"') |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 816 | call assert_equal(expected, getline(1, '$')) |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 817 | if a:nomod |
| 818 | call assert_equal(0, &modifiable) |
| 819 | else |
| 820 | call assert_equal(1, &modifiable) |
| 821 | endif |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 822 | call assert_equal('yes', g:Ch_bufClosed) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 823 | bwipe! |
| 824 | finally |
| 825 | call job_stop(job) |
| 826 | endtry |
| 827 | endfunc |
| 828 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 829 | func Test_pipe_to_buffer_name() |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 830 | call Run_test_pipe_to_buffer(1, 0, 1) |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 831 | endfunc |
| 832 | |
| 833 | func Test_pipe_to_buffer_nr() |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 834 | call Run_test_pipe_to_buffer(0, 0, 1) |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 835 | endfunc |
| 836 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 837 | func Test_pipe_to_buffer_name_nomod() |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 838 | call Run_test_pipe_to_buffer(1, 1, 1) |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 839 | endfunc |
| 840 | |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 841 | func Test_pipe_to_buffer_name_nomsg() |
| 842 | call Run_test_pipe_to_buffer(1, 0, 1) |
| 843 | endfunc |
| 844 | |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 845 | func Test_close_output_buffer() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 846 | let g:test_is_flaky = 1 |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 847 | enew! |
| 848 | let test_lines = ['one', 'two'] |
| 849 | call setline(1, test_lines) |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 850 | let options = {'out_io': 'buffer'} |
| 851 | let options['out_name'] = 'buffer-output' |
| 852 | let options['out_msg'] = 0 |
| 853 | split buffer-output |
| 854 | let job = job_start(s:python . " test_channel_write.py", options) |
| 855 | call assert_equal("run", job_status(job)) |
| 856 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 857 | call WaitForAssert({-> assert_equal(3, line('$'))}) |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 858 | quit! |
| 859 | sleep 100m |
| 860 | " Make sure the write didn't happen to the wrong buffer. |
| 861 | call assert_equal(test_lines, getline(1, line('$'))) |
| 862 | call assert_equal(-1, bufwinnr('buffer-output')) |
| 863 | sbuf buffer-output |
| 864 | call assert_notequal(-1, bufwinnr('buffer-output')) |
| 865 | sleep 100m |
| 866 | close " no more writes |
| 867 | bwipe! |
| 868 | finally |
| 869 | call job_stop(job) |
| 870 | endtry |
| 871 | endfunc |
| 872 | |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 873 | func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 874 | let options = {'err_io': 'buffer'} |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 875 | let expected = ['', 'line one', 'line two', 'this', 'AND this'] |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 876 | if a:use_name |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 877 | let options['err_name'] = 'pipe-err' |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 878 | if a:do_msg |
| 879 | let expected[0] = 'Reading from channel error...' |
| 880 | else |
| 881 | let options['err_msg'] = 0 |
| 882 | call remove(expected, 0) |
| 883 | endif |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 884 | else |
| 885 | sp pipe-err |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 886 | let options['err_buf'] = bufnr('%') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 887 | quit |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 888 | call remove(expected, 0) |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 889 | endif |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 890 | if a:nomod |
| 891 | let options['err_modifiable'] = 0 |
| 892 | endif |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 893 | let job = job_start(s:python . " test_channel_pipe.py", options) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 894 | call assert_equal("run", job_status(job)) |
| 895 | try |
| 896 | let handle = job_getchannel(job) |
| 897 | call ch_sendraw(handle, "echoerr line one\n") |
| 898 | call ch_sendraw(handle, "echoerr line two\n") |
| 899 | call ch_sendraw(handle, "doubleerr this\n") |
| 900 | call ch_sendraw(handle, "quit\n") |
| 901 | sp pipe-err |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 902 | call WaitForAssert({-> assert_equal(expected, getline(1, '$'))}) |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 903 | if a:nomod |
| 904 | call assert_equal(0, &modifiable) |
| 905 | else |
| 906 | call assert_equal(1, &modifiable) |
| 907 | endif |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 908 | bwipe! |
| 909 | finally |
| 910 | call job_stop(job) |
| 911 | endtry |
| 912 | endfunc |
| 913 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 914 | func Test_pipe_err_to_buffer_name() |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 915 | call Run_test_pipe_err_to_buffer(1, 0, 1) |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 916 | endfunc |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 917 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 918 | func Test_pipe_err_to_buffer_nr() |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 919 | call Run_test_pipe_err_to_buffer(0, 0, 1) |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 920 | endfunc |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 921 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 922 | func Test_pipe_err_to_buffer_name_nomod() |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 923 | call Run_test_pipe_err_to_buffer(1, 1, 1) |
| 924 | endfunc |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 925 | |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 926 | func Test_pipe_err_to_buffer_name_nomsg() |
| 927 | call Run_test_pipe_err_to_buffer(1, 0, 0) |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 928 | endfunc |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 929 | |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 930 | func Test_pipe_both_to_buffer() |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 931 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 932 | \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'}) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 933 | call assert_equal("run", job_status(job)) |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 934 | let handle = job_getchannel(job) |
| 935 | call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out')) |
| 936 | call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err')) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 937 | try |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 938 | call ch_sendraw(handle, "echo line one\n") |
| 939 | call ch_sendraw(handle, "echoerr line two\n") |
| 940 | call ch_sendraw(handle, "double this\n") |
| 941 | call ch_sendraw(handle, "doubleerr that\n") |
| 942 | call ch_sendraw(handle, "quit\n") |
| 943 | sp pipe-err |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 944 | call WaitForAssert({-> assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))}) |
Bram Moolenaar | 6ff02c9 | 2016-03-08 20:12:44 +0100 | [diff] [blame] | 945 | bwipe! |
| 946 | finally |
| 947 | call job_stop(job) |
| 948 | endtry |
| 949 | endfunc |
| 950 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 951 | func Run_test_pipe_from_buffer(use_name) |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 952 | sp pipe-input |
| 953 | call setline(1, ['echo one', 'echo two', 'echo three']) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 954 | let options = {'in_io': 'buffer', 'block_write': 1} |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 955 | if a:use_name |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 956 | let options['in_name'] = 'pipe-input' |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 957 | else |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 958 | let options['in_buf'] = bufnr('%') |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 959 | endif |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 960 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 961 | let job = job_start(s:python . " test_channel_pipe.py", options) |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 962 | call assert_equal("run", job_status(job)) |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 963 | if has('unix') && !a:use_name |
| 964 | call assert_equal(bufnr('%'), ch_getbufnr(job, 'in')) |
| 965 | endif |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 966 | try |
| 967 | let handle = job_getchannel(job) |
| 968 | call assert_equal('one', ch_read(handle)) |
| 969 | call assert_equal('two', ch_read(handle)) |
| 970 | call assert_equal('three', ch_read(handle)) |
| 971 | bwipe! |
| 972 | finally |
| 973 | call job_stop(job) |
| 974 | endtry |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 975 | endfunc |
| 976 | |
Bram Moolenaar | 29fd038 | 2016-03-09 23:14:07 +0100 | [diff] [blame] | 977 | func Test_pipe_from_buffer_name() |
| 978 | call Run_test_pipe_from_buffer(1) |
| 979 | endfunc |
| 980 | |
| 981 | func Test_pipe_from_buffer_nr() |
| 982 | call Run_test_pipe_from_buffer(0) |
| 983 | endfunc |
| 984 | |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 985 | func Run_pipe_through_sort(all, use_buffer) |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 986 | CheckExecutable sort |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 987 | let g:test_is_flaky = 1 |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 988 | |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 989 | let options = {'out_io': 'buffer', 'out_name': 'sortout'} |
| 990 | if a:use_buffer |
| 991 | split sortin |
| 992 | call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee']) |
| 993 | let options.in_io = 'buffer' |
| 994 | let options.in_name = 'sortin' |
| 995 | endif |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 996 | if !a:all |
| 997 | let options.in_top = 2 |
| 998 | let options.in_bot = 4 |
| 999 | endif |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1000 | let job = job_start('sort', options) |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1001 | |
| 1002 | if !a:use_buffer |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1003 | call assert_equal("run", job_status(job)) |
| 1004 | call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n") |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1005 | eval job->ch_close_in() |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1006 | endif |
| 1007 | |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1008 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1009 | |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1010 | sp sortout |
Bram Moolenaar | f7f3e32 | 2016-09-03 18:47:24 +0200 | [diff] [blame] | 1011 | call WaitFor('line("$") > 3') |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1012 | call assert_equal('Reading from channel output...', getline(1)) |
| 1013 | if a:all |
| 1014 | call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6)) |
| 1015 | else |
| 1016 | call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4)) |
| 1017 | endif |
| 1018 | |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1019 | call job_stop(job) |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1020 | if a:use_buffer |
| 1021 | bwipe! sortin |
| 1022 | endif |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1023 | bwipe! sortout |
| 1024 | endfunc |
| 1025 | |
| 1026 | func Test_pipe_through_sort_all() |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1027 | call Run_pipe_through_sort(1, 1) |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1028 | endfunc |
| 1029 | |
| 1030 | func Test_pipe_through_sort_some() |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1031 | call Run_pipe_through_sort(0, 1) |
| 1032 | endfunc |
| 1033 | |
| 1034 | func Test_pipe_through_sort_feed() |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1035 | call Run_pipe_through_sort(1, 0) |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1036 | endfunc |
| 1037 | |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1038 | func Test_pipe_to_nameless_buffer() |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1039 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1040 | \ {'out_io': 'buffer'}) |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1041 | call assert_equal("run", job_status(job)) |
| 1042 | try |
| 1043 | let handle = job_getchannel(job) |
| 1044 | call ch_sendraw(handle, "echo line one\n") |
| 1045 | call ch_sendraw(handle, "echo line two\n") |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1046 | exe handle->ch_getbufnr("out") .. 'sbuf' |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1047 | call WaitFor('line("$") >= 3') |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1048 | call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$')) |
| 1049 | bwipe! |
| 1050 | finally |
| 1051 | call job_stop(job) |
| 1052 | endtry |
| 1053 | endfunc |
| 1054 | |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 1055 | func Test_pipe_to_buffer_json() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 1056 | CheckFunction reltimefloat |
| 1057 | |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 1058 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1059 | \ {'out_io': 'buffer', 'out_mode': 'json'}) |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 1060 | call assert_equal("run", job_status(job)) |
| 1061 | try |
| 1062 | let handle = job_getchannel(job) |
| 1063 | call ch_sendraw(handle, "echo [0, \"hello\"]\n") |
| 1064 | call ch_sendraw(handle, "echo [-2, 12.34]\n") |
| 1065 | exe ch_getbufnr(handle, "out") . 'sbuf' |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1066 | call WaitFor('line("$") >= 3') |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 1067 | call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$')) |
| 1068 | bwipe! |
| 1069 | finally |
| 1070 | call job_stop(job) |
| 1071 | endtry |
| 1072 | endfunc |
| 1073 | |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1074 | " 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] | 1075 | func s:wait_for_last_line(line, offset) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1076 | for i in range(100) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1077 | if getline(line('$') - a:offset) == a:line |
| 1078 | break |
| 1079 | endif |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1080 | sleep 10m |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1081 | endfor |
| 1082 | endfunc |
| 1083 | |
| 1084 | func Test_pipe_io_two_buffers() |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1085 | " Create two buffers, one to read from and one to write to. |
| 1086 | split pipe-output |
| 1087 | set buftype=nofile |
| 1088 | split pipe-input |
| 1089 | set buftype=nofile |
| 1090 | |
| 1091 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1092 | \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0, |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1093 | \ 'out_io': 'buffer', 'out_name': 'pipe-output', |
| 1094 | \ 'block_write': 1}) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1095 | call assert_equal("run", job_status(job)) |
| 1096 | try |
| 1097 | exe "normal Gaecho hello\<CR>" |
| 1098 | exe bufwinnr('pipe-output') . "wincmd w" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1099 | call s:wait_for_last_line('hello', 0) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1100 | call assert_equal('hello', getline('$')) |
| 1101 | |
| 1102 | exe bufwinnr('pipe-input') . "wincmd w" |
| 1103 | exe "normal Gadouble this\<CR>" |
| 1104 | exe bufwinnr('pipe-output') . "wincmd w" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1105 | call s:wait_for_last_line('AND this', 0) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1106 | call assert_equal('this', getline(line('$') - 1)) |
| 1107 | call assert_equal('AND this', getline('$')) |
| 1108 | |
| 1109 | bwipe! |
| 1110 | exe bufwinnr('pipe-input') . "wincmd w" |
| 1111 | bwipe! |
| 1112 | finally |
| 1113 | call job_stop(job) |
| 1114 | endtry |
| 1115 | endfunc |
| 1116 | |
| 1117 | func Test_pipe_io_one_buffer() |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1118 | " Create one buffer to read from and to write to. |
| 1119 | split pipe-io |
| 1120 | set buftype=nofile |
| 1121 | |
| 1122 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1123 | \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0, |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1124 | \ 'out_io': 'buffer', 'out_name': 'pipe-io', |
| 1125 | \ 'block_write': 1}) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1126 | call assert_equal("run", job_status(job)) |
| 1127 | try |
| 1128 | exe "normal Goecho hello\<CR>" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1129 | call s:wait_for_last_line('hello', 1) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1130 | call assert_equal('hello', getline(line('$') - 1)) |
| 1131 | |
| 1132 | exe "normal Gadouble this\<CR>" |
Bram Moolenaar | 9fe885e | 2016-03-08 16:06:55 +0100 | [diff] [blame] | 1133 | call s:wait_for_last_line('AND this', 1) |
Bram Moolenaar | 3f39f64 | 2016-03-06 21:35:57 +0100 | [diff] [blame] | 1134 | call assert_equal('this', getline(line('$') - 2)) |
| 1135 | call assert_equal('AND this', getline(line('$') - 1)) |
| 1136 | |
| 1137 | bwipe! |
| 1138 | finally |
| 1139 | call job_stop(job) |
| 1140 | endtry |
| 1141 | endfunc |
| 1142 | |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 1143 | func Test_write_to_buffer_and_scroll() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1144 | CheckScreendump |
| 1145 | |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 1146 | let lines =<< trim END |
| 1147 | new Xscrollbuffer |
| 1148 | call setline(1, range(1, 200)) |
| 1149 | $ |
| 1150 | redraw |
| 1151 | wincmd w |
| 1152 | call deletebufline('Xscrollbuffer', 1, '$') |
| 1153 | if has('win32') |
| 1154 | let cmd = ['cmd', '/c', 'echo sometext'] |
| 1155 | else |
| 1156 | let cmd = [&shell, &shellcmdflag, 'echo sometext'] |
| 1157 | endif |
| 1158 | call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'}) |
| 1159 | END |
Bram Moolenaar | 45bbaef | 2022-09-08 16:39:22 +0100 | [diff] [blame] | 1160 | call writefile(lines, 'XtestBufferScroll', 'D') |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 1161 | let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10}) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1162 | call TermWait(buf, 50) |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 1163 | call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {}) |
| 1164 | |
| 1165 | " clean up |
| 1166 | call StopVimInTerminal(buf) |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 1167 | endfunc |
| 1168 | |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1169 | func Test_pipe_null() |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1170 | " We cannot check that no I/O works, we only check that the job starts |
| 1171 | " properly. |
| 1172 | let job = job_start(s:python . " test_channel_pipe.py something", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1173 | \ {'in_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1174 | call assert_equal("run", job_status(job)) |
| 1175 | try |
| 1176 | call assert_equal('something', ch_read(job)) |
| 1177 | finally |
| 1178 | call job_stop(job) |
| 1179 | endtry |
| 1180 | |
| 1181 | let job = job_start(s:python . " test_channel_pipe.py err-out", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1182 | \ {'out_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1183 | call assert_equal("run", job_status(job)) |
| 1184 | try |
| 1185 | call assert_equal('err-out', ch_read(job, {"part": "err"})) |
| 1186 | finally |
| 1187 | call job_stop(job) |
| 1188 | endtry |
| 1189 | |
| 1190 | let job = job_start(s:python . " test_channel_pipe.py something", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1191 | \ {'err_io': 'null'}) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1192 | call assert_equal("run", job_status(job)) |
| 1193 | try |
| 1194 | call assert_equal('something', ch_read(job)) |
| 1195 | finally |
| 1196 | call job_stop(job) |
| 1197 | endtry |
| 1198 | |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1199 | " This causes spurious leak errors with valgrind. |
| 1200 | if !RunningWithValgrind() |
| 1201 | let job = job_start(s:python . " test_channel_pipe.py something", |
| 1202 | \ {'out_io': 'null', 'err_io': 'out'}) |
| 1203 | call assert_equal("run", job_status(job)) |
| 1204 | call job_stop(job) |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1205 | |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1206 | let job = job_start(s:python . " test_channel_pipe.py something", |
| 1207 | \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'}) |
| 1208 | call assert_equal("run", job_status(job)) |
| 1209 | call assert_equal('channel fail', string(job_getchannel(job))) |
| 1210 | call assert_equal('fail', ch_status(job)) |
| 1211 | call assert_equal('no process', string(test_null_job())) |
| 1212 | call assert_equal('channel fail', string(test_null_channel())) |
| 1213 | call job_stop(job) |
| 1214 | endif |
Bram Moolenaar | f65333c | 2016-03-08 18:27:21 +0100 | [diff] [blame] | 1215 | endfunc |
| 1216 | |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1217 | func Test_pipe_to_buffer_raw() |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1218 | let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'} |
| 1219 | split testout |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 1220 | let job = job_start([s:python, '-c', |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1221 | \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options) |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1222 | " the job may be done quickly, also accept "dead" |
| 1223 | call assert_match('^\%(dead\|run\)$', job_status(job)) |
Bram Moolenaar | 769e9d2 | 2018-04-11 20:53:49 +0200 | [diff] [blame] | 1224 | call WaitFor('len(join(getline(1, "$"), "")) >= 10000') |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1225 | try |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1226 | let totlen = 0 |
| 1227 | for line in getline(1, '$') |
| 1228 | call assert_equal('', substitute(line, '^\.*', '', '')) |
| 1229 | let totlen += len(line) |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1230 | endfor |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1231 | call assert_equal(10000, totlen) |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1232 | finally |
| 1233 | call job_stop(job) |
| 1234 | bwipe! |
| 1235 | endtry |
| 1236 | endfunc |
| 1237 | |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1238 | func Test_reuse_channel() |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1239 | let job = job_start(s:python . " test_channel_pipe.py") |
| 1240 | call assert_equal("run", job_status(job)) |
| 1241 | let handle = job_getchannel(job) |
| 1242 | try |
| 1243 | call ch_sendraw(handle, "echo something\n") |
| 1244 | call assert_equal("something", ch_readraw(handle)) |
| 1245 | finally |
| 1246 | call job_stop(job) |
| 1247 | endtry |
| 1248 | |
| 1249 | let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle}) |
| 1250 | call assert_equal("run", job_status(job)) |
| 1251 | let handle = job_getchannel(job) |
| 1252 | try |
| 1253 | call ch_sendraw(handle, "echo again\n") |
| 1254 | call assert_equal("again", ch_readraw(handle)) |
| 1255 | finally |
| 1256 | call job_stop(job) |
| 1257 | endtry |
| 1258 | endfunc |
| 1259 | |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1260 | func Test_out_cb() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1261 | let g:test_is_flaky = 1 |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1262 | let dict = {'thisis': 'dict: '} |
| 1263 | func dict.outHandler(chan, msg) dict |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 1264 | if type(a:msg) == v:t_string |
| 1265 | let g:Ch_outmsg = self.thisis . a:msg |
| 1266 | else |
| 1267 | let g:Ch_outobj = a:msg |
| 1268 | endif |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1269 | endfunc |
| 1270 | func dict.errHandler(chan, msg) dict |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1271 | let g:Ch_errmsg = self.thisis . a:msg |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1272 | endfunc |
| 1273 | let job = job_start(s:python . " test_channel_pipe.py", |
| 1274 | \ {'out_cb': dict.outHandler, |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1275 | \ 'out_mode': 'json', |
| 1276 | \ 'err_cb': dict.errHandler, |
| 1277 | \ 'err_mode': 'json'}) |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1278 | call assert_equal("run", job_status(job)) |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 1279 | call test_garbagecollect_now() |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1280 | try |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1281 | let g:Ch_outmsg = '' |
| 1282 | let g:Ch_errmsg = '' |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1283 | call ch_sendraw(job, "echo [0, \"hello\"]\n") |
| 1284 | call ch_sendraw(job, "echoerr [0, \"there\"]\n") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1285 | call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)}) |
| 1286 | call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)}) |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 1287 | |
| 1288 | " Receive a json object split in pieces |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1289 | let g:Ch_outobj = '' |
Bram Moolenaar | 9ae3bbd | 2020-02-19 14:31:33 +0100 | [diff] [blame] | 1290 | call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n") |
Bram Moolenaar | bf54dbe | 2020-03-29 16:18:58 +0200 | [diff] [blame] | 1291 | " For unknown reasons this can be very slow on Mac. |
Bram Moolenaar | deda644 | 2021-12-17 11:44:33 +0000 | [diff] [blame] | 1292 | " Increase the timeout on every run. |
| 1293 | if g:run_nr == 1 |
| 1294 | let timeout = 5000 |
| 1295 | elseif g:run_nr == 2 |
| 1296 | let timeout = 10000 |
| 1297 | elseif g:run_nr == 3 |
Bram Moolenaar | bf54dbe | 2020-03-29 16:18:58 +0200 | [diff] [blame] | 1298 | let timeout = 20000 |
| 1299 | else |
Bram Moolenaar | deda644 | 2021-12-17 11:44:33 +0000 | [diff] [blame] | 1300 | let timeout = 40000 |
Bram Moolenaar | bf54dbe | 2020-03-29 16:18:58 +0200 | [diff] [blame] | 1301 | endif |
| 1302 | call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout) |
Bram Moolenaar | 75f7265 | 2016-03-20 22:16:56 +0100 | [diff] [blame] | 1303 | finally |
| 1304 | call job_stop(job) |
| 1305 | endtry |
| 1306 | endfunc |
| 1307 | |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1308 | func Test_out_close_cb() |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1309 | let s:counter = 1 |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1310 | let g:Ch_msg1 = '' |
| 1311 | let g:Ch_closemsg = 0 |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1312 | func! OutHandler(chan, msg) |
Bram Moolenaar | d75263c | 2016-04-30 16:07:23 +0200 | [diff] [blame] | 1313 | if s:counter == 1 |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1314 | let g:Ch_msg1 = a:msg |
Bram Moolenaar | d75263c | 2016-04-30 16:07:23 +0200 | [diff] [blame] | 1315 | endif |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1316 | let s:counter += 1 |
| 1317 | endfunc |
| 1318 | func! CloseHandler(chan) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1319 | let g:Ch_closemsg = s:counter |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1320 | let s:counter += 1 |
| 1321 | endfunc |
| 1322 | let job = job_start(s:python . " test_channel_pipe.py quit now", |
| 1323 | \ {'out_cb': 'OutHandler', |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1324 | \ 'close_cb': 'CloseHandler'}) |
| 1325 | " the job may be done quickly, also accept "dead" |
| 1326 | call assert_match('^\%(dead\|run\)$', job_status(job)) |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1327 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1328 | call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)}) |
| 1329 | call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)}) |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 1330 | finally |
| 1331 | call job_stop(job) |
| 1332 | delfunc OutHandler |
| 1333 | delfunc CloseHandler |
| 1334 | endtry |
| 1335 | endfunc |
| 1336 | |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 1337 | func Test_read_in_close_cb() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1338 | let g:Ch_received = '' |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 1339 | func! CloseHandler(chan) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1340 | let g:Ch_received = ch_read(a:chan) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 1341 | endfunc |
| 1342 | let job = job_start(s:python . " test_channel_pipe.py quit now", |
| 1343 | \ {'close_cb': 'CloseHandler'}) |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1344 | " the job may be done quickly, also accept "dead" |
| 1345 | call assert_match('^\%(dead\|run\)$', job_status(job)) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 1346 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1347 | call WaitForAssert({-> assert_equal('quit', g:Ch_received)}) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 1348 | finally |
| 1349 | call job_stop(job) |
| 1350 | delfunc CloseHandler |
| 1351 | endtry |
| 1352 | endfunc |
| 1353 | |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 1354 | " Use channel in NL mode but received text does not end in NL. |
| 1355 | func Test_read_in_close_cb_incomplete() |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 1356 | let g:Ch_received = '' |
| 1357 | func! CloseHandler(chan) |
| 1358 | while ch_status(a:chan, {'part': 'out'}) == 'buffered' |
| 1359 | let g:Ch_received .= ch_read(a:chan) |
| 1360 | endwhile |
| 1361 | endfunc |
| 1362 | let job = job_start(s:python . " test_channel_pipe.py incomplete", |
| 1363 | \ {'close_cb': 'CloseHandler'}) |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1364 | " the job may be done quickly, also accept "dead" |
| 1365 | call assert_match('^\%(dead\|run\)$', job_status(job)) |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 1366 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1367 | call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)}) |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 1368 | finally |
| 1369 | call job_stop(job) |
| 1370 | delfunc CloseHandler |
| 1371 | endtry |
| 1372 | endfunc |
| 1373 | |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 1374 | func Test_out_cb_lambda() |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 1375 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1376 | \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")}, |
| 1377 | \ 'out_mode': 'json', |
| 1378 | \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")}, |
| 1379 | \ 'err_mode': 'json'}) |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 1380 | call assert_equal("run", job_status(job)) |
| 1381 | try |
| 1382 | let g:Ch_outmsg = '' |
| 1383 | let g:Ch_errmsg = '' |
| 1384 | call ch_sendraw(job, "echo [0, \"hello\"]\n") |
| 1385 | call ch_sendraw(job, "echoerr [0, \"there\"]\n") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1386 | call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)}) |
| 1387 | call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)}) |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 1388 | finally |
| 1389 | call job_stop(job) |
| 1390 | endtry |
| 1391 | endfunc |
| 1392 | |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 1393 | func Test_close_and_exit_cb() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1394 | let g:test_is_flaky = 1 |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1395 | let g:retdict = {'ret': {}} |
| 1396 | func g:retdict.close_cb(ch) dict |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1397 | let self.ret['close_cb'] = a:ch->ch_getjob()->job_status() |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 1398 | endfunc |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1399 | func g:retdict.exit_cb(job, status) dict |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 1400 | let self.ret['exit_cb'] = job_status(a:job) |
| 1401 | endfunc |
| 1402 | |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1403 | let job = job_start([&shell, &shellcmdflag, 'echo'], |
| 1404 | \ {'close_cb': g:retdict.close_cb, |
| 1405 | \ 'exit_cb': g:retdict.exit_cb}) |
| 1406 | " the job may be done quickly, also accept "dead" |
| 1407 | call assert_match('^\%(dead\|run\)$', job_status(job)) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1408 | call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))}) |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1409 | call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb']) |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1410 | call assert_equal('dead', g:retdict.ret['exit_cb']) |
| 1411 | unlet g:retdict |
Bram Moolenaar | 7df915d | 2016-11-17 17:25:32 +0100 | [diff] [blame] | 1412 | endfunc |
| 1413 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1414 | """""""""" |
| 1415 | |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 1416 | function ExitCbWipe(job, status) |
| 1417 | exe g:wipe_buf 'bw!' |
| 1418 | endfunction |
| 1419 | |
| 1420 | " This caused a crash, because messages were handled while peeking for a |
| 1421 | " character. |
| 1422 | func Test_exit_cb_wipes_buf() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1423 | CheckFeature timers |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 1424 | set cursorline lazyredraw |
| 1425 | call test_override('redraw_flag', 1) |
| 1426 | new |
| 1427 | let g:wipe_buf = bufnr('') |
| 1428 | |
Milly | 4f5681d | 2024-10-20 11:06:00 +0200 | [diff] [blame] | 1429 | let job = job_start(has('win32') ? 'cmd /D /c echo:' : ['true'], |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 1430 | \ {'exit_cb': 'ExitCbWipe'}) |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 1431 | let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5}) |
| 1432 | call feedkeys(repeat('g', 1000) . 'o', 'ntx!') |
| 1433 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
| 1434 | call timer_stop(timer) |
| 1435 | |
| 1436 | set nocursorline nolazyredraw |
| 1437 | unlet g:wipe_buf |
| 1438 | call test_override('ALL', 0) |
| 1439 | endfunc |
| 1440 | |
| 1441 | """""""""" |
| 1442 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1443 | let g:Ch_unletResponse = '' |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1444 | func s:UnletHandler(handle, msg) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1445 | let g:Ch_unletResponse = a:msg |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1446 | unlet s:channelfd |
| 1447 | endfunc |
| 1448 | |
| 1449 | " Test that "unlet handle" in a handler doesn't crash Vim. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1450 | func Ch_unlet_handle(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1451 | let s:channelfd = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1452 | eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1453 | call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)}) |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1454 | endfunc |
| 1455 | |
| 1456 | func Test_unlet_handle() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1457 | call s:run_server('Ch_unlet_handle') |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1458 | endfunc |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1459 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1460 | func Test_unlet_handle_ipv6() |
| 1461 | CheckIPv6 |
| 1462 | call Test_unlet_handle() |
| 1463 | endfunc |
| 1464 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1465 | """""""""" |
| 1466 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1467 | let g:Ch_unletResponse = '' |
| 1468 | func Ch_CloseHandler(handle, msg) |
| 1469 | let g:Ch_unletResponse = a:msg |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1470 | eval s:channelfd->ch_close() |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1471 | endfunc |
| 1472 | |
| 1473 | " Test that "unlet handle" in a handler doesn't crash Vim. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1474 | func Ch_close_handle(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1475 | let s:channelfd = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1476 | call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1477 | call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)}) |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1478 | endfunc |
| 1479 | |
| 1480 | func Test_close_handle() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1481 | call s:run_server('Ch_close_handle') |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1482 | endfunc |
| 1483 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1484 | func Test_close_handle_ipv6() |
| 1485 | CheckIPv6 |
| 1486 | call Test_close_handle() |
| 1487 | endfunc |
| 1488 | |
| 1489 | """""""""" |
| 1490 | |
| 1491 | func Ch_open_ipv6(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1492 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1493 | call assert_notequal('fail', ch_status(handle)) |
| 1494 | endfunc |
| 1495 | |
| 1496 | func Test_open_ipv6() |
| 1497 | CheckIPv6 |
| 1498 | call s:run_server('Ch_open_ipv6') |
| 1499 | endfunc |
| 1500 | |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1501 | """""""""" |
| 1502 | |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1503 | func Test_open_fail() |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1504 | call assert_fails("let ch = ch_open('noserver')", 'E475:') |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1505 | echo ch |
| 1506 | let d = ch |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1507 | call assert_fails("let ch = ch_open('noserver', 10)", 'E1206:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1508 | call assert_fails("let ch = ch_open('localhost:-1')", 'E475:') |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1509 | call assert_fails("let ch = ch_open('localhost:65537')", 'E475:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1510 | call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})", |
| 1511 | \ 'E474:') |
| 1512 | call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})", |
| 1513 | \ 'E475:') |
| 1514 | call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})", |
| 1515 | \ 'E475:') |
| 1516 | call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})", |
| 1517 | \ 'E475:') |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1518 | call assert_fails("let ch = ch_open('[::]')", 'E475:') |
| 1519 | call assert_fails("let ch = ch_open('[::.80')", 'E475:') |
| 1520 | call assert_fails("let ch = ch_open('[::]8080')", 'E475:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1521 | endfunc |
| 1522 | |
| 1523 | func Test_ch_info_fail() |
| 1524 | call assert_fails("let x = ch_info(10)", 'E475:') |
Bram Moolenaar | 5cefd40 | 2016-02-16 12:44:26 +0100 | [diff] [blame] | 1525 | endfunc |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1526 | |
| 1527 | """""""""" |
| 1528 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1529 | func Ch_open_delay(port) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1530 | " Wait up to a second for the port to open. |
| 1531 | let s:chopt.waittime = 1000 |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1532 | let channel = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1533 | if ch_status(channel) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 1534 | call assert_report("Can't open channel") |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1535 | return |
| 1536 | endif |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1537 | call assert_equal('got it', channel->ch_evalexpr('hello!')) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1538 | call ch_close(channel) |
| 1539 | endfunc |
| 1540 | |
| 1541 | func Test_open_delay() |
Christian Brabandt | 85ff0c1 | 2023-10-04 21:58:24 +0200 | [diff] [blame] | 1542 | " This fails on BSD (e.g. Cirrus-CI), why? |
| 1543 | CheckNotBSD |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1544 | " The server will wait half a second before creating the port. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1545 | call s:run_server('Ch_open_delay', 'delay') |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1546 | endfunc |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1547 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1548 | func Test_open_delay_ipv6() |
| 1549 | CheckIPv6 |
Christian Brabandt | 85ff0c1 | 2023-10-04 21:58:24 +0200 | [diff] [blame] | 1550 | " This fails on BSD (e.g. Cirrus-CI), why? |
| 1551 | CheckNotBSD |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1552 | call Test_open_delay() |
| 1553 | endfunc |
| 1554 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1555 | """"""""" |
| 1556 | |
| 1557 | function MyFunction(a,b,c) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1558 | let g:Ch_call_ret = [a:a, a:b, a:c] |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1559 | endfunc |
| 1560 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1561 | function Ch_test_call(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1562 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1563 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 1564 | call assert_report("Can't open channel") |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1565 | return |
| 1566 | endif |
| 1567 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1568 | let g:Ch_call_ret = [] |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 1569 | call assert_equal('ok', ch_evalexpr(handle, 'call-func')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1570 | call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)}) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1571 | |
| 1572 | call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:') |
| 1573 | call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:') |
| 1574 | call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:') |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1575 | endfunc |
| 1576 | |
| 1577 | func Test_call() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1578 | call s:run_server('Ch_test_call') |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1579 | endfunc |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1580 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1581 | func Test_call_ipv6() |
| 1582 | CheckIPv6 |
| 1583 | call Test_call() |
| 1584 | endfunc |
| 1585 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1586 | func Test_call_unix() |
| 1587 | CheckUnix |
| 1588 | call Test_call() |
| 1589 | call delete('Xtestsocket') |
| 1590 | endfunc |
| 1591 | |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1592 | """"""""" |
| 1593 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1594 | let g:Ch_job_exit_ret = 'not yet' |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1595 | function MyExitCb(job, status) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1596 | let g:Ch_job_exit_ret = 'done' |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1597 | endfunc |
| 1598 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1599 | function Ch_test_exit_callback(port) |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1600 | eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'}) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1601 | let g:Ch_exit_job = g:currentJob |
| 1602 | call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb']) |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1603 | endfunc |
| 1604 | |
| 1605 | func Test_exit_callback() |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 1606 | call s:run_server('Ch_test_exit_callback') |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1607 | |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 1608 | " wait up to a second for the job to exit |
| 1609 | for i in range(100) |
| 1610 | if g:Ch_job_exit_ret == 'done' |
| 1611 | break |
| 1612 | endif |
| 1613 | sleep 10m |
| 1614 | " calling job_status() triggers the callback |
| 1615 | call job_status(g:Ch_exit_job) |
| 1616 | endfor |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1617 | |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 1618 | call assert_equal('done', g:Ch_job_exit_ret) |
| 1619 | call assert_equal('dead', job_info(g:Ch_exit_job).status) |
| 1620 | unlet g:Ch_exit_job |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1621 | endfunc |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1622 | |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 1623 | function MyExitTimeCb(job, status) |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 1624 | if job_info(a:job).process == g:exit_cb_val.process |
| 1625 | let g:exit_cb_val.end = reltime(g:exit_cb_val.start) |
| 1626 | endif |
| 1627 | call Resume() |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 1628 | endfunction |
| 1629 | |
| 1630 | func Test_exit_callback_interval() |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 1631 | CheckFunction reltimefloat |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1632 | let g:test_is_flaky = 1 |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 1633 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 1634 | let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0} |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1635 | let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'}) |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 1636 | let g:exit_cb_val.process = job_info(job).process |
K.Takata | cd9fa25 | 2022-09-20 11:04:47 +0100 | [diff] [blame] | 1637 | try |
| 1638 | call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0') |
| 1639 | catch |
| 1640 | call add(v:errors, "Job status: " .. string(job->job_info())) |
| 1641 | throw v:exception |
| 1642 | endtry |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 1643 | let elapsed = reltimefloat(g:exit_cb_val.end) |
zeertzjq | cdc6a43 | 2022-06-19 11:45:46 +0100 | [diff] [blame] | 1644 | call assert_inrange(0.5, 1.0, elapsed) |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 1645 | |
| 1646 | " case: unreferenced job, using timer |
| 1647 | if !has('timers') |
| 1648 | return |
| 1649 | endif |
| 1650 | |
| 1651 | let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0} |
| 1652 | let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'}) |
| 1653 | let g:exit_cb_val.process = job_info(g:job).process |
| 1654 | unlet g:job |
| 1655 | call Standby(1000) |
| 1656 | if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0 |
| 1657 | let elapsed = reltimefloat(g:exit_cb_val.end) |
| 1658 | else |
| 1659 | let elapsed = 1.0 |
| 1660 | endif |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1661 | call assert_inrange(0.5, 1.0, elapsed) |
Bram Moolenaar | 97792de | 2016-10-15 18:36:49 +0200 | [diff] [blame] | 1662 | endfunc |
| 1663 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1664 | """"""""" |
| 1665 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1666 | let g:Ch_close_ret = 'alive' |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1667 | function MyCloseCb(ch) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1668 | let g:Ch_close_ret = 'closed' |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1669 | endfunc |
| 1670 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1671 | function Ch_test_close_callback(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1672 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1673 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 1674 | call assert_report("Can't open channel") |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1675 | return |
| 1676 | endif |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1677 | call ch_setoptions(handle, {'close_cb': 'MyCloseCb'}) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1678 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 1679 | call assert_equal('', ch_evalexpr(handle, 'close me')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1680 | call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)}) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1681 | endfunc |
| 1682 | |
| 1683 | func Test_close_callback() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1684 | call s:run_server('Ch_test_close_callback') |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1685 | endfunc |
| 1686 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1687 | func Test_close_callback_ipv6() |
| 1688 | CheckIPv6 |
| 1689 | call Test_close_callback() |
| 1690 | endfunc |
| 1691 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1692 | func Test_close_callback_unix() |
| 1693 | CheckUnix |
| 1694 | call Test_close_callback() |
| 1695 | call delete('Xtestsocket') |
| 1696 | endfunc |
| 1697 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1698 | function Ch_test_close_partial(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1699 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | bdf0bda | 2016-03-30 21:06:57 +0200 | [diff] [blame] | 1700 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 1701 | call assert_report("Can't open channel") |
Bram Moolenaar | bdf0bda | 2016-03-30 21:06:57 +0200 | [diff] [blame] | 1702 | return |
| 1703 | endif |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1704 | let g:Ch_d = {} |
| 1705 | func g:Ch_d.closeCb(ch) dict |
Bram Moolenaar | bdf0bda | 2016-03-30 21:06:57 +0200 | [diff] [blame] | 1706 | let self.close_ret = 'closed' |
| 1707 | endfunc |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1708 | call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb}) |
Bram Moolenaar | bdf0bda | 2016-03-30 21:06:57 +0200 | [diff] [blame] | 1709 | |
| 1710 | call assert_equal('', ch_evalexpr(handle, 'close me')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1711 | call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)}) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1712 | unlet g:Ch_d |
Bram Moolenaar | bdf0bda | 2016-03-30 21:06:57 +0200 | [diff] [blame] | 1713 | endfunc |
| 1714 | |
| 1715 | func Test_close_partial() |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1716 | call s:run_server('Ch_test_close_partial') |
Bram Moolenaar | bdf0bda | 2016-03-30 21:06:57 +0200 | [diff] [blame] | 1717 | endfunc |
| 1718 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1719 | func Test_close_partial_ipv6() |
| 1720 | CheckIPv6 |
| 1721 | call Test_close_partial() |
| 1722 | endfunc |
| 1723 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1724 | func Test_close_partial_unix() |
| 1725 | CheckUnix |
| 1726 | call Test_close_partial() |
| 1727 | call delete('Xtestsocket') |
| 1728 | endfunc |
| 1729 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1730 | func Test_job_start_fails() |
| 1731 | " this was leaking memory |
| 1732 | call assert_fails("call job_start([''])", "E474:") |
Bram Moolenaar | 8038568 | 2016-03-27 19:13:35 +0200 | [diff] [blame] | 1733 | call assert_fails('call job_start($x)', 'E474:') |
| 1734 | call assert_fails('call job_start("")', 'E474:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1735 | call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:') |
| 1736 | call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:') |
| 1737 | call assert_fails('call job_start("ls", [])', 'E715:') |
| 1738 | call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:') |
| 1739 | call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:') |
| 1740 | call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1741 | call assert_fails("call job_start('ls', {'callback' : -1})", 'E921:') |
| 1742 | call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E921:') |
| 1743 | call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E921:') |
| 1744 | call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E921:') |
| 1745 | call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E921:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1746 | call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:') |
| 1747 | call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:') |
| 1748 | call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1749 | call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1750 | call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:') |
| 1751 | call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:') |
| 1752 | call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:') |
| 1753 | call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:') |
| 1754 | call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:') |
| 1755 | call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:') |
| 1756 | call assert_fails("call job_start('ls', |
| 1757 | \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:') |
| 1758 | call assert_fails("call job_start('ls', |
| 1759 | \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:') |
| 1760 | call assert_fails("call job_start('ls', |
| 1761 | \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:') |
| 1762 | |
| 1763 | call assert_fails("call job_start('ls', |
| 1764 | \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:') |
| 1765 | call assert_fails("call job_start('ls', |
| 1766 | \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:') |
| 1767 | call assert_fails("call job_start('ls', |
| 1768 | \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:') |
| 1769 | |
Milly | 4f5681d | 2024-10-20 11:06:00 +0200 | [diff] [blame] | 1770 | let cmd = has('win32') ? "cmd /D /c dir" : "ls" |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1771 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1772 | set nomodifiable |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1773 | call assert_fails("call job_start(cmd, |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1774 | \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:') |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1775 | call assert_fails("call job_start(cmd, |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1776 | \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:') |
| 1777 | set modifiable |
| 1778 | |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1779 | call assert_fails("call job_start(cmd, {'in_io' : 'buffer'})", 'E915:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1780 | |
| 1781 | edit! XXX |
| 1782 | let bnum = bufnr() |
| 1783 | enew |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1784 | call assert_fails("call job_start(cmd, |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1785 | \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:') |
| 1786 | |
| 1787 | " Empty job tests |
| 1788 | " This was crashing on MS-Windows. |
| 1789 | call assert_fails('let job = job_start([""])', 'E474:') |
| 1790 | call assert_fails('let job = job_start([" "])', 'E474:') |
| 1791 | call assert_fails('let job = job_start("")', 'E474:') |
| 1792 | call assert_fails('let job = job_start(" ")', 'E474:') |
Bram Moolenaar | 0015795 | 2020-04-13 17:44:47 +0200 | [diff] [blame] | 1793 | call assert_fails('let job = job_start(["ls", []])', 'E730:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 1794 | call assert_fails('call job_setoptions(test_null_job(), {})', 'E916:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 1795 | %bw! |
Bram Moolenaar | 8038568 | 2016-03-27 19:13:35 +0200 | [diff] [blame] | 1796 | endfunc |
| 1797 | |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1798 | func Test_job_stop_immediately() |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1799 | " With valgrind this causes spurious leak reports |
Bram Moolenaar | cf801d4 | 2022-06-21 18:34:42 +0100 | [diff] [blame] | 1800 | CheckNotValgrind |
Bram Moolenaar | 37bb3b1 | 2022-06-21 17:40:47 +0100 | [diff] [blame] | 1801 | |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1802 | let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)']) |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1803 | try |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1804 | eval g:job->job_stop() |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1805 | call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1806 | finally |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 1807 | call job_stop(g:job, 'kill') |
| 1808 | unlet g:job |
Bram Moolenaar | bb09ceb | 2016-10-18 16:27:23 +0200 | [diff] [blame] | 1809 | endtry |
| 1810 | endfunc |
| 1811 | |
Bram Moolenaar | 271906b | 2021-08-28 12:30:12 +0200 | [diff] [blame] | 1812 | func Test_null_job_eval() |
| 1813 | call assert_fails('eval test_null_job()->eval()', 'E121:') |
| 1814 | endfunc |
| 1815 | |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1816 | " This was leaking memory. |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 1817 | func Test_partial_in_channel_cycle() |
| 1818 | let d = {} |
| 1819 | let d.a = function('string', [d]) |
| 1820 | try |
| 1821 | let d.b = ch_open('nowhere:123', {'close_cb': d.a}) |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 1822 | call test_garbagecollect_now() |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 1823 | catch |
| 1824 | call assert_exception('E901:') |
| 1825 | endtry |
| 1826 | unlet d |
| 1827 | endfunc |
| 1828 | |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1829 | func Test_using_freed_memory() |
| 1830 | let g:a = job_start(['ls']) |
| 1831 | sleep 10m |
Bram Moolenaar | 574860b | 2016-05-24 17:33:34 +0200 | [diff] [blame] | 1832 | call test_garbagecollect_now() |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1833 | endfunc |
| 1834 | |
Bram Moolenaar | b8aefa4 | 2016-06-10 23:02:56 +0200 | [diff] [blame] | 1835 | func Test_collapse_buffers() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1836 | let g:test_is_flaky = 1 |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1837 | CheckExecutable cat |
| 1838 | |
Bram Moolenaar | b8aefa4 | 2016-06-10 23:02:56 +0200 | [diff] [blame] | 1839 | sp test_channel.vim |
| 1840 | let g:linecount = line('$') |
| 1841 | close |
| 1842 | split testout |
| 1843 | 1,$delete |
| 1844 | call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1845 | call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))}) |
Bram Moolenaar | b8aefa4 | 2016-06-10 23:02:56 +0200 | [diff] [blame] | 1846 | bwipe! |
| 1847 | endfunc |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1848 | |
Bram Moolenaar | 8b62d87 | 2019-01-05 00:02:57 +0100 | [diff] [blame] | 1849 | func Test_write_to_deleted_buffer() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1850 | CheckExecutable echo |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 1851 | CheckFeature quickfix |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1852 | |
Bram Moolenaar | 8b62d87 | 2019-01-05 00:02:57 +0100 | [diff] [blame] | 1853 | let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0}) |
Bram Moolenaar | 8b62d87 | 2019-01-05 00:02:57 +0100 | [diff] [blame] | 1854 | let bufnr = bufnr('test_buffer') |
Bram Moolenaar | f780b8a | 2019-01-05 00:35:22 +0100 | [diff] [blame] | 1855 | call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))}) |
Bram Moolenaar | 8b62d87 | 2019-01-05 00:02:57 +0100 | [diff] [blame] | 1856 | call assert_equal('nofile', getbufvar(bufnr, '&buftype')) |
| 1857 | call assert_equal('hide', getbufvar(bufnr, '&bufhidden')) |
Bram Moolenaar | f780b8a | 2019-01-05 00:35:22 +0100 | [diff] [blame] | 1858 | |
Bram Moolenaar | 8b62d87 | 2019-01-05 00:02:57 +0100 | [diff] [blame] | 1859 | bdel test_buffer |
| 1860 | call assert_equal([], getbufline(bufnr, 1, '$')) |
| 1861 | |
| 1862 | let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0}) |
Bram Moolenaar | f780b8a | 2019-01-05 00:35:22 +0100 | [diff] [blame] | 1863 | call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))}) |
Bram Moolenaar | 8b62d87 | 2019-01-05 00:02:57 +0100 | [diff] [blame] | 1864 | call assert_equal('nofile', getbufvar(bufnr, '&buftype')) |
| 1865 | call assert_equal('hide', getbufvar(bufnr, '&bufhidden')) |
| 1866 | |
| 1867 | bwipe! test_buffer |
| 1868 | endfunc |
| 1869 | |
Bram Moolenaar | d78f03f | 2017-10-06 01:07:41 +0200 | [diff] [blame] | 1870 | func Test_cmd_parsing() |
Bram Moolenaar | ec9b017 | 2020-06-19 19:10:59 +0200 | [diff] [blame] | 1871 | CheckUnix |
| 1872 | |
Bram Moolenaar | d78f03f | 2017-10-06 01:07:41 +0200 | [diff] [blame] | 1873 | call assert_false(filereadable("file with space")) |
| 1874 | let job = job_start('touch "file with space"') |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1875 | call WaitForAssert({-> assert_true(filereadable("file with space"))}) |
Bram Moolenaar | d78f03f | 2017-10-06 01:07:41 +0200 | [diff] [blame] | 1876 | call delete("file with space") |
| 1877 | |
| 1878 | let job = job_start('touch file\ with\ space') |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1879 | call WaitForAssert({-> assert_true(filereadable("file with space"))}) |
Bram Moolenaar | d78f03f | 2017-10-06 01:07:41 +0200 | [diff] [blame] | 1880 | call delete("file with space") |
| 1881 | endfunc |
| 1882 | |
Bram Moolenaar | 8211798 | 2016-08-27 19:21:48 +0200 | [diff] [blame] | 1883 | func Test_raw_passes_nul() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1884 | CheckExecutable cat |
Bram Moolenaar | 8211798 | 2016-08-27 19:21:48 +0200 | [diff] [blame] | 1885 | |
| 1886 | " Test lines from the job containing NUL are stored correctly in a buffer. |
| 1887 | new |
| 1888 | call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"]) |
| 1889 | w! Xtestread |
| 1890 | bwipe! |
| 1891 | split testout |
| 1892 | 1,$delete |
| 1893 | call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'}) |
| 1894 | call WaitFor('line("$") > 2') |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1895 | call assert_equal("asdf\nasdf", getline(1)) |
| 1896 | call assert_equal("xxx\n", getline(2)) |
| 1897 | call assert_equal("\nyyy", getline(3)) |
Bram Moolenaar | 8211798 | 2016-08-27 19:21:48 +0200 | [diff] [blame] | 1898 | |
| 1899 | call delete('Xtestread') |
| 1900 | bwipe! |
| 1901 | |
| 1902 | " Test lines from a buffer with NUL bytes are written correctly to the job. |
| 1903 | new mybuffer |
| 1904 | call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"]) |
| 1905 | let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1906 | call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))}) |
Bram Moolenaar | 8211798 | 2016-08-27 19:21:48 +0200 | [diff] [blame] | 1907 | bwipe! |
| 1908 | split Xtestwrite |
| 1909 | call assert_equal("asdf\nasdf", getline(1)) |
| 1910 | call assert_equal("xxx\n", getline(2)) |
| 1911 | call assert_equal("\nyyy", getline(3)) |
Bram Moolenaar | 81952470 | 2018-02-27 19:10:00 +0100 | [diff] [blame] | 1912 | call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$')) |
Bram Moolenaar | 8211798 | 2016-08-27 19:21:48 +0200 | [diff] [blame] | 1913 | |
| 1914 | call delete('Xtestwrite') |
| 1915 | bwipe! |
| 1916 | endfunc |
| 1917 | |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 1918 | func Test_read_nonl_line() |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 1919 | let g:linecount = 0 |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 1920 | let arg = 'import sys;sys.stdout.write("1\n2\n3")' |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1921 | call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1922 | call WaitForAssert({-> assert_equal(3, g:linecount)}) |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1923 | unlet g:linecount |
| 1924 | endfunc |
| 1925 | |
| 1926 | func Test_read_nonl_in_close_cb() |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1927 | func s:close_cb(ch) |
| 1928 | while ch_status(a:ch) == 'buffered' |
| 1929 | let g:out .= ch_read(a:ch) |
| 1930 | endwhile |
| 1931 | endfunc |
| 1932 | |
| 1933 | let g:out = '' |
| 1934 | let arg = 'import sys;sys.stdout.write("1\n2\n3")' |
| 1935 | call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')}) |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 1936 | call test_garbagecollect_now() |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1937 | call WaitForAssert({-> assert_equal('123', g:out)}) |
| 1938 | unlet g:out |
| 1939 | delfunc s:close_cb |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 1940 | endfunc |
| 1941 | |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1942 | func Test_read_from_terminated_job() |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1943 | let g:linecount = 0 |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 1944 | let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")' |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1945 | call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1946 | call WaitForAssert({-> assert_equal(1, g:linecount)}) |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 1947 | call test_garbagecollect_now() |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1948 | unlet g:linecount |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1949 | endfunc |
| 1950 | |
Bram Moolenaar | 1df2fa4 | 2018-10-07 21:36:11 +0200 | [diff] [blame] | 1951 | func Test_job_start_windows() |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 1952 | CheckMSWindows |
Bram Moolenaar | 1df2fa4 | 2018-10-07 21:36:11 +0200 | [diff] [blame] | 1953 | |
| 1954 | " Check that backslash in $COMSPEC is handled properly. |
| 1955 | let g:echostr = '' |
| 1956 | let cmd = $COMSPEC . ' /c echo 123' |
| 1957 | let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}}) |
| 1958 | let info = job_info(job) |
| 1959 | call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd) |
| 1960 | |
| 1961 | call WaitForAssert({-> assert_equal("123", g:echostr)}) |
| 1962 | unlet g:echostr |
| 1963 | endfunc |
| 1964 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1965 | func Test_env() |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 1966 | let g:envstr = '' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1967 | if has('win32') |
Bram Moolenaar | 22efba4 | 2018-04-07 13:22:21 +0200 | [diff] [blame] | 1968 | let cmd = ['cmd', '/c', 'echo %FOO%'] |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1969 | else |
Bram Moolenaar | 22efba4 | 2018-04-07 13:22:21 +0200 | [diff] [blame] | 1970 | let cmd = [&shell, &shellcmdflag, 'echo $FOO'] |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1971 | endif |
Bram Moolenaar | 22efba4 | 2018-04-07 13:22:21 +0200 | [diff] [blame] | 1972 | call assert_fails('call job_start(cmd, {"env": 1})', 'E475:') |
K.Takata | cd9fa25 | 2022-09-20 11:04:47 +0100 | [diff] [blame] | 1973 | let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}}) |
| 1974 | if WaitForAssert({-> assert_equal("bar", g:envstr)}, 500) != 0 |
| 1975 | call add(v:errors, "Job status: " .. string(job->job_info())) |
| 1976 | endif |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 1977 | unlet g:envstr |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1978 | endfunc |
| 1979 | |
| 1980 | func Test_cwd() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1981 | let g:test_is_flaky = 1 |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 1982 | let g:envstr = '' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1983 | if has('win32') |
| 1984 | let expect = $TEMP |
Bram Moolenaar | 22efba4 | 2018-04-07 13:22:21 +0200 | [diff] [blame] | 1985 | let cmd = ['cmd', '/c', 'echo %CD%'] |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1986 | else |
| 1987 | let expect = $HOME |
Bram Moolenaar | 22efba4 | 2018-04-07 13:22:21 +0200 | [diff] [blame] | 1988 | let cmd = ['pwd'] |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1989 | endif |
Bram Moolenaar | 22efba4 | 2018-04-07 13:22:21 +0200 | [diff] [blame] | 1990 | let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect}) |
Bram Moolenaar | 2482069 | 2017-12-02 16:38:12 +0100 | [diff] [blame] | 1991 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1992 | call WaitForAssert({-> assert_notequal("", g:envstr)}) |
Bram Moolenaar | ec1238b | 2022-09-25 11:21:04 +0100 | [diff] [blame] | 1993 | " There may be a trailing slash or not, ignore it |
Bram Moolenaar | 2482069 | 2017-12-02 16:38:12 +0100 | [diff] [blame] | 1994 | let expect = substitute(expect, '[/\\]$', '', '') |
| 1995 | let g:envstr = substitute(g:envstr, '[/\\]$', '', '') |
Bram Moolenaar | ec1238b | 2022-09-25 11:21:04 +0100 | [diff] [blame] | 1996 | " on CI there can be /private prefix or not, ignore it |
| 1997 | if $CI != '' && stridx(expect, '/private/') == 0 |
| 1998 | let expect = expect[8:] |
| 1999 | endif |
Bram Moolenaar | 2482069 | 2017-12-02 16:38:12 +0100 | [diff] [blame] | 2000 | if $CI != '' && stridx(g:envstr, '/private/') == 0 |
| 2001 | let g:envstr = g:envstr[8:] |
| 2002 | endif |
| 2003 | call assert_equal(expect, g:envstr) |
| 2004 | finally |
| 2005 | call job_stop(job) |
| 2006 | unlet g:envstr |
| 2007 | endtry |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 2008 | endfunc |
| 2009 | |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 2010 | function Ch_test_close_lambda(port) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 2011 | let handle = ch_open(s:address(a:port), s:chopt) |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 2012 | if ch_status(handle) == "fail" |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 2013 | call assert_report("Can't open channel") |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 2014 | return |
| 2015 | endif |
| 2016 | let g:Ch_close_ret = '' |
| 2017 | call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}}) |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 2018 | call test_garbagecollect_now() |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 2019 | |
| 2020 | call assert_equal('', ch_evalexpr(handle, 'close me')) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 2021 | call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)}) |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 2022 | endfunc |
| 2023 | |
| 2024 | func Test_close_lambda() |
Bram Moolenaar | 069c1e7 | 2016-07-15 21:25:08 +0200 | [diff] [blame] | 2025 | call s:run_server('Ch_test_close_lambda') |
| 2026 | endfunc |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 2027 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 2028 | func Test_close_lambda_ipv6() |
| 2029 | CheckIPv6 |
| 2030 | call Test_close_lambda() |
| 2031 | endfunc |
| 2032 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 2033 | func Test_close_lambda_unix() |
| 2034 | CheckUnix |
| 2035 | call Test_close_lambda() |
| 2036 | call delete('Xtestsocket') |
| 2037 | endfunc |
| 2038 | |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 2039 | func s:test_list_args(cmd, out, remove_lf) |
| 2040 | try |
| 2041 | let g:out = '' |
Bram Moolenaar | 2482069 | 2017-12-02 16:38:12 +0100 | [diff] [blame] | 2042 | let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'}) |
K.Takata | cd9fa25 | 2022-09-20 11:04:47 +0100 | [diff] [blame] | 2043 | try |
| 2044 | call WaitFor('"" != g:out') |
| 2045 | catch |
| 2046 | call add(v:errors, "Job status: " .. string(job->job_info())) |
| 2047 | throw v:exception |
| 2048 | endtry |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 2049 | if has('win32') |
| 2050 | let g:out = substitute(g:out, '\r', '', 'g') |
| 2051 | endif |
| 2052 | if a:remove_lf |
| 2053 | let g:out = substitute(g:out, '\n$', '', 'g') |
| 2054 | endif |
| 2055 | call assert_equal(a:out, g:out) |
| 2056 | finally |
Bram Moolenaar | 2482069 | 2017-12-02 16:38:12 +0100 | [diff] [blame] | 2057 | call job_stop(job) |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 2058 | unlet g:out |
| 2059 | endtry |
| 2060 | endfunc |
| 2061 | |
| 2062 | func Test_list_args() |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 2063 | call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0) |
| 2064 | call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0) |
| 2065 | call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0) |
| 2066 | call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0) |
| 2067 | call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0) |
| 2068 | call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0) |
| 2069 | call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0) |
| 2070 | call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0) |
| 2071 | call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0) |
| 2072 | call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0) |
| 2073 | call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0) |
| 2074 | call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0) |
| 2075 | call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0) |
| 2076 | |
| 2077 | " tests which not contain spaces in the argument |
| 2078 | call s:test_list_args('print("hello\nworld")', "hello\nworld", 1) |
| 2079 | call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1) |
| 2080 | call s:test_list_args('print(''hello"world'')', "hello\"world", 1) |
| 2081 | call s:test_list_args('print(''hello^world'')', "hello^world", 1) |
| 2082 | call s:test_list_args('print("hello&&world")', "hello&&world", 1) |
| 2083 | call s:test_list_args('print(''hello\\world'')', "hello\\world", 1) |
| 2084 | call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1) |
| 2085 | call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1) |
| 2086 | call s:test_list_args('print("hello\tworld")', "hello\tworld", 1) |
| 2087 | endfunc |
Bram Moolenaar | d5359b2 | 2018-04-05 22:44:39 +0200 | [diff] [blame] | 2088 | |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 2089 | func Test_keep_pty_open() |
Bram Moolenaar | ec9b017 | 2020-06-19 19:10:59 +0200 | [diff] [blame] | 2090 | CheckUnix |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 2091 | |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 2092 | let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"', |
| 2093 | \ {'out_io': 'null', 'err_io': 'null', 'pty': 1}) |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 2094 | let elapsed = WaitFor({-> job_status(job) ==# 'dead'}) |
| 2095 | call assert_inrange(200, 1000, elapsed) |
| 2096 | call job_stop(job) |
| 2097 | endfunc |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 2098 | |
| 2099 | func Test_job_start_in_timer() |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2100 | CheckFeature timers |
Bram Moolenaar | 4f32f9c | 2020-03-15 14:53:35 +0100 | [diff] [blame] | 2101 | CheckFunction reltimefloat |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 2102 | |
| 2103 | func OutCb(chan, msg) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 2104 | let g:val += 1 |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 2105 | endfunc |
| 2106 | |
| 2107 | func ExitCb(job, status) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 2108 | let g:val += 1 |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 2109 | call Resume() |
| 2110 | endfunc |
| 2111 | |
| 2112 | func TimerCb(timer) |
| 2113 | if has('win32') |
| 2114 | let cmd = ['cmd', '/c', 'echo.'] |
| 2115 | else |
| 2116 | let cmd = ['echo'] |
| 2117 | endif |
| 2118 | let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'}) |
| 2119 | call substitute(repeat('a', 100000), '.', '', 'g') |
| 2120 | endfunc |
| 2121 | |
| 2122 | " We should be interrupted before 'updatetime' elapsed. |
| 2123 | let g:val = 0 |
| 2124 | call timer_start(1, 'TimerCb') |
| 2125 | let elapsed = Standby(&ut) |
| 2126 | call assert_inrange(1, &ut / 2, elapsed) |
Bram Moolenaar | cfc1523 | 2019-01-23 22:33:18 +0100 | [diff] [blame] | 2127 | |
| 2128 | " Wait for both OutCb() and ExitCb() to have been called before deleting |
| 2129 | " them. |
| 2130 | call WaitForAssert({-> assert_equal(2, g:val)}) |
Bram Moolenaar | c46af53 | 2019-01-09 22:24:49 +0100 | [diff] [blame] | 2131 | call job_stop(g:job) |
| 2132 | |
| 2133 | delfunc OutCb |
| 2134 | delfunc ExitCb |
| 2135 | delfunc TimerCb |
| 2136 | unlet! g:val |
| 2137 | unlet! g:job |
| 2138 | endfunc |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 2139 | |
| 2140 | func Test_raw_large_data() |
| 2141 | try |
| 2142 | let g:out = '' |
| 2143 | let job = job_start(s:python . " test_channel_pipe.py", |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 2144 | \ {'mode': 'raw', 'drop': 'never', 'noblock': 1, |
| 2145 | \ 'callback': {ch, msg -> execute('let g:out .= msg')}}) |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 2146 | |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 2147 | let outlen = 79999 |
| 2148 | let want = repeat('X', outlen) . "\n" |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 2149 | eval job->ch_sendraw(want) |
Bram Moolenaar | e295609 | 2019-01-25 21:01:17 +0100 | [diff] [blame] | 2150 | call WaitFor({-> len(g:out) >= outlen}, 10000) |
| 2151 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 2152 | call assert_equal(want, substitute(g:out, '\r', '', 'g')) |
| 2153 | finally |
| 2154 | call job_stop(job) |
| 2155 | unlet g:out |
| 2156 | endtry |
| 2157 | endfunc |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 2158 | |
Bram Moolenaar | 6524068 | 2019-02-10 22:23:26 +0100 | [diff] [blame] | 2159 | func Test_no_hang_windows() |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2160 | CheckMSWindows |
Bram Moolenaar | 6524068 | 2019-02-10 22:23:26 +0100 | [diff] [blame] | 2161 | |
| 2162 | try |
| 2163 | let job = job_start(s:python . " test_channel_pipe.py busy", |
| 2164 | \ {'mode': 'raw', 'drop': 'never', 'noblock': 0}) |
| 2165 | call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:') |
| 2166 | finally |
| 2167 | call job_stop(job) |
| 2168 | endtry |
| 2169 | endfunc |
| 2170 | |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 2171 | func Test_job_exitval_and_termsig() |
Bram Moolenaar | ec9b017 | 2020-06-19 19:10:59 +0200 | [diff] [blame] | 2172 | CheckUnix |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 2173 | |
| 2174 | " Terminate job normally |
| 2175 | let cmd = ['echo'] |
| 2176 | let job = job_start(cmd) |
| 2177 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
| 2178 | let info = job_info(job) |
| 2179 | call assert_equal(0, info.exitval) |
| 2180 | call assert_equal("", info.termsig) |
| 2181 | |
| 2182 | " Terminate job by signal |
| 2183 | let cmd = ['sleep', '10'] |
| 2184 | let job = job_start(cmd) |
Bram Moolenaar | 08d2e79 | 2019-12-07 17:10:25 +0100 | [diff] [blame] | 2185 | " 10m usually works but 50m is needed when running Valgrind |
| 2186 | sleep 50m |
Bram Moolenaar | b3051ce | 2019-01-31 15:52:11 +0100 | [diff] [blame] | 2187 | call job_stop(job) |
| 2188 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
| 2189 | let info = job_info(job) |
| 2190 | call assert_equal(-1, info.exitval) |
| 2191 | call assert_equal("term", info.termsig) |
| 2192 | endfunc |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 2193 | |
| 2194 | func Test_job_tty_in_out() |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2195 | CheckUnix |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 2196 | |
Bram Moolenaar | 45bbaef | 2022-09-08 16:39:22 +0100 | [diff] [blame] | 2197 | call writefile(['test'], 'Xtestin', 'D') |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 2198 | let in_opts = [{}, |
| 2199 | \ {'in_io': 'null'}, |
| 2200 | \ {'in_io': 'file', 'in_name': 'Xtestin'}] |
| 2201 | let out_opts = [{}, |
| 2202 | \ {'out_io': 'null'}, |
| 2203 | \ {'out_io': 'file', 'out_name': 'Xtestout'}] |
| 2204 | let err_opts = [{}, |
| 2205 | \ {'err_io': 'null'}, |
| 2206 | \ {'err_io': 'file', 'err_name': 'Xtesterr'}, |
| 2207 | \ {'err_io': 'out'}] |
| 2208 | let opts = [] |
| 2209 | |
| 2210 | for in_opt in in_opts |
| 2211 | let x = copy(in_opt) |
| 2212 | for out_opt in out_opts |
Bram Moolenaar | 05c00c0 | 2019-02-11 22:00:11 +0100 | [diff] [blame] | 2213 | let x = extend(copy(x), out_opt) |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 2214 | for err_opt in err_opts |
Bram Moolenaar | 05c00c0 | 2019-02-11 22:00:11 +0100 | [diff] [blame] | 2215 | let x = extend(copy(x), err_opt) |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 2216 | let opts += [extend({'pty': 1}, x)] |
| 2217 | endfor |
| 2218 | endfor |
| 2219 | endfor |
| 2220 | |
| 2221 | for opt in opts |
| 2222 | let job = job_start('echo', opt) |
| 2223 | let info = job_info(job) |
| 2224 | let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}', |
| 2225 | \ get(opt, 'in_io', 'tty'), |
| 2226 | \ get(opt, 'out_io', 'tty'), |
| 2227 | \ get(opt, 'err_io', 'tty')) |
| 2228 | |
| 2229 | if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io') |
| 2230 | call assert_notequal('', info.tty_in, msg) |
| 2231 | else |
| 2232 | call assert_equal('', info.tty_in, msg) |
| 2233 | endif |
| 2234 | call assert_equal(info.tty_in, info.tty_out, msg) |
| 2235 | |
| 2236 | call WaitForAssert({-> assert_equal('dead', job_status(job))}) |
| 2237 | endfor |
| 2238 | |
Bram Moolenaar | 5938648 | 2019-02-10 22:43:46 +0100 | [diff] [blame] | 2239 | call delete('Xtestout') |
| 2240 | call delete('Xtesterr') |
| 2241 | endfunc |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 2242 | |
| 2243 | " Do this last, it stops any channel log. |
| 2244 | func Test_zz_nl_err_to_out_pipe() |
Bram Moolenaar | ec9b017 | 2020-06-19 19:10:59 +0200 | [diff] [blame] | 2245 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 2246 | eval 'Xlog'->ch_logfile() |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 2247 | call ch_log('Test_zz_nl_err_to_out_pipe()') |
| 2248 | let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'}) |
| 2249 | call assert_equal("run", job_status(job)) |
| 2250 | try |
| 2251 | let handle = job_getchannel(job) |
| 2252 | call ch_sendraw(handle, "echo something\n") |
| 2253 | call assert_equal("something", ch_readraw(handle)) |
| 2254 | |
| 2255 | call ch_sendraw(handle, "echoerr wrong\n") |
| 2256 | call assert_equal("wrong", ch_readraw(handle)) |
| 2257 | finally |
| 2258 | call job_stop(job) |
| 2259 | call ch_logfile('') |
| 2260 | let loglines = readfile('Xlog') |
| 2261 | call assert_true(len(loglines) > 10) |
| 2262 | let found_test = 0 |
| 2263 | let found_send = 0 |
| 2264 | let found_recv = 0 |
| 2265 | let found_stop = 0 |
| 2266 | for l in loglines |
| 2267 | if l =~ 'Test_zz_nl_err_to_out_pipe' |
| 2268 | let found_test = 1 |
| 2269 | endif |
| 2270 | if l =~ 'SEND on.*echo something' |
| 2271 | let found_send = 1 |
| 2272 | endif |
| 2273 | if l =~ 'RECV on.*something' |
| 2274 | let found_recv = 1 |
| 2275 | endif |
| 2276 | if l =~ 'Stopping job with' |
| 2277 | let found_stop = 1 |
| 2278 | endif |
| 2279 | endfor |
| 2280 | call assert_equal(1, found_test) |
| 2281 | call assert_equal(1, found_send) |
| 2282 | call assert_equal(1, found_recv) |
| 2283 | call assert_equal(1, found_stop) |
| 2284 | " On MS-Windows need to sleep for a moment to be able to delete the file. |
| 2285 | sleep 10m |
| 2286 | call delete('Xlog') |
| 2287 | endtry |
| 2288 | endfunc |
| 2289 | |
| 2290 | " Do this last, it stops any channel log. |
| 2291 | func Test_zz_ch_log() |
| 2292 | call ch_logfile('Xlog', 'w') |
| 2293 | call ch_log('hello there') |
| 2294 | call ch_log('%s%s') |
| 2295 | call ch_logfile('') |
| 2296 | let text = readfile('Xlog') |
Bram Moolenaar | 4f50117 | 2022-12-01 11:02:23 +0000 | [diff] [blame] | 2297 | call assert_match("start log session", text[0]) |
| 2298 | call assert_match("ch_log(): hello there", text[1]) |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 2299 | call assert_match("%s%s", text[2]) |
Bram Moolenaar | 45bbaef | 2022-09-08 16:39:22 +0100 | [diff] [blame] | 2300 | call mkdir("Xchlogdir1", 'D') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2301 | call assert_fails("call ch_logfile('Xchlogdir1')", 'E484:') |
Bram Moolenaar | 45bbaef | 2022-09-08 16:39:22 +0100 | [diff] [blame] | 2302 | |
Bram Moolenaar | f386f08 | 2019-07-29 23:03:03 +0200 | [diff] [blame] | 2303 | call delete('Xlog') |
| 2304 | endfunc |
Bram Moolenaar | 538feb5 | 2020-01-20 21:59:39 +0100 | [diff] [blame] | 2305 | |
Bram Moolenaar | b3e195c | 2020-02-10 21:32:19 +0100 | [diff] [blame] | 2306 | func Test_issue_5150() |
Bram Moolenaar | d0d440f | 2020-03-07 17:24:59 +0100 | [diff] [blame] | 2307 | if has('win32') |
Milly | 4f5681d | 2024-10-20 11:06:00 +0200 | [diff] [blame] | 2308 | let cmd = 'cmd /D /c pause' |
Bram Moolenaar | d0d440f | 2020-03-07 17:24:59 +0100 | [diff] [blame] | 2309 | else |
| 2310 | let cmd = 'grep foo' |
| 2311 | endif |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 2312 | |
Bram Moolenaar | d0d440f | 2020-03-07 17:24:59 +0100 | [diff] [blame] | 2313 | let g:job = job_start(cmd, {}) |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 2314 | sleep 50m " give the job time to start |
Bram Moolenaar | b3e195c | 2020-02-10 21:32:19 +0100 | [diff] [blame] | 2315 | call job_stop(g:job) |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 2316 | call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)}) |
| 2317 | |
Bram Moolenaar | d0d440f | 2020-03-07 17:24:59 +0100 | [diff] [blame] | 2318 | let g:job = job_start(cmd, {}) |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 2319 | sleep 50m |
Bram Moolenaar | b3e195c | 2020-02-10 21:32:19 +0100 | [diff] [blame] | 2320 | call job_stop(g:job, 'term') |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 2321 | call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)}) |
| 2322 | |
Bram Moolenaar | d0d440f | 2020-03-07 17:24:59 +0100 | [diff] [blame] | 2323 | let g:job = job_start(cmd, {}) |
Bram Moolenaar | 353c351 | 2020-03-15 14:19:26 +0100 | [diff] [blame] | 2324 | sleep 50m |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 2325 | call job_stop(g:job, 'kill') |
| 2326 | call WaitForAssert({-> assert_equal(-1, job_info(g:job).exitval)}) |
Bram Moolenaar | b3e195c | 2020-02-10 21:32:19 +0100 | [diff] [blame] | 2327 | endfunc |
Bram Moolenaar | 355757a | 2020-02-10 22:06:32 +0100 | [diff] [blame] | 2328 | |
| 2329 | func Test_issue_5485() |
| 2330 | let $VAR1 = 'global' |
| 2331 | let g:Ch_reply = "" |
| 2332 | let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'}) |
| 2333 | let g:Ch_job = l:job |
| 2334 | call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))}) |
| 2335 | unlet $VAR1 |
| 2336 | endfunc |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 2337 | |
Bram Moolenaar | 7851b1c | 2020-03-26 16:27:38 +0100 | [diff] [blame] | 2338 | func Test_job_trailing_space_unix() |
| 2339 | CheckUnix |
| 2340 | CheckExecutable cat |
Bram Moolenaar | ec9b017 | 2020-06-19 19:10:59 +0200 | [diff] [blame] | 2341 | |
Bram Moolenaar | 7851b1c | 2020-03-26 16:27:38 +0100 | [diff] [blame] | 2342 | let job = job_start("cat ", #{in_io: 'null'}) |
| 2343 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
| 2344 | call assert_equal(0, job_info(job).exitval) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 2345 | |
| 2346 | call delete('Xtestsocket') |
Bram Moolenaar | 7851b1c | 2020-03-26 16:27:38 +0100 | [diff] [blame] | 2347 | endfunc |
| 2348 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 2349 | func Test_ch_getbufnr() |
| 2350 | let ch = test_null_channel() |
| 2351 | call assert_equal(-1, ch_getbufnr(ch, 'in')) |
| 2352 | call assert_equal(-1, ch_getbufnr(ch, 'out')) |
| 2353 | call assert_equal(-1, ch_getbufnr(ch, 'err')) |
| 2354 | call assert_equal(-1, ch_getbufnr(ch, '')) |
| 2355 | endfunc |
| 2356 | |
| 2357 | " Test for unsupported options passed to ch_status() |
| 2358 | func Test_invalid_job_chan_options() |
| 2359 | let ch = test_null_channel() |
| 2360 | let invalid_opts = [ |
| 2361 | \ {'in_io' : 'null'}, |
| 2362 | \ {'out_io' : 'null'}, |
| 2363 | \ {'err_io' : 'null'}, |
| 2364 | \ {'mode' : 'json'}, |
| 2365 | \ {'out_mode' : 'json'}, |
| 2366 | \ {'err_mode' : 'json'}, |
| 2367 | \ {'noblock' : 1}, |
| 2368 | \ {'in_name' : '/a/b'}, |
| 2369 | \ {'pty' : 1}, |
| 2370 | \ {'in_buf' : 1}, |
| 2371 | \ {'out_buf' : 1}, |
| 2372 | \ {'err_buf' : 1}, |
| 2373 | \ {'out_modifiable' : 1}, |
| 2374 | \ {'err_modifiable' : 1}, |
| 2375 | \ {'out_msg' : 1}, |
| 2376 | \ {'err_msg' : 1}, |
| 2377 | \ {'in_top' : 1}, |
| 2378 | \ {'in_bot' : 1}, |
| 2379 | \ {'channel' : ch}, |
| 2380 | \ {'callback' : ''}, |
| 2381 | \ {'out_cb' : ''}, |
| 2382 | \ {'err_cb' : ''}, |
| 2383 | \ {'close_cb' : ''}, |
| 2384 | \ {'exit_cb' : ''}, |
| 2385 | \ {'term_opencmd' : ''}, |
| 2386 | \ {'eof_chars' : ''}, |
| 2387 | \ {'term_rows' : 10}, |
| 2388 | \ {'term_cols' : 10}, |
| 2389 | \ {'vertical' : 0}, |
| 2390 | \ {'curwin' : 1}, |
| 2391 | \ {'bufnr' : 1}, |
| 2392 | \ {'hidden' : 0}, |
| 2393 | \ {'norestore' : 0}, |
| 2394 | \ {'term_kill' : 'kill'}, |
| 2395 | \ {'tty_type' : ''}, |
| 2396 | \ {'term_highlight' : ''}, |
| 2397 | \ {'env' : {}}, |
| 2398 | \ {'cwd' : ''}, |
| 2399 | \ {'timeout' : 0}, |
| 2400 | \ {'out_timeout' : 0}, |
| 2401 | \ {'err_timeout' : 0}, |
| 2402 | \ {'id' : 0}, |
| 2403 | \ {'stoponexit' : ''}, |
| 2404 | \ {'block_write' : 1} |
| 2405 | \ ] |
| 2406 | if has('gui') |
| 2407 | call add(invalid_opts, {'ansi_colors' : []}) |
| 2408 | endif |
| 2409 | |
| 2410 | for opt in invalid_opts |
| 2411 | call assert_fails("let x = ch_status(ch, opt)", 'E475:') |
| 2412 | endfor |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 2413 | call assert_equal('fail', ch_status(ch, test_null_dict())) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 2414 | endfunc |
| 2415 | |
| 2416 | " Test for passing the command and the arguments as List on MS-Windows |
| 2417 | func Test_job_with_list_args() |
| 2418 | CheckMSWindows |
| 2419 | |
| 2420 | enew! |
| 2421 | let bnum = bufnr() |
| 2422 | let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum}) |
| 2423 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
| 2424 | call assert_equal('Hello World', getline(1)) |
| 2425 | %bw! |
| 2426 | endfunc |
| 2427 | |
Bram Moolenaar | dfc33a6 | 2020-04-29 22:30:13 +0200 | [diff] [blame] | 2428 | func ExitCb_cb_with_input(job, status) |
| 2429 | call feedkeys(":\<C-u>echo input('', 'default')\<CR>\<CR>", 'nx') |
| 2430 | call assert_equal('default', Screenline(&lines)) |
| 2431 | let g:wait_exit_cb = 0 |
| 2432 | endfunc |
| 2433 | |
| 2434 | func Test_cb_with_input() |
| 2435 | let g:wait_exit_cb = 1 |
| 2436 | |
Bram Moolenaar | f637bce | 2020-11-23 18:14:56 +0100 | [diff] [blame] | 2437 | if has('win32') |
Milly | 4f5681d | 2024-10-20 11:06:00 +0200 | [diff] [blame] | 2438 | let cmd = 'cmd /D /c echo "Vim''s test"' |
Bram Moolenaar | f637bce | 2020-11-23 18:14:56 +0100 | [diff] [blame] | 2439 | else |
| 2440 | let cmd = 'echo "Vim''s test"' |
| 2441 | endif |
| 2442 | |
| 2443 | let job = job_start(cmd, {'out_cb': 'ExitCb_cb_with_input'}) |
| 2444 | call WaitFor({-> job_status(job) == "dead"}) |
Bram Moolenaar | dfc33a6 | 2020-04-29 22:30:13 +0200 | [diff] [blame] | 2445 | call WaitForAssert({-> assert_equal(0, g:wait_exit_cb)}) |
| 2446 | |
| 2447 | unlet g:wait_exit_cb |
| 2448 | endfunc |
| 2449 | |
Bram Moolenaar | 5794223 | 2021-08-04 20:54:55 +0200 | [diff] [blame] | 2450 | function s:HandleBufEnter() abort |
| 2451 | let queue = [] |
| 2452 | let job = job_start(['date'], {'callback': { j, d -> add(queue, d) }}) |
| 2453 | while empty(queue) |
| 2454 | sleep! 10m |
| 2455 | endwhile |
| 2456 | endfunction |
| 2457 | |
| 2458 | func Test_parse_messages_in_autocmd() |
| 2459 | CheckUnix |
| 2460 | |
| 2461 | " Check that in the BufEnter autocommand events are being handled |
| 2462 | augroup bufenterjob |
| 2463 | autocmd! |
| 2464 | autocmd BufEnter Xbufenterjob call s:HandleBufEnter() |
| 2465 | augroup END |
| 2466 | |
| 2467 | only |
| 2468 | split Xbufenterjob |
| 2469 | wincmd p |
| 2470 | redraw |
| 2471 | |
| 2472 | close |
| 2473 | augroup bufenterjob |
| 2474 | autocmd! |
| 2475 | augroup END |
| 2476 | endfunc |
| 2477 | |
Bram Moolenaar | 7c25a7c | 2021-10-05 19:19:35 +0100 | [diff] [blame] | 2478 | func Test_job_start_with_invalid_argument() |
| 2479 | call assert_fails('call job_start([0zff])', 'E976:') |
| 2480 | endfunc |
| 2481 | |
Yegappan Lakshmanan | b80ae6c | 2023-09-24 23:38:46 +0200 | [diff] [blame] | 2482 | " Process requests received from the LSP server |
| 2483 | func LspProcessServerRequests(chan, msg) |
| 2484 | if a:msg['method'] == 'server-req-in-middle' |
| 2485 | \ && a:msg['params']['text'] == 'server-req' |
| 2486 | call ch_sendexpr(a:chan, #{method: 'server-req-in-middle-resp', |
| 2487 | \ id: a:msg['id'], params: #{text: 'client-resp'}}) |
Yegappan Lakshmanan | 1926ae4 | 2023-09-21 16:36:28 +0200 | [diff] [blame] | 2488 | endif |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2489 | endfunc |
| 2490 | |
Yegappan Lakshmanan | b80ae6c | 2023-09-24 23:38:46 +0200 | [diff] [blame] | 2491 | " LSP channel message callback function |
| 2492 | func LspCb(chan, msg) |
| 2493 | call add(g:lspNotif, a:msg) |
| 2494 | if a:msg->has_key('method') |
| 2495 | call LspProcessServerRequests(a:chan, a:msg) |
| 2496 | endif |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2497 | endfunc |
| 2498 | |
Yegappan Lakshmanan | b80ae6c | 2023-09-24 23:38:46 +0200 | [diff] [blame] | 2499 | " LSP one-time message callback function (used for ch_sendexpr()) |
| 2500 | func LspOtCb(chan, msg) |
| 2501 | call add(g:lspOtMsgs, a:msg) |
| 2502 | if a:msg->has_key('method') |
| 2503 | call LspProcessServerRequests(a:chan, a:msg) |
| 2504 | endif |
| 2505 | endfunc |
| 2506 | |
| 2507 | " Test for the 'lsp' channel mode |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2508 | func LspTests(port) |
Yegappan Lakshmanan | bac9a9e | 2022-04-19 10:25:13 +0100 | [diff] [blame] | 2509 | " call ch_logfile('Xlspclient.log', 'w') |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2510 | let ch = ch_open(s:localhost .. a:port, #{mode: 'lsp', callback: 'LspCb'}) |
| 2511 | if ch_status(ch) == "fail" |
| 2512 | call assert_report("Can't open the lsp channel") |
| 2513 | return |
| 2514 | endif |
| 2515 | |
| 2516 | " check for channel information |
| 2517 | let info = ch_info(ch) |
| 2518 | call assert_equal('LSP', info.sock_mode) |
| 2519 | |
| 2520 | " Evaluate an expression |
| 2521 | let resp = ch_evalexpr(ch, #{method: 'simple-rpc', params: [10, 20]}) |
| 2522 | call assert_false(empty(resp)) |
| 2523 | call assert_equal(#{id: 1, jsonrpc: '2.0', result: 'simple-rpc'}, resp) |
| 2524 | |
| 2525 | " Evaluate an expression. While waiting for the response, a notification |
| 2526 | " message is delivered. |
| 2527 | let g:lspNotif = [] |
| 2528 | let resp = ch_evalexpr(ch, #{method: 'rpc-with-notif', params: {'v': 10}}) |
| 2529 | call assert_false(empty(resp)) |
| 2530 | call assert_equal(#{id: 2, jsonrpc: '2.0', result: 'rpc-with-notif-resp'}, |
| 2531 | \ resp) |
| 2532 | call assert_equal([#{jsonrpc: '2.0', result: 'rpc-with-notif-notif'}], |
| 2533 | \ g:lspNotif) |
| 2534 | |
| 2535 | " Wrong payload notification test |
| 2536 | let g:lspNotif = [] |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2537 | let r = ch_sendexpr(ch, #{method: 'wrong-payload', params: {}}) |
| 2538 | call assert_equal({}, r) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2539 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2540 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2541 | call assert_equal([#{jsonrpc: '2.0', result: 'wrong-payload'}], g:lspNotif) |
| 2542 | |
| 2543 | " Test for receiving a response with incorrect 'id' and additional |
| 2544 | " notification messages while evaluating an expression. |
| 2545 | let g:lspNotif = [] |
| 2546 | let resp = ch_evalexpr(ch, #{method: 'rpc-resp-incorrect-id', |
| 2547 | \ params: {'a': [1, 2]}}) |
| 2548 | call assert_false(empty(resp)) |
| 2549 | call assert_equal(#{id: 4, jsonrpc: '2.0', |
| 2550 | \ result: 'rpc-resp-incorrect-id-4'}, resp) |
| 2551 | call assert_equal([#{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-1'}, |
| 2552 | \ #{jsonrpc: '2.0', result: 'rpc-resp-incorrect-id-2'}, |
| 2553 | \ #{jsonrpc: '2.0', id: 1, result: 'rpc-resp-incorrect-id-3'}], |
| 2554 | \ g:lspNotif) |
| 2555 | |
| 2556 | " simple notification test |
| 2557 | let g:lspNotif = [] |
| 2558 | call ch_sendexpr(ch, #{method: 'simple-notif', params: [#{a: 10, b: []}]}) |
| 2559 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2560 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2561 | call assert_equal([#{jsonrpc: '2.0', result: 'simple-notif'}], g:lspNotif) |
| 2562 | |
| 2563 | " multiple notifications test |
| 2564 | let g:lspNotif = [] |
| 2565 | call ch_sendexpr(ch, #{method: 'multi-notif', params: [#{a: {}, b: {}}]}) |
| 2566 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2567 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2568 | call assert_equal([#{jsonrpc: '2.0', result: 'multi-notif1'}, |
| 2569 | \ #{jsonrpc: '2.0', result: 'multi-notif2'}], g:lspNotif) |
| 2570 | |
| 2571 | " Test for sending a message with an identifier. |
| 2572 | let g:lspNotif = [] |
| 2573 | call ch_sendexpr(ch, #{method: 'msg-with-id', id: 93, params: #{s: 'str'}}) |
| 2574 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2575 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2576 | call assert_equal([#{jsonrpc: '2.0', id: 93, result: 'msg-with-id'}], |
| 2577 | \ g:lspNotif) |
| 2578 | |
| 2579 | " Test for setting the 'id' value in a request message |
| 2580 | let resp = ch_evalexpr(ch, #{method: 'ping', id: 1, params: {}}) |
| 2581 | call assert_equal(#{id: 8, jsonrpc: '2.0', result: 'alive'}, resp) |
| 2582 | |
| 2583 | " Test for using a one time callback function to process a response |
| 2584 | let g:lspOtMsgs = [] |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 2585 | let r = ch_sendexpr(ch, #{method: 'msg-specific-cb', params: {}}, |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2586 | \ #{callback: 'LspOtCb'}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2587 | call assert_equal(9, r.id) |
| 2588 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 2589 | call assert_equal([#{id: 9, jsonrpc: '2.0', result: 'msg-specific-cb'}], |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2590 | \ g:lspOtMsgs) |
| 2591 | |
| 2592 | " Test for generating a request message from the other end (server) |
| 2593 | let g:lspNotif = [] |
| 2594 | call ch_sendexpr(ch, #{method: 'server-req', params: #{}}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2595 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2596 | call assert_equal([{'id': 201, 'jsonrpc': '2.0', |
| 2597 | \ 'result': {'method': 'checkhealth', 'params': {'a': 20}}}], |
| 2598 | \ g:lspNotif) |
| 2599 | |
| 2600 | " Test for sending a message without an id |
| 2601 | let g:lspNotif = [] |
| 2602 | call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'msg-without-id'}}) |
| 2603 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2604 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2605 | call assert_equal([#{jsonrpc: '2.0', result: |
| 2606 | \ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'msg-without-id'}}}], |
| 2607 | \ g:lspNotif) |
| 2608 | |
| 2609 | " Test for sending a notification message with an id |
| 2610 | let g:lspNotif = [] |
| 2611 | call ch_sendexpr(ch, #{method: 'echo', id: 110, params: #{s: 'msg-with-id'}}) |
| 2612 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2613 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2614 | call assert_equal([#{jsonrpc: '2.0', result: |
| 2615 | \ #{method: 'echo', jsonrpc: '2.0', id: 110, |
| 2616 | \ params: #{s: 'msg-with-id'}}}], g:lspNotif) |
| 2617 | |
| 2618 | " Test for processing the extra fields in the HTTP header |
| 2619 | let resp = ch_evalexpr(ch, #{method: 'extra-hdr-fields', params: {}}) |
| 2620 | call assert_equal({'id': 14, 'jsonrpc': '2.0', 'result': 'extra-hdr-fields'}, |
| 2621 | \ resp) |
| 2622 | |
Yegappan Lakshmanan | 03cca29 | 2022-04-18 14:07:46 +0100 | [diff] [blame] | 2623 | " Test for processing delayed payload |
| 2624 | let resp = ch_evalexpr(ch, #{method: 'delayed-payload', params: {}}) |
| 2625 | call assert_equal({'id': 15, 'jsonrpc': '2.0', 'result': 'delayed-payload'}, |
| 2626 | \ resp) |
| 2627 | |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2628 | " Test for processing a HTTP header without the Content-Length field |
| 2629 | let resp = ch_evalexpr(ch, #{method: 'hdr-without-len', params: {}}, |
| 2630 | \ #{timeout: 200}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2631 | call assert_equal({}, resp) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2632 | " send a ping to make sure communication still works |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2633 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2634 | |
| 2635 | " Test for processing a HTTP header with wrong length |
| 2636 | let resp = ch_evalexpr(ch, #{method: 'hdr-with-wrong-len', params: {}}, |
| 2637 | \ #{timeout: 200}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2638 | call assert_equal({}, resp) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2639 | " send a ping to make sure communication still works |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2640 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2641 | |
| 2642 | " Test for processing a HTTP header with negative length |
| 2643 | let resp = ch_evalexpr(ch, #{method: 'hdr-with-negative-len', params: {}}, |
| 2644 | \ #{timeout: 200}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2645 | call assert_equal({}, resp) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2646 | " send a ping to make sure communication still works |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2647 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2648 | |
| 2649 | " Test for an empty header |
| 2650 | let resp = ch_evalexpr(ch, #{method: 'empty-header', params: {}}, |
| 2651 | \ #{timeout: 200}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2652 | call assert_equal({}, resp) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2653 | " send a ping to make sure communication still works |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2654 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2655 | |
| 2656 | " Test for an empty payload |
| 2657 | let resp = ch_evalexpr(ch, #{method: 'empty-payload', params: {}}, |
| 2658 | \ #{timeout: 200}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2659 | call assert_equal({}, resp) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2660 | " send a ping to make sure communication still works |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2661 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2662 | |
Yegappan Lakshmanan | bac9a9e | 2022-04-19 10:25:13 +0100 | [diff] [blame] | 2663 | " Test for a large payload |
| 2664 | let content = repeat('abcdef', 11000) |
| 2665 | let resp = ch_evalexpr(ch, #{method: 'large-payload', |
| 2666 | \ params: #{text: content}}) |
| 2667 | call assert_equal(#{jsonrpc: '2.0', id: 26, result: |
| 2668 | \ #{method: 'large-payload', jsonrpc: '2.0', id: 26, |
| 2669 | \ params: #{text: content}}}, resp) |
| 2670 | " send a ping to make sure communication still works |
| 2671 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
| 2672 | |
Yegappan Lakshmanan | 1926ae4 | 2023-09-21 16:36:28 +0200 | [diff] [blame] | 2673 | " Test for processing a request message from the server while the client |
Yegappan Lakshmanan | b80ae6c | 2023-09-24 23:38:46 +0200 | [diff] [blame] | 2674 | " is waiting for a response with the same identifier (sync-rpc) |
Yegappan Lakshmanan | 1926ae4 | 2023-09-21 16:36:28 +0200 | [diff] [blame] | 2675 | let g:lspNotif = [] |
| 2676 | let resp = ch_evalexpr(ch, #{method: 'server-req-in-middle', |
| 2677 | \ params: #{text: 'client-req'}}) |
| 2678 | call assert_equal(#{jsonrpc: '2.0', id: 28, |
| 2679 | \ result: #{text: 'server-resp'}}, resp) |
| 2680 | call assert_equal([ |
| 2681 | \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle', |
| 2682 | \ params: #{text: 'server-notif'}}, |
| 2683 | \ #{id: 28, jsonrpc: '2.0', method: 'server-req-in-middle', |
| 2684 | \ params: #{text: 'server-req'}}], g:lspNotif) |
| 2685 | |
Yegappan Lakshmanan | b80ae6c | 2023-09-24 23:38:46 +0200 | [diff] [blame] | 2686 | " Test for processing a request message from the server while the client |
| 2687 | " is waiting for a response with the same identifier (async-rpc using the |
| 2688 | " channel callback function) |
| 2689 | let g:lspNotif = [] |
| 2690 | call ch_sendexpr(ch, #{method: 'server-req-in-middle', id: 500, |
| 2691 | \ params: #{text: 'client-req'}}) |
| 2692 | " Send three pings to wait for all the notification messages to arrive |
| 2693 | for i in range(3) |
| 2694 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
| 2695 | endfor |
| 2696 | call assert_equal([ |
| 2697 | \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle', |
| 2698 | \ params: #{text: 'server-notif'}}, |
| 2699 | \ #{id: 500, jsonrpc: '2.0', method: 'server-req-in-middle', |
| 2700 | \ params: #{text: 'server-req'}}, |
| 2701 | \ #{id: 500, jsonrpc: '2.0', result: #{text: 'server-resp'}} |
| 2702 | \ ], g:lspNotif) |
| 2703 | |
| 2704 | " Test for processing a request message from the server while the client |
| 2705 | " is waiting for a response with the same identifier (async-rpc using a |
| 2706 | " one-time callback function) |
| 2707 | let g:lspNotif = [] |
| 2708 | let g:lspOtMsgs = [] |
| 2709 | call ch_sendexpr(ch, #{method: 'server-req-in-middle', |
| 2710 | \ params: #{text: 'client-req'}}, #{callback: 'LspOtCb'}) |
| 2711 | " Send a ping to wait for all the notification messages to arrive |
| 2712 | for i in range(3) |
| 2713 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
| 2714 | endfor |
| 2715 | call assert_equal([ |
| 2716 | \ #{id: 32, jsonrpc: '2.0', result: #{text: 'server-resp'}}], |
| 2717 | \ g:lspOtMsgs) |
| 2718 | call assert_equal([ |
| 2719 | \ #{id: -1, jsonrpc: '2.0', method: 'server-req-in-middle', |
| 2720 | \ params: #{text: 'server-notif'}}, |
| 2721 | \ #{id: 32, jsonrpc: '2.0', method: 'server-req-in-middle', |
| 2722 | \ params: {'text': 'server-req'}}], g:lspNotif) |
| 2723 | |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2724 | " Test for invoking an unsupported method |
| 2725 | let resp = ch_evalexpr(ch, #{method: 'xyz', params: {}}, #{timeout: 200}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2726 | call assert_equal({}, resp) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2727 | |
| 2728 | " Test for sending a message without a callback function. Notification |
| 2729 | " message should be dropped but RPC response should not be dropped. |
| 2730 | call ch_setoptions(ch, #{callback: ''}) |
| 2731 | let g:lspNotif = [] |
| 2732 | call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}}) |
| 2733 | " Send a ping to wait for all the notification messages to arrive |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 2734 | call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2735 | call assert_equal([], g:lspNotif) |
| 2736 | " Restore the callback function |
| 2737 | call ch_setoptions(ch, #{callback: 'LspCb'}) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2738 | |
| 2739 | " " Test for sending a raw message |
| 2740 | " let g:lspNotif = [] |
| 2741 | " let s = "Content-Length: 62\r\n" |
Yegappan Lakshmanan | c3eddd2 | 2023-04-25 14:54:54 +0100 | [diff] [blame] | 2742 | " let s ..= "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n" |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2743 | " let s ..= "\r\n" |
| 2744 | " let s ..= '{"method":"echo","jsonrpc":"2.0","params":{"m":"raw-message"}}' |
| 2745 | " call ch_sendraw(ch, s) |
| 2746 | " call ch_evalexpr(ch, #{method: 'ping'}) |
| 2747 | " call assert_equal([{'jsonrpc': '2.0', |
| 2748 | " \ 'result': {'method': 'echo', 'jsonrpc': '2.0', |
| 2749 | " \ 'params': {'m': 'raw-message'}}}], g:lspNotif) |
| 2750 | |
| 2751 | " Invalid arguments to ch_evalexpr() and ch_sendexpr() |
| 2752 | call assert_fails('call ch_sendexpr(ch, #{method: "cookie", id: "cookie"})', |
| 2753 | \ 'E475:') |
| 2754 | call assert_fails('call ch_evalexpr(ch, #{method: "ping", id: [{}]})', 'E475:') |
| 2755 | call assert_fails('call ch_evalexpr(ch, [1, 2, 3])', 'E1206:') |
| 2756 | call assert_fails('call ch_sendexpr(ch, "abc")', 'E1206:') |
| 2757 | call assert_fails('call ch_evalexpr(ch, #{method: "ping"}, #{callback: "LspOtCb"})', 'E917:') |
| 2758 | " call ch_logfile('', 'w') |
| 2759 | endfunc |
| 2760 | |
| 2761 | func Test_channel_lsp_mode() |
Milly | baab7c0 | 2024-10-28 21:56:14 +0100 | [diff] [blame] | 2762 | " The channel lsp mode test is flaky and gives the same error. |
| 2763 | let g:giveup_same_error = 0 |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 2764 | call RunServer('test_channel_lsp.py', 'LspTests', []) |
| 2765 | endfunc |
Bram Moolenaar | 5794223 | 2021-08-04 20:54:55 +0200 | [diff] [blame] | 2766 | |
Christian Brabandt | b50bc9a | 2024-09-30 21:29:43 +0200 | [diff] [blame] | 2767 | func Test_error_callback_terminal() |
| 2768 | CheckUnix |
| 2769 | CheckFeature terminal |
| 2770 | let g:out = '' |
| 2771 | let g:error = '' |
| 2772 | |
| 2773 | func! s:Out(channel, msg) |
| 2774 | let g:out .= string(a:msg) |
| 2775 | endfunc |
| 2776 | |
| 2777 | func! s:Err(channel, msg) |
| 2778 | let g:error .= string(a:msg) |
| 2779 | endfunc |
| 2780 | |
| 2781 | let buf = term_start(['sh'], #{term_finish: 'close', out_cb: 's:Out', err_cb: 's:Err', err_io: 'pipe'}) |
| 2782 | let job = term_getjob(buf) |
| 2783 | let dict = job_info(job).channel->ch_info() |
| 2784 | |
| 2785 | call assert_true(dict.id != 0) |
| 2786 | call assert_equal('open', dict.status) |
| 2787 | call assert_equal('open', dict.out_status) |
| 2788 | call assert_equal('RAW', dict.out_mode) |
| 2789 | call assert_equal('buffer', dict.out_io) |
| 2790 | call assert_equal('open', dict.err_status) |
| 2791 | call assert_equal('RAW', dict.err_mode) |
| 2792 | call assert_equal('pipe', dict.err_io) |
| 2793 | call term_sendkeys(buf, "XXXX\<cr>") |
| 2794 | call term_wait(buf) |
| 2795 | call term_sendkeys(buf, "exit\<cr>") |
| 2796 | call term_wait(buf) |
| 2797 | call assert_match('XXX.*exit', g:out) |
| 2798 | call assert_match('sh:.*XXXX:.*not found', g:error) |
| 2799 | |
| 2800 | delfunc s:Out |
| 2801 | delfunc s:Err |
| 2802 | unlet! g:out g:error |
| 2803 | endfunc |
| 2804 | |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 2805 | " vim: shiftwidth=2 sts=2 expandtab |