Skip to content

Commit 13deab8

Browse files
committed
patch 8.0.1259: search test can be flaky
Problem: Search test can be flaky. Solution: Use WaitFor() instead of a delay. Make it possible to pass a funcref to WaitFor() to avoid the need for global variables. (James McCoy, closes #2282)
1 parent 52a2f0f commit 13deab8

3 files changed

Lines changed: 54 additions & 49 deletions

File tree

src/testdir/shared.vim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ func s:kill_server(cmd)
113113
endif
114114
endfunc
115115

116-
" Wait for up to a second for "expr" to become true.
116+
" Wait for up to a second for "expr" to become true. "expr" can be a
117+
" stringified expression to evaluate, or a funcref without arguments.
118+
"
117119
" Return time slept in milliseconds. With the +reltime feature this can be
118120
" more than the actual waiting time. Without +reltime it can also be less.
119121
func WaitFor(expr, ...)
@@ -124,8 +126,13 @@ func WaitFor(expr, ...)
124126
else
125127
let slept = 0
126128
endif
129+
if type(a:expr) == v:t_func
130+
let Test = a:expr
131+
else
132+
let Test = {-> eval(a:expr) }
133+
endif
127134
for i in range(timeout / 10)
128-
if eval(a:expr)
135+
if Test()
129136
if has('reltime')
130137
return float2nr(reltimefloat(reltime(start)) * 1000)
131138
endif

src/testdir/test_search.vim

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -482,19 +482,17 @@ func Test_search_cmdline8()
482482
" Prepare buffer text
483483
let lines = ['abb vim vim vi', 'vimvivim']
484484
call writefile(lines, 'Xsearch.txt')
485-
let g:buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
486-
487-
call term_wait(g:buf, 200)
488-
call assert_equal(lines[0], term_getline(g:buf, 1))
489-
call assert_equal(lines[1], term_getline(g:buf, 2))
490-
491-
call term_sendkeys(g:buf, ":set incsearch hlsearch\<cr>")
492-
call term_sendkeys(g:buf, ":14vsp\<cr>")
493-
call term_sendkeys(g:buf, "/vim\<cr>")
494-
call term_sendkeys(g:buf, "/b\<esc>")
495-
call term_sendkeys(g:buf, "gg0")
496-
call term_wait(g:buf, 500)
497-
let screen_line = term_scrape(g:buf, 1)
485+
let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
486+
487+
call WaitFor({-> lines == [term_getline(buf, 1), term_getline(buf, 2)] })
488+
489+
call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
490+
call term_sendkeys(buf, ":14vsp\<cr>")
491+
call term_sendkeys(buf, "/vim\<cr>")
492+
call term_sendkeys(buf, "/b\<esc>")
493+
call term_sendkeys(buf, "gg0")
494+
call term_wait(buf, 500)
495+
let screen_line = term_scrape(buf, 1)
498496
let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
499497
\ screen_line[18].attr, screen_line[19].attr]
500498
call assert_notequal(a0, a1)
@@ -607,19 +605,17 @@ func Test_search_cmdline_incsearch_highlight_attr()
607605
endif
608606

609607
" Prepare buffer text
610-
let g:lines = ['abb vim vim vi', 'vimvivim']
611-
call writefile(g:lines, 'Xsearch.txt')
612-
let g:buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
613-
call WaitFor('g:lines[0] == term_getline(g:buf, 1)')
614-
call assert_equal(g:lines[0], term_getline(g:buf, 1))
615-
call assert_equal(g:lines[1], term_getline(g:buf, 2))
616-
unlet g:lines
608+
let lines = ['abb vim vim vi', 'vimvivim']
609+
call writefile(lines, 'Xsearch.txt')
610+
let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
611+
612+
call WaitFor({-> lines == [term_getline(buf, 1), term_getline(buf, 2)] })
617613

618614
" Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
619-
call term_sendkeys(g:buf, ":set incsearch hlsearch\<cr>")
620-
call term_sendkeys(g:buf, '/b')
621-
call term_wait(g:buf, 200)
622-
let screen_line1 = term_scrape(g:buf, 1)
615+
call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
616+
call term_sendkeys(buf, '/b')
617+
call term_wait(buf, 200)
618+
let screen_line1 = term_scrape(buf, 1)
623619
call assert_true(len(screen_line1) > 2)
624620
" a0: attr_normal
625621
let a0 = screen_line1[0].attr
@@ -630,53 +626,53 @@ func Test_search_cmdline_incsearch_highlight_attr()
630626
call assert_notequal(a0, a1)
631627
call assert_notequal(a0, a2)
632628
call assert_notequal(a1, a2)
633-
call term_sendkeys(g:buf, "\<cr>gg0")
629+
call term_sendkeys(buf, "\<cr>gg0")
634630

635631
" Test incremental highlight search
636-
call term_sendkeys(g:buf, "/vim")
637-
call term_wait(g:buf, 200)
632+
call term_sendkeys(buf, "/vim")
633+
call term_wait(buf, 200)
638634
" Buffer:
639635
" abb vim vim vi
640636
" vimvivim
641637
" Search: /vim
642638
let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
643639
let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
644-
call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
645-
call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
640+
call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
641+
call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
646642

647643
" Test <C-g>
648-
call term_sendkeys(g:buf, "\<C-g>\<C-g>")
649-
call term_wait(g:buf, 200)
644+
call term_sendkeys(buf, "\<C-g>\<C-g>")
645+
call term_wait(buf, 200)
650646
let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
651647
let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
652-
call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
653-
call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
648+
call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
649+
call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
654650

655651
" Test <C-t>
656-
call term_sendkeys(g:buf, "\<C-t>")
657-
call term_wait(g:buf, 200)
652+
call term_sendkeys(buf, "\<C-t>")
653+
call term_wait(buf, 200)
658654
let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
659655
let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
660-
call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
661-
call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
656+
call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
657+
call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
662658

663659
" Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
664-
call term_sendkeys(g:buf, "\<cr>")
665-
call term_wait(g:buf, 200)
660+
call term_sendkeys(buf, "\<cr>")
661+
call term_wait(buf, 200)
666662
let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
667663
let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
668-
call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
669-
call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
664+
call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
665+
call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
670666

671667
" Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
672-
call term_sendkeys(g:buf, ":1\<cr>")
673-
call term_sendkeys(g:buf, ":set nohlsearch\<cr>")
674-
call term_sendkeys(g:buf, "/vim")
675-
call term_wait(g:buf, 200)
668+
call term_sendkeys(buf, ":1\<cr>")
669+
call term_sendkeys(buf, ":set nohlsearch\<cr>")
670+
call term_sendkeys(buf, "/vim")
671+
call term_wait(buf, 200)
676672
let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
677673
let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
678-
call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
679-
call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
674+
call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
675+
call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
680676
call delete('Xsearch.txt')
681677

682678
call delete('Xsearch.txt')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1259,
764766
/**/
765767
1258,
766768
/**/

0 commit comments

Comments
 (0)