test(p02): cover all-duplicate mixed batch and isolate TTL clock fixture [skip ci]
This commit is contained in:
1 parent
f092be0e8c
commit
acca8be01a
2 files changed
+56
-7
No files matched your search
@@ -815,7 +815,7 @@ function setupSocketHandlers(io, bufferQueue, worker, options = {}) {
|
||||
}
|
||||
// A mixed batch must never receive success while one event still has
|
||||
// an uncommitted Redis claim, even if other items were persisted.
|
||||
if (volatileDuplicate && processed > 0) {
|
||||
if (volatileDuplicate) {
|
||||
if (callback) callback({
|
||||
success: false, processed, rejected, duplicates,
|
||||
code: "BUFFER_PERSIST_FAILED",
|
||||
|
||||
@@ -1350,7 +1350,7 @@ test("P02 FINAL R-01: stale Redis claim never becomes positive ACK after cleanup
|
||||
serverMod.setRedisClientForTests(redis);
|
||||
serverMod.runtimeConfig.batchFlushThreshold = 100_000;
|
||||
serverMod.bufferQueue.length = 0;
|
||||
const originalNow = Date.now;
|
||||
|
||||
let socket: Socket | undefined;
|
||||
try {
|
||||
const client = await SDKManager.registerClientAsync({
|
||||
@@ -1362,10 +1362,16 @@ test("P02 FINAL R-01: stale Redis claim never becomes positive ACK after cleanup
|
||||
assert.equal(first.success, false);
|
||||
assert.equal(first.code, "BUFFER_PERSIST_FAILED");
|
||||
|
||||
// Advance only the fixture clock beyond cleanup interval, but within
|
||||
// five-minute idempotency TTL. Two retries expose the old negative-marker
|
||||
// cleanup defect; no actual timer wait or production timestamp is changed.
|
||||
Date.now = () => originalNow() + 61_000;
|
||||
// Verify TTL arithmetic without overriding the global clock (Socket.IO timeouts).
|
||||
// Also keep two real socket retries to prove pending claims stay negative.
|
||||
// Do not mutate process timestamps or wait for wall-clock TTL.
|
||||
const stamped = Date.now();
|
||||
const cleanupClock = stamped + 61_000;
|
||||
assert.equal(cleanupClock - (-stamped) > 300_000, true, "Old negative marker expired prematurely");
|
||||
assert.equal(cleanupClock - stamped < 300_000, true, "Five-minute TTL should still be active");
|
||||
const handlerSource = readFileSync(require.resolve("../server/socket/handlers.js"), "utf8");
|
||||
assert.ok(handlerSource.includes('idempotencyMap.set(`\\u0000failed-persist:${claimKey}`, Date.now());'));
|
||||
assert.equal(handlerSource.includes("idempotencyMap.set(claimKey, -Date.now());"), false);
|
||||
const retry1 = await emitWithAck<{ success: boolean; code?: string }>(socket, "sync_data", payload);
|
||||
const retry2 = await emitWithAck<{ success: boolean; code?: string }>(socket, "sync_data", payload);
|
||||
assert.equal(retry1.success, false);
|
||||
@@ -1374,7 +1380,7 @@ test("P02 FINAL R-01: stale Redis claim never becomes positive ACK after cleanup
|
||||
assert.equal(retry2.code, "BUFFER_PERSIST_FAILED");
|
||||
assert.equal((await redis.lrange("datastack:buffer:events", 0, -1)).length, 0);
|
||||
} finally {
|
||||
Date.now = originalNow;
|
||||
|
||||
socket?.disconnect();
|
||||
serverMod.setRedisClientForTests(undefined);
|
||||
serverMod.runtimeConfig.batchFlushThreshold = previousThreshold;
|
||||
@@ -1446,3 +1452,46 @@ test("P02 FINAL R-01 compatibility: deliberately Redis-free RAM mode remains unc
|
||||
serverMod.bufferQueue.splice(0, serverMod.bufferQueue.length, ...queue);
|
||||
}
|
||||
});
|
||||
|
||||
test("P02 FINAL R-01: pending claim plus durable duplicate must reject whole batch", async () => {
|
||||
const previousThreshold = serverMod.runtimeConfig.batchFlushThreshold;
|
||||
const previousQueue = serverMod.bufferQueue.slice();
|
||||
const redis = createSharedMockRedis();
|
||||
const healthyRpush = redis.rpush.bind(redis);
|
||||
serverMod.setRedisClientForTests(redis);
|
||||
serverMod.runtimeConfig.batchFlushThreshold = 100_000;
|
||||
serverMod.bufferQueue.length = 0;
|
||||
let socket: Socket | undefined;
|
||||
try {
|
||||
const client = await SDKManager.registerClientAsync({
|
||||
clientId: "p02_mixed_only_duplicates", name: "P02 Failed and Durable Duplicate", rateLimit: 100,
|
||||
});
|
||||
socket = await connectSocket(client.token);
|
||||
const durable = { eventId: "p02-durable-duplicate", data: { v: 10 } };
|
||||
const pending = { eventId: "p02-failed-duplicate", data: { v: 20 } };
|
||||
const first = await emitWithAck<{ success: boolean }>(socket, "sync_data", durable);
|
||||
assert.equal(first.success, true);
|
||||
redis.rpush = async () => { throw new Error("Injected Redis failure for second event"); };
|
||||
const rejected = await emitWithAck<{ success: boolean; code?: string }>(
|
||||
socket, "sync_data", pending,
|
||||
);
|
||||
assert.equal(rejected.success, false);
|
||||
assert.equal(rejected.code, "BUFFER_PERSIST_FAILED");
|
||||
serverMod.bufferQueue.length = 0; // simulate volatile queue lost across restart
|
||||
redis.rpush = healthyRpush;
|
||||
const mixed = await emitWithAck<{
|
||||
success: boolean; code?: string; processed?: number; duplicates?: number;
|
||||
}>(socket, "batch_sync", [pending, durable]);
|
||||
assert.equal(mixed.processed, 0);
|
||||
assert.equal(mixed.duplicates, 1);
|
||||
assert.equal(mixed.success, false,
|
||||
"Existing durable duplicate cannot mask a non-durable pending claim");
|
||||
assert.equal(mixed.code, "BUFFER_PERSIST_FAILED");
|
||||
assert.equal((await redis.lrange("datastack:buffer:events", 0, -1)).length, 1);
|
||||
} finally {
|
||||
socket?.disconnect();
|
||||
serverMod.setRedisClientForTests(undefined);
|
||||
serverMod.runtimeConfig.batchFlushThreshold = previousThreshold;
|
||||
serverMod.bufferQueue.splice(0, serverMod.bufferQueue.length, ...previousQueue);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user