96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package inbound
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
)
|
|
|
|
func TestRedisPresenceStoreTouchListAndRemove(t *testing.T) {
|
|
mr := miniredis.RunT(t)
|
|
store, err := NewRedisPresenceStore("redis://" + mr.Addr())
|
|
if err != nil {
|
|
t.Fatalf("new presence store: %v", err)
|
|
}
|
|
store.TTL = time.Minute
|
|
store.Prefix = "test:presence"
|
|
|
|
now := time.Now().UTC().Truncate(time.Second)
|
|
err = store.TouchAccount(context.Background(), DownstreamPresence{
|
|
Account: "100001",
|
|
SrcID: "10690000",
|
|
RemoteIP: "127.0.0.1",
|
|
GatewayInstanceID: "gateway-a",
|
|
State: "connected",
|
|
ConnectedAt: now,
|
|
UpdatedAt: now,
|
|
LastSubmitAt: now,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("touch account: %v", err)
|
|
}
|
|
|
|
accounts, err := store.ListAccounts(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("list accounts: %v", err)
|
|
}
|
|
if len(accounts) != 1 {
|
|
t.Fatalf("accounts len = %d, want 1", len(accounts))
|
|
}
|
|
if accounts[0].Account != "100001" || accounts[0].GatewayInstanceID != "gateway-a" || accounts[0].State != "connected" {
|
|
t.Fatalf("unexpected presence snapshot: %+v", accounts[0])
|
|
}
|
|
|
|
if err := store.RemoveAccount(context.Background(), "100001"); err != nil {
|
|
t.Fatalf("remove account: %v", err)
|
|
}
|
|
accounts, err = store.ListAccounts(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("list accounts after remove: %v", err)
|
|
}
|
|
if len(accounts) != 0 {
|
|
t.Fatalf("accounts len after remove = %d, want 0", len(accounts))
|
|
}
|
|
}
|
|
|
|
func TestListRecoveryCandidatesMergesPresenceAndInMemory(t *testing.T) {
|
|
resetDownstreamRegistry()
|
|
defer resetDownstreamRegistry()
|
|
|
|
store := &memoryPresenceStore{
|
|
snapshots: map[string]DownstreamPresence{
|
|
"100001": {
|
|
Account: "100001",
|
|
GatewayInstanceID: "gateway-a",
|
|
State: "connected",
|
|
UpdatedAt: time.Now().UTC().Add(-time.Minute),
|
|
},
|
|
},
|
|
}
|
|
|
|
downstreamRegistry.Lock()
|
|
downstreamRegistry.byAccount["100002"] = &downstreamSession{account: "100002"}
|
|
downstreamRegistry.Unlock()
|
|
|
|
candidates, err := ListRecoveryCandidates(context.Background(), store)
|
|
if err != nil {
|
|
t.Fatalf("list recovery candidates: %v", err)
|
|
}
|
|
if len(candidates) != 2 {
|
|
t.Fatalf("candidates len = %d, want 2", len(candidates))
|
|
}
|
|
|
|
accounts := map[string]DownstreamPresence{}
|
|
for _, item := range candidates {
|
|
accounts[item.Account] = item
|
|
}
|
|
if _, ok := accounts["100001"]; !ok {
|
|
t.Fatal("expected redis presence candidate 100001")
|
|
}
|
|
if snapshot, ok := accounts["100002"]; !ok || snapshot.State != "connected" {
|
|
t.Fatalf("expected in-memory candidate 100002 connected, got %+v", snapshot)
|
|
}
|
|
}
|