Fix insert-score tests to reflect the new cache safety

This commit is contained in:
zkldi
2021-04-26 00:52:37 +01:00
parent a791b0739c
commit 5a8ddca8cf
2 changed files with 33 additions and 7 deletions
@@ -15,11 +15,11 @@ t.test("#QueueScoreInsert, #InsertQueue", async (t) => {
t.test("Single Queue Test", async (t) => {
// fake score doc
let res = await QueueScoreInsert(({ testDocument: "foo" } as unknown) as ScoreDocument);
let res = await QueueScoreInsert(({ scoreID: "foo" } as unknown) as ScoreDocument);
t.equal(
res,
null,
true,
"QueueScoreInsert should not insert a score when the queue is not full."
);
@@ -29,7 +29,7 @@ t.test("#QueueScoreInsert, #InsertQueue", async (t) => {
t.equal(flushSize, 1, "QueueScoreInsert should append the score to the queue.");
let dbRes = await db.scores.find({
testDocument: "foo",
scoreID: "foo",
});
t.equal(
@@ -48,11 +48,12 @@ t.test("#QueueScoreInsert, #InsertQueue", async (t) => {
t.test("Queue Overflow Test", async (t) => {
for (let i = 0; i < 499; i++) {
// eslint-disable-next-line no-await-in-loop
await QueueScoreInsert(({ testDocument: "foo" } as unknown) as ScoreDocument);
await QueueScoreInsert(({ scoreID: i, chartID: "test" } as unknown) as ScoreDocument);
}
let overflowRes = await QueueScoreInsert(({
testDocument: "foo",
scoreID: "foo",
chartID: "test",
} as unknown) as ScoreDocument);
t.equal(
@@ -66,7 +67,8 @@ t.test("#QueueScoreInsert, #InsertQueue", async (t) => {
t.equal(flushRes, 0, "The queue should now be empty.");
let dbRes = await db.scores.find({
testDocument: "foo",
scoreID: { $exists: true },
chartID: "test",
});
t.equal(
@@ -78,6 +80,30 @@ t.test("#QueueScoreInsert, #InsertQueue", async (t) => {
t.end();
});
r = await InsertQueue(); // flush queue just incase former test fails.
t.equal(r, 0, "Queue should be empty after test.");
t.test("Queue Dedupe Test", async (t) => {
await QueueScoreInsert(({ scoreID: 1, chartID: "foo" } as unknown) as ScoreDocument);
let r2 = await QueueScoreInsert(({
scoreID: 1,
chartID: "foo",
} as unknown) as ScoreDocument);
t.equal(r2, null, "Should return null when a duplicate scoreID is submitted");
let flushRes = await InsertQueue();
t.equal(flushRes, 1, "Should flush 1 score document");
let dbRes = await db.scores.find({
chartID: "foo",
});
t.equal(dbRes.length, 1, "Should only insert one document");
});
t.end();
});
@@ -5,7 +5,7 @@ import CreateLogCtx from "../../../logger";
const logger = CreateLogCtx("insert-score.ts");
let ScoreQueue: ScoreDocument[] = [];
export let ScoreIDs: Set<string> = new Set();
const MAX_PIPELINE_LENGTH = 10;
const MAX_PIPELINE_LENGTH = 500;
/**
* Adds a score to a queue to be inserted in batch to the database.