Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Recover the panic caused by close sendCh #50

Merged
merged 1 commit into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions reporter/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ func (r *gRPCReporter) Send(spans []go2sky.ReportedSpan) {
r.logger.Printf("marshal segment object err %v", err)
return
}
defer func() {
// recover the panic caused by close sendCh
if err := recover(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this recover do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it in TestGRPCReporter_Close

r.logger.Printf("reporter segment err %v", err)
}
}()
segment.Segment = b
select {
case r.sendCh <- segment:
Expand Down
20 changes: 20 additions & 0 deletions reporter/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/golang/mock/gomock"

Expand Down Expand Up @@ -85,6 +86,25 @@ func Test_e2e(t *testing.T) {
}
}

func TestGRPCReporter_Close(t *testing.T) {
serviceName, _, instance, _, reporter := createMockReporter(t)
reporter.sendCh = make(chan *common.UpstreamSegment, 1)
tracer, err := go2sky.NewTracer(serviceName, go2sky.WithReporter(reporter), go2sky.WithInstance(instance))
if err != nil {
t.Error(err)
}
tracer.WaitUntilRegister()
entry, _, err := tracer.CreateEntrySpan(context.Background(), "/close", func() (s string, err error) {
return header, nil
})
if err != nil {
t.Error(err)
}
reporter.Close()
entry.End()
time.Sleep(time.Second)
}

func createMockReporter(t *testing.T) (string, int32, string, int32, *gRPCReporter) {
ctrl := gomock.NewController(t)
mockRegisterClient := mock_register.NewMockRegisterClient(ctrl)
Expand Down