From 9519a8049bf92f4a5f3765cd2d1e52e4ba64180e Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 30 Jul 2014 06:56:42 -0700 Subject: [PATCH] Fixed tests. --- pkg/proxy/proxier.go | 5 +++++ pkg/proxy/proxier_test.go | 14 ++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/proxy/proxier.go b/pkg/proxy/proxier.go index 280685b3451..a300fb53105 100644 --- a/pkg/proxy/proxier.go +++ b/pkg/proxy/proxier.go @@ -156,9 +156,14 @@ func (proxier *Proxier) addService(service string, port int) (net.Listener, erro return l, nil } +// used to globally lock around unused ports. Only used in testing. +var unusedPortLock sync.Mutex + // addService starts listening for a new service, returning the port it's using. // For testing on a system with unknown ports used. func (proxier *Proxier) addServiceOnUnusedPort(service string) (string, error) { + unusedPortLock.Lock() + defer unusedPortLock.Unlock() // Make sure we can start listening on the port before saying all's well. l, err := net.Listen("tcp", ":0") if err != nil { diff --git a/pkg/proxy/proxier_test.go b/pkg/proxy/proxier_test.go index 1cb8c7b8af2..e62907b0f57 100644 --- a/pkg/proxy/proxier_test.go +++ b/pkg/proxy/proxier_test.go @@ -91,7 +91,7 @@ func TestProxyStop(t *testing.T) { } lb := NewLoadBalancerRR() - lb.OnUpdate([]api.Endpoints{{"echo", []string{net.JoinHostPort("127.0.0.1", port)}}}) + lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}}) p := NewProxier(lb) @@ -106,6 +106,7 @@ func TestProxyStop(t *testing.T) { conn.Close() 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 { @@ -120,7 +121,7 @@ func TestProxyUpdateDelete(t *testing.T) { } lb := NewLoadBalancerRR() - lb.OnUpdate([]api.Endpoints{{"echo", []string{net.JoinHostPort("127.0.0.1", port)}}}) + lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}}) p := NewProxier(lb) @@ -149,7 +150,7 @@ func TestProxyUpdatePort(t *testing.T) { } lb := NewLoadBalancerRR() - lb.OnUpdate([]api.Endpoints{{"echo", []string{net.JoinHostPort("127.0.0.1", port)}}}) + lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}}) p := NewProxier(lb) @@ -157,11 +158,6 @@ func TestProxyUpdatePort(t *testing.T) { if err != nil { t.Fatalf("error adding new service: %#v", err) } - conn, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", proxyPort)) - if err != nil { - t.Fatalf("error connecting to proxy: %v", err) - } - conn.Close() // add a new dummy listener in order to get a port that is free l, _ := net.Listen("tcp", ":0") @@ -169,6 +165,8 @@ func TestProxyUpdatePort(t *testing.T) { portNum, _ := strconv.Atoi(port) l.Close() + // Wait for the socket to actually get free. + time.Sleep(2 * time.Second) p.OnUpdate([]api.Service{ {JSONBase: api.JSONBase{ID: "echo"}, Port: portNum}, })