EventBus
EventBus copied to clipboard
Unsubscribe middle closure function
import (
"github.com/asaskevich/EventBus"
"github.com/stretchr/testify/require"
"testing"
)
func TestAsaskevichBus_UnsubscribeClosureMiddle(t *testing.T) {
callCount := 0
sum := 0
makeFn := func(a int) func() {
return func() {
callCount++
sum += a
}
}
const evName = "ev1"
evBus := EventBus.New()
f1 := makeFn(11)
f2 := makeFn(22)
f3 := makeFn(33)
require.NoError(t, evBus.Subscribe(evName, f1))
require.NoError(t, evBus.Subscribe(evName, f2))
require.NoError(t, evBus.Subscribe(evName, f3))
//
require.NoError(t, evBus.Unsubscribe(evName, f2))
//
evBus.Publish(evName)
require.Equal(t, callCount, 2)
require.Equal(t, sum, 11+33)
}
like #47