Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"slices"
"strconv"
"strings"
"testing"
"testing/synctest"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -2588,3 +2590,70 @@ func cancelledCtx(ctx context.Context) context.Context {
cancel()
return cancelCtx
}

type fakeRESTMapper struct {
meta.RESTMapper
}

func (f *fakeRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
return &meta.RESTMapping{Scope: meta.RESTScopeNamespace}, nil
}

func TestReaderWaitsForCacheSync(t *testing.T) {
t.Parallel()
for _, readerFailOnMissingInformer := range []bool{true, false} {
t.Run(fmt.Sprintf("ReaderFailOnMissingInformer=%v", readerFailOnMissingInformer), func(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
g := NewWithT(t)

fakeInformer := &controllertest.FakeInformer{Synced: false}
c, err := cache.New(&rest.Config{}, cache.Options{
ReaderFailOnMissingInformer: readerFailOnMissingInformer,
Mapper: &fakeRESTMapper{},
NewInformer: func(kcache.ListerWatcher, runtime.Object, time.Duration, kcache.Indexers) kcache.SharedIndexInformer {
return fakeInformer
},
})
g.Expect(err).NotTo(HaveOccurred())

ctx, cancel := context.WithCancel(t.Context())
defer cancel()
cacheDone := make(chan struct{})
go func() {
g.Expect(c.Start(ctx)).To(Succeed())
close(cacheDone)
}()
synctest.Wait() // Let the cache finish starting
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinating "cache has finished starting up" with the rest would require us to sleep without synctest, as Start is blocking

_, err = c.GetInformer(ctx, &corev1.Service{}, cache.BlockUntilSynced(false))
g.Expect(err).ToNot(HaveOccurred())

listCtx, listCtxCancel := context.WithTimeout(ctx, time.Second)
defer listCtxCancel()
services := &corev1.ServiceList{}
err = c.List(listCtx, services)
g.Expect(err).To(HaveOccurred())
g.Expect(apierrors.IsTimeout(err)).To(BeTrue())

getCtx, getCtxCancel := context.WithTimeout(ctx, time.Second)
defer getCtxCancel()
err = c.Get(getCtx, client.ObjectKey{Name: "default", Namespace: "kubernetes"}, &corev1.Service{})
g.Expect(err).To(HaveOccurred())
g.Expect(apierrors.IsTimeout(err)).To(BeTrue())

fakeInformer.SyncedLock.Lock()
fakeInformer.Synced = true
fakeInformer.SyncedLock.Unlock()

g.Expect(c.List(ctx, services)).To(Succeed())

err = c.Get(getCtx, client.ObjectKey{Name: "default", Namespace: "kubernetes"}, &corev1.Service{})
g.Expect(err).To(HaveOccurred())
g.Expect(apierrors.IsNotFound(err)).To(BeTrue())

cancel()
<-cacheDone
})
})
}
}
12 changes: 10 additions & 2 deletions pkg/controller/controllertest/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllertest

import (
"context"
"sync"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -29,7 +30,8 @@ var _ cache.SharedIndexInformer = &FakeInformer{}
// FakeInformer provides fake Informer functionality for testing.
type FakeInformer struct {
// Synced is returned by the HasSynced functions to implement the Informer interface
Synced bool
Synced bool
SyncedLock sync.Mutex

// RunCount is incremented each time RunInformersAndControllers is called
RunCount int
Expand All @@ -44,6 +46,9 @@ type fakeHandlerRegistration struct {

// HasSynced implements cache.ResourceEventHandlerRegistration.
func (f *fakeHandlerRegistration) HasSynced() bool {
f.informer.SyncedLock.Lock()
defer f.informer.SyncedLock.Unlock()

return f.informer.Synced
}

Expand All @@ -54,7 +59,7 @@ func (f *FakeInformer) AddIndexers(indexers cache.Indexers) error {

// GetIndexer does nothing. TODO(community): Implement this.
func (f *FakeInformer) GetIndexer() cache.Indexer {
return nil
return cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, nil)
}

// Informer returns the fake Informer.
Expand All @@ -64,6 +69,9 @@ func (f *FakeInformer) Informer() cache.SharedIndexInformer {

// HasSynced implements the Informer interface. Returns f.Synced.
func (f *FakeInformer) HasSynced() bool {
f.SyncedLock.Lock()
defer f.SyncedLock.Unlock()

return f.Synced
}

Expand Down
Loading