Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eventbus): Idempotent wildcardSub close #3045

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions p2p/host/eventbus/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,21 @@ type wildcardSub struct {
w *wildcardNode
metricsTracer MetricsTracer
name string
closeOnce sync.Once
}

func (w *wildcardSub) Out() <-chan interface{} {
return w.ch
}

func (w *wildcardSub) Close() error {
w.w.removeSink(w.ch)
if w.metricsTracer != nil {
w.metricsTracer.RemoveSubscriber(reflect.TypeOf(event.WildcardSubscription))
}
w.closeOnce.Do(func() {
w.w.removeSink(w.ch)
if w.metricsTracer != nil {
w.metricsTracer.RemoveSubscriber(reflect.TypeOf(event.WildcardSubscription))
}
})

return nil
}

Expand Down
15 changes: 11 additions & 4 deletions p2p/host/eventbus/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,13 @@ func TestManyWildcardSubscriptions(t *testing.T) {
require.NoError(t, em1.Emit(EventA{}))
require.NoError(t, em2.Emit(EventB(1)))

// the first five still have 2 events, while the other five have 4 events.
for _, s := range subs[:5] {
require.Len(t, s.Out(), 2)
}
// the first five 0 events because it was closed. The other five
// have 4 events.
require.EventuallyWithT(t, func(t *assert.CollectT) {
for _, s := range subs[:5] {
require.Len(t, s.Out(), 0, "expected closed subscription to have flushed events")
}
}, 2*time.Second, 100*time.Millisecond)

for _, s := range subs[5:] {
require.Len(t, s.Out(), 4)
Expand All @@ -407,6 +410,10 @@ func TestManyWildcardSubscriptions(t *testing.T) {
for _, s := range subs {
require.NoError(t, s.Close())
}

for _, s := range subs {
require.Zero(t, s.(*wildcardSub).w.nSinks.Load())
}
}

func TestWildcardValidations(t *testing.T) {
Expand Down
Loading