From 60df44df4f1ea6dd54eb74227b75ce53d7ae6988 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 28 Oct 2020 18:35:27 -0400 Subject: [PATCH] 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 --- agent/consul/stream/subscription.go | 2 +- agent/consul/stream/subscription_test.go | 56 ++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/agent/consul/stream/subscription.go b/agent/consul/stream/subscription.go index bcc76acef..472b0ce90 100644 --- a/agent/consul/stream/subscription.go +++ b/agent/consul/stream/subscription.go @@ -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 } diff --git a/agent/consul/stream/subscription_test.go b/agent/consul/stream/subscription_test.go index 458acd007..2c192b184 100644 --- a/agent/consul/stream/subscription_test.go +++ b/agent/consul/stream/subscription_test.go @@ -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) +}