From fe8f7f128dd428a238f7e03cbc42fd75102c1551 Mon Sep 17 00:00:00 2001 From: Wessie Date: Fri, 21 Jun 2024 23:14:16 +0100 Subject: [PATCH] website/api/v1: fix flaky sse test So the SSE Client Library that we use has an arbitrary limit of each line being no longer than bufio.MaxScanTokenSize (which is 64*1024) and the error handling is dramatic enough that this error does not ever show its face to the user of said library. We fixed the test by pretty-printing our test json so that it is split across many lines. --- website/api/v1/sse_test.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/website/api/v1/sse_test.go b/website/api/v1/sse_test.go index 14d193fb..6512bae6 100644 --- a/website/api/v1/sse_test.go +++ b/website/api/v1/sse_test.go @@ -3,7 +3,9 @@ package v1 import ( "context" "encoding/json" + "io" "net" + "net/http" "net/http/httptest" "reflect" "sync" @@ -16,6 +18,7 @@ import ( "github.com/leanovate/gopter" "github.com/leanovate/gopter/arbitrary" "github.com/leanovate/gopter/gen" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -23,9 +26,12 @@ import ( ) func TestStream(t *testing.T) { + ctx := context.Background() + ctx = zerolog.New(zerolog.NewTestWriter(t)).WithContext(ctx) + exec := &mocks.ExecutorMock{ ExecuteAllFunc: func(input templates.TemplateSelectable) (map[string][]byte, error) { - jsonData, err := json.Marshal(input) + jsonData, err := json.MarshalIndent(input, "", " ") if err != nil { return nil, err } @@ -34,6 +40,15 @@ func TestStream(t *testing.T) { "json": jsonData, }, nil }, + ExecuteFunc: func(w io.Writer, r *http.Request, input templates.TemplateSelectable) error { + jsonData, err := json.MarshalIndent(input, "", " ") + if err != nil { + return err + } + + _, err = w.Write(jsonData) + return err + }, } var muExpected sync.Mutex @@ -41,6 +56,7 @@ func TestStream(t *testing.T) { stream := NewStream(exec) server := httptest.NewUnstartedServer(stream) + server.Config.BaseContext = func(l net.Listener) context.Context { return ctx } server.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context { return templates.SetTheme(ctx, "json", true) } @@ -49,6 +65,7 @@ func TestStream(t *testing.T) { t.Log(server.URL) es1, err := eventsource.New(server.URL) require.NoError(t, err) + defer es1.Close() sync := make(chan struct{}) go func() { @@ -80,12 +97,12 @@ func TestStream(t *testing.T) { jsonEvent := <-ch1 - assert.Equal(t, EventTime, jsonEvent.Name) + require.Equal(t, EventTime, jsonEvent.Name) for i := 0; i < 100; i++ { sync <- struct{}{} jsonEvent = <-ch1 - assert.Equal(t, EventMetadata, jsonEvent.Name) + require.Equal(t, EventMetadata, jsonEvent.Name) var status radio.Status