Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
288 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect } from "chai"; | ||
|
||
import Queue from "../../throttler/queue"; | ||
import { createHandler, createTask, Task } from "./throttler.spec"; | ||
|
||
describe("Queue", () => { | ||
it("should have default name of queue", () => { | ||
const queue = new Queue({}); | ||
expect(queue.name).to.equal("queue"); | ||
}); | ||
|
||
it("should be first-in-first-out", async () => { | ||
const order: string[] = []; | ||
const queue = new Queue<Task>({ | ||
handler: createHandler(order), | ||
concurrency: 1, | ||
}); | ||
|
||
const blocker = await createTask("blocker", false); | ||
queue.add(blocker); | ||
queue.add(await createTask("1", true)); | ||
queue.add(await createTask("2", true)); | ||
|
||
blocker.resolve(); | ||
|
||
queue.close(); | ||
await queue.wait(); | ||
expect(order).to.deep.equal(["blocker", "1", "2"]); | ||
}); | ||
}); |
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,64 @@ | ||
import { expect } from "chai"; | ||
|
||
import Stack from "../../throttler/stack"; | ||
import { createHandler, createTask, Task } from "./throttler.spec"; | ||
|
||
describe("Stack", () => { | ||
it("should have default name of stack", () => { | ||
const stack = new Stack({}); | ||
expect(stack.name).to.equal("stack"); | ||
}); | ||
|
||
it("should be first-in-last-out", async () => { | ||
const order: string[] = []; | ||
const queue = new Stack<Task>({ | ||
handler: createHandler(order), | ||
concurrency: 1, | ||
}); | ||
|
||
const blocker = await createTask("blocker", false); | ||
queue.add(blocker); | ||
queue.add(await createTask("1", true)); | ||
queue.add(await createTask("2", true)); | ||
|
||
blocker.resolve(); | ||
|
||
queue.close(); | ||
await queue.wait(); | ||
expect(order).to.deep.equal(["blocker", "2", "1"]); | ||
}); | ||
|
||
it("should not repeat completed tasks", async () => { | ||
const order: string[] = []; | ||
const queue = new Stack<Task>({ | ||
handler: createHandler(order), | ||
concurrency: 1, | ||
}); | ||
|
||
const t1 = await createTask("t1", false); | ||
queue.add(t1); | ||
const t2 = await createTask("t2", false); | ||
queue.add(t2); | ||
|
||
queue.add(await createTask("added before t1 finished a", true)); | ||
queue.add(await createTask("added before t1 finished b", true)); | ||
t1.resolve(); | ||
|
||
await t2.startExecutePromise; // wait until t2 starts to execute | ||
|
||
queue.add(await createTask("added before t2 finished a", true)); | ||
queue.add(await createTask("added before t2 finished b", true)); | ||
t2.resolve(); | ||
|
||
queue.close(); | ||
await queue.wait(); | ||
expect(order).to.deep.equal([ | ||
"t1", | ||
"added before t1 finished b", | ||
"added before t1 finished a", | ||
"t2", | ||
"added before t2 finished b", | ||
"added before t2 finished a", | ||
]); | ||
}); | ||
}); |
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,24 @@ | ||
import { Throttler, ThrottlerOptions } from "./throttler"; | ||
|
||
export class Queue<T> extends Throttler<T> { | ||
public cursor: number = 0; | ||
|
||
constructor(options: ThrottlerOptions<T>) { | ||
super(options); | ||
this.name = this.name || "queue"; | ||
} | ||
|
||
public hasWaitingTask(): boolean { | ||
return this.cursor !== this.total; | ||
} | ||
|
||
public nextWaitingTaskIndex(): number { | ||
if (this.cursor >= this.total) { | ||
throw new Error("There is no more task in queue"); | ||
} | ||
this.cursor++; | ||
return this.cursor - 1; | ||
} | ||
} | ||
|
||
export default Queue; |
Oops, something went wrong.