From 3a1dd78778eeb4c54c3741838e0ad29206914f1b Mon Sep 17 00:00:00 2001 From: 3pointer Date: Tue, 14 Dec 2021 19:54:49 +0800 Subject: [PATCH 1/2] ut: fix unstable test tracing_serial_test --- br/pkg/trace/tracing_serial_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/br/pkg/trace/tracing_serial_test.go b/br/pkg/trace/tracing_serial_test.go index 6ddaf3aac1a84..540c73eb38d64 100644 --- a/br/pkg/trace/tracing_serial_test.go +++ b/br/pkg/trace/tracing_serial_test.go @@ -20,7 +20,7 @@ func jobA(ctx context.Context) { ctx = opentracing.ContextWithSpan(ctx, span1) } jobB(ctx) - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) } func jobB(ctx context.Context) { @@ -28,7 +28,7 @@ func jobB(ctx context.Context) { span1 := span.Tracer().StartSpan("jobB", opentracing.ChildOf(span.Context())) defer span1.Finish() } - time.Sleep(10 * time.Millisecond) + time.Sleep(100 * time.Millisecond) } func TestSpan(t *testing.T) { @@ -46,7 +46,7 @@ func TestSpan(t *testing.T) { require.NoError(t, err) s := string(content) // possible result: - // "jobA 22:02:02.545296 20.621764ms\n" - // " └─jobB 22:02:02.545297 10.293444ms\n" - require.Regexp(t, `^jobA.*2[0-9]\.[0-9]+ms\n └─jobB.*1[0-9]\.[0-9]+ms\n$`, s) + // "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) } From eb14f587c0dcacc54f55f2dbb7f42c4608fdf177 Mon Sep 17 00:00:00 2001 From: 3pointer Date: Tue, 14 Dec 2021 20:09:59 +0800 Subject: [PATCH 2/2] ut: try start at random port 10 times for mock cluster --- br/pkg/mock/mock_cluster.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/br/pkg/mock/mock_cluster.go b/br/pkg/mock/mock_cluster.go index e00246835c365..d1ece26505d05 100644 --- a/br/pkg/mock/mock_cluster.go +++ b/br/pkg/mock/mock_cluster.go @@ -6,6 +6,7 @@ import ( "database/sql" "fmt" "io" + "net" "net/http" "net/http/pprof" "net/url" @@ -86,18 +87,40 @@ func NewCluster() (*Cluster, error) { // Start runs a mock cluster. func (mock *Cluster) Start() error { - statusURL, err := url.Parse(tempurl.Alloc()) - if err != nil { - return errors.Trace(err) + 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) } - addrURL, err := url.Parse(tempurl.Alloc()) - if err != nil { - return errors.Trace(err) + 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 {