-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
571 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,9 @@ node_modules | |
|
||
# Yarn | ||
yarn-error.log | ||
|
||
# Compiled JavaScript | ||
src/**/*.js | ||
src/**/*.d.ts | ||
test/**/*.js | ||
test/**/*.d.ts |
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,7 @@ | ||
{ | ||
"extension": ["ts", "tsx"], | ||
"watch-files": ["lib/**/*.ts", "test/**/*.ts"], | ||
"spec": ["test/*.ts"], | ||
"loader": ["ts-node/esm"], | ||
"extensions": ["ts", "tsx"] | ||
} |
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,46 @@ | ||
import {queue} from '../src/bounded-queue.js'; | ||
import {assert} from 'chai'; | ||
|
||
class MockProducer<T> { | ||
|
||
constructor(private numberOfItems: number, private timeToProduce: number) { | ||
} | ||
|
||
async produce(): Promise<T | null> { | ||
if (this.numberOfItems-- === 0) { | ||
return null; | ||
} | ||
return new Promise(resolve => setTimeout(resolve, this.timeToProduce)); | ||
} | ||
} | ||
|
||
class MockConsumer<T> { | ||
|
||
public itemsReceived: number = 0; | ||
|
||
constructor(private timeToProduce: number) { | ||
} | ||
|
||
async consume(batchedItem: T): Promise<void> { | ||
++this.itemsReceived; | ||
return new Promise(resolve => setTimeout(resolve, this.timeToProduce)); | ||
} | ||
} | ||
|
||
describe('bounded-queue', () => { | ||
|
||
it('slow consumer, fast consumer', async () => { | ||
const producer = new MockProducer<object>(10, 50); | ||
const consumer = new MockConsumer<object>(25); | ||
await queue<object>(5, () => producer.produce(), item => consumer.consume(item)); | ||
assert.equal(consumer.itemsReceived, 10, "Consumer should receive all items"); | ||
}); | ||
|
||
it('fast consumer, slow consumer', async () => { | ||
const producer = new MockProducer<object>(10, 25); | ||
const consumer = new MockConsumer<object>(50); | ||
await queue<object>(5, () => producer.produce(), item => consumer.consume(item)); | ||
assert.equal(consumer.itemsReceived, 10, "Consumer should receive all items"); | ||
}); | ||
|
||
}); |
Oops, something went wrong.