Fixes events subscribe for non-root namespaces (#22580)

* Fixes events subscribe for non-root namespaces

* Adds a test
This commit is contained in:
Austin Gebauer 2023-08-28 09:17:33 -07:00 committed by GitHub
parent 4264c5a262
commit 36174bc913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 1 deletions

View File

@ -202,7 +202,7 @@ func NewEventBus(logger hclog.Logger) (*EventBus, error) {
}
func (bus *EventBus) Subscribe(ctx context.Context, ns *namespace.Namespace, pattern string) (<-chan *eventlogger.Event, context.CancelFunc, error) {
return bus.SubscribeMultipleNamespaces(ctx, []string{ns.Path}, pattern)
return bus.SubscribeMultipleNamespaces(ctx, []string{strings.Trim(ns.Path, "/")}, pattern)
}
func (bus *EventBus) SubscribeMultipleNamespaces(ctx context.Context, namespacePathPatterns []string, pattern string) (<-chan *eventlogger.Event, context.CancelFunc, error) {

View File

@ -73,6 +73,50 @@ func TestBusBasics(t *testing.T) {
}
}
// TestSubscribeNonRootNamespace verifies that events for non-root namespaces
// aren't filtered out by the bus.
func TestSubscribeNonRootNamespace(t *testing.T) {
bus, err := NewEventBus(nil)
if err != nil {
t.Fatal(err)
}
bus.Start()
ctx := context.Background()
eventType := logical.EventType("someType")
ns := &namespace.Namespace{
ID: "abc",
Path: "abc/",
}
ch, cancel, err := bus.Subscribe(ctx, ns, string(eventType))
if err != nil {
t.Fatal(err)
}
defer cancel()
event, err := logical.NewEvent()
if err != nil {
t.Fatal(err)
}
err = bus.SendEventInternal(ctx, ns, nil, eventType, event)
if err != nil {
t.Error(err)
}
timeout := time.After(1 * time.Second)
select {
case message := <-ch:
if message.Payload.(*logical.EventReceived).Event.Id != event.Id {
t.Errorf("Got unexpected message: %+v", message)
}
case <-timeout:
t.Error("Timeout waiting for message")
}
}
// TestNamespaceFiltering verifies that events for other namespaces are filtered out by the bus.
func TestNamespaceFiltering(t *testing.T) {
bus, err := NewEventBus(nil)