From 54196c4003087709e3d08be23223f7e07736935a Mon Sep 17 00:00:00 2001 From: 3pointer Date: Thu, 16 Dec 2021 12:22:35 +0800 Subject: [PATCH 1/3] use listen to find random port --- br/pkg/mock/mock_cluster.go | 52 +++++++------------------------------ 1 file changed, 9 insertions(+), 43 deletions(-) diff --git a/br/pkg/mock/mock_cluster.go b/br/pkg/mock/mock_cluster.go index d1ece26505d05..7928300c19252 100644 --- a/br/pkg/mock/mock_cluster.go +++ b/br/pkg/mock/mock_cluster.go @@ -9,8 +9,6 @@ import ( "net" "net/http" "net/http/pprof" - "net/url" - "strconv" "strings" "sync" "time" @@ -27,7 +25,6 @@ import ( "github.com/tikv/client-go/v2/testutils" "github.com/tikv/client-go/v2/tikv" pd "github.com/tikv/pd/client" - "github.com/tikv/pd/pkg/tempurl" "go.uber.org/zap" ) @@ -87,46 +84,15 @@ func NewCluster() (*Cluster, error) { // Start runs a mock cluster. func (mock *Cluster) Start() error { - var ( - err error - statusURL *url.URL - addrURL *url.URL - ) - for i := 0; i < 10; i++ { - // retry 10 times to get available port - statusURL, err = url.Parse(tempurl.Alloc()) - if err != nil { - return errors.Trace(err) - } - listen, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", statusURL.Port())) - if err == nil { - // release port listening - listen.Close() - break - } - } - statusPort, err := strconv.ParseInt(statusURL.Port(), 10, 32) - if err != nil { - return errors.Trace(err) - } + // choose a random available port + listen, _ := net.Listen("tcp", "127.0.0.1:") + statusPort := listen.Addr().(*net.TCPAddr).Port + listen.Close() - for i := 0; i < 10; i++ { - addrURL, err = url.Parse(tempurl.Alloc()) - if err != nil { - return errors.Trace(err) - } - listen, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", addrURL.Port())) - if err == nil { - // release port listening - listen.Close() - break - } - } - addrPort, err := strconv.ParseInt(addrURL.Port(), 10, 32) - if err != nil { - return errors.Trace(err) - } - _ = addrPort + // choose a random available port + listen, _ = net.Listen("tcp", "127.0.0.1:") + addrPort := listen.Addr().(*net.TCPAddr).Port + listen.Close() mock.TiDBDriver = server.NewTiDBDriver(mock.Storage) cfg := config.NewConfig() @@ -146,7 +112,7 @@ func (mock *Cluster) Start() error { panic(err1) } }() - mock.DSN = waitUntilServerOnline(addrURL.Host, cfg.Status.StatusPort) + mock.DSN = waitUntilServerOnline("127.0.0.1", cfg.Status.StatusPort) return nil } From f3c75f6ed3212ee7d8ebf043c2d1412822e5cac3 Mon Sep 17 00:00:00 2001 From: 3pointer Date: Thu, 30 Dec 2021 13:37:29 +0800 Subject: [PATCH 2/3] fix --- br/pkg/mock/mock_cluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/br/pkg/mock/mock_cluster.go b/br/pkg/mock/mock_cluster.go index 7928300c19252..21bc53a26394e 100644 --- a/br/pkg/mock/mock_cluster.go +++ b/br/pkg/mock/mock_cluster.go @@ -108,7 +108,7 @@ func (mock *Cluster) Start() error { } mock.Server = svr go func() { - if err1 := svr.Run(); err != nil { + if err1 := svr.Run(); err1 != nil { panic(err1) } }() From ddff68465f658279c1dd190a1eddccbe69ca677e Mon Sep 17 00:00:00 2001 From: 3pointer Date: Thu, 30 Dec 2021 15:33:14 +0800 Subject: [PATCH 3/3] fix --- br/pkg/mock/mock_cluster.go | 13 +++++++------ br/pkg/restore/client_test.go | 10 ++-------- br/pkg/trace/tracing_serial_test.go | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/br/pkg/mock/mock_cluster.go b/br/pkg/mock/mock_cluster.go index 21bc53a26394e..ffb4c6d40fdcb 100644 --- a/br/pkg/mock/mock_cluster.go +++ b/br/pkg/mock/mock_cluster.go @@ -85,14 +85,12 @@ func NewCluster() (*Cluster, error) { // Start runs a mock cluster. func (mock *Cluster) Start() error { // choose a random available port - listen, _ := net.Listen("tcp", "127.0.0.1:") - statusPort := listen.Addr().(*net.TCPAddr).Port - listen.Close() + l1, _ := net.Listen("tcp", "127.0.0.1:") + statusPort := l1.Addr().(*net.TCPAddr).Port // choose a random available port - listen, _ = net.Listen("tcp", "127.0.0.1:") - addrPort := listen.Addr().(*net.TCPAddr).Port - listen.Close() + l2, _ := net.Listen("tcp", "127.0.0.1:") + addrPort := l2.Addr().(*net.TCPAddr).Port mock.TiDBDriver = server.NewTiDBDriver(mock.Storage) cfg := config.NewConfig() @@ -102,6 +100,9 @@ func (mock *Cluster) Start() error { cfg.Status.ReportStatus = true cfg.Socket = fmt.Sprintf("/tmp/tidb-mock-%d.sock", time.Now().UnixNano()) + // close port for next listen in NewServer + l1.Close() + l2.Close() svr, err := server.NewServer(cfg, mock.TiDBDriver) if err != nil { return errors.Trace(err) diff --git a/br/pkg/restore/client_test.go b/br/pkg/restore/client_test.go index e0709d2f0edd3..ef704b256f7b4 100644 --- a/br/pkg/restore/client_test.go +++ b/br/pkg/restore/client_test.go @@ -38,15 +38,15 @@ func (s *testRestoreClientSuite) SetUpTest(c *C) { var err error s.mock, err = mock.NewCluster() c.Assert(err, IsNil) + c.Assert(s.mock.Start(), IsNil) } func (s *testRestoreClientSuite) TearDownTest(c *C) { + s.mock.Stop() testleak.AfterTest(c)() } func (s *testRestoreClientSuite) TestCreateTables(c *C) { - c.Assert(s.mock.Start(), IsNil) - defer s.mock.Stop() client, err := restore.NewRestoreClient(gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg) c.Assert(err, IsNil) @@ -102,8 +102,6 @@ func (s *testRestoreClientSuite) TestCreateTables(c *C) { } func (s *testRestoreClientSuite) TestIsOnline(c *C) { - c.Assert(s.mock.Start(), IsNil) - defer s.mock.Stop() client, err := restore.NewRestoreClient(gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg) c.Assert(err, IsNil) @@ -114,8 +112,6 @@ func (s *testRestoreClientSuite) TestIsOnline(c *C) { } func (s *testRestoreClientSuite) TestPreCheckTableClusterIndex(c *C) { - c.Assert(s.mock.Start(), IsNil) - defer s.mock.Stop() client, err := restore.NewRestoreClient(gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg) c.Assert(err, IsNil) @@ -185,8 +181,6 @@ func (fpdc fakePDClient) GetAllStores(context.Context, ...pd.GetStoreOption) ([] } func (s *testRestoreClientSuite) TestPreCheckTableTiFlashReplicas(c *C) { - c.Assert(s.mock.Start(), IsNil) - defer s.mock.Stop() mockStores := []*metapb.Store{ { diff --git a/br/pkg/trace/tracing_serial_test.go b/br/pkg/trace/tracing_serial_test.go index 540c73eb38d64..65950d025164d 100644 --- a/br/pkg/trace/tracing_serial_test.go +++ b/br/pkg/trace/tracing_serial_test.go @@ -48,5 +48,5 @@ func TestSpan(t *testing.T) { // possible result: // "jobA 22:02:02.545296 200.621764ms\n" // " └─jobB 22:02:02.545297 100.293444ms\n" - require.Regexp(t, `^jobA.*20[0-9]\.[0-9]+ms\n └─jobB.*10[0-9]\.[0-9]+ms\n$`, s) + require.Regexp(t, `^jobA.*2[0-9][0-9]\.[0-9]+ms\n └─jobB.*1[0-9][0-9]\.[0-9]+ms\n$`, s) }