Skip to content

Commit e8a77bd

Browse files
Merge pull request #130910 from googs1025/fix/datarace
flake: fix data race for func TestBackoff_Step Kubernetes-commit: 3a14b619d5862b058f941e07f381e8965c5c7702
2 parents 7e8c77e + 27fd396 commit e8a77bd

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

pkg/util/wait/wait_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ func TestUntil(t *testing.T) {
4040

4141
ch = make(chan struct{})
4242
called := make(chan struct{})
43+
wg := sync.WaitGroup{}
44+
wg.Add(1)
4345
go func() {
46+
defer wg.Done()
4447
Until(func() {
4548
called <- struct{}{}
4649
}, 0, ch)
@@ -49,6 +52,7 @@ func TestUntil(t *testing.T) {
4952
<-called
5053
close(ch)
5154
<-called
55+
wg.Wait()
5256
}
5357

5458
func TestUntilWithContext(t *testing.T) {
@@ -60,7 +64,10 @@ func TestUntilWithContext(t *testing.T) {
6064

6165
ctx, cancel = context.WithCancel(context.TODO())
6266
called := make(chan struct{})
67+
wg := sync.WaitGroup{}
68+
wg.Add(1)
6369
go func() {
70+
defer wg.Done()
6471
UntilWithContext(ctx, func(context.Context) {
6572
called <- struct{}{}
6673
}, 0)
@@ -69,6 +76,7 @@ func TestUntilWithContext(t *testing.T) {
6976
<-called
7077
cancel()
7178
<-called
79+
wg.Wait()
7280
}
7381

7482
func TestNonSlidingUntil(t *testing.T) {
@@ -80,7 +88,10 @@ func TestNonSlidingUntil(t *testing.T) {
8088

8189
ch = make(chan struct{})
8290
called := make(chan struct{})
91+
wg := sync.WaitGroup{}
92+
wg.Add(1)
8393
go func() {
94+
defer wg.Done()
8495
NonSlidingUntil(func() {
8596
called <- struct{}{}
8697
}, 0, ch)
@@ -89,6 +100,7 @@ func TestNonSlidingUntil(t *testing.T) {
89100
<-called
90101
close(ch)
91102
<-called
103+
wg.Wait()
92104
}
93105

94106
func TestNonSlidingUntilWithContext(t *testing.T) {
@@ -100,7 +112,10 @@ func TestNonSlidingUntilWithContext(t *testing.T) {
100112

101113
ctx, cancel = context.WithCancel(context.TODO())
102114
called := make(chan struct{})
115+
wg := sync.WaitGroup{}
116+
wg.Add(1)
103117
go func() {
118+
defer wg.Done()
104119
NonSlidingUntilWithContext(ctx, func(context.Context) {
105120
called <- struct{}{}
106121
}, 0)
@@ -109,6 +124,7 @@ func TestNonSlidingUntilWithContext(t *testing.T) {
109124
<-called
110125
cancel()
111126
<-called
127+
wg.Wait()
112128
}
113129

114130
func TestUntilReturnsImmediately(t *testing.T) {
@@ -138,7 +154,10 @@ func TestJitterUntil(t *testing.T) {
138154

139155
ch = make(chan struct{})
140156
called := make(chan struct{})
157+
wg := sync.WaitGroup{}
158+
wg.Add(1)
141159
go func() {
160+
defer wg.Done()
142161
JitterUntil(func() {
143162
called <- struct{}{}
144163
}, 0, 1.0, true, ch)
@@ -147,6 +166,7 @@ func TestJitterUntil(t *testing.T) {
147166
<-called
148167
close(ch)
149168
<-called
169+
wg.Wait()
150170
}
151171

152172
func TestJitterUntilWithContext(t *testing.T) {
@@ -158,7 +178,10 @@ func TestJitterUntilWithContext(t *testing.T) {
158178

159179
ctx, cancel = context.WithCancel(context.TODO())
160180
called := make(chan struct{})
181+
wg := sync.WaitGroup{}
182+
wg.Add(1)
161183
go func() {
184+
defer wg.Done()
162185
JitterUntilWithContext(ctx, func(context.Context) {
163186
called <- struct{}{}
164187
}, 0, 1.0, true)
@@ -167,6 +190,7 @@ func TestJitterUntilWithContext(t *testing.T) {
167190
<-called
168191
cancel()
169192
<-called
193+
wg.Wait()
170194
}
171195

172196
func TestJitterUntilReturnsImmediately(t *testing.T) {
@@ -220,7 +244,10 @@ func TestJitterUntilNegativeFactor(t *testing.T) {
220244
ch := make(chan struct{})
221245
called := make(chan struct{})
222246
received := make(chan struct{})
247+
wg := sync.WaitGroup{}
248+
wg.Add(1)
223249
go func() {
250+
defer wg.Done()
224251
JitterUntil(func() {
225252
called <- struct{}{}
226253
<-received
@@ -238,6 +265,7 @@ func TestJitterUntilNegativeFactor(t *testing.T) {
238265
if now.Add(3 * time.Second).Before(time.Now()) {
239266
t.Errorf("JitterUntil did not returned after predefined period with negative jitter factor when the stop chan was closed inside the func")
240267
}
268+
wg.Wait()
241269
}
242270

243271
func TestExponentialBackoff(t *testing.T) {
@@ -440,7 +468,10 @@ func TestPollForever(t *testing.T) {
440468
errc := make(chan error, 1)
441469
done := make(chan struct{}, 1)
442470
complete := make(chan struct{})
471+
wg := sync.WaitGroup{}
472+
wg.Add(1)
443473
go func() {
474+
defer wg.Done()
444475
f := ConditionFunc(func() (bool, error) {
445476
ch <- struct{}{}
446477
select {
@@ -479,7 +510,9 @@ func TestPollForever(t *testing.T) {
479510

480511
// at most one poll notification should be sent once we return from the condition
481512
done <- struct{}{}
513+
wg.Add(1)
482514
go func() {
515+
defer wg.Done()
483516
for i := 0; i < 2; i++ {
484517
_, open := <-ch
485518
if !open {
@@ -493,6 +526,7 @@ func TestPollForever(t *testing.T) {
493526
if len(errc) != 0 {
494527
t.Fatal(<-errc)
495528
}
529+
wg.Wait()
496530
}
497531

498532
func Test_waitFor(t *testing.T) {
@@ -631,8 +665,10 @@ func TestPollUntil(t *testing.T) {
631665
stopCh := make(chan struct{})
632666
called := make(chan bool)
633667
pollDone := make(chan struct{})
634-
668+
wg := sync.WaitGroup{}
669+
wg.Add(1)
635670
go func() {
671+
defer wg.Done()
636672
PollUntil(time.Microsecond, ConditionFunc(func() (bool, error) {
637673
called <- true
638674
return false, nil
@@ -655,6 +691,7 @@ func TestPollUntil(t *testing.T) {
655691
// make sure we finished the poll
656692
<-pollDone
657693
close(called)
694+
wg.Wait()
658695
}
659696

660697
func TestBackoff_Step(t *testing.T) {

0 commit comments

Comments
 (0)