ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
Checkers and status page servers keep one WebSocket open to the API at /agent/v1/stream and send their requests over it. The API answers them through the agent API as if they had come over HTTP, each authenticated again, so the protocol stays the same and a revoked token ends the stream. While the stream is down agents fall back to plain HTTP, and an API without it is asked again every five minutes. Over the stream the API tells status page servers which of their pages changed, so they fetch them at once instead of on their next refresh: when a region of a monitor changes between up and down, an incident opens or resolves, or anything of the organization is edited. Changes are announced through Redis to every API instance and sent at most every five seconds per stream. A page that changes while it is fetched is fetched once more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
)
|
|
|
|
func TestAgentStream(t *testing.T) {
|
|
e := newEnv(t)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("st")+".test", uniq("st-"))
|
|
slug := uniq("stream-")
|
|
_, body := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Before"})
|
|
pageID := body["id"].(string)
|
|
owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{"slug": slug, "title": "Before", "published": true})
|
|
|
|
sp := e.statuspageClient(t, nil)
|
|
events := make(chan agentapi.PagesEvent, 16)
|
|
sp.UseStream(ctx, agentapi.StreamOptions{OnEvent: func(event string, body []byte) {
|
|
var ev agentapi.PagesEvent
|
|
if event == agentapi.EventPages && json.Unmarshal(body, &ev) == nil {
|
|
events <- ev
|
|
}
|
|
}})
|
|
waitFor(t, 10*time.Second, "stream to open", sp.Streaming)
|
|
|
|
snap, err := sp.Page(ctx, "", slug)
|
|
if err != nil || snap.Title != "Before" {
|
|
t.Fatalf("Page() over the stream = %q, %v", snap.Title, err)
|
|
}
|
|
if _, err := sp.PageIfChanged(ctx, "", slug, snap.Checksum()); !errors.Is(err, agentapi.ErrNotModified) {
|
|
t.Errorf("PageIfChanged(current checksum) over the stream error = %v, want not modified", err)
|
|
}
|
|
if _, err := sp.Page(ctx, "", uniq("missing-")); !errors.Is(err, agentapi.ErrNotFound) {
|
|
t.Errorf("Page(missing) over the stream error = %v, want not found", err)
|
|
}
|
|
|
|
// Editing the page tells the server at once.
|
|
owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{"slug": slug, "title": "After", "published": true})
|
|
deadline := time.After(10 * time.Second)
|
|
for told := false; !told; {
|
|
select {
|
|
case ev := <-events:
|
|
told = slices.ContainsFunc(ev.Pages, func(r agentapi.PageRef) bool { return r.Slug == slug })
|
|
case <-deadline:
|
|
t.Fatal("no event for the edited page")
|
|
}
|
|
}
|
|
if snap, err := sp.Page(ctx, "", slug); err != nil || snap.Title != "After" {
|
|
t.Errorf("Page() after the edit = %q, %v", snap.Title, err)
|
|
}
|
|
|
|
// A revoked token ends the stream.
|
|
hello, err := sp.Hello(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
|
_, err := tx.Exec(ctx, `DELETE FROM agents WHERE id = $1`, hello.AgentID)
|
|
return err
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := sp.Hello(ctx); !errors.Is(err, agentapi.ErrUnauthorized) {
|
|
t.Errorf("Hello() with a revoked token error = %v, want unauthorized", err)
|
|
}
|
|
waitFor(t, 10*time.Second, "stream to close", func() bool { return !sp.Streaming() })
|
|
}
|