Files
zeekay 0ecfc65ce4 style: gofmt the four files the format gate was never reaching
Import order only, one line per file. These sat unformatted because fmt.yml
filtered on pushes to `main` while this repo's default branch is `master`, so
the check had nothing to look at on the branch every commit lands on. With the
filter corrected the gate ran and named them immediately.
2026-08-13 21:27:44 -07:00

124 lines
3.3 KiB
Go

package extras
import (
"log"
"net/http"
"os"
"testing"
"time"
insights "github.com/hanzoai/insights-go"
"github.com/orian/flakyhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestEOFScenarios test what happens when a response is not fully received.
func TestEOFScenarios(t *testing.T) {
tests := []struct {
name string
scenario flakyhttp.Scenario
partialBodySize int
disableRetries bool
expectFailure bool
}{
{
name: "HeadersOnlyClose_PartialBody",
scenario: flakyhttp.ScenarioHeadersOnlyClose,
partialBodySize: 10,
disableRetries: true,
expectFailure: false, // may succeed or fail depending on timing
},
{
name: "HeadersOnlyClose_NoBody",
scenario: flakyhttp.ScenarioHeadersOnlyClose,
disableRetries: true,
expectFailure: false, // may succeed or fail
},
{
name: "NoResponse",
scenario: flakyhttp.ScenarioNoResponse,
disableRetries: true,
expectFailure: true, // must fail - server sends nothing
},
{
name: "StatusOnly",
scenario: flakyhttp.ScenarioStatusOnly,
disableRetries: true,
expectFailure: true, // must fail - incomplete response
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
config := flakyhttp.Config{
Scenario: tc.scenario,
PartialBodySize: tc.partialBodySize,
}
server := flakyhttp.NewServer(config)
url, err := server.Start()
require.NoError(t, err, "Failed to start mock server")
defer server.Close()
t.Logf("Mock server started at %s", url)
callback := newTestCallback(t)
clientConfig := insights.Config{
Endpoint: url,
Transport: &timeoutTransport{
rt: http.DefaultTransport,
timeout: 1 * time.Second,
},
Interval: 50 * time.Millisecond,
BatchSize: 1,
Logger: insights.StdLogger(log.New(os.Stderr, "[insights] ", log.LstdFlags), true),
Callback: callback,
RetryAfter: func(i int) time.Duration { return time.Millisecond },
}
if tc.disableRetries {
clientConfig.MaxRetries = insights.Ptr[int](0)
}
client, err := insights.NewWithConfig("test-api-key", clientConfig)
require.NoError(t, err, "Failed to create Insights client")
err = client.Enqueue(insights.Capture{
DistinctId: "testuser",
Event: "test event",
Properties: map[string]interface{}{
"hello": "world",
},
})
require.NoError(t, err, "Failed to enqueue event")
t.Log("Event enqueued, waiting for callback...")
// Wait for callback
select {
case capturedErr := <-callback.failureChan:
t.Logf("Received error callback: %v", capturedErr)
case <-callback.successChan:
if tc.expectFailure {
require.Fail(t, "Expected failure but got success")
}
t.Log("Received success callback")
case <-time.After(2 * time.Second):
require.Fail(t, "Timeout waiting for callback")
}
client.Close()
success, failure := callback.GetCounts()
assert.Equal(t, 1, server.RequestCount(), "Expected at least 1 request to the server")
assert.Equal(t, 1, success+failure, "Expected at least 1 callback")
if tc.expectFailure {
assert.Equal(t, 1, failure, "Expected failure callback")
} else {
assert.Equal(t, 1, success, "Expected success callback")
}
})
}
}