Merge pull request #116251 from wojtek-t/fix_ready_test

Fix deadlock in ready test
This commit is contained in:
Kubernetes Prow Robot 2023-03-03 10:25:19 -08:00 committed by GitHub
commit a1b12e49ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ package cacher
import ( import (
"context" "context"
"sync"
"testing" "testing"
"time" "time"
) )
@ -81,18 +82,26 @@ func Test_newReadyRacy(t *testing.T) {
errCh := make(chan error, concurrency) errCh := make(chan error, concurrency)
ready := newReady() ready := newReady()
ready.set(false) ready.set(false)
wg := sync.WaitGroup{}
wg.Add(2 * concurrency)
for i := 0; i < concurrency; i++ { for i := 0; i < concurrency; i++ {
go func() { go func() {
errCh <- ready.wait(context.Background()) errCh <- ready.wait(context.Background())
}() }()
go func() { go func() {
defer wg.Done()
ready.set(false) ready.set(false)
}() }()
go func() { go func() {
defer wg.Done()
ready.set(true) ready.set(true)
}() }()
} }
// Last one has to be set to true.
wg.Wait()
ready.set(true) ready.set(true)
for i := 0; i < concurrency; i++ { for i := 0; i < concurrency; i++ {
if err := <-errCh; err != nil { if err := <-errCh; err != nil {
t.Errorf("unexpected error %v on channel %d", err, i) t.Errorf("unexpected error %v on channel %d", err, i)