fix: harden result polling and Redis 8.10 operations
Preserve tournament result jobs through lock contention and transient failures, atomically schedule retries before acknowledgement, and publish initial polls after releasing the preparation lock. Fix Redis connection blocking, durability Hash cleanup, duplicate auction request ordering, and scheduled-job promotion. Use native Redis 8.10 stream, lock, counter, and field-expiring cache operations with invalidation fencing. Repair 90 test type errors and add a repeatable API typecheck command. Validation: 704 API tests, 129 web tests, 31 real Redis integration tests, 3 dependency checks, workspace type checks and builds, and changed-file lint.
This commit is contained in:
1 parent
0c4c4239ec
commit
4b8ccf0077
49 files changed
+2214
-371
No files matched your search
@@ -0,0 +1,662 @@
|
||||
import { createClient } from 'redis';
|
||||
import { RedisService } from '../src/modules/global/redis/redis.service';
|
||||
import { AuctionRedisService } from '../src/modules/auction/redis/auction-redis.service';
|
||||
import { AuctionRedisCommandService } from '../src/modules/auction/redis/auction-redis-command.service';
|
||||
import { AuctionService } from '../src/modules/auction/auction.service';
|
||||
import { buildAuctionRedisKeys } from '../src/modules/auction/redis/auction-redis.keys';
|
||||
import { RiotJobQueueService } from '../src/modules/riot-job-queue/riot-job-queue.service';
|
||||
import {
|
||||
RIOT_JOB_HIGH_STREAM,
|
||||
RIOT_JOB_LOW_STREAM,
|
||||
RIOT_JOB_SCHEDULED_ZSET,
|
||||
} from '../src/modules/riot-job-queue/riot-job-queue.types';
|
||||
import { buildAuctionReserveActionRequestScriptInvocation } from '../src/modules/auction/redis/auction-redis.reserve-action';
|
||||
import { buildAuctionApplyTransitionScriptInvocation } from '../src/modules/auction/redis/auction-redis.apply-transition';
|
||||
import { DistributedLockService } from '../src/modules/global/redis/distributed-lock.service';
|
||||
import { BackgroundRefreshScheduler } from '../src/modules/riot-background-refresh/background-refresh.scheduler';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import {
|
||||
CommunityStatsCacheKey,
|
||||
CommunityStatsCacheService,
|
||||
} from '../src/modules/community-stats/community-stats-cache.service';
|
||||
import { CommunityStatsService } from '../src/modules/community-stats/community-stats.service';
|
||||
import { PROMOTE_SCHEDULED_RIOT_JOB_SCRIPT } from '../src/modules/riot-job-queue/riot-job-queue.scripts';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const client = createClient({
|
||||
url: process.env.REDIS_TEST_URL,
|
||||
socket: { reconnectStrategy: false },
|
||||
});
|
||||
const redis = new RedisService(client);
|
||||
const auctionRedis = new AuctionRedisService(client);
|
||||
const queue = new RiotJobQueueService({} as never, redis);
|
||||
|
||||
beforeAll(async () => {
|
||||
if (process.env.REDIS_TEST_ISOLATED !== '1' || !process.env.REDIS_TEST_URL) {
|
||||
throw new Error(
|
||||
'Run via pnpm --filter api test:redis to create an isolated Redis process.',
|
||||
);
|
||||
}
|
||||
client.on('error', () => {});
|
||||
await client.connect();
|
||||
});
|
||||
|
||||
describe('Redis native operations', () => {
|
||||
it('serves normal commands while a dedicated stream connection is blocked', async () => {
|
||||
const reader = await redis.createDedicatedConnection();
|
||||
try {
|
||||
await redis.createConsumerGroup('stream', 'workers', '0');
|
||||
const pending = reader.readGroup('stream', 'workers', 'reader', '>', {
|
||||
blockMs: 2000,
|
||||
count: 1,
|
||||
});
|
||||
void pending.catch(() => {}); // A failed assertion must still allow connection cleanup.
|
||||
let isBlocked = false;
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const clients = await client.sendCommand<string>(['CLIENT', 'LIST']);
|
||||
if (/flags=b\b.*cmd=xreadgroup/.test(clients)) {
|
||||
isBlocked = true;
|
||||
break;
|
||||
}
|
||||
await delay(5);
|
||||
}
|
||||
expect(isBlocked).toBe(true);
|
||||
await redis.setString('cache', 'available');
|
||||
expect(await redis.getString('cache')).toBe('available');
|
||||
const id = await redis.appendStream('stream', { job: 'wake' });
|
||||
expect((await pending)?.[0].messages[0].id).toBe(id);
|
||||
} finally {
|
||||
if (reader.raw().isOpen) reader.raw().destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it('claims abandoned pending messages before reading new work with the same idle threshold', async () => {
|
||||
await redis.createConsumerGroup('stream', 'workers', '0');
|
||||
const oldId = await redis.appendStream('stream', { job: 'old' });
|
||||
await redis.readGroup('stream', 'workers', 'dead-worker', '>', {
|
||||
count: 1,
|
||||
});
|
||||
const newId = await redis.appendStream('stream', { job: 'new' });
|
||||
await client.sendCommand([
|
||||
'XCLAIM',
|
||||
'stream',
|
||||
'workers',
|
||||
'dead-worker',
|
||||
'0',
|
||||
oldId,
|
||||
'IDLE',
|
||||
'1800001',
|
||||
]);
|
||||
const recovered = await redis.readGroup(
|
||||
'stream',
|
||||
'workers',
|
||||
'live-worker',
|
||||
'>',
|
||||
{ count: 1, claimMinIdleMs: 1800000 },
|
||||
);
|
||||
expect(recovered?.[0].messages[0]).toEqual(
|
||||
expect.objectContaining({ id: oldId, message: { job: 'old' } }),
|
||||
);
|
||||
const fresh = await redis.readGroup(
|
||||
'stream',
|
||||
'workers',
|
||||
'live-worker',
|
||||
'>',
|
||||
{ count: 1, claimMinIdleMs: 1800000 },
|
||||
);
|
||||
expect(fresh?.[0].messages[0].id).toBe(newId);
|
||||
expect(
|
||||
await redis.readGroup('stream', 'workers', 'other-worker', '>', {
|
||||
count: 1,
|
||||
claimMinIdleMs: 1800000,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it('acknowledges atomically but retains a message until every group has processed it', async () => {
|
||||
for (const group of ['workers', 'audit'])
|
||||
await redis.createConsumerGroup('stream', group, '0');
|
||||
const id = await redis.appendStream('stream', { job: 'one' });
|
||||
await redis.readGroup('stream', 'workers', 'worker', '>', { count: 1 });
|
||||
await redis.acknowledgeAndDeleteStream('stream', 'workers', id);
|
||||
expect(await client.xLen('stream')).toBe(1);
|
||||
expect((await client.xPending('stream', 'workers')).pending).toBe(0);
|
||||
await redis.readGroup('stream', 'audit', 'auditor', '>', { count: 1 });
|
||||
await redis.acknowledgeAndDeleteStream('stream', 'audit', id);
|
||||
expect(await client.xLen('stream')).toBe(0);
|
||||
await expect(
|
||||
redis.acknowledgeAndDeleteStream('stream', 'workers', id),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('never trims unread or pending jobs even when MAXLEN is exceeded', async () => {
|
||||
await redis.createConsumerGroup('stream', 'workers', '0');
|
||||
const first = await redis.appendStream(
|
||||
'stream',
|
||||
{ job: 'pending' },
|
||||
{ maxLen: 1 },
|
||||
);
|
||||
await redis.readGroup('stream', 'workers', 'worker', '>', { count: 1 });
|
||||
for (let i = 0; i < 220; i++)
|
||||
await redis.appendStream('stream', { job: String(i) }, { maxLen: 1 });
|
||||
expect(await client.xLen('stream')).toBe(221);
|
||||
expect((await client.xRange('stream', first, first))[0].message).toEqual({
|
||||
job: 'pending',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not release, renew, or recreate a lock belonging to another owner', async () => {
|
||||
const locks = new DistributedLockService(redis);
|
||||
const owner = await locks.acquireLock({
|
||||
lockKey: 'lock',
|
||||
lockOwnershipToken: 'old',
|
||||
lockExpirationDurationMilliseconds: 1000,
|
||||
});
|
||||
expect(owner).not.toBeNull();
|
||||
expect(await locks.extendLockIfOwned(owner!, 4000)).toBe(true);
|
||||
expect(await client.pTTL('lock')).toBeGreaterThan(3000);
|
||||
await client.set('lock', 'new', { PX: 5000 });
|
||||
expect(await locks.releaseLockIfOwned(owner!)).toBe(false);
|
||||
expect(await auctionRedis.releaseTokenLock('lock', 'old')).toBe(false);
|
||||
expect(await locks.extendLockIfOwned(owner!, 9000)).toBe(false);
|
||||
expect(await client.get('lock')).toBe('new');
|
||||
expect(await auctionRedis.releaseTokenLock('lock', 'new')).toBe(true);
|
||||
expect(await locks.extendLockIfOwned(owner!)).toBe(false);
|
||||
expect(await client.exists('lock')).toBe(0);
|
||||
});
|
||||
|
||||
it('does not clear the dedup token of a newer refresh job', async () => {
|
||||
const job = {
|
||||
jobId: 'old',
|
||||
puuid: 'player',
|
||||
queueGroup: 'summoners-rift',
|
||||
} as const;
|
||||
const key = 'riot:job:dedup:player:summoners-rift';
|
||||
await redis.setJson(
|
||||
'riot:job:status:old',
|
||||
{ ...job, state: 'processing' },
|
||||
86400,
|
||||
);
|
||||
await redis.setString(key, 'new', 300);
|
||||
await queue.markCompleted(job as never);
|
||||
expect(await redis.getString(key)).toBe('new');
|
||||
await queue.markFailed(job as never, new Error('failure'));
|
||||
expect(await redis.getString(key)).toBe('new');
|
||||
await redis.setString(key, 'old', 300);
|
||||
await queue.markCompleted(job as never);
|
||||
expect(await redis.getString(key)).toBeNull();
|
||||
});
|
||||
|
||||
it('increments failure counts without losing concurrent updates and refreshes the TTL', async () => {
|
||||
const scheduler = Object.assign(
|
||||
Object.create(BackgroundRefreshScheduler.prototype),
|
||||
{
|
||||
redis,
|
||||
logger: { warn: jest.fn() },
|
||||
},
|
||||
) as {
|
||||
bumpFailureCounter(puuid: string): Promise<void>;
|
||||
clearFailureCounter(puuid: string): Promise<void>;
|
||||
};
|
||||
await client.set('riot:bg:fail:player', '1', { EX: 10 });
|
||||
await Promise.all(
|
||||
Array.from({ length: 10 }, () => scheduler.bumpFailureCounter('player')),
|
||||
);
|
||||
expect(await client.get('riot:bg:fail:player')).toBe('11');
|
||||
expect(await client.ttl('riot:bg:fail:player')).toBeGreaterThan(86390);
|
||||
await scheduler.clearFailureCounter('player');
|
||||
expect(await client.exists('riot:bg:fail:player')).toBe(0);
|
||||
});
|
||||
});
|
||||
beforeEach(async () => {
|
||||
await client.flushDb();
|
||||
});
|
||||
afterAll(async () => {
|
||||
if (client.isOpen) await client.quit();
|
||||
});
|
||||
|
||||
function scheduledJob(priority = 'high', suffix = '1') {
|
||||
return JSON.stringify({
|
||||
kind: 'poll-tournament-result',
|
||||
jobId: `job-${suffix}`,
|
||||
matchId: 'match-1',
|
||||
attempt: 1,
|
||||
expandedToAllPuuids: false,
|
||||
enqueuedAt: Date.now(),
|
||||
priority,
|
||||
});
|
||||
}
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>((accept) => {
|
||||
resolve = accept;
|
||||
});
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
describe('Community Hash response cache', () => {
|
||||
const cache = new CommunityStatsCacheService(redis);
|
||||
|
||||
it('does not share an old in-flight detail request after invalidation or overwrite the fresh response', async () => {
|
||||
const service = new CommunityStatsService(
|
||||
{} as never,
|
||||
cache,
|
||||
{} as never,
|
||||
{} as never,
|
||||
);
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const dataset = (title: string) => ({
|
||||
matches: [
|
||||
{
|
||||
matchId: matchId.toString(),
|
||||
title,
|
||||
scheduledAt: null,
|
||||
status: 'finished',
|
||||
gameCount: 0,
|
||||
games: [],
|
||||
teams: [],
|
||||
seriesResults: [],
|
||||
},
|
||||
],
|
||||
detailsByMatchId: new Map(),
|
||||
});
|
||||
const oldDataset = deferred<ReturnType<typeof dataset>>();
|
||||
const oldStarted = deferred<void>();
|
||||
const build = jest
|
||||
.spyOn(
|
||||
service as unknown as {
|
||||
buildBaseDataset(): Promise<ReturnType<typeof dataset>>;
|
||||
},
|
||||
'buildBaseDataset',
|
||||
)
|
||||
.mockImplementationOnce(() => {
|
||||
oldStarted.resolve();
|
||||
return oldDataset.promise;
|
||||
})
|
||||
.mockResolvedValue(dataset('fresh'));
|
||||
const oldRequest = service.getMatchDetail({ communityId, matchId });
|
||||
await oldStarted.promise;
|
||||
await cache.invalidateCommunity(communityId.toString());
|
||||
try {
|
||||
const fresh = await service.getMatchDetail({ communityId, matchId });
|
||||
expect(fresh.match.title).toBe('fresh');
|
||||
expect(build).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
oldDataset.resolve(dataset('old'));
|
||||
await oldRequest;
|
||||
}
|
||||
expect((await oldRequest).match.title).toBe('old');
|
||||
expect(
|
||||
(await service.getMatchDetail({ communityId, matchId })).match.title,
|
||||
).toBe('fresh');
|
||||
expect(build).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('preserves independent normal, detail and incomplete-detail TTLs', async () => {
|
||||
const normal = new CommunityStatsCacheKey('community', 'matches', 'page-1');
|
||||
const detail = new CommunityStatsCacheKey(
|
||||
'community',
|
||||
'matchDetail',
|
||||
'match-1',
|
||||
);
|
||||
const incomplete = new CommunityStatsCacheKey(
|
||||
'community',
|
||||
'matchDetail',
|
||||
'match-2',
|
||||
);
|
||||
for (const key of [normal, detail, incomplete])
|
||||
expect(await cache.get(key)).toBeNull();
|
||||
await cache.set(normal, { matches: [] });
|
||||
await cache.setDetail(detail, { games: [1] });
|
||||
await cache.set(incomplete, { games: [] }, 60);
|
||||
for (const [key, ttl] of [
|
||||
[normal, 300],
|
||||
[detail, 86400],
|
||||
[incomplete, 60],
|
||||
] as const) {
|
||||
const [remaining] = await client.sendCommand<number[]>([
|
||||
'HTTL',
|
||||
key.hashKey,
|
||||
'FIELDS',
|
||||
'1',
|
||||
key.field,
|
||||
]);
|
||||
expect(remaining).toBeGreaterThan(ttl - 3);
|
||||
expect(remaining).toBeLessThanOrEqual(ttl);
|
||||
}
|
||||
expect(await cache.get(detail)).toEqual({ games: [1] });
|
||||
expect(await client.ttl(detail.hashKey)).toBe(-1);
|
||||
await client.sendCommand([
|
||||
'HPEXPIRE',
|
||||
incomplete.hashKey,
|
||||
'20',
|
||||
'FIELDS',
|
||||
'1',
|
||||
incomplete.field,
|
||||
]);
|
||||
await delay(50);
|
||||
expect(await cache.get(incomplete)).toBeNull();
|
||||
expect(await cache.get(detail)).toEqual({ games: [1] });
|
||||
});
|
||||
|
||||
it('invalidates all scopes for one community while preserving other communities', async () => {
|
||||
const entries = (
|
||||
[
|
||||
'matches',
|
||||
'leaderboard',
|
||||
'teamInsights',
|
||||
'matchDetail',
|
||||
'userInternal',
|
||||
] as const
|
||||
).map((scope) => new CommunityStatsCacheKey('one', scope, 'field'));
|
||||
const other = new CommunityStatsCacheKey('two', 'matches', 'field');
|
||||
for (const key of [...entries, other]) {
|
||||
await cache.get(key);
|
||||
await cache.set(key, { fresh: true });
|
||||
}
|
||||
expect(await cache.invalidateCommunity('one')).toBe(5);
|
||||
for (const key of entries) expect(await cache.get(key)).toBeNull();
|
||||
expect(await cache.get(other)).toEqual({ fresh: true });
|
||||
expect(await cache.invalidateCommunity('one')).toBe(0);
|
||||
});
|
||||
|
||||
it('rejects stale fills and separates in-flight identities across invalidation', async () => {
|
||||
const old = new CommunityStatsCacheKey('one', 'matches', 'page');
|
||||
await cache.get(old);
|
||||
const oldIdentity = old.identity;
|
||||
await cache.invalidateCommunity('one');
|
||||
const fresh = new CommunityStatsCacheKey('one', 'matches', 'page');
|
||||
await cache.get(fresh);
|
||||
expect(fresh.identity).not.toBe(oldIdentity);
|
||||
await cache.set(fresh, { version: 2 });
|
||||
await cache.set(old, { version: 1 });
|
||||
expect(await cache.get(fresh)).toEqual({ version: 2 });
|
||||
await cache.invalidateCommunity('one');
|
||||
await cache.set(old, { version: 0 });
|
||||
expect(await client.exists(old.hashKey)).toBe(0);
|
||||
});
|
||||
|
||||
it('treats malformed data as a miss and refreshes only the written field TTL', async () => {
|
||||
const key = new CommunityStatsCacheKey('one', 'matches', 'page');
|
||||
await cache.get(key);
|
||||
await client.hSet(key.hashKey, key.field, 'broken-json');
|
||||
expect(await cache.get(key)).toBeNull();
|
||||
await cache.set(key, { ok: true }, 1);
|
||||
await cache.set(key, { ok: true }, 300);
|
||||
const [ttl] = await client.sendCommand<number[]>([
|
||||
'HTTL',
|
||||
key.hashKey,
|
||||
'FIELDS',
|
||||
'1',
|
||||
key.field,
|
||||
]);
|
||||
expect(ttl).toBeGreaterThan(297);
|
||||
expect(await cache.get(key)).toEqual({ ok: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe('Redis correctness regressions', () => {
|
||||
it('rechecks the due timestamp after selection so rescheduled work is not promoted early', async () => {
|
||||
const job = scheduledJob();
|
||||
const selectedAt = Date.now();
|
||||
await client.zAdd(RIOT_JOB_SCHEDULED_ZSET, {
|
||||
score: selectedAt + 60000,
|
||||
value: job,
|
||||
});
|
||||
expect(
|
||||
await client.sendCommand([
|
||||
'EVAL',
|
||||
PROMOTE_SCHEDULED_RIOT_JOB_SCRIPT,
|
||||
'2',
|
||||
RIOT_JOB_SCHEDULED_ZSET,
|
||||
RIOT_JOB_HIGH_STREAM,
|
||||
job,
|
||||
String(selectedAt),
|
||||
]),
|
||||
).toBe(0);
|
||||
expect(await client.xLen(RIOT_JOB_HIGH_STREAM)).toBe(0);
|
||||
expect(await client.zCard(RIOT_JOB_SCHEDULED_ZSET)).toBe(1);
|
||||
});
|
||||
|
||||
it('discards malformed scheduled jobs and keeps the requested promotion batch limit', async () => {
|
||||
await client.zAdd(RIOT_JOB_SCHEDULED_ZSET, [
|
||||
{ score: 0, value: 'invalid-json' },
|
||||
{ score: 0, value: JSON.stringify({ kind: 'unsupported' }) },
|
||||
...Array.from({ length: 3 }, (_, i) => ({
|
||||
score: 1,
|
||||
value: scheduledJob('high', String(i)),
|
||||
})),
|
||||
]);
|
||||
expect(await queue.promoteDueScheduledJobs(2)).toBe(0);
|
||||
expect(await client.zCard(RIOT_JOB_SCHEDULED_ZSET)).toBe(3);
|
||||
expect(await queue.promoteDueScheduledJobs(2)).toBe(2);
|
||||
expect(await client.xLen(RIOT_JOB_HIGH_STREAM)).toBe(2);
|
||||
expect(await client.zCard(RIOT_JOB_SCHEDULED_ZSET)).toBe(1);
|
||||
});
|
||||
|
||||
it('keeps a scheduled job when destination append fails', async () => {
|
||||
const job = scheduledJob();
|
||||
await client.zAdd(RIOT_JOB_SCHEDULED_ZSET, {
|
||||
score: Date.now() - 1,
|
||||
value: job,
|
||||
});
|
||||
await client.set(RIOT_JOB_HIGH_STREAM, 'wrong-type');
|
||||
await expect(queue.promoteDueScheduledJobs()).rejects.toThrow('WRONGTYPE');
|
||||
expect(await client.zScore(RIOT_JOB_SCHEDULED_ZSET, job)).not.toBeNull();
|
||||
await client.del(RIOT_JOB_HIGH_STREAM);
|
||||
expect(await queue.promoteDueScheduledJobs()).toBe(1);
|
||||
expect(await client.xLen(RIOT_JOB_HIGH_STREAM)).toBe(1);
|
||||
});
|
||||
|
||||
it('promotes each due job once under concurrent workers and leaves future jobs scheduled', async () => {
|
||||
const jobs = Array.from({ length: 12 }, (_, i) =>
|
||||
scheduledJob(i % 2 ? 'low' : 'high', String(i)),
|
||||
);
|
||||
await client.zAdd(
|
||||
RIOT_JOB_SCHEDULED_ZSET,
|
||||
jobs.map((value) => ({ score: Date.now() - 1, value })),
|
||||
);
|
||||
const future = scheduledJob('high', 'future');
|
||||
await client.zAdd(RIOT_JOB_SCHEDULED_ZSET, {
|
||||
score: Date.now() + 60_000,
|
||||
value: future,
|
||||
});
|
||||
const counts = await Promise.all(
|
||||
Array.from({ length: 4 }, () => queue.promoteDueScheduledJobs(5)),
|
||||
);
|
||||
await queue.promoteDueScheduledJobs();
|
||||
expect(counts.reduce((sum, value) => sum + value, 0)).toBeLessThanOrEqual(
|
||||
12,
|
||||
);
|
||||
expect(await client.xLen(RIOT_JOB_HIGH_STREAM)).toBe(6);
|
||||
expect(await client.xLen(RIOT_JOB_LOW_STREAM)).toBe(6);
|
||||
expect(await client.zCard(RIOT_JOB_SCHEDULED_ZSET)).toBe(1);
|
||||
});
|
||||
|
||||
it('cleans only the requested lifecycle Hash payloads and queue references', async () => {
|
||||
const keys = buildAuctionRedisKeys('match-1');
|
||||
const oldKey = keys.durabilityPayloadKey('old');
|
||||
const newKey = keys.durabilityPayloadKey('new');
|
||||
for (const [key, lifecycle] of [
|
||||
[oldKey, 'old-life'],
|
||||
[newKey, 'new-life'],
|
||||
]) {
|
||||
await client.hSet(key, {
|
||||
auctionLifecycleId: lifecycle,
|
||||
payloadJson: JSON.stringify({ auctionLifecycleId: lifecycle }),
|
||||
});
|
||||
await client.zAdd(keys.durabilityRetryQueueKey, { score: 1, value: key });
|
||||
await client.zAdd(keys.durabilityDeadLetterQueueKey, {
|
||||
score: 1,
|
||||
value: key,
|
||||
});
|
||||
}
|
||||
const service = Object.assign(Object.create(AuctionService.prototype), {
|
||||
auctionRedisService: auctionRedis,
|
||||
}) as {
|
||||
clearRedisDurabilityPayloadsForLifecycle(
|
||||
keys: ReturnType<typeof buildAuctionRedisKeys>,
|
||||
lifecycle: string,
|
||||
): Promise<void>;
|
||||
};
|
||||
await service.clearRedisDurabilityPayloadsForLifecycle(keys, 'old-life');
|
||||
expect(await client.exists(oldKey)).toBe(0);
|
||||
expect(await client.exists(newKey)).toBe(1);
|
||||
expect(await client.zRange(keys.durabilityRetryQueueKey, 0, -1)).toEqual([
|
||||
newKey,
|
||||
]);
|
||||
expect(
|
||||
await client.zRange(keys.durabilityDeadLetterQueueKey, 0, -1),
|
||||
).toEqual([newKey]);
|
||||
});
|
||||
|
||||
it('supports legacy JSON cleanup and preserves malformed or foreign payloads until an unconditional cleanup', async () => {
|
||||
const keys = buildAuctionRedisKeys('legacy');
|
||||
const legacy = keys.durabilityPayloadKey('old');
|
||||
const foreign = keys.durabilityPayloadKey('foreign');
|
||||
const malformed = keys.durabilityPayloadKey('malformed');
|
||||
await client.set(
|
||||
legacy,
|
||||
JSON.stringify({ auctionLifecycleId: 'old-life' }),
|
||||
);
|
||||
await client.set(
|
||||
foreign,
|
||||
JSON.stringify({ auctionLifecycleId: 'new-life' }),
|
||||
);
|
||||
await client.set(malformed, 'broken-json');
|
||||
await client.zAdd(
|
||||
keys.durabilityRetryQueueKey,
|
||||
[legacy, foreign, malformed].map((value) => ({ score: 1, value })),
|
||||
);
|
||||
const service = Object.assign(Object.create(AuctionService.prototype), {
|
||||
auctionRedisService: auctionRedis,
|
||||
}) as {
|
||||
clearRedisDurabilityPayloadsForLifecycle(
|
||||
keys: ReturnType<typeof buildAuctionRedisKeys>,
|
||||
lifecycle?: string,
|
||||
): Promise<void>;
|
||||
};
|
||||
await service.clearRedisDurabilityPayloadsForLifecycle(keys, 'old-life');
|
||||
expect(await client.exists(legacy)).toBe(0);
|
||||
expect(await client.exists([foreign, malformed])).toBe(2);
|
||||
await service.clearRedisDurabilityPayloadsForLifecycle(keys);
|
||||
expect(await client.exists([foreign, malformed])).toBe(0);
|
||||
expect(await client.zCard(keys.durabilityRetryQueueKey)).toBe(0);
|
||||
});
|
||||
|
||||
it('preserves the 500-request cap, replay window, and fingerprint conflict checks', async () => {
|
||||
const keys = buildAuctionRedisKeys('retention');
|
||||
const reserve = async (id: string, revision = 1) => {
|
||||
const invocation = buildAuctionReserveActionRequestScriptInvocation(
|
||||
keys,
|
||||
{
|
||||
auctionActionRequestId: id,
|
||||
auctionActionType: 'next',
|
||||
auctionStateTransitionId: `transition-${id}`,
|
||||
clientObservedAuctionLifecycleId: 'life',
|
||||
clientObservedPhase: 'sold',
|
||||
clientObservedRevision: revision,
|
||||
leaseDurationMS: 15000,
|
||||
maximumStoredAuctionActionRequestResultCount: 500,
|
||||
minimumAuctionActionRequestResultReplayWindowMS: 300000,
|
||||
processingOwnerToken: 'owner',
|
||||
},
|
||||
);
|
||||
return auctionRedis.evalScript<string[]>(
|
||||
invocation.script,
|
||||
invocation.keys,
|
||||
invocation.values,
|
||||
);
|
||||
};
|
||||
for (let i = 0; i < 500; i++)
|
||||
expect((await reserve(String(i)))[0]).toBe('RESERVED');
|
||||
expect((await reserve('0', 2)).slice(0, 2)).toEqual([
|
||||
'REJECTED',
|
||||
'DUPLICATE_ACTION_REQUEST_CONFLICT',
|
||||
]);
|
||||
expect((await reserve('501')).slice(0, 2)).toEqual([
|
||||
'REJECTED',
|
||||
'ACTION_REQUEST_RESULT_LIMIT',
|
||||
]);
|
||||
expect(await client.lLen(keys.actionRequestResultOrderKey)).toBe(500);
|
||||
expect((await reserve('0'))[0]).toBe('RESERVED');
|
||||
expect(await client.lLen(keys.actionRequestResultOrderKey)).toBe(500);
|
||||
await client.hSet(
|
||||
keys.actionRequestResultsKey,
|
||||
'life:0:createdAtTimestampMS',
|
||||
String(Date.now() - 300001),
|
||||
);
|
||||
expect((await reserve('501'))[0]).toBe('RESERVED');
|
||||
expect(await client.lLen(keys.actionRequestResultOrderKey)).toBe(500);
|
||||
expect(
|
||||
await client.hExists(keys.actionRequestResultsKey, 'life:0:fingerprint'),
|
||||
).toBe(0);
|
||||
});
|
||||
|
||||
it('counts one order entry per reserved and applied action and still replays it', async () => {
|
||||
const keys = buildAuctionRedisKeys('match-1');
|
||||
const commands = new AuctionRedisCommandService(auctionRedis);
|
||||
await commands.hydrateState('match-1', {
|
||||
matchId: 'match-1',
|
||||
auctionHistorySequence: 0,
|
||||
auctionLifecycleId: 'life',
|
||||
phase: 'sold',
|
||||
revision: 0,
|
||||
maximumBidRoundDurationSeconds: 30,
|
||||
structureStateJson: '{}',
|
||||
});
|
||||
const reservation = buildAuctionReserveActionRequestScriptInvocation(keys, {
|
||||
auctionActionRequestId: 'request',
|
||||
auctionActionType: 'next',
|
||||
auctionStateTransitionId: 'transition',
|
||||
clientObservedAuctionLifecycleId: 'life',
|
||||
clientObservedPhase: 'sold',
|
||||
clientObservedRevision: 1,
|
||||
leaseDurationMS: 15000,
|
||||
maximumStoredAuctionActionRequestResultCount: 500,
|
||||
minimumAuctionActionRequestResultReplayWindowMS: 300000,
|
||||
processingOwnerToken: 'owner',
|
||||
});
|
||||
await auctionRedis.evalScript(
|
||||
reservation.script,
|
||||
reservation.keys,
|
||||
reservation.values,
|
||||
);
|
||||
const apply = buildAuctionApplyTransitionScriptInvocation(keys, {
|
||||
actionRequest: {
|
||||
scopeKey: 'life:request',
|
||||
processingOwnerToken: 'owner',
|
||||
observedProcessingLeaseVersion: 1,
|
||||
},
|
||||
afterAuctionStructureStateJson: '{}',
|
||||
auctionHistorySequence: 1,
|
||||
auctionLifecycleId: 'life',
|
||||
auctionStateTransitionId: 'transition',
|
||||
broadcastStateRevision: 2,
|
||||
deadline: { mode: 'none' },
|
||||
durabilityPayloadJson: '{}',
|
||||
expectedPhase: 'sold',
|
||||
expectedRevision: 1,
|
||||
liveStatePatch: { phase: 'sold' },
|
||||
matchId: 'match-1',
|
||||
shouldBroadcastState: false,
|
||||
});
|
||||
const result = await auctionRedis.evalScript<string[]>(
|
||||
apply.script,
|
||||
apply.keys,
|
||||
apply.values,
|
||||
);
|
||||
expect(result[0]).toBe('OK');
|
||||
expect(
|
||||
await client.lRange(keys.actionRequestResultOrderKey, 0, -1),
|
||||
).toEqual(['life:request']);
|
||||
const replay = await auctionRedis.evalScript<string[]>(
|
||||
reservation.script,
|
||||
reservation.keys,
|
||||
reservation.values,
|
||||
);
|
||||
expect(replay.slice(0, 2)).toEqual(['REPLAY', 'applied']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user