From 786b0399c3cea197c2579a802e1babdd58c3ba23 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Mon, 4 Aug 2014 15:34:43 -0400 Subject: [PATCH 1/2] Make interval a constant on proxy/config/etcd Allow future testing of intervals --- pkg/proxy/config/etcd.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/proxy/config/etcd.go b/pkg/proxy/config/etcd.go index e045130691..ead13b73de 100644 --- a/pkg/proxy/config/etcd.go +++ b/pkg/proxy/config/etcd.go @@ -51,6 +51,7 @@ type ConfigSourceEtcd struct { client *etcd.Client serviceChannel chan ServiceUpdate endpointsChannel chan EndpointsUpdate + interval time.Duration } // NewConfigSourceEtcd creates a new ConfigSourceEtcd and immediately runs the created ConfigSourceEtcd in a goroutine. @@ -59,6 +60,7 @@ func NewConfigSourceEtcd(client *etcd.Client, serviceChannel chan ServiceUpdate, client: client, serviceChannel: serviceChannel, endpointsChannel: endpointsChannel, + interval: 2 * time.Second, } go config.Run() return config @@ -76,7 +78,7 @@ func (s ConfigSourceEtcd) Run() { break } glog.Errorf("Failed to get any services: %v", err) - time.Sleep(2 * time.Second) + time.Sleep(s.interval) } if len(services) > 0 { From 0c33ed09d448c88e9aa8d9b54f1f22b2a34f8635 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Mon, 4 Aug 2014 15:35:03 -0400 Subject: [PATCH 2/2] Remove long sleeps from proxy tests --- pkg/proxy/proxier_test.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/proxy/proxier_test.go b/pkg/proxy/proxier_test.go index 5a94b623fb..07312d8e69 100644 --- a/pkg/proxy/proxier_test.go +++ b/pkg/proxy/proxier_test.go @@ -27,6 +27,17 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" ) +func waitForClosedPort(p *Proxier, proxyPort string) error { + for i := 0; i < 50; i++ { + _, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", proxyPort)) + if err != nil { + return nil + } + time.Sleep(1 * time.Millisecond) + } + return fmt.Errorf("port %s still open", proxyPort) +} + // a simple echoServer that only accepts one connection. Returns port actually // being listened on, or an error. func echoServer(t *testing.T, addr string) (string, error) { @@ -107,10 +118,8 @@ func TestProxyStop(t *testing.T) { p.StopProxy("echo") // Wait for the port to really close. - time.Sleep(2 * time.Second) - _, err = net.Dial("tcp", net.JoinHostPort("127.0.0.1", proxyPort)) - if err == nil { - t.Fatalf("Unexpected non-error.") + if err := waitForClosedPort(p, proxyPort); err != nil { + t.Fatalf(err.Error()) } } @@ -136,11 +145,8 @@ func TestProxyUpdateDelete(t *testing.T) { conn.Close() p.OnUpdate([]api.Service{}) - // Wait for the port to close. - time.Sleep(2 * time.Second) - _, err = net.Dial("tcp", net.JoinHostPort("127.0.0.1", proxyPort)) - if err == nil { - t.Fatalf("Unexpected non-error.") + if err := waitForClosedPort(p, proxyPort); err != nil { + t.Fatalf(err.Error()) } } @@ -167,14 +173,14 @@ func TestProxyUpdatePort(t *testing.T) { l.Close() // Wait for the socket to actually get free. - time.Sleep(2 * time.Second) + if err := waitForClosedPort(p, port); err != nil { + t.Fatalf(err.Error()) + } p.OnUpdate([]api.Service{ {JSONBase: api.JSONBase{ID: "echo"}, Port: portNum}, }) - time.Sleep(2 * time.Second) - _, err = net.Dial("tcp", net.JoinHostPort("127.0.0.1", proxyPort)) - if err == nil { - t.Fatalf("Unexpected non-error.") + if err := waitForClosedPort(p, proxyPort); err != nil { + t.Fatalf(err.Error()) } testEchoConnection(t, "127.0.0.1", port) }