open-nomad/nomad/stream/subscription_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

93 lines
2.2 KiB
Go

package stream
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSubscription(t *testing.T) {
}
func TestFilter_AllTopics(t *testing.T) {
events := make([]Event, 0, 5)
events = append(events, Event{Topic: "Test", Key: "One"}, Event{Topic: "Test", Key: "Two"})
req := &SubscribeRequest{
Topics: map[Topic][]string{
"*": []string{"*"},
},
}
actual := filter(req, events)
require.Equal(t, events, actual)
// ensure new array was not allocated
require.Equal(t, cap(actual), 5)
}
func TestFilter_AllKeys(t *testing.T) {
events := make([]Event, 0, 5)
events = append(events, Event{Topic: "Test", Key: "One"}, Event{Topic: "Test", Key: "Two"})
req := &SubscribeRequest{
Topics: map[Topic][]string{
"Test": []string{"*"},
},
}
actual := filter(req, events)
require.Equal(t, events, actual)
// ensure new array was not allocated
require.Equal(t, cap(actual), 5)
}
func TestFilter_PartialMatch_Topic(t *testing.T) {
events := make([]Event, 0, 5)
events = append(events, Event{Topic: "Test", Key: "One"}, Event{Topic: "Test", Key: "Two"}, Event{Topic: "Exclude", Key: "Two"})
req := &SubscribeRequest{
Topics: map[Topic][]string{
"Test": []string{"*"},
},
}
actual := filter(req, events)
expected := []Event{{Topic: "Test", Key: "One"}, {Topic: "Test", Key: "Two"}}
require.Equal(t, expected, actual)
require.Equal(t, cap(actual), 2)
}
func TestFilter_PartialMatch_Key(t *testing.T) {
events := make([]Event, 0, 5)
events = append(events, Event{Topic: "Test", Key: "One"}, Event{Topic: "Test", Key: "Two"})
req := &SubscribeRequest{
Topics: map[Topic][]string{
"Test": []string{"One"},
},
}
actual := filter(req, events)
expected := []Event{{Topic: "Test", Key: "One"}}
require.Equal(t, expected, actual)
require.Equal(t, cap(actual), 1)
}
func TestFilter_NoMatch(t *testing.T) {
events := make([]Event, 0, 5)
events = append(events, Event{Topic: "Test", Key: "One"}, Event{Topic: "Test", Key: "Two"})
req := &SubscribeRequest{
Topics: map[Topic][]string{
"NodeEvents": []string{"*"},
"Test": []string{"Highly-Specific-Key"},
},
}
actual := filter(req, events)
var expected []Event
require.Equal(t, expected, actual)
require.Equal(t, cap(actual), 0)
}