Use real sleeps

This commit is contained in:
Adria Navarro 2023-11-29 22:42:29 +01:00
parent 078384941a
commit db6517bc0c
1 changed files with 2 additions and 14 deletions

View File

@ -1,18 +1,8 @@
import { LockName, LockType } from "@budibase/types" import { LockName, LockType } from "@budibase/types"
import { doWithLock } from "../redlockImpl" import { doWithLock } from "../redlockImpl"
import { DBTestConfiguration } from "../../../tests" import { DBTestConfiguration } from "../../../tests"
import { Duration } from "../../utils"
describe("redlockImpl", () => { describe("redlockImpl", () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.runOnlyPendingTimers()
jest.useRealTimers()
})
describe("doWithLock", () => { describe("doWithLock", () => {
it("should execute the task and return the result", async () => { it("should execute the task and return the result", async () => {
const mockTask = jest.fn().mockResolvedValue("mockResult") const mockTask = jest.fn().mockResolvedValue("mockResult")
@ -21,16 +11,14 @@ describe("redlockImpl", () => {
const testOpts = { const testOpts = {
name: LockName.PERSIST_WRITETHROUGH, name: LockName.PERSIST_WRITETHROUGH,
type: LockType.AUTO_EXTEND, type: LockType.AUTO_EXTEND,
ttl: 30000, ttl: 5,
} }
// Call the function with the mock lock and task // Call the function with the mock lock and task
const config = new DBTestConfiguration() const config = new DBTestConfiguration()
const result = await config.doInTenant(() => const result = await config.doInTenant(() =>
doWithLock(testOpts, async () => { doWithLock(testOpts, async () => {
jest.advanceTimersByTime(Duration.fromSeconds(10).toMs()) await new Promise<void>(r => setTimeout(() => r(), 10))
jest.advanceTimersByTime(Duration.fromSeconds(10).toMs())
jest.advanceTimersByTime(Duration.fromSeconds(10).toMs())
return mockTask() return mockTask()
}) })
) )