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

Expose MockTransport, MockScope in root sentry package #972

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
24 changes: 12 additions & 12 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (
)

func TestNewClientAllowsEmptyDSN(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
client, err := NewClient(ClientOptions{
Transport: transport,
})
if err != nil {
t.Fatalf("expected no error when creating client without a DNS but got %v", err)
}

client.CaptureException(errors.New("custom error"), nil, &ScopeMock{})
client.CaptureException(errors.New("custom error"), nil, &MockScope{})
assertEqual(t, transport.lastEvent.Exception[0].Value, "custom error")
}

Expand All @@ -41,9 +41,9 @@ func (e customComplexError) AnswerToLife() string {
return "42"
}

func setupClientTest() (*Client, *ScopeMock, *TransportMock) {
scope := &ScopeMock{}
transport := &TransportMock{}
func setupClientTest() (*Client, *MockScope, *MockTransport) {
scope := &MockScope{}
transport := &MockTransport{}
client, _ := NewClient(ClientOptions{
Dsn: "http://whatever@example.com/1337",
Transport: transport,
Expand Down Expand Up @@ -533,7 +533,7 @@ func TestBeforeSendGetAccessToEventHint(t *testing.T) {
}

func TestBeforeSendTransactionCanDropTransaction(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Expand All @@ -559,7 +559,7 @@ func TestBeforeSendTransactionCanDropTransaction(t *testing.T) {
}

func TestBeforeSendTransactionIsCalled(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Expand Down Expand Up @@ -621,8 +621,8 @@ func TestIgnoreErrors(t *testing.T) {

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
scope := &ScopeMock{}
transport := &TransportMock{}
scope := &MockScope{}
transport := &MockTransport{}
client, err := NewClient(ClientOptions{
Transport: transport,
IgnoreErrors: tt.ignoreErrors,
Expand Down Expand Up @@ -676,7 +676,7 @@ func TestIgnoreTransactions(t *testing.T) {

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Expand Down Expand Up @@ -755,7 +755,7 @@ func TestSampleRate(t *testing.T) {
func BenchmarkProcessEvent(b *testing.B) {
c, err := NewClient(ClientOptions{
SampleRate: 0.25,
Transport: &TransportMock{},
Transport: &MockTransport{},
})
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -856,7 +856,7 @@ func TestCustomMaxSpansProperty(t *testing.T) {

properClient, _ := NewClient(ClientOptions{
MaxSpans: 3000,
Transport: &TransportMock{},
Transport: &MockTransport{},
})

assertEqual(t, properClient.Options().MaxSpans, 3000)
Expand Down
8 changes: 4 additions & 4 deletions hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const testDsn = "http://whatever@example.com/1337"

func setupHubTest() (*Hub, *Client, *Scope) {
client, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
client, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &MockTransport{}})
scope := NewScope()
hub := NewHub(client, scope)
return hub, client, scope
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestPopScopeCannotLeaveStackEmpty(t *testing.T) {
func TestBindClient(t *testing.T) {
hub, client, _ := setupHubTest()
hub.PushScope()
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &MockTransport{}})
hub.BindClient(newClient)

if (*hub.stack)[0].client == (*hub.stack)[1].client {
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestWithScopeBindClient(t *testing.T) {
hub, client, _ := setupHubTest()

hub.WithScope(func(scope *Scope) {
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &MockTransport{}})
hub.BindClient(newClient)
if hub.stackTop().client != newClient {
t.Error("should use newly bound client")
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestConcurrentHubClone(t *testing.T) {
const goroutineCount = 3

hub, client, _ := setupHubTest()
transport := &TransportMock{}
transport := &MockTransport{}
client.Transport = transport

var wg sync.WaitGroup
Expand Down
4 changes: 2 additions & 2 deletions integrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func TestExtractModules(t *testing.T) {
}

func TestEnvironmentIntegrationDoesNotOverrideExistingContexts(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
client, err := NewClient(ClientOptions{
Transport: transport,
Integrations: func([]Integration) []Integration {
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestGlobalTagsIntegration(t *testing.T) {
defer os.Unsetenv("SENTRY_TAGS_bar")
defer os.Unsetenv("SENTRY_TAGS_baz")

transport := &TransportMock{}
transport := &MockTransport{}
client, err := NewClient(ClientOptions{
Transport: transport,
Tags: map[string]string{
Expand Down
47 changes: 47 additions & 0 deletions mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sentry

import (
"sync"
"time"
)

// MockScope implements [Scope] for use in tests.
type MockScope struct {
breadcrumb *Breadcrumb
shouldDropEvent bool
}

func (scope *MockScope) AddBreadcrumb(breadcrumb *Breadcrumb, _ int) {
scope.breadcrumb = breadcrumb

Check warning on line 15 in mocks.go

View check run for this annotation

Codecov / codecov/patch

mocks.go#L14-L15

Added lines #L14 - L15 were not covered by tests
}

func (scope *MockScope) ApplyToEvent(event *Event, _ *EventHint, _ *Client) *Event {
if scope.shouldDropEvent {
return nil
}
return event
}

// MockTransport implements [Transport] for use in tests.
type MockTransport struct {
mu sync.Mutex
events []*Event
lastEvent *Event
}

func (t *MockTransport) Configure(_ ClientOptions) {}
func (t *MockTransport) SendEvent(event *Event) {
t.mu.Lock()
defer t.mu.Unlock()
t.events = append(t.events, event)
t.lastEvent = event
}
func (t *MockTransport) Flush(_ time.Duration) bool {
return true
}
func (t *MockTransport) Events() []*Event {
t.mu.Lock()
defer t.mu.Unlock()
return t.events
}
func (t *MockTransport) Close() {}

Check warning on line 47 in mocks.go

View check run for this annotation

Codecov / codecov/patch

mocks.go#L47

Added line #L47 was not covered by tests
46 changes: 0 additions & 46 deletions mocks_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion otel/event_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLinkTraceContextToErrorEventSetsContext(t *testing.T) {
hub.Scope(),
)

transport := client.Transport.(*TransportMock)
transport := client.Transport.(*sentry.MockTransport)
events := transport.Events()
assertEqual(t, len(events), 1)
err := events[0]
Expand Down
36 changes: 0 additions & 36 deletions otel/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package sentryotel
import (
"encoding/hex"
"sort"
"sync"
"testing"
"time"

"github.com/getsentry/sentry-go"
"github.com/getsentry/sentry-go/internal/otel/baggage"
Expand Down Expand Up @@ -112,37 +110,3 @@ func otelSpanIDFromHex(s string) trace.SpanID {
}
return spanID
}

// FIXME(anton): TransportMock is copied from mocks_test.go
// I don't see an easy way right now to reuse this struct in "sentry" and
// "sentryotel" packages: it naturally depends on "sentry", but tests in "sentry"
// package also depend on it, so if we move it to a new package, we'll get an
// import cycle.
// Alternatively, it could be made public on "sentry" package, but it doesn't
// feel right.

type TransportMock struct {
mu sync.Mutex
events []*sentry.Event
lastEvent *sentry.Event
}

func (t *TransportMock) Configure(options sentry.ClientOptions) {}
func (t *TransportMock) SendEvent(event *sentry.Event) {
t.mu.Lock()
defer t.mu.Unlock()
t.events = append(t.events, event)
t.lastEvent = event
}
func (t *TransportMock) Flush(timeout time.Duration) bool {
return true
}
func (t *TransportMock) Events() []*sentry.Event {
t.mu.Lock()
defer t.mu.Unlock()
return t.events
}

func (t *TransportMock) Close() {}

//
6 changes: 3 additions & 3 deletions otel/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func emptyContextWithSentry() context.Context {
Release: "1.2.3",
EnableTracing: true,
TracesSampleRate: 1.0,
Transport: &TransportMock{},
Transport: &sentry.MockTransport{},
})
hub := sentry.NewHub(client, sentry.NewScope())
return sentry.SetHubOnContext(context.Background(), hub)
}

func getSentryTransportFromContext(ctx context.Context) *TransportMock {
func getSentryTransportFromContext(ctx context.Context) *sentry.MockTransport {
hub := sentry.GetHubFromContext(ctx)
transport, ok := hub.Client().Transport.(*TransportMock)
transport, ok := hub.Client().Transport.(*sentry.MockTransport)
if !ok {
log.Fatal(
"Cannot get mock transport from context",
Expand Down
14 changes: 7 additions & 7 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testMarshalJSONOmitEmptyParentSpanID(t *testing.T, v interface{}) {
}

func TestStartSpan(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
Transport: transport,
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestStartSpan(t *testing.T) {
}

func TestStartChild(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestStartChild(t *testing.T) {
}

func TestStartTransaction(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
Transport: transport,
Expand Down Expand Up @@ -398,7 +398,7 @@ type testContextValue struct{}

func NewTestContext(options ClientOptions) context.Context {
if options.Transport == nil {
options.Transport = &TransportMock{}
options.Transport = &MockTransport{}
}
client, err := NewClient(options)
if err != nil {
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestSpanFromContext(_ *testing.T) {
}

func TestDoubleSampling(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
// A SampleRate set to 0.0 will be transformed to 1.0,
// hence we're using math.SmallestNonzeroFloat64.
Expand Down Expand Up @@ -975,7 +975,7 @@ func TestAdjustingTransactionSourceBeforeSending(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
transport := &TransportMock{}
transport := &MockTransport{}
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func TestSpanFinishConcurrentlyWithoutRaces(_ *testing.T) {

func TestSpanScopeManagement(t *testing.T) {
// Initialize a test hub and client
transport := &TransportMock{}
transport := &MockTransport{}
client, err := NewClient(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Expand Down
Loading