Skip to content
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
14 changes: 3 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,9 @@ func (client *Client) RecoverWithContext(
// call to Init.
func (client *Client) Flush(timeout time.Duration) bool {
if client.batchLogger != nil {
start := time.Now()
timeoutCh := make(chan struct{})
time.AfterFunc(timeout, func() {
close(timeoutCh)
})
client.batchLogger.Flush(timeoutCh)
// update the timeout with the time passed
timeout -= time.Since(start)
if timeout <= 0 {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return client.FlushWithContext(ctx)
}
return client.Transport.Flush(timeout)
}
Expand Down
16 changes: 14 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -871,8 +873,18 @@ func TestSDKIdentifier(t *testing.T) {
}

func TestClientSetsUpTransport(t *testing.T) {
client, _ := NewClient(ClientOptions{Dsn: testDsn})
require.IsType(t, &HTTPTransport{}, client.Transport)
client, _ := NewClient(ClientOptions{
Dsn: testDsn,
HTTPClient: &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return nil, fmt.Errorf("mock transport - no real connections")
},
},
},
Transport: &MockTransport{},
})
require.IsType(t, &MockTransport{}, client.Transport)
Copy link

Choose a reason for hiding this comment

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

Bug: Test Fails to Verify Real Transport Setup

The TestClientSetsUpTransport test no longer verifies that NewClient with a DSN sets up an HTTPTransport. It now uses setupClientTest(), which provides a MockTransport, causing the test to lose coverage of the actual client transport setup logic.

Fix in Cursor Fix in Web


client, _ = NewClient(ClientOptions{})
require.IsType(t, &noopTransport{}, client.Transport)
Expand Down
5 changes: 5 additions & 0 deletions log_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func testConcurrentLoggerSetAttributes(t *testing.T) {
client, _ := NewClient(ClientOptions{
Dsn: testDsn,
EnableLogs: true,
Transport: &MockTransport{},
})
hub := NewHub(client, NewScope())
ctx := SetHubOnContext(context.Background(), hub)
Expand Down Expand Up @@ -130,6 +131,7 @@ func testConcurrentLogEmission(_ *testing.T) {
client, _ := NewClient(ClientOptions{
Dsn: testDsn,
EnableLogs: true,
Transport: &MockTransport{},
})
hub := NewHub(client, NewScope())
ctx := SetHubOnContext(context.Background(), hub)
Expand Down Expand Up @@ -208,6 +210,7 @@ func testConcurrentLogEntryOperations(t *testing.T) {
client, _ := NewClient(ClientOptions{
Dsn: testDsn,
EnableLogs: true,
Transport: &MockTransport{},
})
hub := NewHub(client, NewScope())
ctx := SetHubOnContext(context.Background(), hub)
Expand Down Expand Up @@ -263,6 +266,7 @@ func testConcurrentLoggerCreationAndUsage(_ *testing.T) {
client, _ := NewClient(ClientOptions{
Dsn: testDsn,
EnableLogs: true,
Transport: &MockTransport{},
})
hub := NewHub(client, NewScope())

Expand Down Expand Up @@ -315,6 +319,7 @@ func testConcurrentLogWithSpanOperations(_ *testing.T) {
EnableLogs: true,
EnableTracing: true,
TracesSampleRate: 1.0,
Transport: &MockTransport{},
})
hub := NewHub(client, NewScope())
ctx := SetHubOnContext(context.Background(), hub)
Expand Down
8 changes: 3 additions & 5 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,9 @@ func (t *HTTPTransport) SendEventWithContext(ctx context.Context, event *Event)
// have the SDK send events over the network synchronously, configure it to use
// the HTTPSyncTransport in the call to Init.
func (t *HTTPTransport) Flush(timeout time.Duration) bool {
timeoutCh := make(chan struct{})
time.AfterFunc(timeout, func() {
close(timeoutCh)
})
return t.flushInternal(timeoutCh)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return t.FlushWithContext(ctx)
}

// FlushWithContext works like Flush, but it accepts a context.Context instead of a timeout.
Expand Down
23 changes: 18 additions & 5 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/http/httptrace"
Expand Down Expand Up @@ -789,19 +790,31 @@ func TestHTTPTransportDoesntLeakGoroutines(t *testing.T) {

transport := NewHTTPTransport()
transport.Configure(ClientOptions{
Dsn: "https://test@foobar/1",
HTTPClient: http.DefaultClient,
Dsn: "https://test@foobar/1",
HTTPClient: &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return nil, fmt.Errorf("mock transport - no real connections")
},
},
},
})

transport.Flush(0)
transport.Flush(testutils.FlushTimeout())
transport.Close()
}

func TestHTTPTransportClose(t *testing.T) {
transport := NewHTTPTransport()
transport.Configure(ClientOptions{
Dsn: "https://test@foobar/1",
HTTPClient: http.DefaultClient,
Dsn: "https://test@foobar/1",
HTTPClient: &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return nil, fmt.Errorf("mock transport - no real connections")
},
},
},
})

transport.Close()
Expand Down
Loading