mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #98496 from lavalamp/connrotation
Remove unbounded connection creation; also change worst case delay from 50s to 5s
This commit is contained in:
commit
9f2f1b8c46
@ -26,7 +26,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCloseAll(t *testing.T) {
|
func TestCloseAll(t *testing.T) {
|
||||||
closed := make(chan struct{})
|
closed := make(chan struct{}, 50)
|
||||||
dialFn := func(ctx context.Context, network, address string) (net.Conn, error) {
|
dialFn := func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
return closeOnlyConn{onClose: func() { closed <- struct{}{} }}, nil
|
return closeOnlyConn{onClose: func() { closed <- struct{}{} }}, nil
|
||||||
}
|
}
|
||||||
@ -42,10 +42,11 @@ func TestCloseAll(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dialer.CloseAll()
|
dialer.CloseAll()
|
||||||
|
deadline := time.After(time.Second)
|
||||||
for j := 0; j < numConns; j++ {
|
for j := 0; j < numConns; j++ {
|
||||||
select {
|
select {
|
||||||
case <-closed:
|
case <-closed:
|
||||||
case <-time.After(time.Second):
|
case <-deadline:
|
||||||
t.Fatalf("iteration %d: 1s after CloseAll only %d/%d connections closed", i, j, numConns)
|
t.Fatalf("iteration %d: 1s after CloseAll only %d/%d connections closed", i, j, numConns)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,48 +60,42 @@ func TestCloseAllRace(t *testing.T) {
|
|||||||
return closeOnlyConn{onClose: func() { atomic.AddInt64(&conns, -1) }}, nil
|
return closeOnlyConn{onClose: func() { atomic.AddInt64(&conns, -1) }}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
done := make(chan struct{})
|
const raceCount = 5000
|
||||||
|
begin := &sync.WaitGroup{}
|
||||||
|
begin.Add(1)
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
// Close all as fast as we can
|
// Close all as fast as we can
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
begin.Wait()
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for {
|
for i := 0; i < raceCount; i++ {
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
dialer.CloseAll()
|
dialer.CloseAll()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Dial as fast as we can
|
// Dial as fast as we can
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
begin.Wait()
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for {
|
for i := 0; i < raceCount; i++ {
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
if _, err := dialer.Dial("", ""); err != nil {
|
if _, err := dialer.Dial("", ""); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
atomic.AddInt64(&conns, 1)
|
atomic.AddInt64(&conns, 1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Soak to ensure no races
|
// Trigger both goroutines as close to the same time as possible
|
||||||
time.Sleep(time.Second)
|
begin.Done()
|
||||||
|
|
||||||
// Signal completion
|
|
||||||
close(done)
|
|
||||||
// Wait for goroutines
|
// Wait for goroutines
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
// Ensure CloseAll ran after all dials
|
// Ensure CloseAll ran after all dials
|
||||||
dialer.CloseAll()
|
dialer.CloseAll()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user