Skip to content

Commit

Permalink
Add coverage to assert that callInspectorWithBatchBody is thread safe
Browse files Browse the repository at this point in the history
The new test can be executed using:
    go test -run TestAvoNetworkCallsHandler_callInspectorWithBatchBody -race -timeout 3s
  • Loading branch information
kovin committed Nov 20, 2023
1 parent 4d14dc0 commit 729b315
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions avoNetworkCallsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"reflect"
"strings"
"sync"
"testing"
)

Expand Down Expand Up @@ -93,6 +94,53 @@ func TestAvoNetworkCallsHandler_callInspectorWithBatchBody(t *testing.T) {
}
}

func TestAvoNetworkCallsHandler_callInspectorWithBatchBodyConcurrent(t *testing.T) {
// Setup a test server to mock the communication with Avo Inspector
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"samplingRate": 0.001}`))
}))
defer srv.Close()

// Create a single instance of AvoNetworkCallsHandler meant to be used concurrently
handler := &AvoNetworkCallsHandler{
apiKey: "test-api-key",
envName: "test",
appName: "test-app",
appVersion: "1.0.0",
libVersion: "1.0.0",
samplingRate: 1.0,
shouldLog: false,
trackingEndpoint: srv.URL,
}

// Define a function to be called concurrently to assert that the sampling rate can be read/written safely
call := func(wg *sync.WaitGroup) {
// Signal that we're done with this goroutine
defer wg.Done()

// Create a valid event
event := map[string]any{
"number_prop": 1,
"string_prop": "test",
"boolean_prop": true,
}

// Verify the result
err := handler.callInspectorWithBatchBody([]any{event})
if err != nil {
t.Errorf("unexpected error, got: %s", err)
}
}

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go call(&wg)
}
wg.Wait()
}

func TestAvoNetworkCallsHandler_bodyForSessionStartedCall(t *testing.T) {
// Create an instance of AvoNetworkCallsHandler
handler := &AvoNetworkCallsHandler{
Expand Down

0 comments on commit 729b315

Please sign in to comment.