Add and dry tests
This commit is contained in:
parent
db6517bc0c
commit
c86d949680
|
@ -1,32 +1,98 @@
|
||||||
import { LockName, LockType } from "@budibase/types"
|
import { LockName, LockType, LockOptions } from "@budibase/types"
|
||||||
|
import tk from "timekeeper"
|
||||||
import { doWithLock } from "../redlockImpl"
|
import { doWithLock } from "../redlockImpl"
|
||||||
import { DBTestConfiguration } from "../../../tests"
|
import { DBTestConfiguration, generator } from "../../../tests"
|
||||||
|
|
||||||
|
tk.reset()
|
||||||
|
|
||||||
describe("redlockImpl", () => {
|
describe("redlockImpl", () => {
|
||||||
describe("doWithLock", () => {
|
describe("doWithLock", () => {
|
||||||
it("should execute the task and return the result", async () => {
|
const config = new DBTestConfiguration()
|
||||||
const mockTask = jest.fn().mockResolvedValue("mockResult")
|
const lockTtl = 30
|
||||||
|
|
||||||
// Define test options
|
function runLockWithExecutionTime({
|
||||||
const testOpts = {
|
opts,
|
||||||
name: LockName.PERSIST_WRITETHROUGH,
|
task,
|
||||||
type: LockType.AUTO_EXTEND,
|
executionTimeMs,
|
||||||
ttl: 5,
|
}: {
|
||||||
}
|
opts: LockOptions
|
||||||
|
task: () => Promise<string>
|
||||||
// Call the function with the mock lock and task
|
executionTimeMs: number
|
||||||
const config = new DBTestConfiguration()
|
}) {
|
||||||
const result = await config.doInTenant(() =>
|
return config.doInTenant(() =>
|
||||||
doWithLock(testOpts, async () => {
|
doWithLock(opts, async () => {
|
||||||
await new Promise<void>(r => setTimeout(() => r(), 10))
|
await new Promise<void>(r => setTimeout(() => r(), executionTimeMs))
|
||||||
return mockTask()
|
return task()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
it.each(Object.values(LockType))(
|
||||||
|
"should return the task value",
|
||||||
|
async (lockType: LockType) => {
|
||||||
|
const expectedResult = generator.guid()
|
||||||
|
const mockTask = jest.fn().mockResolvedValue(expectedResult)
|
||||||
|
|
||||||
|
const opts = {
|
||||||
|
name: LockName.PERSIST_WRITETHROUGH,
|
||||||
|
type: lockType,
|
||||||
|
ttl: lockTtl,
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await runLockWithExecutionTime({
|
||||||
|
opts,
|
||||||
|
task: mockTask,
|
||||||
|
executionTimeMs: 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.executed).toBe(true)
|
||||||
|
expect(result.executed && result.result).toBe(expectedResult)
|
||||||
|
expect(mockTask).toHaveBeenCalledTimes(1)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("should extend when type is autoextend", async () => {
|
||||||
|
const expectedResult = generator.guid()
|
||||||
|
const mockTask = jest.fn().mockResolvedValue(expectedResult)
|
||||||
|
|
||||||
|
const opts = {
|
||||||
|
name: LockName.PERSIST_WRITETHROUGH,
|
||||||
|
type: LockType.AUTO_EXTEND,
|
||||||
|
ttl: lockTtl,
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await runLockWithExecutionTime({
|
||||||
|
opts,
|
||||||
|
task: mockTask,
|
||||||
|
executionTimeMs: lockTtl * 2,
|
||||||
|
})
|
||||||
|
|
||||||
// Assert the result and verify function calls
|
|
||||||
expect(result.executed).toBe(true)
|
expect(result.executed).toBe(true)
|
||||||
expect(result.executed && result.result).toBe("mockResult")
|
expect(result.executed && result.result).toBe(expectedResult)
|
||||||
expect(mockTask).toHaveBeenCalledTimes(1)
|
expect(mockTask).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it.each(Object.values(LockType).filter(t => t !== LockType.AUTO_EXTEND))(
|
||||||
|
"should timeout when type is %s",
|
||||||
|
async (lockType: LockType) => {
|
||||||
|
const mockTask = jest.fn().mockResolvedValue("mockResult")
|
||||||
|
|
||||||
|
const opts = {
|
||||||
|
name: LockName.PERSIST_WRITETHROUGH,
|
||||||
|
type: lockType,
|
||||||
|
ttl: lockTtl,
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
runLockWithExecutionTime({
|
||||||
|
opts,
|
||||||
|
task: mockTask,
|
||||||
|
executionTimeMs: lockTtl * 2,
|
||||||
|
})
|
||||||
|
).rejects.toThrowError(
|
||||||
|
`Unable to fully release the lock on resource \"lock:${config.tenantId}_persist_writethrough\".`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue