Files
go-sip/internal/dispatcher/agent_inbound_session_test.go
T

57 lines
2.0 KiB
Go

package dispatcher
import (
"errors"
"testing"
"time"
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
"git.ipao.vip/rogee/go-sip/internal/store"
"google.golang.org/protobuf/proto"
)
type inboundSessionTestClient struct {
agentpb.AgentControlServiceClient
}
func TestInboundAgentFactRequiresCurrentUnexpiredActivatedSession(t *testing.T) {
now := time.Date(2026, 9, 21, 2, 0, 0, 0, time.UTC)
coordinator := NewAgentCoordinator(func() time.Time { return now })
if err := coordinator.Register("agent-a", &inboundSessionTestClient{}); err != nil {
t.Fatal(err)
}
coordinator.sessions["agent-a"] = AgentSession{
AgentID: "agent-a", CellID: "cell-a", BootID: "boot-a", DispatcherEpoch: "epoch-a",
SessionGeneration: 3, ExpiresAtUnixMs: now.Add(time.Minute).UnixMilli(),
}
meta := &agentpb.RequestMeta{
ProtocolVersion: "agent.v1", AgentId: "agent-a", CellId: "cell-a", BootId: "boot-a",
DispatcherEpoch: "epoch-a", SessionGeneration: 3,
}
if err := coordinator.AuthorizeInboundMeta(meta); err != nil {
t.Fatalf("active Agent was refused: %v", err)
}
for _, tc := range []struct {
name string
change func(*agentpb.RequestMeta)
}{
{"old boot", func(m *agentpb.RequestMeta) { m.BootId = "boot-old" }},
{"old generation", func(m *agentpb.RequestMeta) { m.SessionGeneration = 2 }},
{"other cell", func(m *agentpb.RequestMeta) { m.CellId = "cell-other" }},
{"other epoch", func(m *agentpb.RequestMeta) { m.DispatcherEpoch = "epoch-other" }},
{"unregistered Agent", func(m *agentpb.RequestMeta) { m.AgentId = "agent-other" }},
} {
t.Run(tc.name, func(t *testing.T) {
changed := proto.Clone(meta).(*agentpb.RequestMeta)
tc.change(changed)
if err := coordinator.AuthorizeInboundMeta(changed); !errors.Is(err, store.ErrCommandConflict) {
t.Fatalf("unbound Agent report passed: %v", err)
}
})
}
now = now.Add(time.Minute)
if err := coordinator.AuthorizeInboundMeta(meta); !errors.Is(err, store.ErrCommandConflict) {
t.Fatalf("expired Agent report passed: %v", err)
}
}