-
-
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
1 parent
4e0e51e
commit b03f045
Showing
12 changed files
with
320 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
const { strict: assert } = require('node:assert') | ||
const { defer, Deferred, generatorify, createPoll, combine } = require('../../build/index.cjs'); | ||
const { strict: assert } = require('node:assert'); | ||
const { | ||
defer, | ||
Deferred, | ||
generatorify, | ||
createQueue, | ||
combine, | ||
} = require('../../build/index.cjs'); | ||
|
||
assert(typeof defer === 'function'); | ||
assert(typeof Deferred === 'function'); | ||
assert(typeof generatorify === 'function'); | ||
assert(typeof createPoll === 'function'); | ||
assert(typeof createQueue === 'function'); | ||
assert(typeof combine === 'function'); | ||
|
||
console.log('CJS import test passed'); |
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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { defer, Deferred, generatorify, createPoll, combine } from '../../build/index.js'; | ||
import { | ||
defer, | ||
Deferred, | ||
generatorify, | ||
createQueue, | ||
combine, | ||
} from '../../build/index.js'; | ||
|
||
assert(typeof defer === 'function'); | ||
assert(typeof Deferred === 'function'); | ||
assert(typeof generatorify === 'function'); | ||
assert(typeof createPoll === 'function'); | ||
assert(typeof createQueue === 'function'); | ||
assert(typeof combine === 'function'); | ||
|
||
console.log('MJS import test passed'); |
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,87 @@ | ||
import { createQueue, Deferred, Status } from '../index'; | ||
|
||
const wait = (timeout: number, value: unknown) => | ||
new Promise((resolve) => setTimeout(resolve, timeout, value)); | ||
|
||
describe('createQueue test suite', () => { | ||
it('should create a queue', async () => { | ||
createQueue(); | ||
}); | ||
|
||
it('should push a value to the queue', async () => { | ||
const value = Symbol('value'); | ||
const queue = createQueue(); | ||
const result = queue.push(value); | ||
|
||
expect(result.status).toBe(Status.PENDING); | ||
await expect(result.promise).resolves.toBe(value); | ||
expect(result).toBeInstanceOf(Deferred); | ||
expect(result.status).toBe(Status.RESOLVED); | ||
}); | ||
|
||
it('should pull a value from the queue', async () => { | ||
const value = Symbol('value'); | ||
const queue = createQueue(); | ||
const result = queue.pull(); | ||
await expect(Promise.race([wait(1, value), result.promise])).resolves.toBe( | ||
value | ||
); | ||
expect(result).toBeInstanceOf(Deferred); | ||
expect(result.status).toBe(Status.PENDING); | ||
}); | ||
|
||
it('should pull a pushed value from the queue', async () => { | ||
const value = Symbol('value'); | ||
const queue = createQueue(); | ||
queue.push(wait(1, value)); | ||
const result = queue.pull(); | ||
await expect(result.promise).resolves.toBe(value); | ||
expect(result).toBeInstanceOf(Deferred); | ||
expect(result.status).toBe(Status.RESOLVED); | ||
expect(queue.size).toBe(0); | ||
}); | ||
|
||
it('should pull a few pushed values from the queue', async () => { | ||
const value1 = Symbol('value1'); | ||
const value2 = Symbol('value2'); | ||
const queue = createQueue(); | ||
queue.push(wait(10, value2)); | ||
queue.push(wait(0, value1)); | ||
const result1 = queue.pull(); | ||
const result2 = queue.pull(); | ||
|
||
expect(result1.status).toBe(Status.PENDING); | ||
await expect(result1.promise).resolves.toBe(value1); | ||
expect(result1.status).toBe(Status.RESOLVED); | ||
|
||
expect(result2.status).toBe(Status.PENDING); | ||
await expect(result2.promise).resolves.toBe(value2); | ||
expect(result2.status).toBe(Status.RESOLVED); | ||
|
||
expect(queue.size).toBe(0); | ||
}); | ||
|
||
it('should pull a few not pushed values from the queue and wait them', async () => { | ||
const value1 = Symbol('value1'); | ||
const value2 = Symbol('value2'); | ||
const queue = createQueue(); | ||
|
||
const result1 = queue.pull(); | ||
const result2 = queue.pull(); | ||
|
||
setTimeout(() => { | ||
queue.push(wait(10, value2)); | ||
queue.push(wait(0, value1)); | ||
}, 100); | ||
|
||
expect(result1.status).toBe(Status.PENDING); | ||
await expect(result1.promise).resolves.toBe(value1); | ||
expect(result1.status).toBe(Status.RESOLVED); | ||
|
||
expect(result2.status).toBe(Status.PENDING); | ||
await expect(result2.promise).resolves.toBe(value2); | ||
expect(result2.status).toBe(Status.RESOLVED); | ||
|
||
expect(queue.size).toBe(0); | ||
}); | ||
}); |
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
Oops, something went wrong.