Skip to content

Commit d1d7675

Browse files
mchangrhajayyy
andcommitted
test fixes
test fixes - fix timeout in redis (by @ajayyy) - allow "errors" in tempVIP test - remove duplicate warning in postSkipSegments - remove duplicate VIP in tempVIP - run tests against different user once tempVIP removed - fix typo in getHashCache fetching syntax and wording - use standard syntax in redisTest - fix spacing in getLockReason - typo in npm script name test cases - add getHashCache test case - add more tests to redisTest configuration - update config to use redis timeout - update docker-compose to use newest pinned version Co-Authored-By: Ajay Ramachandran <[email protected]>
1 parent ab6fcb8 commit d1d7675

File tree

12 files changed

+88
-30
lines changed

12 files changed

+88
-30
lines changed

ci.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
"port": 5432
1818
},
1919
"redis": {
20+
"enabled": true,
2021
"socket": {
2122
"host": "localhost",
2223
"port": 6379
23-
}
24+
},
25+
"expiryTime": 86400
2426
},
2527
"createDatabaseIfNotExist": true,
2628
"schemaFolder": "./databases",

docker/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3'
22
services:
33
database:
44
container_name: database
5-
image: postgres:13
5+
image: postgres:14
66
env_file:
77
- database.env
88
volumes:
@@ -12,7 +12,7 @@ services:
1212
restart: always
1313
redis:
1414
container_name: redis
15-
image: redis:6.0
15+
image: redis:7.0
1616
command: /usr/local/etc/redis/redis.conf
1717
volumes:
1818
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "src/index.ts",
66
"scripts": {
77
"test": "npm run tsc && ts-node test/test.ts",
8-
"test:coverate": "nyc npm run test",
8+
"test:coverage": "nyc npm run test",
99
"dev": "nodemon",
1010
"dev:bash": "nodemon -x 'npm test ; npm start'",
1111
"postgres:docker": "docker run --rm -p 5432:5432 -e POSTGRES_USER=ci_db_user -e POSTGRES_PASSWORD=ci_db_pass postgres:alpine",

src/routes/dumpDatabase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { config } from "../config";
55
import util from "util";
66
import fs from "fs";
77
import path from "path";
8-
import { ChildProcess, exec, ExecOptions, spawn } from "child_process";
8+
import { exec, ExecOptions } from "child_process";
99
const unlink = util.promisify(fs.unlink);
1010

1111
const ONE_MINUTE = 1000 * 60;
@@ -44,7 +44,7 @@ const credentials: ExecOptions = {
4444
PGPASSWORD: String(config.postgres.password),
4545
PGDATABASE: "sponsorTimes",
4646
}
47-
}
47+
};
4848

4949
interface TableDumpList {
5050
fileName: string;
@@ -232,7 +232,7 @@ async function queueDump(): Promise<void> {
232232

233233
resolve(error ? stderr : stdout);
234234
});
235-
})
235+
});
236236

237237
dumpFiles.push({
238238
fileName,

src/utils/getHashCache.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ async function getFromRedis<T extends string>(key: HashedValue): Promise<T & Has
2626
Logger.debug(`Got data from redis: ${reply}`);
2727
return reply as T & HashedValue;
2828
}
29-
} catch (e) {} // eslint-disable-line no-empty
29+
} catch (err) {
30+
Logger.error(err as string);
31+
}
3032

3133
// Otherwise, calculate it
3234
const data = getHash(key, cachedHashTimes);
33-
redis.set(key, data).catch((err) => Logger.error(err));
35+
redis.set(redisKey, data).catch((err) => Logger.error(err));
3436

3537
return data as T & HashedValue;
3638
}

