commit b5ef18803f08364db15ba66b0189371d5e79d821 Author: Shelikhoo xiaokangwang@outlook.com Date: Fri Nov 26 16:24:21 2021 +0000
Add Event Bus Test --- common/event/bus_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
diff --git a/common/event/bus_test.go b/common/event/bus_test.go new file mode 100644 index 0000000..df40d3d --- /dev/null +++ b/common/event/bus_test.go @@ -0,0 +1,32 @@ +package event + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +type stubReceiver struct { + counter int +} + +func (s *stubReceiver) OnNewSnowflakeEvent(event SnowflakeEvent) { + s.counter++ +} + +func TestBusDispatch(t *testing.T) { + EventBus := NewSnowflakeEventDispatcher() + StubReceiverA := &stubReceiver{} + StubReceiverB := &stubReceiver{} + EventBus.AddSnowflakeEventListener(StubReceiverA) + EventBus.AddSnowflakeEventListener(StubReceiverB) + assert.Equal(t, 0, StubReceiverA.counter) + assert.Equal(t, 0, StubReceiverB.counter) + EventBus.OnNewSnowflakeEvent(EventOnSnowflakeConnected{}) + assert.Equal(t, 1, StubReceiverA.counter) + assert.Equal(t, 1, StubReceiverB.counter) + EventBus.RemoveSnowflakeEventListener(StubReceiverB) + EventBus.OnNewSnowflakeEvent(EventOnSnowflakeConnected{}) + assert.Equal(t, 2, StubReceiverA.counter) + assert.Equal(t, 1, StubReceiverB.counter) + +}