86 lines
3.2 KiB
Go
86 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/dispatcher"
|
|
)
|
|
|
|
func testAgentSession(expiresAt time.Time) dispatcher.AgentSession {
|
|
return dispatcher.AgentSession{
|
|
AgentID: "agent-1", CellID: "cell-1", BootID: "boot-1", DispatcherEpoch: "epoch-1",
|
|
SessionGeneration: 1, ExpiresAtUnixMs: expiresAt.UnixMilli(),
|
|
}
|
|
}
|
|
|
|
func TestMaintainAgentSessionRenewsBeforeExpiryAndReportsFailure(t *testing.T) {
|
|
initial := testAgentSession(time.Now().Add(3 * time.Second))
|
|
failure := errors.New("Agent unavailable")
|
|
calls := 0
|
|
err := maintainAgentSession(context.Background(), initial, 2*time.Second, func(_ context.Context, previous dispatcher.AgentSession) (dispatcher.AgentSession, error) {
|
|
calls++
|
|
if time.Now().UnixMilli() >= previous.ExpiresAtUnixMs {
|
|
t.Fatal("session expired before renewal attempt")
|
|
}
|
|
if calls == 1 {
|
|
if previous != initial {
|
|
t.Fatalf("first renewal changed identity: %+v", previous)
|
|
}
|
|
next := previous
|
|
next.SessionGeneration++
|
|
next.ExpiresAtUnixMs = time.Now().Add(3 * time.Second).UnixMilli()
|
|
return next, nil
|
|
}
|
|
return dispatcher.AgentSession{}, failure
|
|
})
|
|
if calls != 2 || !errors.Is(err, failure) {
|
|
t.Fatalf("session renewal did not repeat and report a definite failure: calls=%d err=%v", calls, err)
|
|
}
|
|
}
|
|
|
|
func TestMaintainAgentSessionFailsClosedWithoutAUsableNextSession(t *testing.T) {
|
|
for name, next := range map[string]func(dispatcher.AgentSession) dispatcher.AgentSession{
|
|
"stale generation": func(previous dispatcher.AgentSession) dispatcher.AgentSession { return previous },
|
|
"changed boot": func(previous dispatcher.AgentSession) dispatcher.AgentSession {
|
|
previous.BootID = "unknown-boot"
|
|
previous.SessionGeneration++
|
|
previous.ExpiresAtUnixMs = time.Now().Add(time.Minute).UnixMilli()
|
|
return previous
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
initial := testAgentSession(time.Now().Add(2 * time.Second))
|
|
calls := 0
|
|
err := maintainAgentSession(context.Background(), initial, time.Second, func(_ context.Context, previous dispatcher.AgentSession) (dispatcher.AgentSession, error) {
|
|
calls++
|
|
return next(previous), nil
|
|
})
|
|
if err == nil || calls != 1 {
|
|
t.Fatalf("invalid renewal advanced admission or looped: calls=%d err=%v", calls, err)
|
|
}
|
|
})
|
|
}
|
|
called := false
|
|
if err := maintainAgentSession(context.Background(), testAgentSession(time.Now().Add(-time.Second)), time.Second, func(context.Context, dispatcher.AgentSession) (dispatcher.AgentSession, error) {
|
|
called = true
|
|
return dispatcher.AgentSession{}, nil
|
|
}); err == nil || !strings.Contains(err.Error(), "expired") || called {
|
|
t.Fatalf("an expired session was silently renewed: err=%v called=%t", err, called)
|
|
}
|
|
}
|
|
|
|
func TestMaintainAgentSessionStopsOnShutdown(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if err := maintainAgentSession(ctx, testAgentSession(time.Now().Add(10*time.Minute)), 5*time.Minute, func(context.Context, dispatcher.AgentSession) (dispatcher.AgentSession, error) {
|
|
t.Fatal("shutdown started another activation")
|
|
return dispatcher.AgentSession{}, nil
|
|
}); err != nil {
|
|
t.Fatalf("normal shutdown reported an activation failure: %v", err)
|
|
}
|
|
}
|