src/utils/redis.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if (config.redis?.enabled) {
3333

3434
const get = client.get.bind(client);
3535
exportClient.get = (key) => new Promise((resolve, reject) => {
36-
const timeout = setTimeout(() => reject(), config.redis.getTimeout);
36+
const timeout = config.redis.getTimeout ? setTimeout(() => reject(), config.redis.getTimeout) : null;
3737
get(key).then((reply) => {
3838
clearTimeout(timeout);
3939
resolve(reply);
@@ -48,7 +48,10 @@ if (config.redis?.enabled) {
4848
.catch((err) => reject(err))
4949
);
5050
client.on("error", function(error) {
51-
Logger.error(error);
51+
Logger.error(`Redis Error: ${error}`);
52+
});
53+
client.on("reconnect", () => {
54+
Logger.info("Redis: trying to reconnect");
5255
});
5356
}
5457

test/cases/getHashCache.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { config } from "../../src/config";
2+
import { getHashCache } from "../../src/utils/getHashCache";
3+
import { shaHashKey } from "../../src/utils/redisKeys";
4+
import { getHash } from "../../src/utils/getHash";
5+
import redis from "../../src/utils/redis";
6+
import crypto from "crypto";
7+
import assert from "assert";
8+
import { setTimeout } from "timers/promises";
9+
10+
const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
11+
12+
const rand1Hash = genRandom(24);
13+
const rand1Hash_Key = getHash(rand1Hash, 1);
14+
const rand1Hash_Result = getHash(rand1Hash);
15+
16+
describe("getHashCache test", function() {
17+
before(function() {
18+
if (!config.redis?.enabled) this.skip();
19+
});
20+
it("Should set hashKey and be able to retreive", (done) => {
21+
const redisKey = shaHashKey(rand1Hash_Key);
22+
getHashCache(rand1Hash)
23+
.then(() => setTimeout(50)) // add timeout for redis to complete async
24+
.then(() => redis.get(redisKey))
25+
.then(result => {
26+
assert.strictEqual(result, rand1Hash_Result);
27+
done();
28+
})
29+
.catch(err => done(err === undefined ? "no set value" : err));
30+
}).timeout(5000);
31+
});

test/cases/getLockReason.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("getLockReason", () => {
3131
});
3232

3333
after(async () => {
34-
const deleteUserNameQuery = 'DELETE FROM "userNames" WHERE "userID" = ? AND "userName" = ? LIMIT 1';
34+
const deleteUserNameQuery = 'DELETE FROM "userNames" WHERE "userID" = ? AND "userName" = ?';
3535
await db.prepare("run", deleteUserNameQuery, [vipUserID1, vipUserName1]);
3636
await db.prepare("run", deleteUserNameQuery, [vipUserID2, vipUserName2]);
3737
});

test/cases/postSkipSegments.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ describe("postSkipSegments", () => {
9393
db.prepare("run", insertWarningQuery, [warnUser01Hash, warnVip01Hash, 1, reason01, (now - 3601000)]);
9494
// User 2
9595
db.prepare("run", insertWarningQuery, [warnUser02Hash, warnVip01Hash, 1, reason02, now]);
96-
db.prepare("run", insertWarningQuery, [warnUser02Hash, warnVip01Hash, 1, reason02, now]);
9796
db.prepare("run", insertWarningQuery, [warnUser02Hash, warnVip01Hash, 1, reason02, (now - (warningExpireTime + 1000))]);
9897
db.prepare("run", insertWarningQuery, [warnUser02Hash, warnVip01Hash, 1, reason02, (now - (warningExpireTime + 2000))]);
9998
// User 3

test/cases/redisTest.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
88
const randKey1 = genRandom();
99
const randValue1 = genRandom();
1010
const randKey2 = genRandom(16);
11+
const randKey3 = genRandom();
12+
const randValue3 = genRandom();
1113

1214
describe("redis test", function() {
1315
before(async function() {
@@ -19,13 +21,41 @@ describe("redis test", function() {
1921
.then(res => {
2022
assert.strictEqual(res, randValue1);
2123
done();
22-
}).catch(err => assert.fail(err));
24+
}).catch(err => done(err));
2325
});
2426
it("Should not be able to get not stored value", (done) => {
2527
redis.get(randKey2)
2628
.then(res => {
27-
if (res) assert.fail("Value should not be found");
29+
if (res) done("Value should not be found");
2830
done();
29-
}).catch(err => assert.fail(err));
31+
}).catch(err => done(err));
32+
});
33+
it("Should be able to delete stored value", (done) => {
34+
redis.del(randKey1)
35+
.then(() => {
36+
redis.get(randKey1)
37+
.then(res => {
38+
assert.strictEqual(res, null);
39+
done();
40+
}).catch(err => done(err));
41+
}).catch(err => done(err));
42+
});
43+
it("Should be able to set expiring value", (done) => {
44+
redis.setEx(randKey3, 8400, randValue3)
45+
.then(() => {
46+
redis.get(randKey3)
47+
.then(res => {
48+
assert.strictEqual(res, randValue3);
49+
done();
50+
}).catch(err => done(err));
51+
}).catch(err => done(err));
52+
});
53+
it("Should continue when undefined value is fetched", (done) => {
54+
const undefkey = `undefined.${genRandom()}`;
55+
redis.get(undefkey)
56+
.then(result => {
57+
assert.ok(!result); // result should be falsy
58+
done();
59+
});
3060
});
3161
});

0 commit comments

Comments
 (0)