client-go: don't wait too long after EventBroadcaster.Shutdown

When Shutdown was called, delivery of each pending event would still be retried
12 times with a delay of ~10s between each retry. In apiserver integration
tests that caused the goroutine to linger long after the corresponding
apiserver of the test was shut down.

Kubernetes-commit: 15b01af9c18a0840d71e2bb7dff4d8c29b158aad
This commit is contained in:
Patrick Ohly
2023-02-03 14:52:20 +01:00
committed by Kubernetes Publisher
parent 00b9d76f44
commit f6a5a1f139
2 changed files with 73 additions and 22 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package record
import (
"context"
"encoding/json"
"fmt"
"net/http"
@@ -452,7 +453,12 @@ func TestWriteEventError(t *testing.T) {
},
}
ev := &v1.Event{}
recordToSink(sink, ev, eventCorrelator, 0)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e := eventBroadcasterImpl{
cancelationCtx: ctx,
}
e.recordToSink(sink, ev, eventCorrelator)
if attempts != ent.attemptsWanted {
t.Errorf("case %v: wanted %d, got %d attempts", caseName, ent.attemptsWanted, attempts)
}
@@ -482,7 +488,12 @@ func TestUpdateExpiredEvent(t *testing.T) {
ev := &v1.Event{}
ev.ResourceVersion = "updated-resource-version"
ev.Count = 2
recordToSink(sink, ev, eventCorrelator, 0)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e := eventBroadcasterImpl{
cancelationCtx: ctx,
}
e.recordToSink(sink, ev, eventCorrelator)
if createdEvent == nil {
t.Error("Event did not get created after patch failed")
@@ -494,6 +505,33 @@ func TestUpdateExpiredEvent(t *testing.T) {
}
}
func TestCancelEvent(t *testing.T) {
clock := testclocks.SimpleIntervalClock{Time: time.Now(), Duration: time.Second}
eventCorrelator := NewEventCorrelator(&clock)
attempts := 0
sink := &testEventSink{
OnCreate: func(event *v1.Event) (*v1.Event, error) {
attempts++
return nil, &errors.UnexpectedObjectError{}
},
}
ev := &v1.Event{}
// Cancel before even calling recordToSink.
ctx, cancel := context.WithCancel(context.Background())
cancel()
e := eventBroadcasterImpl{
cancelationCtx: ctx,
sleepDuration: time.Second,
}
e.recordToSink(sink, ev, eventCorrelator)
if attempts != 1 {
t.Errorf("recordToSink should have tried once, then given up immediately. Instead it tried %d times.", attempts)
}
}
func TestLotsOfEvents(t *testing.T) {
recorderCalled := make(chan struct{})
loggerCalled := make(chan struct{})