feat(wshub): per-task event hub with non-blocking publish

This commit is contained in:
2026-07-01 17:59:43 +07:00
parent a54ec1d148
commit ec8835538b
2 changed files with 88 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
package wshub
import (
"testing"
"time"
)
func TestPublishReachesSubscriber(t *testing.T) {
h := New()
_, ch := h.Subscribe(7)
h.Publish(Event{Type: "progress", TaskID: 7, Data: map[string]int{"copied": 3}})
select {
case ev := <-ch:
if ev.Type != "progress" || ev.TaskID != 7 {
t.Fatalf("bad event %+v", ev)
}
case <-time.After(time.Second):
t.Fatal("no event received")
}
}
func TestPublishIsolatedByTask(t *testing.T) {
h := New()
_, ch := h.Subscribe(1)
h.Publish(Event{Type: "x", TaskID: 2})
select {
case <-ch:
t.Fatal("subscriber for task 1 must not get task 2 event")
case <-time.After(100 * time.Millisecond):
}
}