Skip to content

Commit

Permalink
test: use mock transport to avoid many hanging worker routines (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Aug 14, 2023
1 parent 41f45b5 commit 34ce12e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 12 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
pkgErrors "github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func TestNewClientAllowsEmptyDSN(t *testing.T) {
Expand Down Expand Up @@ -609,6 +610,7 @@ func TestSampleRate(t *testing.T) {
func BenchmarkProcessEvent(b *testing.B) {
c, err := NewClient(ClientOptions{
SampleRate: 0.25,
Transport: &TransportMock{},
})
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -708,8 +710,17 @@ func TestCustomMaxSpansProperty(t *testing.T) {
assertEqual(t, client.Options().MaxSpans, 2000)

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

assertEqual(t, properClient.Options().MaxSpans, 3000)
}

func TestClientSetsUpTransport(t *testing.T) {
client, _ := NewClient(ClientOptions{Dsn: testDsn})
require.IsType(t, &HTTPTransport{}, client.Transport)

client, _ = NewClient(ClientOptions{})
require.IsType(t, &noopTransport{}, client.Transport)
}
6 changes: 3 additions & 3 deletions hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const testDsn = "http://whatever@example.com/1337"

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

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

hub.WithScope(func(scope *Scope) {
newClient, _ := NewClient(ClientOptions{Dsn: testDsn})
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
hub.BindClient(newClient)
if hub.stackTop().client != newClient {
t.Error("should use newly bound client")
Expand Down
3 changes: 3 additions & 0 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ type testContextKey struct{}
type testContextValue struct{}

func NewTestContext(options ClientOptions) context.Context {
if options.Transport == nil {
options.Transport = &TransportMock{}
}
client, err := NewClient(options)
if err != nil {
panic(err)
Expand Down

0 comments on commit 34ce12e

Please sign in to comment.