comparison src/testdir/test_channel.vim @ 15975:915ed7ca92fa v8.1.0993

patch 8.1.0993: ch_read() may return garbage if terminating NL is missing commit https://github.com/vim/vim/commit/772153f8d85c83e08427d93460a676d7f079f002 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Mar 4 12:09:49 2019 +0100 patch 8.1.0993: ch_read() may return garbage if terminating NL is missing Problem: ch_read() may return garbage if terminating NL is missing. Solution: Add terminating NUL. (Ozaki Kiichi, closes https://github.com/vim/vim/issues/4065)
author Bram Moolenaar <Bram@vim.org>
date Mon, 04 Mar 2019 12:15:05 +0100
parents 5b6c3c7feba8
children ba7727889385
comparison
equal deleted inserted replaced
15974:7d3d7e407216 15975:915ed7ca92fa
201 " Reading while there is nothing available. 201 " Reading while there is nothing available.
202 call assert_equal(v:none, ch_read(handle, {'timeout': 0})) 202 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
203 let start = reltime() 203 let start = reltime()
204 call assert_equal(v:none, ch_read(handle, {'timeout': 333})) 204 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
205 let elapsed = reltime(start) 205 let elapsed = reltime(start)
206 call assert_true(reltimefloat(elapsed) > 0.3) 206 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
207 call assert_true(reltimefloat(elapsed) < 0.6)
208 207
209 " Send without waiting for a response, then wait for a response. 208 " Send without waiting for a response, then wait for a response.
210 call ch_sendexpr(handle, 'wait a bit') 209 call ch_sendexpr(handle, 'wait a bit')
211 let resp = ch_read(handle) 210 let resp = ch_read(handle)
212 call assert_equal(type([]), type(resp)) 211 call assert_equal(type([]), type(resp))
432 " Oops, port does exists. 431 " Oops, port does exists.
433 call ch_close(handle) 432 call ch_close(handle)
434 else 433 else
435 " Failed connection should wait about 500 msec. Can be longer if the 434 " Failed connection should wait about 500 msec. Can be longer if the
436 " computer is busy with other things. 435 " computer is busy with other things.
437 let elapsed = reltime(start) 436 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
438 call assert_true(reltimefloat(elapsed) > 0.3)
439 call assert_true(reltimefloat(elapsed) < 1.5)
440 endif 437 endif
441 catch 438 catch
442 if v:exception !~ 'Connection reset by peer' 439 if v:exception !~ 'Connection reset by peer'
443 call assert_report("Caught exception: " . v:exception) 440 call assert_report("Caught exception: " . v:exception)
444 endif 441 endif
1588 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0 1585 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1589 let elapsed = reltimefloat(g:exit_cb_val.end) 1586 let elapsed = reltimefloat(g:exit_cb_val.end)
1590 else 1587 else
1591 let elapsed = 1.0 1588 let elapsed = 1.0
1592 endif 1589 endif
1593 call assert_true(elapsed > 0.5) 1590 call assert_inrange(0.5, 1.0, elapsed)
1594 call assert_true(elapsed < 1.0)
1595 endfunc 1591 endfunc
1596 1592
1597 """"""""" 1593 """""""""
1598 1594
1599 let g:Ch_close_ret = 'alive' 1595 let g:Ch_close_ret = 'alive'
1762 1758
1763 call delete('Xtestwrite') 1759 call delete('Xtestwrite')
1764 bwipe! 1760 bwipe!
1765 endfunc 1761 endfunc
1766 1762
1767 func MyLineCountCb(ch, msg)
1768 let g:linecount += 1
1769 endfunc
1770
1771 func Test_read_nonl_line() 1763 func Test_read_nonl_line()
1772 if !has('job') 1764 if !has('job')
1773 return 1765 return
1774 endif 1766 endif
1775 1767
1776 let g:linecount = 0 1768 let g:linecount = 0
1777 let arg = 'import sys;sys.stdout.write("1\n2\n3")' 1769 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1778 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'}) 1770 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
1779 call WaitForAssert({-> assert_equal(3, g:linecount)}) 1771 call WaitForAssert({-> assert_equal(3, g:linecount)})
1772 unlet g:linecount
1773 endfunc
1774
1775 func Test_read_nonl_in_close_cb()
1776 if !has('job')
1777 return
1778 endif
1779
1780 func s:close_cb(ch)
1781 while ch_status(a:ch) == 'buffered'
1782 let g:out .= ch_read(a:ch)
1783 endwhile
1784 endfunc
1785
1786 let g:out = ''
1787 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1788 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1789 call WaitForAssert({-> assert_equal('123', g:out)})
1790 unlet g:out
1791 delfunc s:close_cb
1780 endfunc 1792 endfunc
1781 1793
1782 func Test_read_from_terminated_job() 1794 func Test_read_from_terminated_job()
1783 if !has('job') 1795 if !has('job')
1784 return 1796 return
1785 endif 1797 endif
1786 1798
1787 let g:linecount = 0 1799 let g:linecount = 0
1788 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")' 1800 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
1789 call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'}) 1801 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
1790 call WaitForAssert({-> assert_equal(1, g:linecount)}) 1802 call WaitForAssert({-> assert_equal(1, g:linecount)})
1803 unlet g:linecount
1791 endfunc 1804 endfunc
1792 1805
1793 func Test_job_start_windows() 1806 func Test_job_start_windows()
1794 if !has('job') || !has('win32') 1807 if !has('job') || !has('win32')
1795 return 1808 return