mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 05:04:31 +00:00
142 lines
5.4 KiB
Go
142 lines
5.4 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package db
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/polling"
|
|
)
|
|
|
|
func TestPollingObserverTargetsPostgres(t *testing.T) {
|
|
// Observers report their own telemetry: no request, no election and no owners entry polls one.
|
|
w := newReqWorld(t, "")
|
|
ctx, s, pool := w.ctx, w.s, w.pool
|
|
alice, bob, carol := newUser(t, ctx, s, "alice"), newUser(t, ctx, s, "bob"), newUser(t, ctx, s, "carol")
|
|
o := newReqDevice(1)
|
|
requested, local := byte(0xA1), byte(0xA2)
|
|
n1 := w.node(requested)
|
|
n2 := w.node(local)
|
|
heard := w.now.Add(-time.Hour)
|
|
for _, tg := range []byte{requested, local} {
|
|
pollHearAdvert(t, ctx, pool, pollKey(tg), pollObserver(t, ctx, s, o.pub), heard, 9)
|
|
}
|
|
w.claim(o, 2, reqNode(pollKey(local), false, "ok"))
|
|
r1, err := w.reqs.Create(ctx, alice.ID, n1, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := w.status(alice.ID, r1.ID); got.Status != polling.StatusActive {
|
|
t.Fatalf("before: %+v", got)
|
|
}
|
|
if e, ok := w.owners(o, requested); !ok || e.Role != "polling" || !e.Requested {
|
|
t.Fatalf("requested before: %+v", e)
|
|
}
|
|
if e, ok := w.owners(o, local); !ok || e.Role != "polling" {
|
|
t.Fatalf("local before: %+v", e)
|
|
}
|
|
|
|
// Both repeaters start sending observer status. A third stopped over a week ago and is polled as before.
|
|
statusAt := func(k byte, at time.Time) {
|
|
t.Helper()
|
|
pollObserver(t, ctx, s, pollKey(k))
|
|
if _, err := pool.Exec(ctx, `UPDATE observers SET last_status_at = $2 WHERE public_key = $1`, pollKey(k), at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
statusAt(requested, w.now.Add(-time.Hour))
|
|
statusAt(local, w.now)
|
|
stale := byte(0xA3)
|
|
n3 := w.node(stale)
|
|
pollHearAdvert(t, ctx, pool, pollKey(stale), pollObserver(t, ctx, s, o.pub), heard, 9)
|
|
statusAt(stale, w.now.Add(-polling.ObserverActiveWindow-time.Hour))
|
|
r3, err := w.reqs.Create(ctx, bob.ID, n3, nil)
|
|
if err != nil {
|
|
t.Fatalf("create on a stale observer: %v", err)
|
|
}
|
|
if got := w.status(bob.ID, r3.ID); got.Status != polling.StatusActive {
|
|
t.Fatalf("stale observer not elected: %+v", got)
|
|
}
|
|
// A key with an observer row that never sent status is not an observer either.
|
|
pollObserver(t, ctx, s, pollKey(0xA4))
|
|
if _, err := w.reqs.Create(ctx, alice.ID, w.node(0xA4), nil); err != nil {
|
|
t.Fatalf("create on a key that never sent status: %v", err)
|
|
}
|
|
|
|
if _, err := w.reqs.Create(ctx, bob.ID, n1, nil); !errors.Is(err, polling.ErrObserverNode) {
|
|
t.Fatalf("create on an observer: %v", err)
|
|
}
|
|
if _, err := w.reqs.Create(ctx, carol.ID, n2, nil); !errors.Is(err, polling.ErrObserverNode) {
|
|
t.Fatalf("create on a local observer: %v", err)
|
|
}
|
|
|
|
// The election clears both, so the owners answer no longer has the device poll either.
|
|
if err := w.svc.ElectAll(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var n int
|
|
observerRows := `SELECT count(*) FROM polling_assignments WHERE target_public_key = ANY($1)`
|
|
both := [][]byte{pollKey(requested), pollKey(local)}
|
|
if err := pool.QueryRow(ctx, observerRows, both).Scan(&n); err != nil || n != 0 {
|
|
t.Fatalf("assignments left: %d %v", n, err)
|
|
}
|
|
if e, ok := w.owners(o, stale); !ok || e.Role != "polling" {
|
|
t.Fatalf("stale observer not polled: %+v %v", e, ok)
|
|
}
|
|
if e, ok := w.owners(o, requested); ok {
|
|
t.Fatalf("requested observer still handed out: %+v", e)
|
|
}
|
|
if e, ok := w.owners(o, local); !ok || e.Role != "excluded" {
|
|
t.Fatalf("claimed observer not excluded: %+v %v", e, ok)
|
|
}
|
|
// A new claim re-elects through HandleClaim and still assigns nothing.
|
|
w.now = w.now.Add(time.Minute)
|
|
w.claim(o, 2, reqNode(pollKey(local), false, "ok"))
|
|
if err := pool.QueryRow(ctx, observerRows, both).Scan(&n); err != nil || n != 0 {
|
|
t.Fatalf("assignments after a claim: %d %v", n, err)
|
|
}
|
|
targets, err := s.ListPollingTargets(ctx, w.now.Add(-polling.ClaimTTL))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tg := range targets {
|
|
if bytes.Equal(tg, pollKey(local)) {
|
|
t.Fatal("a claimed observer is still a polling target")
|
|
}
|
|
}
|
|
|
|
// The sweep retires the request with the observer reason, once.
|
|
if n, err := s.DisableObserverRequests(ctx, w.now, polling.ReasonObserver); err != nil || n != 1 {
|
|
t.Fatalf("disabled %d %v", n, err)
|
|
}
|
|
got := w.status(alice.ID, r1.ID)
|
|
if code, _ := polling.ReasonCode(got.Reason); got.Status != polling.StatusDisabled || code != polling.CodeObserver {
|
|
t.Fatalf("after the sweep: %+v", got)
|
|
}
|
|
if n, _ := s.DisableObserverRequests(ctx, w.now, polling.ReasonObserver); n != 0 {
|
|
t.Fatalf("disabled again: %d", n)
|
|
}
|
|
if _, err := w.reqs.Update(ctx, alice.ID, r1.ID, polling.RequestUpdate{Enable: true}); !errors.Is(err, polling.ErrObserverNode) {
|
|
t.Fatalf("enable on an observer: %v", err)
|
|
}
|
|
if got := w.status(alice.ID, r1.ID); got.Status != polling.StatusDisabled {
|
|
t.Fatalf("refused enable changed it: %+v", got)
|
|
}
|
|
if got := w.status(bob.ID, r3.ID); got.Status != polling.StatusActive {
|
|
t.Fatalf("the sweep touched a stale observer: %+v", got)
|
|
}
|
|
|
|
// Once its status is a week old it is polled again: the user re-enables the request by hand.
|
|
statusAt(requested, w.now.Add(-polling.ObserverActiveWindow-time.Minute))
|
|
if _, err := w.reqs.Update(ctx, alice.ID, r1.ID, polling.RequestUpdate{Enable: true}); err != nil {
|
|
t.Fatalf("enable after the observer went quiet: %v", err)
|
|
}
|
|
if got := w.status(alice.ID, r1.ID); got.Status != polling.StatusActive {
|
|
t.Fatalf("not elected after the observer went quiet: %+v", got)
|
|
}
|
|
}
|