Files
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

116 lines
3.0 KiB
Go

package streamgo
import (
"context"
"testing"
)
type fakeCatalog struct{}
func (fakeCatalog) TopicVersion(string) (int, bool) { return 1, true }
func (fakeCatalog) ValidatePublish(string, Direction, []byte) error { return nil }
func (fakeCatalog) ValidateSubscription(string) error { return nil }
func TestClosedSessionRejectsFurtherEnqueueWithoutPanic(t *testing.T) {
t.Parallel()
service := NewService[string](fakeCatalog{}, nil, nil)
session := service.OpenSession()
service.CloseSession(session)
if session.enqueue(PublishedEvent[string]{Topic: "topic.x"}) {
t.Fatal("enqueue() = true, want false for a closed session")
}
}
func TestScopeRoutingDeliversAndFilters(t *testing.T) {
t.Parallel()
service := NewService[string](fakeCatalog{}, nil, nil)
scoped := service.OpenSession()
other := service.OpenSession()
wildcard := service.OpenSession()
if err := service.Subscribe(scoped, []string{"topic.x"}, "room-1"); err != nil {
t.Fatalf("subscribe scoped: %v", err)
}
if err := service.Subscribe(other, []string{"topic.x"}, "room-2"); err != nil {
t.Fatalf("subscribe other: %v", err)
}
if err := service.Subscribe(wildcard, []string{"topic.x"}, ""); err != nil {
t.Fatalf("subscribe wildcard: %v", err)
}
if err := service.PublishFromServerScoped(context.Background(), "topic.x", []byte(`{}`), "room-1"); err != nil {
t.Fatalf("publish: %v", err)
}
if got := receive(t, scoped); got.Scope != "room-1" {
t.Fatalf("scoped session scope = %q, want room-1", got.Scope)
}
if got := receive(t, wildcard); got.Scope != "room-1" {
t.Fatalf("wildcard session scope = %q, want room-1", got.Scope)
}
select {
case ev := <-other.Events():
t.Fatalf("other-scoped session unexpectedly received %#v", ev)
default:
}
}
func TestSessionQueueAbsorbsNormalBurstAndClosesOnOverflow(t *testing.T) {
t.Parallel()
service := NewService[string](fakeCatalog{}, nil, nil)
session := service.OpenSession()
if err := service.Subscribe(session, []string{"topic.x"}, "room-1"); err != nil {
t.Fatalf("subscribe: %v", err)
}
for index := range defaultSessionEventBufferSize {
if err := service.PublishFromServerScoped(
context.Background(),
"topic.x",
[]byte(`{}`),
"room-1",
); err != nil {
t.Fatalf("publish %d: %v", index, err)
}
}
if err := service.PublishFromServerScoped(
context.Background(),
"topic.x",
[]byte(`{}`),
"room-1",
); err != nil {
t.Fatalf("overflow publish: %v", err)
}
var received int
for range session.Events() {
received++
}
if received != defaultSessionEventBufferSize {
t.Fatalf(
"received buffered events = %d, want %d",
received,
defaultSessionEventBufferSize,
)
}
if session.enqueue(PublishedEvent[string]{Topic: "topic.x"}) {
t.Fatal("overflowed session accepted another event")
}
}
func receive(t *testing.T, session *Session[string]) PublishedEvent[string] {
t.Helper()
select {
case event := <-session.Events():
return event
default:
t.Fatal("event not received")
}
return PublishedEvent[string]{}
}