From 24002fb099d30218c6e120a42f85133afbb86969 Mon Sep 17 00:00:00 2001 From: ahrtr Date: Mon, 25 Apr 2022 08:32:26 +0800 Subject: [PATCH] allocate unique port for each member in test cases --- tests/framework/integration/cluster.go | 6 +++--- tests/integration/snapshot/member_test.go | 2 +- tests/integration/snapshot/v3_snapshot_test.go | 18 +++++++++++------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/framework/integration/cluster.go b/tests/framework/integration/cluster.go index 6a941f95b..594aafa4a 100644 --- a/tests/framework/integration/cluster.go +++ b/tests/framework/integration/cluster.go @@ -522,7 +522,7 @@ func isMembersEqual(membs []*pb.Member, wmembs []*pb.Member) bool { return cmp.Equal(membs, wmembs, cmpopts.IgnoreFields(pb.Member{}, "ID", "PeerURLs", "ClientURLs")) } -func newLocalListener(t testutil.TB) net.Listener { +func NewLocalListener(t testutil.TB) net.Listener { c := atomic.AddInt32(&LocalListenCount, 1) // Go 1.8+ allows only numbers in port addr := fmt.Sprintf("127.0.0.1:%05d%05d", c+BasePort, os.Getpid()) @@ -620,7 +620,7 @@ func MustNewMember(t testutil.TB, mcfg MemberConfig) *Member { peerScheme := SchemeFromTLSInfo(mcfg.PeerTLS) clientScheme := SchemeFromTLSInfo(mcfg.ClientTLS) - pln := newLocalListener(t) + pln := NewLocalListener(t) m.PeerListeners = []net.Listener{pln} m.PeerURLs, err = types.NewURLs([]string{peerScheme + "://" + pln.Addr().String()}) if err != nil { @@ -628,7 +628,7 @@ func MustNewMember(t testutil.TB, mcfg MemberConfig) *Member { } m.PeerTLSInfo = mcfg.PeerTLS - cln := newLocalListener(t) + cln := NewLocalListener(t) m.ClientListeners = []net.Listener{cln} m.ClientURLs, err = types.NewURLs([]string{clientScheme + "://" + cln.Addr().String()}) if err != nil { diff --git a/tests/integration/snapshot/member_test.go b/tests/integration/snapshot/member_test.go index fb6091533..a55c93960 100644 --- a/tests/integration/snapshot/member_test.go +++ b/tests/integration/snapshot/member_test.go @@ -54,7 +54,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) { } defer cli.Close() - urls := newEmbedURLs(2) + urls := newEmbedURLs(t, 2) newCURLs, newPURLs := urls[:1], urls[1:] if _, err = cli.MemberAdd(context.Background(), []string{newPURLs[0].String()}); err != nil { t.Fatal(err) diff --git a/tests/integration/snapshot/v3_snapshot_test.go b/tests/integration/snapshot/v3_snapshot_test.go index c49798650..ace338876 100644 --- a/tests/integration/snapshot/v3_snapshot_test.go +++ b/tests/integration/snapshot/v3_snapshot_test.go @@ -17,7 +17,6 @@ package snapshot_test import ( "context" "fmt" - "math/rand" "net/url" "os" "path/filepath" @@ -42,7 +41,7 @@ func TestSnapshotV3RestoreSingle(t *testing.T) { dbPath := createSnapshotFile(t, kvs) clusterN := 1 - urls := newEmbedURLs(clusterN * 2) + urls := newEmbedURLs(t, clusterN*2) cURLs, pURLs := urls[:clusterN], urls[clusterN:] cfg := integration2.NewEmbedConfig(t, "s1") @@ -172,7 +171,7 @@ func createSnapshotFile(t *testing.T, kvs []kv) string { testutil.SkipTestIfShortMode(t, "Snapshot creation tests are depending on embedded etcd server so are integration-level tests.") clusterN := 1 - urls := newEmbedURLs(clusterN * 2) + urls := newEmbedURLs(t, clusterN*2) cURLs, pURLs := urls[:clusterN], urls[clusterN:] cfg := integration2.NewEmbedConfig(t, "default") @@ -223,7 +222,7 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) ( cURLs []url.URL, pURLs []url.URL, srvs []*embed.Etcd) { - urls := newEmbedURLs(clusterN * 2) + urls := newEmbedURLs(t, clusterN*2) cURLs, pURLs = urls[:clusterN], urls[clusterN:] ics := "" @@ -284,11 +283,16 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) ( } // TODO: TLS -func newEmbedURLs(n int) (urls []url.URL) { +func newEmbedURLs(t testutil.TB, n int) (urls []url.URL) { urls = make([]url.URL, n) for i := 0; i < n; i++ { - rand.Seed(int64(time.Now().Nanosecond())) - u, _ := url.Parse(fmt.Sprintf("unix://localhost:%d", rand.Intn(45000))) + l := integration2.NewLocalListener(t) + defer l.Close() + + u, err := url.Parse(fmt.Sprintf("unix://%s", l.Addr())) + if err != nil { + t.Fatal(err) + } urls[i] = *u } return urls