Files
archie/internal/webui/event_pump_test.go
T
sam 13d32f34b0 fix(webui): retry event pump priming until the state store answers
archie-ui binds before archie-state-store is guaranteed to be listening, so
the first prime is routinely refused. PumpEvents returned on that error, which
left the activity feed history-only for the life of the process. Prime now
retries with capped backoff and gives up only when the context ends.

Orders archie-ui.service after archie-state-store.service as well, which
narrows the window without closing it: After= sequences the start, it does
not wait for the listener.
2026-09-18 23:59:40 +10:00

220 lines
6.9 KiB
Go

package webui
import (
"bufio"
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/events"
)
// newTestPump builds a pump over the remote State Store contract, which is
// the composition the UI process runs: the pump's only view of an event is
// the one that came back over the wire.
func newTestPump(t *testing.T, srv *Server) *eventPump {
t.Helper()
return srv.newEventPump()
}
// Priming is what keeps the pump from replaying the whole events table into
// every connected browser on startup. History is a client's own catch-up
// (sseStream.catchUp), not the pump's job.
func TestEventPumpPrimesPastHistoryThenDeliversNewEvents(t *testing.T) {
srv := newRemoteTestServer(t)
for _, detail := range []string{"first", "second"} {
if _, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_created", Detail: detail}); err != nil {
t.Fatalf("seed history: %v", err)
}
}
pump := newTestPump(t, srv)
if err := pump.prime(t.Context()); err != nil {
t.Fatalf("prime: %v", err)
}
delivered, err := pump.deliver(t.Context())
if err != nil {
t.Fatalf("deliver after prime: %v", err)
}
if delivered != 0 {
t.Fatalf("pump delivered %d pre-existing events; priming must advance past history", delivered)
}
id, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_started", Detail: "after the pump"})
if err != nil {
t.Fatalf("insert live event: %v", err)
}
delivered, err = pump.deliver(t.Context())
if err != nil {
t.Fatalf("deliver live event: %v", err)
}
if delivered != 1 {
t.Fatalf("pump delivered %d events, want the 1 inserted after priming", delivered)
}
if pump.watermark != id {
t.Fatalf("watermark = %d, want %d: the pump must not refetch a delivered event", pump.watermark, id)
}
}
// A store failure must not kill the pump: the next tick retries from the
// same watermark, so a State Store restart costs latency, not the feed.
func TestEventPumpKeepsItsWatermarkAcrossAStoreFailure(t *testing.T) {
srv := newRemoteTestServer(t)
pump := newTestPump(t, srv)
if err := pump.prime(t.Context()); err != nil {
t.Fatalf("prime: %v", err)
}
before := pump.watermark
cancelled, cancel := context.WithCancel(t.Context())
cancel()
if _, err := pump.deliver(cancelled); err == nil {
t.Fatal("deliver against a cancelled context returned no error")
}
if pump.watermark != before {
t.Fatalf("watermark moved to %d on a failed fetch, want %d", pump.watermark, before)
}
}
// The bead's headline claim: an event written to the State Store by another
// process reaches a browser hanging on /events, with no in-process bus.
func TestEventPumpReachesALiveSSEClient(t *testing.T) {
srv := newRemoteTestServer(t)
pump := newTestPump(t, srv)
if err := pump.prime(t.Context()); err != nil {
t.Fatalf("prime: %v", err)
}
// handleSSE writes no response headers until its first send, so the
// client blocks on Do() until something is on the wire. Seed one event
// for catch-up to flush, then prove the pump delivers what comes after.
if _, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_created", Detail: "flushes the headers"}); err != nil {
t.Fatalf("seed catch-up event: %v", err)
}
ts := newSSETestServer(t, srv)
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
lines := openSSEStream(t, ctx, ts)
go pump.run(ctx, 5*time.Millisecond)
if _, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_started", Detail: "over the wire"}); err != nil {
t.Fatalf("insert live event: %v", err)
}
deadline := time.After(5 * time.Second)
for {
select {
case line := <-lines:
if strings.Contains(line, "over the wire") {
return
}
case <-deadline:
t.Fatal("live event never reached the SSE client; the pump is the only delivery path in the UI process")
}
}
}
// newSSETestServer serves the dashboard's real route set, so /events goes
// through requireToken and the mux exactly as a browser reaches it.
func newSSETestServer(t *testing.T, srv *Server) *httptest.Server {
t.Helper()
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
return ts
}
// openSSEStream connects to /events and relays each response line on a
// channel. The request context ends the handler, which is what lets the
// test server close.
func openSSEStream(t *testing.T, ctx context.Context, ts *httptest.Server) <-chan string {
t.Helper()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil)
if err != nil {
t.Fatalf("sse request: %v", err)
}
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("GET /events: %v", err)
}
// Closed at cleanup rather than by the reader goroutine: the body is the
// live stream and must outlive this function, but it still has exactly
// one owner and one close.
t.Cleanup(func() { _ = resp.Body.Close() })
if resp.StatusCode != http.StatusOK {
t.Fatalf("GET /events = %d, want 200", resp.StatusCode)
}
lines := make(chan string, 64)
go func() {
defer close(lines)
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
select {
case lines <- scanner.Text():
case <-ctx.Done():
return
}
}
}()
return lines
}
// flakyEventReader fails its first failures calls, then answers an empty
// table. It stands in for the State Store during the window where the UI
// process is up and the store has not bound its listener yet.
type flakyEventReader struct {
failures int
calls int
}
func (f *flakyEventReader) EventsSince(context.Context, int64, int) ([]events.Event, error) {
f.calls++
if f.calls <= f.failures {
return nil, errors.New("connection refused")
}
return nil, nil
}
// archie-ui binds before archie-state-store is guaranteed to be listening, so
// the first prime is routinely refused. Returning on that error left the
// activity feed history-only until the process was restarted by hand.
func TestPumpPrimingRetriesUntilTheStoreAnswers(t *testing.T) {
reader := &flakyEventReader{failures: 3}
pump := &eventPump{
store: reader,
log: func(string, ...any) {},
broadcast: func(events.Event) {},
primeRetryMin: time.Millisecond,
}
if err := pump.primeWithRetry(t.Context()); err != nil {
t.Fatalf("primeWithRetry: %v", err)
}
if reader.calls != 4 {
t.Fatalf("EventsSince called %d times, want 4 (3 refusals then success)", reader.calls)
}
}
// A shutdown during the retry wait must end the pump, not hold the process
// open until the store appears.
func TestPumpPrimingStopsRetryingWhenTheContextEnds(t *testing.T) {
reader := &flakyEventReader{failures: 1 << 30}
pump := &eventPump{
store: reader,
log: func(string, ...any) {},
broadcast: func(events.Event) {},
primeRetryMin: time.Millisecond,
}
ctx, cancel := context.WithTimeout(t.Context(), 20*time.Millisecond)
defer cancel()
if err := pump.primeWithRetry(ctx); err == nil {
t.Fatal("primeWithRetry returned no error after the context ended")
}
}