Skip to content

Commit

Permalink
website/api/v1: fix flaky sse test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Wessie committed Jun 21, 2024
1 parent 6e044b4 commit fe8f7f1
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions website/api/v1/sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package v1
import (
"context"
"encoding/json"
"io"
"net"
"net/http"
"net/http/httptest"
"reflect"
"sync"
Expand All @@ -16,16 +18,20 @@ 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"

"github.com/alevinval/sse/pkg/eventsource"
)

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
}
Expand All @@ -34,13 +40,23 @@ 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
var expected radio.Status

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)
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit fe8f7f1

Please sign in to comment.