Fix TestCacherDontMissEventsOnReinitialization test

This commit is contained in:
Wojciech Tyczyński 2024-10-31 12:33:04 +01:00
parent 6d1bcd738f
commit 7682b74271

View File

@ -899,6 +899,7 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) {
case 1:
podList.ListMeta = metav1.ListMeta{ResourceVersion: "10"}
default:
t.Errorf("unexpected list call: %d", listCalls)
err = fmt.Errorf("unexpected list call")
}
listCalls++
@ -921,8 +922,11 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) {
for i := 12; i < 18; i++ {
w.Add(makePod(i))
}
w.Stop()
// Keep the watch open to avoid another reinitialization,
// but register it for cleanup.
t.Cleanup(func() { w.Stop() })
default:
t.Errorf("unexpected watch call: %d", watchCalls)
err = fmt.Errorf("unexpected watch call")
}
watchCalls++
@ -944,7 +948,6 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
errCh := make(chan error, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
@ -968,11 +971,11 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) {
}
rv, err := strconv.Atoi(object.(*example.Pod).ResourceVersion)
if err != nil {
errCh <- fmt.Errorf("incorrect resource version: %v", err)
t.Errorf("incorrect resource version: %v", err)
return
}
if prevRV != -1 && prevRV+1 != rv {
errCh <- fmt.Errorf("unexpected event received, prevRV=%d, rv=%d", prevRV, rv)
t.Errorf("unexpected event received, prevRV=%d, rv=%d", prevRV, rv)
return
}
prevRV = rv
@ -981,11 +984,6 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) {
}()
}
wg.Wait()
close(errCh)
for err := range errCh {
t.Error(err)
}
}
func TestCacherNoLeakWithMultipleWatchers(t *testing.T) {