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 { 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) }