@@ -13,6 +13,7 @@ import (
13
13
"runtime"
14
14
"runtime/debug"
15
15
"strings"
16
+ "sync"
16
17
"time"
17
18
"unicode"
18
19
"unicode/utf8"
@@ -1862,10 +1863,13 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
1862
1863
// CollectT implements the TestingT interface and collects all errors.
1863
1864
type CollectT struct {
1864
1865
errors []error
1866
+ mu sync.RWMutex
1865
1867
}
1866
1868
1867
1869
// Errorf collects the error.
1868
1870
func (c * CollectT ) Errorf (format string , args ... interface {}) {
1871
+ c .mu .Lock ()
1872
+ defer c .mu .Unlock ()
1869
1873
c .errors = append (c .errors , fmt .Errorf (format , args ... ))
1870
1874
}
1871
1875
@@ -1876,6 +1880,8 @@ func (c *CollectT) FailNow() {
1876
1880
1877
1881
// Reset clears the collected errors.
1878
1882
func (c * CollectT ) Reset () {
1883
+ c .mu .Lock ()
1884
+ defer c .mu .Unlock ()
1879
1885
c .errors = nil
1880
1886
}
1881
1887
@@ -1884,11 +1890,20 @@ func (c *CollectT) Copy(t TestingT) {
1884
1890
if tt , ok := t .(tHelper ); ok {
1885
1891
tt .Helper ()
1886
1892
}
1893
+ c .mu .RLock ()
1894
+ defer c .mu .RUnlock ()
1887
1895
for _ , err := range c .errors {
1888
1896
t .Errorf ("%v" , err )
1889
1897
}
1890
1898
}
1891
1899
1900
+ // hasErrors returns true if any errors were collected.
1901
+ func (c * CollectT ) hasErrors () bool {
1902
+ c .mu .RLock ()
1903
+ defer c .mu .RUnlock ()
1904
+ return len (c .errors ) > 0
1905
+ }
1906
+
1892
1907
// EventuallyWithT asserts that given condition will be met in waitFor time,
1893
1908
// periodically checking target function each tick. In contrast to Eventually,
1894
1909
// it supplies a CollectT to the condition function, so that the condition
@@ -1931,10 +1946,10 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
1931
1946
collect .Reset ()
1932
1947
go func () {
1933
1948
condition (collect )
1934
- ch <- len ( collect .errors ) == 0
1949
+ ch <- collect .hasErrors ()
1935
1950
}()
1936
- case v := <- ch :
1937
- if v {
1951
+ case hasErrors := <- ch :
1952
+ if ! hasErrors {
1938
1953
return true
1939
1954
}
1940
1955
tick = ticker .C
0 commit comments