fix(clock): preserve UTC leases and unification recovery
This commit is contained in:
@@ -207,6 +207,57 @@ integration('database turn daemon lease and fencing', () => {
|
||||
expect(row.ownerId).toBe(firstToken ? 'owner-a' : 'owner-b');
|
||||
});
|
||||
|
||||
it('persists and evaluates lease wall time as UTC under a non-UTC database session', async () => {
|
||||
const profile = `${profilePrefix}utc-wall`;
|
||||
const zonedUrl = new URL(databaseUrl!);
|
||||
const schema = zonedUrl.searchParams.get('schema') ?? 'public';
|
||||
zonedUrl.searchParams.delete('schema');
|
||||
zonedUrl.searchParams.set('options', `-c search_path=${schema} -c TimeZone=Asia/Seoul`);
|
||||
const zonedConnector = createGamePostgresConnector({ url: zonedUrl.toString() });
|
||||
await zonedConnector.connect();
|
||||
const lease = await DatabaseTurnDaemonLease.connect(zonedUrl.toString(), {
|
||||
profile,
|
||||
ownerId: 'utc-owner',
|
||||
leaseDurationMs: 60_000,
|
||||
heartbeat: false,
|
||||
});
|
||||
leases.push(lease);
|
||||
try {
|
||||
await expect(lease.acquire()).resolves.toMatchObject({ profile, ownerId: 'utc-owner' });
|
||||
const [leaseRows, wallRows] = await Promise.all([
|
||||
zonedConnector.prisma.$queryRaw<Array<{ leaseUntil: Date }>>`
|
||||
SELECT lease_until AS "leaseUntil"
|
||||
FROM turn_daemon_lease
|
||||
WHERE profile = ${profile}
|
||||
`,
|
||||
zonedConnector.prisma.$queryRaw<Array<{ zone: string; wallNow: Date }>>`
|
||||
SELECT current_setting('TimeZone') AS zone,
|
||||
(CURRENT_TIMESTAMP AT TIME ZONE 'UTC')::timestamp(3) AS "wallNow"
|
||||
`,
|
||||
]);
|
||||
expect(wallRows[0]?.zone).toBe('Asia/Seoul');
|
||||
expect(leaseRows[0]!.leaseUntil.getTime() - wallRows[0]!.wallNow.getTime()).toBeGreaterThan(50_000);
|
||||
expect(leaseRows[0]!.leaseUntil.getTime() - wallRows[0]!.wallNow.getTime()).toBeLessThanOrEqual(60_000);
|
||||
|
||||
await lease.release();
|
||||
const [releasedRows, releasedWall] = await Promise.all([
|
||||
zonedConnector.prisma.$queryRaw<Array<{ leaseUntil: Date }>>`
|
||||
SELECT lease_until AS "leaseUntil"
|
||||
FROM turn_daemon_lease
|
||||
WHERE profile = ${profile}
|
||||
`,
|
||||
zonedConnector.prisma.$queryRaw<Array<{ wallNow: Date }>>`
|
||||
SELECT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')::timestamp(3) AS "wallNow"
|
||||
`,
|
||||
]);
|
||||
expect(Math.abs(releasedRows[0]!.leaseUntil.getTime() - releasedWall[0]!.wallNow.getTime())).toBeLessThan(
|
||||
1_000
|
||||
);
|
||||
} finally {
|
||||
await zonedConnector.disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
it('increments the epoch on expiry takeover and fences the stale owner', async () => {
|
||||
const profile = `${profilePrefix}takeover`;
|
||||
const first = await createLease(profile, 'owner-a');
|
||||
|
||||
@@ -312,7 +312,17 @@ integration('unification finalization transaction', () => {
|
||||
const bidWorld = new InMemoryTurnWorld(beforeBid.state, beforeBid.snapshot, {
|
||||
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
||||
});
|
||||
const bidProcessingTick = bidWorld.getGameClockState().tick;
|
||||
const futureCloseTick = BigInt(bidProcessingTick) + 86_400_000n;
|
||||
await db.auction.updateMany({
|
||||
where: { id: { in: [uniqueAuction.id, resourceAuction.id] } },
|
||||
data: { closeTick: futureCloseTick },
|
||||
});
|
||||
const bidder = await createAuctionBidder({ databaseUrl: databaseUrl!, world: bidWorld });
|
||||
const bidClockContext = {
|
||||
processingGameTick: bidProcessingTick,
|
||||
requestedAtWall: new Date('2026-09-03T00:00:00.000Z'),
|
||||
};
|
||||
try {
|
||||
await expect(
|
||||
bidder.bid({
|
||||
@@ -322,7 +332,8 @@ integration('unification finalization transaction', () => {
|
||||
generalId: fixtureId,
|
||||
amount: 30,
|
||||
tryExtendCloseDate: false,
|
||||
})
|
||||
...bidClockContext,
|
||||
} as Parameters<typeof bidder.bid>[0])
|
||||
).resolves.toMatchObject({ ok: true, auctionId: uniqueAuction.id });
|
||||
await expect(
|
||||
bidder.bid({
|
||||
@@ -332,7 +343,8 @@ integration('unification finalization transaction', () => {
|
||||
generalId: fixtureId,
|
||||
amount: 50,
|
||||
tryExtendCloseDate: false,
|
||||
})
|
||||
...bidClockContext,
|
||||
} as Parameters<typeof bidder.bid>[0])
|
||||
).resolves.toMatchObject({ ok: true, auctionId: uniqueAuction.id });
|
||||
} finally {
|
||||
await bidder.close();
|
||||
@@ -699,6 +711,24 @@ integration('unification finalization transaction', () => {
|
||||
});
|
||||
const commandResult = await stateManager.transaction(executeCommand);
|
||||
expect(commandResult).toMatchObject({ type: 'messageRespond', ok: true, action: 'raiseInvader' });
|
||||
const selectedPromptAction = await db.messageAction.findUniqueOrThrow({
|
||||
where: { messageId: invaderPrompt!.id },
|
||||
});
|
||||
const siblingPromptActions = await db.messageAction.findMany({
|
||||
where: {
|
||||
actionType: 'raiseInvader',
|
||||
createdGameTick: selectedPromptAction.createdGameTick,
|
||||
},
|
||||
});
|
||||
expect(siblingPromptActions.length).toBeGreaterThan(1);
|
||||
expect(
|
||||
siblingPromptActions.every(
|
||||
(action) =>
|
||||
action.status === 'RESOLVED' &&
|
||||
action.resolvedGameTick !== null &&
|
||||
action.resolvedGameTick >= action.createdGameTick
|
||||
)
|
||||
).toBe(true);
|
||||
const reconciledWorld = await db.worldState.findUniqueOrThrow({ where: { id: worldRow.id } });
|
||||
const appliedSuspension = await db.clockSuspension.findUniqueOrThrow({ where: { id: suspension.id } });
|
||||
expect(reconciledWorld).toMatchObject({
|
||||
|
||||
Reference in New Issue
Block a user