open-nomad/nomad/stream/ndjson_test.go
Drew Bailey a4a2975edf
Event Stream API/RPC (#8947)
This Commit adds an /v1/events/stream endpoint to stream events from.

The stream framer has been updated to include a SendFull method which
does not fragment the data between multiple frames. This essentially
treats the stream framer as a envelope to adhere to the stream framer
interface in the UI.

If the `encode` query parameter is omitted events will be streamed as
newline delimted JSON.
2020-10-14 12:44:36 -04:00

73 lines
1.3 KiB
Go

package stream
import (
"bytes"
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type testObj struct {
Name string `json:"name"`
}
func TestNDJson(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
out := make(chan *NDJson)
s := NewNDJsonStream(out, 1*time.Second)
s.Run(ctx)
require.NoError(t, s.Send(testObj{Name: "test"}))
out1 := <-out
var expected bytes.Buffer
expected.Write([]byte(`{"name":"test"}`))
expected.Write([]byte("\n"))
require.Equal(t, expected.Bytes(), out1.Data)
select {
case _ = <-out:
t.Fatalf("Did not expect another message")
case <-time.After(100 * time.Millisecond):
}
}
func TestNDJson_Send_After_Stop(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
out := make(chan *NDJson)
s := NewNDJsonStream(out, 1*time.Second)
s.Run(ctx)
// stop the stream
cancel()
time.Sleep(10 * time.Millisecond)
require.Error(t, s.Send(testObj{}))
}
func TestNDJson_HeartBeat(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
out := make(chan *NDJson)
s := NewNDJsonStream(out, 10*time.Millisecond)
s.Run(ctx)
heartbeat := <-out
require.Equal(t, NDJsonHeartbeat, heartbeat)
}