-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renaming Lock to Mutex and use appConfig
- Loading branch information
Showing
8 changed files
with
84 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const {AcquireTimeoutError, MutexService} = require('../lib/MutexService') | ||
test('release will unlock the queue', async () => { | ||
const mutexService = new MutexService() | ||
expect(mutexService.locked).toBe(false) | ||
|
||
await mutexService.acquire(() => { | ||
expect(mutexService.locked).toBe(true) | ||
}) | ||
|
||
expect(mutexService.locked).toBe(false) | ||
}) | ||
|
||
test('call lock twice will fill the queue', async () => { | ||
let oneCalled = false | ||
let twoCalled = false | ||
const mutexService = new MutexService() | ||
const one = mutexService.acquire(() => oneCalled = true) | ||
const two = mutexService.acquire(() => twoCalled = true) | ||
|
||
expect(mutexService.queue).toHaveLength(1) | ||
expect(mutexService.locked).toBe(true) | ||
|
||
await one | ||
await two | ||
expect(oneCalled).toBe(true) | ||
expect(twoCalled).toBe(true) | ||
expect(mutexService.queue).toHaveLength(0) | ||
expect(mutexService.locked).toBe(false) | ||
}) | ||
|
||
test('lock timeout will trow an error if locked by another "process" for too long', async () => { | ||
expect.assertions(1); | ||
|
||
const mutexService = new MutexService() | ||
|
||
await mutexService.acquire(async () => { | ||
try { | ||
await mutexService.acquire(() => {}, 1) | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(AcquireTimeoutError) | ||
} | ||
}) | ||
}); | ||
|
||
test('lock timeout will remove it from queue', async () => { | ||
expect.assertions(2); | ||
|
||
const mutexService = new MutexService() | ||
|
||
await mutexService.acquire(async () => { | ||
try { | ||
await mutexService.acquire(() => { | ||
}, 1) | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(AcquireTimeoutError) | ||
expect(mutexService.queue).toHaveLength(0); | ||
} | ||
|
||
}) | ||
}) | ||
|