Files
ai/object/cache_bus_integration_test.go
zeekay 6846a971d5 refactor(kv): bump kv-go to v9.22.0 and rename qualifier redis -> kv
hanzokv/go v9.22.0 renames its Go package from `redis` to `kv` (import path
unchanged). Both cache-bus files imported it under an explicit `redis` alias;
they now use `kv`.

v9.22.0 ALSO dropped the redis:// URL scheme: ParseURL now accepts only
kv://, kvs:// and unix://. The integration test pointed KV_URL at
redis://<miniredis> and started failing for real (bus refused to parse the
URL and fell back to per-pod TTL), so it now uses kv://.

OPERATOR IMPACT: any deployment setting KV_URL=redis://... silently loses the
fleet-wide cache-invalidation bus and degrades to per-pod TTL, because
InitCacheBus fails soft by design. Move KV_URL to kv:// when this ships.

Verified: go build ./object/... and go vet ./object/... clean;
TestCacheBus_CrossPodInvalidation PASSES (full pub/sub loop).
2026-07-26 08:45:34 -07:00

59 lines
2.0 KiB
Go

package object
import (
"context"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
kv "github.com/hanzokv/go/v9"
)
// TestCacheBus_CrossPodInvalidation proves the FULL pub/sub loop end-to-end against
// a real Redis-protocol broker (miniredis, in-process): a DIFFERENT pod's write,
// published to the invalidation channel, reaches THIS pod's subscriber and flushes
// its local org-settings cache — the sub-second cross-pod convergence KV_URL buys.
func TestCacheBus_CrossPodInvalidation(t *testing.T) {
mr := miniredis.RunT(t) // in-process Redis w/ pub-sub; auto-closed by t.Cleanup
// Bring up THIS pod's bus (subscriber) pointed at the broker. kv-go v9.22.0
// accepts ONLY the kv:// (and kvs://, unix://) scheme — redis:// was dropped.
t.Setenv("KV_URL", "kv://"+mr.Addr())
InitCacheBus()
t.Cleanup(func() {
cacheBusMu.Lock()
if cacheBusClient != nil {
_ = cacheBusClient.Close()
cacheBusClient = nil
}
cacheBusMu.Unlock()
})
// Seed this pod's cache as if it had served a read.
invalidateOrgSettingsCacheLocal()
orgSettingsCacheMu.Lock()
orgSettingsCache["acme"] = &orgSettingsCacheEntry{settings: &OrgSettings{Owner: "acme"}, fetchedAt: time.Now()}
orgSettingsCacheMu.Unlock()
// A SEPARATE pod publishes an org_settings write. Retry until it lands — pub/sub
// is lossy until this pod's subscriber has attached — or time out. A live pod
// subscribes at boot, long before any write, so it needs no retry in production.
pub := kv.NewClient(&kv.Options{Addr: mr.Addr()})
defer pub.Close()
deadline := time.Now().Add(5 * time.Second)
for {
_ = pub.Publish(context.Background(), cacheBusChannel, "org_settings").Err()
orgSettingsCacheMu.RLock()
n := len(orgSettingsCache)
orgSettingsCacheMu.RUnlock()
if n == 0 {
return // subscriber received the broadcast and flushed — loop proven
}
if time.Now().After(deadline) {
t.Fatal("cross-pod broadcast did not flush the local cache within 5s")
}
time.Sleep(20 * time.Millisecond)
}
}