Skip to content

Commit

Permalink
Move to ioredis and mock prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
needs committed Feb 16, 2025
1 parent 99a96c6 commit d410527
Show file tree
Hide file tree
Showing 14 changed files with 498 additions and 293 deletions.
1 change: 1 addition & 0 deletions apps/worker/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default {
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/worker',
setupFiles: ['./testSetup.ts'],
setupFilesAfterEnv: ['./src/mockPrisma.ts'],
};
17 changes: 7 additions & 10 deletions apps/worker/src/redis.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { createClient } from 'redis';
import { REDIS_HOST, REDIS_PORT, REDIS_FAMILY } from '@teerank/teerank';
import { Redis } from 'ioredis';

export const redisClientPromise = createClient({
url: `redis://${REDIS_HOST}:${REDIS_PORT}`,
socket: {
family: REDIS_FAMILY,
},
})
.on('error', err => console.log('Redis Client Error', err))
.connect();
export const redis = new Redis({
host: REDIS_HOST,
port: REDIS_PORT,
family: REDIS_FAMILY,
});

export type RedisClient = Awaited<typeof redisClientPromise>;
redis.on('error', err => console.error('Redis Client Error', err));
8 changes: 3 additions & 5 deletions apps/worker/src/workers/fillClanActivePlayerCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { Worker } from "bullmq";
import { prisma } from "../prisma";
import { QUEUE_NAME_FILL_CLAN_ACTIVE_PLAYER_COUNT } from "@teerank/teerank";
import { bullmqConnection } from "@teerank/teerank";
import { redisClientPromise } from "../redis";
import { redis } from "../redis";
import { minutesToSeconds } from "date-fns";

const MIN_CREATED_AT_KEY = 'fill-clan-active-player-count-min-created-at';

async function processor() {
const redisClient = await redisClientPromise;

const minCreatedAt = new Date(Number(await redisClient.get(MIN_CREATED_AT_KEY) || "0"));
const minCreatedAt = new Date(Number(await redis.get(MIN_CREATED_AT_KEY) || "0"));
console.log(`Min created at: ${minCreatedAt}`);

const clans = await prisma.clan.findMany({
Expand Down Expand Up @@ -58,7 +56,7 @@ async function processor() {
console.log(`Filled clan active player count for ${clans.length} clans`);

const newMinCreatedAt = clans[clans.length - 1].createdAt;
await redisClient.set(MIN_CREATED_AT_KEY, newMinCreatedAt.getTime());
await redis.set(MIN_CREATED_AT_KEY, newMinCreatedAt.getTime());
console.log(`Updated min created at to ${newMinCreatedAt.toISOString()}`);

return {
Expand Down
145 changes: 111 additions & 34 deletions apps/worker/src/workers/updateGlobalCounts.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,112 @@
import { updateCount } from "./updateGlobalCounts";
import redis from "redis-mock";

describe("updateCounts", () => {
test("create a new count", async () => {
const redisClient = redis.createClient();

await updateCount({
redisClient,
getNewEntities: async () => [{ createdAt: new Date() }],
lastUpdatedAtKey: "test-0-lastUpdatedAt",
countKey: "test-0-count",
});

expect(await redisClient.get("test-0-count")).toBe("1");
expect(await redisClient.get("test-0-lastUpdatedAt")).toBe(new Date().toISOString());
});

test("update an existing count", async () => {
const redisClient = redis.createClient();

await redisClient.set("test-1-count", "1");
await redisClient.set("test-1-lastUpdatedAt", new Date().toISOString());

await updateCount({
redisClient,
getNewEntities: async () => [{ createdAt: new Date() }],
lastUpdatedAtKey: "test-1-lastUpdatedAt",
countKey: "test-1-count",
});

expect(await redisClient.get("test-1-count")).toBe("2");
expect(await redisClient.get("test-1-lastUpdatedAt")).toBe(new Date().toISOString());
});
import { updateClansCount, updateGameServersCount, updateGameTypesCount, updateMapsCount, updatePlayersCount } from "./updateGlobalCounts";
import Redis from "ioredis-mock";
import { prismaMock } from "../../test/mockPrisma";
import { Clan, GameServer, GameType, Map, Player, RankMethod } from "@prisma/client";
import { getGlobalCounts } from "@teerank/teerank";

const mockPlayer: Player = {
createdAt: new Date(),
name: "test",
updatedAt: new Date(),
lastSeenAt: new Date(),
clanName: "test",
playTime: BigInt(1),
}

const mockClan: Clan = {
createdAt: new Date(),
name: "test",
updatedAt: new Date(),
playTime: BigInt(1),
activePlayerCount: 1,
}

const mockMap: Map = {
createdAt: new Date(),
name: "test",
playTime: BigInt(1),
playerCount: 1,
clanCount: 1,
gameServerCount: 1,
id: 1,
gameTypeName: "test",
}

const mockGameType: GameType = {
createdAt: new Date(),
name: "test",
playTime: BigInt(1),
playerCount: 1,
clanCount: 1,
gameServerCount: 1,
rankMethod: RankMethod.ELO,
mapCount: 1,
}

const mockGameServer: GameServer = {
createdAt: new Date(),
updatedAt: new Date(),
lastSeenAt: new Date(),
playTime: BigInt(1),
failureCount: 1,
ip: "127.0.0.1",
port: 1,
masterServerId: 1,
id: 1,
}

const redis = new Redis();

beforeEach(async () => {
await redis.flushall();
});

test("updatePlayersCount", async () => {
prismaMock.player.findMany.mockResolvedValue([mockPlayer]);

await updatePlayersCount(redis, new Date(0));
await updatePlayersCount(redis, new Date(0));

const globalCounts = await getGlobalCounts(redis);
expect(globalCounts.players).toBe(2);
});

test("updateClansCount", async () => {
prismaMock.clan.findMany.mockResolvedValue([mockClan]);

await updateClansCount(redis, new Date(0));
await updateClansCount(redis, new Date(0));

const globalCounts = await getGlobalCounts(redis);
expect(globalCounts.clans).toBe(2);
});

test("updateGameServersCount", async () => {
prismaMock.gameServer.findMany.mockResolvedValue([mockGameServer]);

await updateGameServersCount(redis, new Date(0));
await updateGameServersCount(redis, new Date(0));

const globalCounts = await getGlobalCounts(redis);
expect(globalCounts.gameServers).toBe(2);
});

test("updateMapsCount", async () => {
prismaMock.map.findMany.mockResolvedValue([mockMap]);

await updateMapsCount(redis, new Date(0));
await updateMapsCount(redis, new Date(0));

const globalCounts = await getGlobalCounts(redis);
expect(globalCounts.maps).toBe(2);
});

test("updateGameTypesCount", async () => {
prismaMock.gameType.findMany.mockResolvedValue([mockGameType]);

await updateGameTypesCount(redis, new Date(0));
await updateGameTypesCount(redis, new Date(0));

const globalCounts = await getGlobalCounts(redis);
expect(globalCounts.gameTypes).toBe(2);
});
Loading

0 comments on commit d410527

Please sign in to comment.