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 { getDocWritethroughClient } from "../redis/init"
|
||||
import { AnyDocument, Database, LockName, LockType } from "@budibase/types"
|
||||
|
||||
import { JobQueue, createQueue } from "../queue"
|
||||
import * as dbUtils from "../db"
|
||||
import { Duration, newid } from "../utils"
|
||||
import { context, locks } from ".."
|
||||
import { locks } from ".."
|
||||
|
||||
let CACHE: BaseCache | null = null
|
||||
async function getCache() {
|
||||
|
@ -36,6 +36,8 @@ export const docWritethroughProcessorQueue = createQueue<ProcessDocMessage>(
|
|||
}
|
||||
)
|
||||
|
||||
class DocWritethroughProcessor {
|
||||
init() {
|
||||
docWritethroughProcessorQueue.process(async message => {
|
||||
const { cacheKeyPrefix, messageId } = message.data
|
||||
|
||||
|
@ -64,7 +66,7 @@ docWritethroughProcessorQueue.process(async message => {
|
|||
return
|
||||
}
|
||||
|
||||
await persistToDb(cache, message.data)
|
||||
await this.persistToDb(cache, message.data)
|
||||
console.log("DocWritethrough persisted", { data: message.data })
|
||||
|
||||
await cache.deleteIfValue(
|
||||
|
@ -78,8 +80,10 @@ docWritethroughProcessorQueue.process(async message => {
|
|||
throw new Error(`Ignoring redlock conflict in write-through cache`)
|
||||
}
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
export async function persistToDb(
|
||||
private async persistToDb(
|
||||
cache: BaseCache,
|
||||
{
|
||||
dbName,
|
||||
|
@ -113,6 +117,9 @@ export async function persistToDb(
|
|||
await cache.delete(key, { useTenancy: false })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const processor = new DocWritethroughProcessor().init()
|
||||
|
||||
export class DocWritethrough {
|
||||
private db: Database
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
import _ from "lodash"
|
||||
import { DBTestConfiguration, generator, structures } from "../../../tests"
|
||||
import { getDB } from "../../db"
|
||||
import _ from "lodash"
|
||||
|
||||
import {
|
||||
DocWritethrough,
|
||||
docWritethroughProcessorQueue,
|
||||
} from "../docWritethrough"
|
||||
import { DocWritethrough, processor } from "../docWritethrough"
|
||||
|
||||
import InMemoryQueue from "../../queue/inMemoryQueue"
|
||||
import { docWritethroughProcessorQueue } from "../docWritethrough"
|
||||
|
||||
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")
|
||||
|
||||
await config.doInTenant(async () => {
|
||||
await parallelPatch(5)
|
||||
expect(storeToCacheSpy).toBeCalledTimes(5)
|
||||
expect(persistToDbSpy).not.toBeCalled()
|
||||
expect(await db.exists(documentId)).toBe(false)
|
||||
|
||||
await travelForward(WRITE_RATE_MS)
|
||||
|
@ -253,7 +253,7 @@ describe("docWritethrough", () => {
|
|||
await parallelPatch(40)
|
||||
|
||||
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.
|
||||
// We are relying on the document being stored only once (otherwise we would have _rev updated)
|
||||
expect(await db.get(documentId)).toEqual(
|
||||
|
|
Loading…
Reference in New Issue