Skip to content

Commit d410527

Browse files
committed
Move to ioredis and mock prisma
1 parent 99a96c6 commit d410527

File tree

14 files changed

+498
-293
lines changed

14 files changed

+498
-293
lines changed

apps/worker/jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export default {
99
moduleFileExtensions: ['ts', 'js', 'html'],
1010
coverageDirectory: '../../coverage/apps/worker',
1111
setupFiles: ['./testSetup.ts'],
12+
setupFilesAfterEnv: ['./src/mockPrisma.ts'],
1213
};

apps/worker/src/redis.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { createClient } from 'redis';
21
import { REDIS_HOST, REDIS_PORT, REDIS_FAMILY } from '@teerank/teerank';
2+
import { Redis } from 'ioredis';
33

4-
export const redisClientPromise = createClient({
5-
url: `redis://${REDIS_HOST}:${REDIS_PORT}`,
6-
socket: {
7-
family: REDIS_FAMILY,
8-
},
9-
})
10-
.on('error', err => console.log('Redis Client Error', err))
11-
.connect();
4+
export const redis = new Redis({
5+
host: REDIS_HOST,
6+
port: REDIS_PORT,
7+
family: REDIS_FAMILY,
8+
});
129

13-
export type RedisClient = Awaited<typeof redisClientPromise>;
10+
redis.on('error', err => console.error('Redis Client Error', err));

apps/worker/src/workers/fillClanActivePlayerCount.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import { Worker } from "bullmq";
22
import { prisma } from "../prisma";
33
import { QUEUE_NAME_FILL_CLAN_ACTIVE_PLAYER_COUNT } from "@teerank/teerank";
44
import { bullmqConnection } from "@teerank/teerank";
5-
import { redisClientPromise } from "../redis";
5+
import { redis } from "../redis";
66
import { minutesToSeconds } from "date-fns";
77

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

