runMatchTick ran on a 1s interval per API instance with no coordination (flagged in DEVELOPMENT.md/matcher.ts as the blocker before running >1 instance). Two instances racing the same tick could both read the queue before either removed the matched players, pairing the same players twice and allocating two separate sessions for one match. Add lib/lock.ts: a Redis SET NX PX lock with a compare-and-delete release script, so a lock whose TTL already expired can't be released out from under a newer holder. runMatchTick now acquires this lock before touching the queue and releases it in a finally block; a losing instance just skips that cycle instead of racing. Tested: unit tests for the lock primitive and the matcher's lock-skip/ acquire/release-on-throw behavior (mocked Redis), plus real-Redis integration tests (mutual exclusion, release-after-TTL-expiry, stale-holder can't delete a newer lock) and an end-to-end test proving three concurrent runMatchTick() calls against live Postgres+Redis pair a queued pair exactly once with exactly one allocated session. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
|
|
const { set, eval: redisEval } = vi.hoisted(() => ({
|
|
set: vi.fn(),
|
|
eval: vi.fn(),
|
|
}));
|
|
vi.mock("../src/lib/redis", () => ({ redis: { set, eval: redisEval } }));
|
|
|
|
import { acquireLock } from "../src/lib/lock";
|
|
|
|
beforeEach(() => {
|
|
set.mockReset();
|
|
redisEval.mockReset();
|
|
});
|
|
|
|
describe("acquireLock", () => {
|
|
it("acquires the lock via SET NX and returns a releasable handle", async () => {
|
|
set.mockResolvedValue("OK");
|
|
redisEval.mockResolvedValue(1);
|
|
|
|
const lock = await acquireLock("my:lock", 5000);
|
|
|
|
expect(lock).not.toBeNull();
|
|
expect(set).toHaveBeenCalledWith("my:lock", expect.any(String), "PX", 5000, "NX");
|
|
|
|
await lock!.release();
|
|
expect(redisEval).toHaveBeenCalledTimes(1);
|
|
const [script, numKeys, key, token] = redisEval.mock.calls[0];
|
|
expect(script).toContain("redis.call(\"get\", KEYS[1]) == ARGV[1]");
|
|
expect(numKeys).toBe(1);
|
|
expect(key).toBe("my:lock");
|
|
expect(typeof token).toBe("string");
|
|
expect(token.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("returns null when another holder already owns the key", async () => {
|
|
set.mockResolvedValue(null); // NX failed: key already exists
|
|
|
|
const lock = await acquireLock("my:lock", 5000);
|
|
|
|
expect(lock).toBeNull();
|
|
});
|
|
|
|
it("uses a fresh random token per acquisition so two holders can't release each other's lock", async () => {
|
|
set.mockResolvedValue("OK");
|
|
|
|
const lockA = await acquireLock("my:lock", 5000);
|
|
const lockB = await acquireLock("my:lock", 5000);
|
|
|
|
const tokenA = set.mock.calls[0][1];
|
|
const tokenB = set.mock.calls[1][1];
|
|
expect(tokenA).not.toBe(tokenB);
|
|
|
|
await lockA!.release();
|
|
await lockB!.release();
|
|
expect(redisEval.mock.calls[0][3]).toBe(tokenA);
|
|
expect(redisEval.mock.calls[1][3]).toBe(tokenB);
|
|
});
|
|
});
|