mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [eventbus] let listeners change subscriptions in Publish (#416)
* fix(package): [eventbus] let listeners change subscriptions in Publish * test(package): [eventbus] cover reentrant and concurrent subscribe
This commit is contained in:
1 parent
6eb12896e3
commit
d859b91c57
2 files changed
+66
-6
No files matched your search
+11
-6
@@ -56,12 +56,16 @@ func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async
|
|||||||
listenersInterface, _ := eb.listeners.LoadOrStore(topic, []*EventListener[T]{})
|
listenersInterface, _ := eb.listeners.LoadOrStore(topic, []*EventListener[T]{})
|
||||||
listeners := listenersInterface.([]*EventListener[T])
|
listeners := listenersInterface.([]*EventListener[T])
|
||||||
|
|
||||||
listeners = append(listeners, el)
|
// Build a new slice instead of appending in place: Publish iterates over the
|
||||||
sort.Slice(listeners, func(i, j int) bool {
|
// stored slice after releasing the lock, so it must never be modified.
|
||||||
return listeners[i].priority > listeners[j].priority
|
updated := make([]*EventListener[T], len(listeners), len(listeners)+1)
|
||||||
|
copy(updated, listeners)
|
||||||
|
updated = append(updated, el)
|
||||||
|
sort.Slice(updated, func(i, j int) bool {
|
||||||
|
return updated[i].priority > updated[j].priority
|
||||||
})
|
})
|
||||||
|
|
||||||
eb.listeners.Store(topic, listeners)
|
eb.listeners.Store(topic, updated)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unsubscribe unsubscribes from an event with a specific event topic and listener function.
|
// Unsubscribe unsubscribes from an event with a specific event topic and listener function.
|
||||||
@@ -91,10 +95,11 @@ func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T)) {
|
|||||||
// Publish publishes an event with a specific event topic and data payload.
|
// Publish publishes an event with a specific event topic and data payload.
|
||||||
// Play: https://go.dev/play/p/gHTtVexFSH9
|
// Play: https://go.dev/play/p/gHTtVexFSH9
|
||||||
func (eb *EventBus[T]) Publish(event Event[T]) {
|
func (eb *EventBus[T]) Publish(event Event[T]) {
|
||||||
|
// The lock is only held to read the listeners, so that a listener can call
|
||||||
|
// Subscribe, Unsubscribe or Publish without deadlocking.
|
||||||
eb.mu.RLock()
|
eb.mu.RLock()
|
||||||
defer eb.mu.RUnlock()
|
|
||||||
|
|
||||||
listenersInterface, exists := eb.listeners.Load(event.Topic)
|
listenersInterface, exists := eb.listeners.Load(event.Topic)
|
||||||
|
eb.mu.RUnlock()
|
||||||
if !exists {
|
if !exists {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -220,3 +220,58 @@ func TestEventBus_GetEvents(t *testing.T) {
|
|||||||
assert.Equal(2, len(events))
|
assert.Equal(2, len(events))
|
||||||
assert.Equal([]string{"event1", "event2"}, events)
|
assert.Equal([]string{"event1", "event2"}, events)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEventBus_ListenerCanChangeSubscriptions(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestEventBus_ListenerCanChangeSubscriptions")
|
||||||
|
|
||||||
|
eb := NewEventBus[int]()
|
||||||
|
|
||||||
|
var self func(eventData int)
|
||||||
|
self = func(eventData int) {
|
||||||
|
eb.Unsubscribe("event1", self)
|
||||||
|
eb.Subscribe("event2", func(eventData int) {}, false, 0, nil)
|
||||||
|
}
|
||||||
|
eb.Subscribe("event1", self, false, 0, nil)
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Publish did not return when a listener changed the subscriptions")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(0, eb.GetListenersCount("event1"))
|
||||||
|
assert.Equal(1, eb.GetListenersCount("event2"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEventBus_PublishWhileSubscribing(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
eb := NewEventBus[int]()
|
||||||
|
eb.Subscribe("event1", func(eventData int) {}, false, 1, nil)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for i := 0; i < 200; i++ {
|
||||||
|
eb.Publish(Event[int]{Topic: "event1", Payload: i})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for i := 0; i < 200; i++ {
|
||||||
|
eb.Subscribe("event1", func(eventData int) {}, false, i%7, nil)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
Reference in new issue
Block a user