Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -1,17 +1,17 @@
|
||||
import { describe, expect, it, beforeEach } from "vitest";
|
||||
import { createSharedStore, createTwoTierCache } from "@hivegrid/shared-store";
|
||||
import { createSharedStore, createTwoTierCache, MemoryAdapter } from "@hivegrid/shared-store";
|
||||
import { buildCacheKey, createFlagEvalCache, hashContext } from "./cache.js";
|
||||
import type { EvaluateFlagResult } from "./evaluate.js";
|
||||
|
||||
function mkStore() {
|
||||
const store = createSharedStore({ namespace: "ff-test" });
|
||||
const store = createSharedStore({ namespace: "ff-test", adapter: new MemoryAdapter() });
|
||||
const tier = createTwoTierCache<EvaluateFlagResult>({
|
||||
store,
|
||||
namespace: "feature-flags",
|
||||
ttlSeconds: 60,
|
||||
pubsubEnabled: false,
|
||||
});
|
||||
return tier;
|
||||
return { store, tier };
|
||||
}
|
||||
|
||||
describe("hashContext", () => {
|
||||
@@ -46,10 +46,11 @@ describe("buildCacheKey", () => {
|
||||
|
||||
describe("createFlagEvalCache", () => {
|
||||
let cache: ReturnType<typeof createFlagEvalCache>;
|
||||
let tier: ReturnType<typeof mkStore>;
|
||||
let tier: ReturnType<typeof mkStore>["tier"];
|
||||
beforeEach(() => {
|
||||
tier = mkStore();
|
||||
cache = createFlagEvalCache(tier);
|
||||
const built = mkStore();
|
||||
tier = built.tier;
|
||||
cache = createFlagEvalCache(tier, built.store);
|
||||
});
|
||||
|
||||
it("set then get returns the same value", async () => {
|
||||
@@ -69,21 +70,81 @@ describe("createFlagEvalCache", () => {
|
||||
await cache.set("flag1", { flagName: "flag1", userId: "u1" }, result);
|
||||
expect(await cache.get("flag1", { flagName: "flag1", userId: "u1" })).toEqual(result);
|
||||
await cache.invalidateAll();
|
||||
// After flush, L1 is empty. L2 still has the entry (TTL-based) but
|
||||
// a different namespace would have to look it up; with pubsub off
|
||||
// we only flush local, so L2 still has it.
|
||||
expect(tier.l1.get(buildCacheKey("flag1", { flagName: "flag1", userId: "u1" }))).toBeUndefined();
|
||||
expect(await cache.get("flag1", { flagName: "flag1", userId: "u1" })).toBeNull();
|
||||
});
|
||||
|
||||
it("invalidateFlag publishes a token that flushes L1", async () => {
|
||||
it("invalidateFlag makes old L1 and L2 evaluations unreachable", async () => {
|
||||
const result: EvaluateFlagResult = { enabled: true, reason: "matched" };
|
||||
await cache.set("flag1", { flagName: "flag1" }, result);
|
||||
await cache.invalidateFlag("flag1");
|
||||
// local L1 entry remains because the wildcard is not '*' — verify
|
||||
// the L1 still holds 'flag1' (specific-key invalidation only purges
|
||||
// the exact key, not by prefix). This documents current behaviour:
|
||||
// we publish a sentinel and rely on subscribers to flush local L1
|
||||
// entirely on receipt — see subscribeAndFlushOnInvalidate.
|
||||
expect(tier.l1.get(buildCacheKey("flag1", { flagName: "flag1" }))).toEqual(result);
|
||||
expect(await cache.get("flag1", { flagName: "flag1" })).toBeNull();
|
||||
});
|
||||
|
||||
it("invalidates two replica caches without mixing tenant contexts", async () => {
|
||||
const adapter = new MemoryAdapter();
|
||||
const storeA = createSharedStore({ namespace: "ff-replicas", adapter });
|
||||
const storeB = createSharedStore({ namespace: "ff-replicas", adapter });
|
||||
const tierA = createTwoTierCache<EvaluateFlagResult>({
|
||||
store: storeA,
|
||||
namespace: "feature-flags",
|
||||
ttlSeconds: 60,
|
||||
});
|
||||
const tierB = createTwoTierCache<EvaluateFlagResult>({
|
||||
store: storeB,
|
||||
namespace: "feature-flags",
|
||||
ttlSeconds: 60,
|
||||
});
|
||||
const replicaA = createFlagEvalCache(tierA, storeA);
|
||||
const replicaB = createFlagEvalCache(tierB, storeB);
|
||||
const dispose = await tierB.subscribe((token) => replicaB.handleInvalidation(token));
|
||||
const tenantA = { flagName: "billing.beta", tenantId: "tenant-a" };
|
||||
const tenantB = { flagName: "billing.beta", tenantId: "tenant-b" };
|
||||
await replicaA.set("billing.beta", tenantA, { enabled: true, reason: "matched" });
|
||||
await replicaA.set("billing.beta", tenantB, { enabled: false, reason: "disabled" });
|
||||
expect(await replicaB.get("billing.beta", tenantA)).toEqual({ enabled: true, reason: "matched" });
|
||||
expect(await replicaB.get("billing.beta", tenantB)).toEqual({ enabled: false, reason: "disabled" });
|
||||
|
||||
await replicaA.invalidateFlag("billing.beta");
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(await replicaB.get("billing.beta", tenantA)).toBeNull();
|
||||
expect(await replicaB.get("billing.beta", tenantB)).toBeNull();
|
||||
await replicaA.set("billing.beta", tenantA, { enabled: false, reason: "no-match" });
|
||||
expect(await replicaB.get("billing.beta", tenantA)).toEqual({ enabled: false, reason: "no-match" });
|
||||
await dispose();
|
||||
await storeA.close();
|
||||
});
|
||||
|
||||
it("self-heals from a missed pub/sub event after the generation TTL", async () => {
|
||||
let now = 0;
|
||||
const adapter = new MemoryAdapter({ now: () => now });
|
||||
const storeA = createSharedStore({ namespace: "ff-missed-event", adapter });
|
||||
const storeB = createSharedStore({ namespace: "ff-missed-event", adapter });
|
||||
const tierA = createTwoTierCache<EvaluateFlagResult>({
|
||||
store: storeA,
|
||||
namespace: "feature-flags",
|
||||
ttlSeconds: 60,
|
||||
l1Now: () => now,
|
||||
pubsubEnabled: false,
|
||||
});
|
||||
const tierB = createTwoTierCache<EvaluateFlagResult>({
|
||||
store: storeB,
|
||||
namespace: "feature-flags",
|
||||
ttlSeconds: 60,
|
||||
l1Now: () => now,
|
||||
pubsubEnabled: false,
|
||||
});
|
||||
const replicaA = createFlagEvalCache(tierA, storeA, { generationTtlMs: 10, now: () => now });
|
||||
const replicaB = createFlagEvalCache(tierB, storeB, { generationTtlMs: 10, now: () => now });
|
||||
const context = { flagName: "billing.beta", tenantId: "tenant-a" };
|
||||
await replicaA.set("billing.beta", context, { enabled: true, reason: "matched" });
|
||||
expect(await replicaB.get("billing.beta", context)).toEqual({ enabled: true, reason: "matched" });
|
||||
|
||||
await replicaA.invalidateFlag("billing.beta");
|
||||
expect(await replicaB.get("billing.beta", context)).toEqual({ enabled: true, reason: "matched" });
|
||||
now = 11;
|
||||
expect(await replicaB.get("billing.beta", context)).toBeNull();
|
||||
await storeA.close();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user