Unverified Commit 314982ee authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Adapt Lockfile.acquireWithWait to the result-based lockfile API

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 4c575b05
...@@ -7,6 +7,31 @@ jest.mock('./output/log', () => ({ info: jest.fn(), warn: jest.fn() })) ...@@ -7,6 +7,31 @@ jest.mock('./output/log', () => ({ info: jest.fn(), warn: jest.fn() }))
const WAITING_MESSAGE = 'Waiting for the lock...' const WAITING_MESSAGE = 'Waiting for the lock...'
const nativeLockfile = { __napiType: 'Lockfile' }
const ACQUIRED = { type: 'acquired', lockfile: nativeLockfile }
const WOULD_BLOCK = { type: 'wouldBlock' }
function lockfileError({
transient = false,
unsupported = false,
}: {
transient?: boolean
unsupported?: boolean
}) {
return {
type: 'error',
error: { message: 'An IO error occurred', transient, unsupported },
}
}
function mockBindings(lockfileTryAcquireSync: jest.Mock) {
;(getBindingsSync as jest.Mock).mockReturnValue({
isWasm: false,
lockfileTryAcquireSync,
lockfileUnlock: jest.fn().mockResolvedValue(undefined),
lockfileUnlockSync: jest.fn(),
})
}
describe('Lockfile.unlock', () => { describe('Lockfile.unlock', () => {
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks() jest.clearAllMocks()
...@@ -18,7 +43,7 @@ describe('Lockfile.unlock', () => { ...@@ -18,7 +43,7 @@ describe('Lockfile.unlock', () => {
.mockRejectedValue(new Error('An IO error occurred')) .mockRejectedValue(new Error('An IO error occurred'))
;(getBindingsSync as jest.Mock).mockReturnValue({ ;(getBindingsSync as jest.Mock).mockReturnValue({
isWasm: false, isWasm: false,
lockfileTryAcquireSync: () => ({ type: 'acquired', lockfile: {} }), lockfileTryAcquireSync: () => ACQUIRED,
lockfileUnlock, lockfileUnlock,
}) })
...@@ -30,49 +55,55 @@ describe('Lockfile.unlock', () => { ...@@ -30,49 +55,55 @@ describe('Lockfile.unlock', () => {
}) })
describe('Lockfile.acquireWithWait', () => { describe('Lockfile.acquireWithWait', () => {
const lockfile = {} as Lockfile const acquiredLocks: Lockfile[] = []
let tryAcquire: jest.SpyInstance
async function acquireWithWait(
options: Partial<Parameters<typeof Lockfile.acquireWithWait>[1]> = {}
) {
const lockfile = await Lockfile.acquireWithWait('/lock', {
waitingMessage: WAITING_MESSAGE,
...options,
})
if (lockfile) acquiredLocks.push(lockfile)
return lockfile
}
beforeEach(() => { beforeEach(() => {
jest.useFakeTimers() jest.useFakeTimers()
jest.clearAllMocks() jest.clearAllMocks()
;(getBindingsSync as jest.Mock).mockReturnValue({ isWasm: false })
tryAcquire = jest.spyOn(Lockfile, 'tryAcquire')
}) })
afterEach(() => { afterEach(async () => {
tryAcquire.mockRestore() // Remove the exit listeners registered by `tryAcquire`.
await Promise.all(acquiredLocks.splice(0).map((lock) => lock.unlock()))
jest.useRealTimers() jest.useRealTimers()
}) })
it('waits silently for a lock that is released quickly', async () => { it('waits silently for a lock that is released quickly', async () => {
tryAcquire const tryAcquireSync = jest
.mockReturnValueOnce(undefined) .fn()
.mockReturnValueOnce(undefined) .mockReturnValueOnce(WOULD_BLOCK)
.mockReturnValue(lockfile) .mockReturnValueOnce(WOULD_BLOCK)
.mockReturnValue(ACQUIRED)
mockBindings(tryAcquireSync)
const acquired = Lockfile.acquireWithWait('/lock', { const acquired = acquireWithWait({ pollMs: 1, warnAfterMs: 10_000 })
pollMs: 1,
warnAfterMs: 10_000,
waitingMessage: WAITING_MESSAGE,
})
await jest.advanceTimersByTimeAsync(2) await jest.advanceTimersByTimeAsync(2)
await expect(acquired).resolves.toBe(lockfile) await expect(acquired).resolves.toBeInstanceOf(Lockfile)
expect(tryAcquire).toHaveBeenCalledTimes(3) expect(tryAcquireSync).toHaveBeenCalledTimes(3)
expect(Log.info).not.toHaveBeenCalled() expect(Log.info).not.toHaveBeenCalled()
expect(Log.warn).not.toHaveBeenCalled() expect(Log.warn).not.toHaveBeenCalled()
}) })
it('logs the waiting message once after the threshold', async () => { it('logs the waiting message once after the threshold', async () => {
let released = false let released = false
tryAcquire.mockImplementation(() => (released ? lockfile : undefined)) mockBindings(jest.fn(() => (released ? ACQUIRED : WOULD_BLOCK)))
const acquired = Lockfile.acquireWithWait('/lock', { const acquired = acquireWithWait({
pollMs: 1, pollMs: 1,
warnAfterMs: 10, warnAfterMs: 10,
timeoutMs: 10_000, timeoutMs: 10_000,
waitingMessage: WAITING_MESSAGE,
}) })
await jest.advanceTimersByTimeAsync(9) await jest.advanceTimersByTimeAsync(9)
expect(Log.info).not.toHaveBeenCalled() expect(Log.info).not.toHaveBeenCalled()
...@@ -80,20 +111,19 @@ describe('Lockfile.acquireWithWait', () => { ...@@ -80,20 +111,19 @@ describe('Lockfile.acquireWithWait', () => {
released = true released = true
await jest.advanceTimersByTimeAsync(1) await jest.advanceTimersByTimeAsync(1)
await expect(acquired).resolves.toBe(lockfile) await expect(acquired).resolves.toBeInstanceOf(Lockfile)
expect(Log.info).toHaveBeenCalledTimes(1) expect(Log.info).toHaveBeenCalledTimes(1)
expect(Log.info).toHaveBeenCalledWith(WAITING_MESSAGE) expect(Log.info).toHaveBeenCalledWith(WAITING_MESSAGE)
expect(Log.warn).not.toHaveBeenCalled() expect(Log.warn).not.toHaveBeenCalled()
}) })
it('proceeds without the lock after the timeout', async () => { it('proceeds without the lock after the timeout', async () => {
tryAcquire.mockReturnValue(undefined) mockBindings(jest.fn(() => WOULD_BLOCK))
const acquired = Lockfile.acquireWithWait('/lock', { const acquired = acquireWithWait({
pollMs: 1, pollMs: 1,
warnAfterMs: 5, warnAfterMs: 5,
timeoutMs: 20, timeoutMs: 20,
waitingMessage: WAITING_MESSAGE,
}) })
await jest.advanceTimersByTimeAsync(20) await jest.advanceTimersByTimeAsync(20)
...@@ -102,24 +132,46 @@ describe('Lockfile.acquireWithWait', () => { ...@@ -102,24 +132,46 @@ describe('Lockfile.acquireWithWait', () => {
expect(Log.warn).toHaveBeenCalledTimes(1) expect(Log.warn).toHaveBeenCalledTimes(1)
}) })
it('proceeds without the lock if it cannot be created', async () => { it('keeps waiting through transient errors', async () => {
tryAcquire.mockImplementation(() => { const tryAcquireSync = jest
throw new Error('An IO error occurred') .fn()
}) .mockReturnValueOnce(lockfileError({ transient: true }))
.mockReturnValue(ACQUIRED)
mockBindings(tryAcquireSync)
const acquired = acquireWithWait({ pollMs: 1 })
await jest.advanceTimersByTimeAsync(1)
await expect(acquired).resolves.toBeInstanceOf(Lockfile)
expect(tryAcquireSync).toHaveBeenCalledTimes(2)
expect(Log.warn).not.toHaveBeenCalled()
})
await expect( it('proceeds without the lock on other I/O errors', async () => {
Lockfile.acquireWithWait('/lock', { waitingMessage: WAITING_MESSAGE }) const tryAcquireSync = jest.fn(() => lockfileError({}))
).resolves.toBeUndefined() mockBindings(tryAcquireSync)
expect(tryAcquire).toHaveBeenCalledTimes(1)
await expect(acquireWithWait()).resolves.toBeUndefined()
expect(tryAcquireSync).toHaveBeenCalledTimes(1)
expect(Log.warn).not.toHaveBeenCalled() expect(Log.warn).not.toHaveBeenCalled()
}) })
it('proceeds without locking if it is unsupported', async () => {
const tryAcquireSync = jest.fn(() => lockfileError({ unsupported: true }))
mockBindings(tryAcquireSync)
await expect(acquireWithWait()).resolves.toBeInstanceOf(Lockfile)
expect(tryAcquireSync).toHaveBeenCalledTimes(1)
})
it('proceeds without the lock with WASM bindings', async () => { it('proceeds without the lock with WASM bindings', async () => {
;(getBindingsSync as jest.Mock).mockReturnValue({ isWasm: true }) const tryAcquireSync = jest.fn()
;(getBindingsSync as jest.Mock).mockReturnValue({
isWasm: true,
lockfileTryAcquireSync: tryAcquireSync,
})
await expect( await expect(acquireWithWait()).resolves.toBeUndefined()
Lockfile.acquireWithWait('/lock', { waitingMessage: WAITING_MESSAGE }) expect(tryAcquireSync).not.toHaveBeenCalled()
).resolves.toBeUndefined()
expect(tryAcquire).not.toHaveBeenCalled()
}) })
}) })
...@@ -274,8 +274,10 @@ export class Lockfile { ...@@ -274,8 +274,10 @@ export class Lockfile {
* `warnAfterMs`. * `warnAfterMs`.
* - Returns `undefined` (after a warning) once `timeoutMs` elapses, so the * - Returns `undefined` (after a warning) once `timeoutMs` elapses, so the
* caller can proceed without the lock. * caller can proceed without the lock.
* - Returns `undefined` if the lockfile can't be created, e.g. on filesystems * - Keeps waiting through transient I/O errors, like a held lock.
* without lock support. Callers must be safe to run without the lock. * - Returns `undefined` on other I/O errors, and the dummy `Lockfile` from
* `tryAcquire` when filesystem locking is unsupported. Callers must be safe
* to run without the lock.
*/ */
static async acquireWithWait( static async acquireWithWait(
path: string, path: string,
...@@ -303,8 +305,11 @@ export class Lockfile { ...@@ -303,8 +305,11 @@ export class Lockfile {
try { try {
lockfile = Lockfile.tryAcquire(path) lockfile = Lockfile.tryAcquire(path)
} catch (e) { } catch (e) {
debugLockfile(`Proceeding without lockfile ${path}: ${e}`) if (!(e instanceof LockfileError) || !e.transient) {
return undefined debugLockfile(`Proceeding without lockfile ${path}: ${e}`)
return undefined
}
debugLockfile(`Transient error acquiring lockfile ${path}: ${e}`)
} }
if (lockfile !== undefined) { if (lockfile !== undefined) {
return lockfile return lockfile
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment