feat(19.3-04): Journal purges through the framework's cooler.SiteCachePurger

- classes.SiteCachePurger, NewSiteCachePurger, PurgeSiteCacheAfterCommit and
  the key and limit constants stay as deprecated aliases for one release
- wireSiteCache: framework keys site_cache.* win (Journal keys ignored with
  a warning); with only golem15.journal.site_cache.* set it publishes the
  purger from them with a deprecation warning, so menu and FAQ saves purge
  through it too
- controllers take the purger from cooler.SiteCacheFromApp
- TestSiteCacheFrameworkKeysWin; README documents both key sets
This commit is contained in:
Jakub Zych
2026-10-15 18:34:27 +02:00
parent 31d901cf25
commit d7943fb3f1
6 changed files with 131 additions and 271 deletions

View File

@@ -8,11 +8,9 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
)
const (
@@ -164,6 +162,9 @@ func assertNoToken(t *testing.T, logs *syncBuffer) {
}
}
// TestSiteCachePurgeRequest checks that a purger built from the Journal's
// deprecated keys sends the framework's contract request; cooler's own tests
// cover failures, timeouts and cancellation.
func TestSiteCachePurgeRequest(t *testing.T) {
t.Run("sends the contract request and logs the purged count", func(t *testing.T) {
logs := captureLogs(t)
@@ -187,7 +188,7 @@ func TestSiteCachePurgeRequest(t *testing.T) {
t.Fatalf("body = %#v", h.body)
}
lines := logs.lines()
if len(lines) != 1 || !strings.Contains(lines[0], "level=INFO") || !strings.Contains(lines[0], `msg="journal: site cache purged"`) ||
if len(lines) != 1 || !strings.Contains(lines[0], "level=INFO") || !strings.Contains(lines[0], `msg="cooler: site cache purged"`) ||
!strings.Contains(lines[0], "kind=category") || !strings.Contains(lines[0], "slug=cordyceps") ||
!strings.Contains(lines[0], "status=200") || !strings.Contains(lines[0], "purged=7") {
t.Fatalf("log = %q", lines)
@@ -195,83 +196,6 @@ func TestSiteCachePurgeRequest(t *testing.T) {
assertNoToken(t, logs)
})
t.Run("an answer without purged logs the status only", func(t *testing.T) {
logs := captureLogs(t)
srv, _ := purgeReceiver(t, func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })
newTestPurger(t, srv).Purge(context.Background(), "post", "hello")
lines := logs.lines()
if len(lines) != 1 || !strings.Contains(lines[0], "level=INFO") || !strings.Contains(lines[0], "status=204") || strings.Contains(lines[0], "purged=") {
t.Fatalf("log = %q", lines)
}
})
for _, status := range []int{http.StatusInternalServerError, http.StatusUnauthorized} {
t.Run("a "+http.StatusText(status)+" answer logs one warning", func(t *testing.T) {
logs := captureLogs(t)
srv, hits := purgeReceiver(t, func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(status) })
newTestPurger(t, srv).Purge(context.Background(), "post", "hello")
if len(hits()) != 1 {
t.Fatalf("%d requests, want 1", len(hits()))
}
lines := logs.lines()
if len(lines) != 1 || !strings.Contains(lines[0], "level=WARN") || !strings.Contains(lines[0], `msg="journal: site cache purge failed"`) ||
!strings.Contains(lines[0], "kind=post") || !strings.Contains(lines[0], "slug=hello") {
t.Fatalf("log = %q", lines)
}
if !strings.Contains(lines[0], "status="+strconv.Itoa(status)) {
t.Fatalf("log %q lacks status=%d", lines[0], status)
}
assertNoToken(t, logs)
})
}
t.Run("a slow receiver is abandoned after the timeout", func(t *testing.T) {
logs := captureLogs(t)
release := make(chan struct{})
srv, _ := purgeReceiver(t, func(_ http.ResponseWriter, r *http.Request) {
select {
case <-release:
case <-r.Context().Done():
}
})
defer close(release)
start := time.Now()
newTestPurger(t, srv).Purge(context.Background(), "post", "slow")
elapsed := time.Since(start)
if elapsed < SiteCachePurgeTimeout-200*time.Millisecond || elapsed > SiteCachePurgeTimeout+1500*time.Millisecond {
t.Fatalf("Purge returned after %s, want about %s", elapsed, SiteCachePurgeTimeout)
}
lines := logs.lines()
if len(lines) != 1 || !strings.Contains(lines[0], "level=WARN") || !strings.Contains(lines[0], "error=timeout") {
t.Fatalf("log = %q", lines)
}
assertNoToken(t, logs)
})
t.Run("an unreachable receiver logs one warning without the URL's token", func(t *testing.T) {
logs := captureLogs(t)
srv, _ := purgeReceiver(t, func(http.ResponseWriter, *http.Request) {})
p := newTestPurger(t, srv)
srv.Close()
p.Purge(context.Background(), "category", "gone")
lines := logs.lines()
if len(lines) != 1 || !strings.Contains(lines[0], "level=WARN") || !strings.Contains(lines[0], "kind=category") {
t.Fatalf("log = %q", lines)
}
assertNoToken(t, logs)
})
t.Run("a cancelled parent context still sends", func(t *testing.T) {
captureLogs(t)
srv, hits := purgeReceiver(t, func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(`{"purged":1}`)) })
ctx, cancel := context.WithCancel(context.Background())
cancel()
newTestPurger(t, srv).Purge(ctx, "post", "after-cancel")
if got := hits(); len(got) != 1 || got[0].body["slug"] != "after-cancel" {
t.Fatalf("requests = %#v, want one", got)
}
})
t.Run("a nil purger sends nothing", func(t *testing.T) {
logs := captureLogs(t)
var p *SiteCachePurger