Test with spies
This commit is contained in:
parent
8e8378d1be
commit
61c4b83650
|
@ -1,11 +1,11 @@
|
||||||
|
import { AnyDocument, Database, LockName, LockType } from "@budibase/types"
|
||||||
import BaseCache from "./base"
|
import BaseCache from "./base"
|
||||||
import { getDocWritethroughClient } from "../redis/init"
|
import { getDocWritethroughClient } from "../redis/init"
|
||||||
import { AnyDocument, Database, LockName, LockType } from "@budibase/types"
|
|
||||||
|
|
||||||
import { JobQueue, createQueue } from "../queue"
|
import { JobQueue, createQueue } from "../queue"
|
||||||
import * as dbUtils from "../db"
|
import * as dbUtils from "../db"
|
||||||
import { Duration, newid } from "../utils"
|
import { Duration, newid } from "../utils"
|
||||||
import { context, locks } from ".."
|
import { locks } from ".."
|
||||||
|
|
||||||
let CACHE: BaseCache | null = null
|
let CACHE: BaseCache | null = null
|
||||||
async function getCache() {
|
async function getCache() {
|
||||||
|
@ -36,6 +36,8 @@ export const docWritethroughProcessorQueue = createQueue<ProcessDocMessage>(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class DocWritethroughProcessor {
|
||||||
|
init() {
|
||||||
docWritethroughProcessorQueue.process(async message => {
|
docWritethroughProcessorQueue.process(async message => {
|
||||||
const { cacheKeyPrefix, messageId } = message.data
|
const { cacheKeyPrefix, messageId } = message.data
|
||||||
|
|
||||||
|
@ -64,7 +66,7 @@ docWritethroughProcessorQueue.process(async message => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await persistToDb(cache, message.data)
|
await this.persistToDb(cache, message.data)
|
||||||
console.log("DocWritethrough persisted", { data: message.data })
|
console.log("DocWritethrough persisted", { data: message.data })
|
||||||
|
|
||||||
await cache.deleteIfValue(
|
await cache.deleteIfValue(
|
||||||
|
@ -78,8 +80,10 @@ docWritethroughProcessorQueue.process(async message => {
|
||||||
throw new Error(`Ignoring redlock conflict in write-through cache`)
|
throw new Error(`Ignoring redlock conflict in write-through cache`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
export async function persistToDb(
|
private async persistToDb(
|
||||||
cache: BaseCache,
|
cache: BaseCache,
|
||||||
{
|
{
|
||||||
dbName,
|
dbName,
|
||||||
|
@ -113,6 +117,9 @@ export async function persistToDb(
|
||||||
await cache.delete(key, { useTenancy: false })
|
await cache.delete(key, { useTenancy: false })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const processor = new DocWritethroughProcessor().init()
|
||||||
|
|
||||||
export class DocWritethrough {
|
export class DocWritethrough {
|
||||||
private db: Database
|
private db: Database
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
|
import _ from "lodash"
|
||||||
import { DBTestConfiguration, generator, structures } from "../../../tests"
|
import { DBTestConfiguration, generator, structures } from "../../../tests"
|
||||||
import { getDB } from "../../db"
|
import { getDB } from "../../db"
|
||||||
import _ from "lodash"
|
|
||||||
|
|
||||||
import {
|
import { DocWritethrough, processor } from "../docWritethrough"
|
||||||
DocWritethrough,
|
|
||||||
docWritethroughProcessorQueue,
|
|
||||||
} from "../docWritethrough"
|
|
||||||
import InMemoryQueue from "../../queue/inMemoryQueue"
|
import InMemoryQueue from "../../queue/inMemoryQueue"
|
||||||
|
import { docWritethroughProcessorQueue } from "../docWritethrough"
|
||||||
|
|
||||||
const WRITE_RATE_MS = 1000
|
const WRITE_RATE_MS = 1000
|
||||||
|
|
||||||
|
@ -240,12 +239,13 @@ describe("docWritethrough", () => {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
const persistToDbSpy = jest.spyOn(processor as any, "persistToDb")
|
||||||
const storeToCacheSpy = jest.spyOn(docWritethrough as any, "storeToCache")
|
const storeToCacheSpy = jest.spyOn(docWritethrough as any, "storeToCache")
|
||||||
|
|
||||||
await config.doInTenant(async () => {
|
await config.doInTenant(async () => {
|
||||||
await parallelPatch(5)
|
await parallelPatch(5)
|
||||||
expect(storeToCacheSpy).toBeCalledTimes(5)
|
expect(storeToCacheSpy).toBeCalledTimes(5)
|
||||||
|
expect(persistToDbSpy).not.toBeCalled()
|
||||||
expect(await db.exists(documentId)).toBe(false)
|
expect(await db.exists(documentId)).toBe(false)
|
||||||
|
|
||||||
await travelForward(WRITE_RATE_MS)
|
await travelForward(WRITE_RATE_MS)
|
||||||
|
@ -253,7 +253,7 @@ describe("docWritethrough", () => {
|
||||||
await parallelPatch(40)
|
await parallelPatch(40)
|
||||||
|
|
||||||
expect(storeToCacheSpy).toBeCalledTimes(45)
|
expect(storeToCacheSpy).toBeCalledTimes(45)
|
||||||
|
expect(persistToDbSpy).toBeCalledTimes(1)
|
||||||
// Ideally we want to spy on persistToDb from ./docWritethrough, but due our barrel files configuration required quite of a complex setup.
|
// Ideally we want to spy on persistToDb from ./docWritethrough, but due our barrel files configuration required quite of a complex setup.
|
||||||
// We are relying on the document being stored only once (otherwise we would have _rev updated)
|
// We are relying on the document being stored only once (otherwise we would have _rev updated)
|
||||||
expect(await db.get(documentId)).toEqual(
|
expect(await db.get(documentId)).toEqual(
|
||||||
|
|
Loading…
Reference in New Issue