stream: Add tests for filterByKey with namespace

And fix a bug where a request with a Namespace but no Key would not be properly filtered
This commit is contained in:
Daniel Nephin 2020-10-28 18:35:27 -04:00
parent 318dfbe6e4
commit 60df44df4f
2 changed files with 54 additions and 4 deletions

View File

@ -134,7 +134,7 @@ func newEventFromBatch(req SubscribeRequest, events []Event) Event {
func filterByKey(req SubscribeRequest, events []Event) (Event, bool) {
event := newEventFromBatch(req, events)
if req.Key == "" {
if req.Key == "" && req.Namespace == "" {
return event, true
}

View File

@ -198,7 +198,21 @@ func TestFilterByKey(t *testing.T) {
newSimpleEvent("Same", 103)}},
expectedCap: 5,
},
// TODO: all events match, no key
{
name: "all events match, no key",
req: SubscribeRequest{Topic: testTopic, Namespace: "apps"},
events: []Event{
newNSEvent("Something", "apps"),
newNSEvent("Other", "apps")},
expectEvent: true,
expected: Event{
Topic: testTopic,
Index: 22,
Payload: PayloadEvents{
newNSEvent("Something", "apps"),
newNSEvent("Other", "apps")}},
expectedCap: 5,
},
{
name: "some evens match, no namespace",
req: SubscribeRequest{Topic: testTopic, Key: "Same"},
@ -215,7 +229,22 @@ func TestFilterByKey(t *testing.T) {
newSimpleEvent("Same", 104)}},
expectedCap: 2,
},
// TODO: some events match, no key
{
name: "some events match, no key",
req: SubscribeRequest{Topic: testTopic, Namespace: "apps"},
events: []Event{
newNSEvent("app1", "apps"),
newNSEvent("db1", "dbs"),
newNSEvent("app2", "apps")},
expectEvent: true,
expected: Event{
Topic: testTopic,
Index: 22,
Payload: PayloadEvents{
newNSEvent("app1", "apps"),
newNSEvent("app2", "apps")}},
expectedCap: 2,
},
{
name: "no events match key",
req: SubscribeRequest{Topic: testTopic, Key: "Other"},
@ -223,7 +252,14 @@ func TestFilterByKey(t *testing.T) {
newSimpleEvent("Same", 0),
newSimpleEvent("Same", 0)},
},
// TODO: no events match namespace
{
name: "no events match namespace",
req: SubscribeRequest{Topic: testTopic, Namespace: "apps"},
events: []Event{
newNSEvent("app1", "group1"),
newNSEvent("app2", "group2")},
expectEvent: false,
},
}
for _, tc := range testCases {
@ -232,3 +268,17 @@ func TestFilterByKey(t *testing.T) {
})
}
}
func newNSEvent(key, namespace string) Event {
return Event{Index: 22, Payload: nsPayload{key: key, namespace: namespace}}
}
type nsPayload struct {
key string
namespace string
value string
}
func (p nsPayload) FilterByKey(key, namespace string) bool {
return (key == "" || key == p.key) && (namespace == "" || namespace == p.namespace)
}