1010
async function processor() {
11-
const redisClient = await redisClientPromise;
12-
13-
const minCreatedAt = new Date(Number(await redisClient.get(MIN_CREATED_AT_KEY) || "0"));
11+
const minCreatedAt = new Date(Number(await redis.get(MIN_CREATED_AT_KEY) || "0"));
1412
console.log(`Min created at: ${minCreatedAt}`);
1513

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

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

6462
return {
Lines changed: 111 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,112 @@
1-
import { updateCount } from "./updateGlobalCounts";
2-
import redis from "redis-mock";
3-
4-
describe("updateCounts", () => {
5-
test("create a new count", async () => {
6-
const redisClient = redis.createClient();
7-
8-
await updateCount({
9-
redisClient,
10-
getNewEntities: async () => [{ createdAt: new Date() }],
11-
lastUpdatedAtKey: "test-0-lastUpdatedAt",
12-
countKey: "test-0-count",
13-
});
14-
15-
expect(await redisClient.get("test-0-count")).toBe("1");
16-
expect(await redisClient.get("test-0-lastUpdatedAt")).toBe(new Date().toISOString());
17-
});
18-
19-
test("update an existing count", async () => {
20-
const redisClient = redis.createClient();
21-
22-
await redisClient.set("test-1-count", "1");
23-
await redisClient.set("test-1-lastUpdatedAt", new Date().toISOString());
24-
25-
await updateCount({
26-
redisClient,
27-
getNewEntities: async () => [{ createdAt: new Date() }],
28-
lastUpdatedAtKey: "test-1-lastUpdatedAt",
29-
countKey: "test-1-count",
30-
});
31-
32-
expect(await redisClient.get("test-1-count")).toBe("2");
33-
expect(await redisClient.get("test-1-lastUpdatedAt")).toBe(new Date().toISOString());
34-
});
1+
import { updateClansCount, updateGameServersCount, updateGameTypesCount, updateMapsCount, updatePlayersCount } from "./updateGlobalCounts";
2+
import Redis from "ioredis-mock";
3+
import { prismaMock } from "../../test/mockPrisma";
4+
import { Clan, GameServer, GameType, Map, Player, RankMethod } from "@prisma/client";
5+
import { getGlobalCounts } from "@teerank/teerank";
6+
7+
const mockPlayer: Player = {
8+
createdAt: new Date(),
9+
name: "test",
10+
updatedAt: new Date(),
11+
lastSeenAt: new Date(),
12+
clanName: "test",
13+
playTime: BigInt(1),
14+
}
15+
16+
const mockClan: Clan = {
17+
createdAt: new Date(),
18+
name: "test",
19+
updatedAt: new Date(),
20+
playTime: BigInt(1),
21+
activePlayerCount: 1,
22+
}
23+
24+
const mockMap: Map = {
25+
createdAt: new Date(),
26+
name: "test",
27+
playTime: BigInt(1),
28+
playerCount: 1,
29+
clanCount: 1,
30+
gameServerCount: 1,
31+
id: 1,
32+
gameTypeName: "test",
33+
}
34+
35+
const mockGameType: GameType = {
36+
createdAt: new Date(),
37+
name: "test",
38+
playTime: BigInt(1),
39+
playerCount: 1,
40+
clanCount: 1,
41+
gameServerCount: 1,
42+
rankMethod: RankMethod.ELO,
43+
mapCount: 1,
44+
}
45+
46+
const mockGameServer: GameServer = {
47+
createdAt: new Date(),
48+
updatedAt: new Date(),
49+
lastSeenAt: new Date(),
50+
playTime: BigInt(1),
51+
failureCount: 1,
52+
ip: "127.0.0.1",
53+
port: 1,
54+
masterServerId: 1,
55+
id: 1,
56+
}
57+
58+
const redis = new Redis();
59+
60+
beforeEach(async () => {
61+
await redis.flushall();
62+
});
63+
64+
test("updatePlayersCount", async () => {
65+
prismaMock.player.findMany.mockResolvedValue([mockPlayer]);
66+
67+
await updatePlayersCount(redis, new Date(0));
68+
await updatePlayersCount(redis, new Date(0));
69+
70+
const globalCounts = await getGlobalCounts(redis);
71+
expect(globalCounts.players).toBe(2);
72+
});
73+
74+
test("updateClansCount", async () => {
75+
prismaMock.clan.findMany.mockResolvedValue([mockClan]);
76+
77+
await updateClansCount(redis, new Date(0));
78+
await updateClansCount(redis, new Date(0));
79+
80+
const globalCounts = await getGlobalCounts(redis);
81+
expect(globalCounts.clans).toBe(2);
82+
});
83+
84+
test("updateGameServersCount", async () => {
85+
prismaMock.gameServer.findMany.mockResolvedValue([mockGameServer]);
86+
87+
await updateGameServersCount(redis, new Date(0));
88+
await updateGameServersCount(redis, new Date(0));
89+
90+
const globalCounts = await getGlobalCounts(redis);
91+
expect(globalCounts.gameServers).toBe(2);
92+
});
93+
94+
test("updateMapsCount", async () => {
95+
prismaMock.map.findMany.mockResolvedValue([mockMap]);
96+
97+
await updateMapsCount(redis, new Date(0));
98+
await updateMapsCount(redis, new Date(0));
99+
100+
const globalCounts = await getGlobalCounts(redis);
101+
expect(globalCounts.maps).toBe(2);
102+
});
103+
104+
test("updateGameTypesCount", async () => {
105+
prismaMock.gameType.findMany.mockResolvedValue([mockGameType]);
106+
107+
await updateGameTypesCount(redis, new Date(0));
108+
await updateGameTypesCount(redis, new Date(0));
109+
110+
const globalCounts = await getGlobalCounts(redis);
111+
expect(globalCounts.gameTypes).toBe(2);
35112
});

0 commit comments

Comments
 (0)