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

otlptracegrpc: Fix start/stop sync in mockCollector #3989

Merged
merged 3 commits into from
Apr 13, 2023
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: 0 additions & 6 deletions exporters/otlp/otlptrace/otlptracegrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ func newGRPCExporter(t *testing.T, ctx context.Context, endpoint string, additio
func newExporterEndToEndTest(t *testing.T, additionalOpts []otlptracegrpc.Option) {
mc := runMockCollector(t)

<-time.After(5 * time.Millisecond)

ctx := context.Background()
exp := newGRPCExporter(t, ctx, mc.endpoint, additionalOpts...)
t.Cleanup(func() {
Expand Down Expand Up @@ -248,8 +246,6 @@ func TestExportSpansTimeoutHonored(t *testing.T) {
func TestNewWithMultipleAttributeTypes(t *testing.T) {
mc := runMockCollector(t)

<-time.After(5 * time.Millisecond)

ctx, cancel := contextWithTimeout(context.Background(), t, 10*time.Second)
t.Cleanup(cancel)

Expand Down Expand Up @@ -384,8 +380,6 @@ func TestEmptyData(t *testing.T) {
mc := runMockCollector(t)
t.Cleanup(func() { require.NoError(t, mc.stop()) })

<-time.After(5 * time.Millisecond)

ctx := context.Background()
exp := newGRPCExporter(t, ctx, mc.endpoint)
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
Expand Down
24 changes: 17 additions & 7 deletions exporters/otlp/otlptrace/otlptracegrpc/mock_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"net"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/otlptracetest"
Expand All @@ -38,6 +39,7 @@ func makeMockCollector(t *testing.T, mockConfig *mockConfig) *mockCollector {
errors: mockConfig.errors,
partial: mockConfig.partial,
},
stopped: make(chan struct{}),
}
}

Expand Down Expand Up @@ -105,6 +107,7 @@ type mockCollector struct {
endpoint string
stopFunc func()
stopOnce sync.Once
stopped chan struct{}
}

type mockConfig struct {
Expand All @@ -118,15 +121,15 @@ var _ collectortracepb.TraceServiceServer = (*mockTraceService)(nil)
var errAlreadyStopped = fmt.Errorf("already stopped")

func (mc *mockCollector) stop() error {
var err = errAlreadyStopped
err := errAlreadyStopped
mc.stopOnce.Do(func() {
err = nil
if mc.stopFunc != nil {
mc.stopFunc()
}
})
// Give it sometime to shutdown.
<-time.After(160 * time.Millisecond)
// Wait until gRPC server is down.
<-mc.stopped

// Getting the lock ensures the traceSvc is done flushing.
mc.traceSvc.mu.Lock()
Expand Down Expand Up @@ -157,28 +160,35 @@ func (mc *mockCollector) getHeaders() metadata.MD {

// runMockCollector is a helper function to create a mock Collector.
func runMockCollector(t *testing.T) *mockCollector {
t.Helper()
return runMockCollectorAtEndpoint(t, "localhost:0")
}

func runMockCollectorAtEndpoint(t *testing.T, endpoint string) *mockCollector {
t.Helper()
return runMockCollectorWithConfig(t, &mockConfig{endpoint: endpoint})
}

func runMockCollectorWithConfig(t *testing.T, mockConfig *mockConfig) *mockCollector {
t.Helper()
ln, err := net.Listen("tcp", mockConfig.endpoint)
if err != nil {
t.Fatalf("Failed to get an endpoint: %v", err)
}
require.NoError(t, err, "net.Listen")

srv := grpc.NewServer()
mc := makeMockCollector(t, mockConfig)
collectortracepb.RegisterTraceServiceServer(srv, mc.traceSvc)
go func() {
_ = srv.Serve(ln)
close(mc.stopped)
}()

mc.endpoint = ln.Addr().String()
mc.stopFunc = srv.Stop

// Wait until gRPC server is up.
conn, err := grpc.Dial(mc.endpoint, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "grpc.Dial")
require.NoError(t, conn.Close(), "conn.Close")

return mc
}