diff options
| author | 2026-01-24 17:46:54 +0300 | |
|---|---|---|
| committer | 2026-01-24 17:46:54 +0300 | |
| commit | 95fc6de88975781abfe576977c960197a9743442 (patch) | |
| tree | 00ed76b0ff22f71af4b450c604f16ada72ed5241 /bus_test.go | |
| download | eventbus-1.0.0.tar.gz eventbus-1.0.0.tar.bz2 eventbus-1.0.0.tar.xz eventbus-1.0.0.zip | |
v1.0.0v1.0.0
Diffstat (limited to 'bus_test.go')
| -rw-r--r-- | bus_test.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/bus_test.go b/bus_test.go new file mode 100644 index 0000000..d7d5a08 --- /dev/null +++ b/bus_test.go @@ -0,0 +1,88 @@ +package eventbus_test + +import ( + "testing" + "time" + + "go.neonxp.ru/eventbus" +) + +type testEvent struct { + name string +} + +func (e testEvent) Event() string { + return e.name +} + +func TestBusFire(t *testing.T) { + bus := eventbus.New() + + // Подписываемся на событие + ch := bus.Subscribe("test.event") + defer bus.Unsubscribe(ch) + + // Создаем канал для проверки получения события + received := make(chan bool, 1) + + // Запускаем горутину для прослушивания канала + go func() { + select { + case ev := <-ch: + if ev.Event() == "test.event" { + received <- true + } + case <-time.After(100 * time.Millisecond): + received <- false + } + }() + + // Отправляем событие + bus.Fire(testEvent{name: "test.event"}) + + // Проверяем получение события + select { + case got := <-received: + if !got { + t.Error("Event was not received") + } + case <-time.After(200 * time.Millisecond): + t.Error("Timeout waiting for event") + } +} + +func TestBusFireWithWildcard(t *testing.T) { + bus := eventbus.New(eventbus.NameSeparator("/"), eventbus.Wildcard("#"), eventbus.Capacity(32)) + defer bus.Close() + + // Подписываемся на wildcard событие + ch := bus.Subscribe("/test/#") + + // Создаем канал для проверки получения события + received := make(chan bool, 1) + + // Запускаем горутину для прослушивания канала + go func() { + select { + case ev := <-ch: + if ev.Event() == "/test/event" { + received <- true + } + case <-time.After(100 * time.Millisecond): + received <- false + } + }() + + // Отправляем событие + bus.Fire(testEvent{name: "/test/event"}) + + // Проверяем получение события + select { + case got := <-received: + if !got { + t.Error("Event was not received") + } + case <-time.After(200 * time.Millisecond): + t.Error("Timeout waiting for event") + } +} |
