forked from DirtyHairy/async-mutex
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/generic queues #1
Open
PanopticaRising
wants to merge
6
commits into
master
Choose a base branch
from
feature/generic-queues
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6df7056
test!: Added esModuleInterop flag to import 'assert' as a module.
PanopticaRising 1fd91c8
feat: Added the ability to specify a queue to the Semaphore and Mutex…
PanopticaRising 3038a06
test: Added tests to validate that explicitly setting the queue, eith…
PanopticaRising 7d8b5e2
fix: Fixed default typings on the Semaphore.
PanopticaRising bf00f4d
fix: Fixed typing issues with generic mutexes and withTimeout. Potent…
PanopticaRising 7229614
Merge pull request #2 from rederlyhq/feature/adding-custom-queue-tests
PanopticaRising File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 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,109 @@ | ||
import Semaphore, { QueueEntry, QueueLike } from '../src/Semaphore'; | ||
import TinyQueue from "tinyqueue"; | ||
import { semaphoreSuite } from './semaphore'; | ||
import { mutexSuite } from './mutex'; | ||
import Mutex from '../src/Mutex'; | ||
import assert from 'assert'; | ||
import { InstalledClock, install } from '@sinonjs/fake-timers'; | ||
import SemaphoreInterface from '../src/SemaphoreInterface'; | ||
|
||
export default class HeapHelper<T> implements QueueLike<T> { | ||
constructor(private _queue: TinyQueue<T> = new TinyQueue()) { | ||
} | ||
|
||
pop = (): T | undefined => this._queue.pop(); | ||
|
||
push = (...items: T[]): number => { | ||
for (const item of items) { | ||
this._queue.push(item); | ||
} | ||
return items.length; | ||
} | ||
|
||
shift = this.pop; | ||
|
||
unshift = this.push; | ||
|
||
get length(): number { | ||
return this._queue.length; | ||
} | ||
|
||
set length(n: number) { | ||
this._queue.length = n; | ||
this._queue.data.length = n; | ||
} | ||
|
||
forEach = (callbackfn: (value: T, index: number, array: T[]) => void): void => { | ||
this._queue.data.forEach((value, index, array) => { | ||
callbackfn(value, index, array); | ||
}); | ||
} | ||
|
||
toString = (): string => JSON.stringify(this._queue.data); | ||
} | ||
|
||
suite('Semaphore with Priority Queue', () => { | ||
const priorityQueue = new HeapHelper<QueueEntry>(); | ||
semaphoreSuite((maxConcurrency: number, err?: Error) => new Semaphore(maxConcurrency, err, priorityQueue)); | ||
|
||
// These tests validate the expected behavior of TinyQueue + Semaphore. | ||
suite('TinyQueue Implementation Tests', () => { | ||
let semaphore: SemaphoreInterface<number>; | ||
let clock: InstalledClock; | ||
const maxPriorityQueue = new TinyQueue<QueueEntry<number>>([], (a, b) => b.data - a.data); | ||
const heap = new HeapHelper<QueueEntry<number>>(maxPriorityQueue); | ||
|
||
setup(() => { | ||
clock = install(); | ||
semaphore = new Semaphore(2, undefined, heap); | ||
}); | ||
|
||
teardown(() => clock.uninstall()); | ||
|
||
test('Semaphore releases higher priority tasks first', async () => { | ||
|
||
const [, release1] = await semaphore.acquire(0); | ||
const [,] = await semaphore.acquire(2); | ||
let prio5Finished = false; | ||
let prio1Finished = false; | ||
let prio10Finished = false; | ||
|
||
(async () => { | ||
await semaphore.acquire(5); | ||
prio5Finished = true; | ||
})(); | ||
|
||
(async () => { | ||
await semaphore.acquire(1); | ||
prio1Finished = true; | ||
})(); | ||
|
||
(async () => { | ||
await semaphore.acquire(10); | ||
prio10Finished = true; | ||
})(); | ||
|
||
release1(); | ||
await clock.tickAsync(1); | ||
|
||
assert(prio5Finished === false, 'Priority 5 finished before Priority 10 AND Priority 1.'); | ||
assert(prio1Finished === false, 'Priority 1 finished before Priority 10.'); | ||
//@ts-expect-error Typescript doesn't know if a promise will run before this. | ||
assert(prio10Finished === true, 'Priority 10 was not completed after semaphore was released.'); | ||
}); | ||
}) | ||
}); | ||
|
||
suite('Mutex with Priority Queue', () => { | ||
const priorityQueue = new HeapHelper<QueueEntry>(); | ||
mutexSuite((err?: Error) => new Mutex(err, priorityQueue)); | ||
|
||
// TODO: These tests validate the expected behavior of TinyQueue + Mutex. | ||
}); | ||
|
||
suite('withTimeout with Priority Queue', () => { | ||
// const priorityQueue = new HeapHelper<QueueEntry>(); | ||
|
||
// TODO: These tests validate the expected behavior of TinyQueue + withTimeout. | ||
}); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing tsconfig seems more sensitive and more likely to not be accepted upstream, what do we gain from this configuration?