From f64539bef9acbf18cc28083499fb2c46daf646c7 Mon Sep 17 00:00:00 2001 From: Helin Wang Date: Tue, 8 Aug 2017 22:20:32 +0000 Subject: [PATCH] use random port for embed etcd to avoid port collision --- go/master/service_test.go | 20 ++++++++++++-------- go/pserver/client/etcd_client_test.go | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/go/master/service_test.go b/go/master/service_test.go index 87ce9c904..2d00c22d6 100644 --- a/go/master/service_test.go +++ b/go/master/service_test.go @@ -2,7 +2,9 @@ package master_test import ( "io/ioutil" + "net/url" "os" + "strings" "testing" "time" @@ -19,6 +21,10 @@ func TestNewServiceWithEtcd(t *testing.T) { t.Fatal(err) } cfg := embed.NewConfig() + lpurl, _ := url.Parse("http://localhost:0") + lcurl, _ := url.Parse("http://localhost:0") + cfg.LPUrls = []url.URL{*lpurl} + cfg.LCUrls = []url.URL{*lcurl} cfg.Dir = etcdDir e, err := embed.StartEtcd(cfg) if err != nil { @@ -30,15 +36,13 @@ func TestNewServiceWithEtcd(t *testing.T) { t.Fatal(err) } }() - select { - case <-e.Server.ReadyNotify(): - t.Log("Server is ready!") - case <-time.After(60 * time.Second): - e.Server.Stop() // trigger a shutdown - t.Fatal("Server took too long to start!") - } - ep := []string{"127.0.0.1:2379"} + <-e.Server.ReadyNotify() + + port := strings.Split(e.Clients[0].Addr().String(), ":")[1] + endpoint := "127.0.0.1:" + port + + ep := []string{endpoint} masterAddr := "127.0.0.1:3306" store, err := master.NewEtcdClient(ep, masterAddr, master.DefaultLockPath, master.DefaultAddrPath, master.DefaultStatePath, 30) if err != nil { diff --git a/go/pserver/client/etcd_client_test.go b/go/pserver/client/etcd_client_test.go index 9daeb97d4..08742433e 100644 --- a/go/pserver/client/etcd_client_test.go +++ b/go/pserver/client/etcd_client_test.go @@ -2,7 +2,9 @@ package client_test import ( "io/ioutil" + "net/url" "os" + "strings" "sync" "testing" @@ -16,6 +18,10 @@ func TestSelector(t *testing.T) { t.Fatal(err) } cfg := embed.NewConfig() + lpurl, _ := url.Parse("http://localhost:0") + lcurl, _ := url.Parse("http://localhost:0") + cfg.LPUrls = []url.URL{*lpurl} + cfg.LCUrls = []url.URL{*lcurl} cfg.Dir = etcdDir e, err := embed.StartEtcd(cfg) if err != nil { @@ -31,6 +37,9 @@ func TestSelector(t *testing.T) { <-e.Server.ReadyNotify() + port := strings.Split(e.Clients[0].Addr().String(), ":")[1] + endpoint := "127.0.0.1:" + port + var mu sync.Mutex selectedCount := 0 var wg sync.WaitGroup @@ -53,10 +62,10 @@ func TestSelector(t *testing.T) { } } - c0 := client.NewEtcd("127.0.0.1:2379") - c1 := client.NewEtcd("127.0.0.1:2379") - c2 := client.NewEtcd("127.0.0.1:2379") - c3 := client.NewEtcd("127.0.0.1:2379") + c0 := client.NewEtcd(endpoint) + c1 := client.NewEtcd(endpoint) + c2 := client.NewEtcd(endpoint) + c3 := client.NewEtcd(endpoint) wg.Add(3) go selectAndDone(c0) go selectAndDone(c1) -- GitLab