From 05b60a30cfcc937efbabc8adeeaff3966c6fd352 Mon Sep 17 00:00:00 2001 From: Wojciech Tyczynski Date: Mon, 28 Dec 2015 15:28:07 +0100 Subject: [PATCH] Fix flakes in cacher_test --- pkg/storage/cacher.go | 7 ++++++- pkg/storage/cacher_test.go | 22 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/storage/cacher.go b/pkg/storage/cacher.go index be9e14cddba..13eabb2b8c9 100644 --- a/pkg/storage/cacher.go +++ b/pkg/storage/cacher.go @@ -293,7 +293,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, f return err } - // To avoid situation when List is proceesed before the underlying + // To avoid situation when List is processed before the underlying // watchCache is propagated for the first time, we acquire and immediately // release the 'usable' lock. // We don't need to hold it all the time, because watchCache is thread-safe @@ -399,6 +399,11 @@ func filterFunction(key string, keyFunc func(runtime.Object) (string, error), fi // Returns resource version to which the underlying cache is synced. func (c *Cacher) LastSyncResourceVersion() (uint64, error) { + // To avoid situation when LastSyncResourceVersion is processed before the + // underlying watchCache is propagated, we acquire 'usable' lock. + c.usable.RLock() + defer c.usable.RUnlock() + c.RLock() defer c.RUnlock() diff --git a/pkg/storage/cacher_test.go b/pkg/storage/cacher_test.go index a6b7496b54c..5ae7977d25a 100644 --- a/pkg/storage/cacher_test.go +++ b/pkg/storage/cacher_test.go @@ -178,8 +178,16 @@ func TestWatch(t *testing.T) { podFooBis := makeTestPod("foo") podFooBis.Spec.NodeName = "anotherFakeNode" + // initialVersion is used to initate the watcher at the beginning of the world, + // which is not defined precisely in etcd. + initialVersion, err := cacher.LastSyncResourceVersion() + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + startVersion := strconv.Itoa(int(initialVersion)) + // Set up Watch for object "podFoo". - watcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", "1", storage.Everything) + watcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", startVersion, storage.Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -227,15 +235,23 @@ func TestWatcherTimeout(t *testing.T) { cacher := newTestCacher(etcdStorage) defer cacher.Stop() + // initialVersion is used to initate the watcher at the beginning of the world, + // which is not defined precisely in etcd. + initialVersion, err := cacher.LastSyncResourceVersion() + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + startVersion := strconv.Itoa(int(initialVersion)) + // Create a watcher that will not be reading any result. - watcher, err := cacher.WatchList(context.TODO(), "pods/ns", "1", storage.Everything) + watcher, err := cacher.WatchList(context.TODO(), "pods/ns", startVersion, storage.Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } defer watcher.Stop() // Create a second watcher that will be reading result. - readingWatcher, err := cacher.WatchList(context.TODO(), "pods/ns", "1", storage.Everything) + readingWatcher, err := cacher.WatchList(context.TODO(), "pods/ns", startVersion, storage.Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) }