Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br: fix some unstable unit test cases. #30716

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions br/pkg/mock/mock_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"fmt"
"io"
"net"
"net/http"
"net/http/pprof"
"net/url"
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions br/pkg/trace/tracing_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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) {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
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) {
Expand All @@ -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)
}