Files
zeekayandHanzo Dev 8ee8950230 Go 1.27.1, and the code it can say more simply
The module declares go 1.27.1 and the adapter image follows. go fix
applied the modernizers and left ptrString and ptrBool with no callers,
so they are gone; omitzero is not applied, since it changes what the
JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-16 19:52:31 -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 = new(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]any{
"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")
}
})
}
}