From 0b116d915f12181e36f02ed3a87767513074e270 Mon Sep 17 00:00:00 2001 From: Tomas Nozicka Date: Wed, 6 Mar 2019 08:36:15 +0100 Subject: [PATCH 1/2] Fix client-go fake client example flake --- .../client-go/examples/fake-client/main_test.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/staging/src/k8s.io/client-go/examples/fake-client/main_test.go b/staging/src/k8s.io/client-go/examples/fake-client/main_test.go index 6f83c3e8d5c..f478a013699 100644 --- a/staging/src/k8s.io/client-go/examples/fake-client/main_test.go +++ b/staging/src/k8s.io/client-go/examples/fake-client/main_test.go @@ -23,6 +23,7 @@ import ( "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/tools/cache" @@ -30,8 +31,8 @@ import ( // TestFakeClient demonstrates how to use a fake client with SharedInformerFactory in tests. func TestFakeClient(t *testing.T) { - // Use a timeout to keep the test from hanging. - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Create the fake client. client := fake.NewSimpleClientset() @@ -45,7 +46,6 @@ func TestFakeClient(t *testing.T) { pod := obj.(*v1.Pod) t.Logf("pod added: %s/%s", pod.Namespace, pod.Name) pods <- pod - cancel() }, }) @@ -55,23 +55,19 @@ func TestFakeClient(t *testing.T) { // This is not required in tests, but it serves as a proof-of-concept by // ensuring that the informer goroutine have warmed up and called List before // we send any events to it. - for !podInformer.HasSynced() { - time.Sleep(10 * time.Millisecond) - } + cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced) // Inject an event into the fake client. p := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "my-pod"}} _, err := client.CoreV1().Pods("test-ns").Create(p) if err != nil { - t.Errorf("error injecting pod add: %v", err) + t.Fatalf("error injecting pod add: %v", err) } - // Wait and check result. - <-ctx.Done() select { case pod := <-pods: t.Logf("Got pod from channel: %s/%s", pod.Namespace, pod.Name) - default: + case <-time.After(wait.ForeverTestTimeout): t.Error("Informer did not get the added pod") } } From 88cab678ea541aa2edda2ef2971415c49edf8f86 Mon Sep 17 00:00:00 2001 From: Tomas Nozicka Date: Wed, 6 Mar 2019 08:36:37 +0100 Subject: [PATCH 2/2] Generated: Update Bazel --- staging/src/k8s.io/client-go/examples/fake-client/BUILD | 1 + 1 file changed, 1 insertion(+) diff --git a/staging/src/k8s.io/client-go/examples/fake-client/BUILD b/staging/src/k8s.io/client-go/examples/fake-client/BUILD index 8e633789b06..4b535e3f17c 100644 --- a/staging/src/k8s.io/client-go/examples/fake-client/BUILD +++ b/staging/src/k8s.io/client-go/examples/fake-client/BUILD @@ -7,6 +7,7 @@ go_test( deps = [ "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library", "//staging/src/k8s.io/client-go/informers:go_default_library", "//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library", "//staging/src/k8s.io/client-go/tools/cache:go_default_library",