|
@@ -8,26 +8,25 @@ describe('Source', function () {
|
|
|
|
|
|
|
|
it('should run a simple sum pipeline', async () => {
|
|
it('should run a simple sum pipeline', async () => {
|
|
|
await fc.assert(
|
|
await fc.assert(
|
|
|
- fc.asyncProperty(fc.array<number>(fc.integer()), async (arr) => {
|
|
|
|
|
- const result =
|
|
|
|
|
- await Source.fromArray(arr)
|
|
|
|
|
- .into(Sink.sum)
|
|
|
|
|
- .run()
|
|
|
|
|
- assert.equal(result, arr.reduce((a, b) => a + b, 0))
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ fc.asyncProperty(fc.array<number>(fc.integer()), async arr => {
|
|
|
|
|
+ const result = await Source.fromArray(arr).into(Sink.sum).run()
|
|
|
|
|
+ assert.equal(
|
|
|
|
|
+ result,
|
|
|
|
|
+ arr.reduce((a, b) => a + b, 0),
|
|
|
|
|
+ )
|
|
|
|
|
+ }),
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should run a simple sum with map', async () => {
|
|
it('should run a simple sum with map', async () => {
|
|
|
await fc.assert(
|
|
await fc.assert(
|
|
|
- fc.asyncProperty(fc.array<number>(fc.integer()), async (arr) => {
|
|
|
|
|
- const result =
|
|
|
|
|
- await Source.fromArray<number>(arr)
|
|
|
|
|
- .map(x => x * 2)
|
|
|
|
|
- .into(Sink.sum)
|
|
|
|
|
- .run()
|
|
|
|
|
|
|
+ fc.asyncProperty(fc.array<number>(fc.integer()), async arr => {
|
|
|
|
|
+ const result = await Source.fromArray<number>(arr)
|
|
|
|
|
+ .map(x => x * 2)
|
|
|
|
|
+ .into(Sink.sum)
|
|
|
|
|
+ .run()
|
|
|
assert.equal(result, 2 * arr.reduce((a, b) => a + b, 0))
|
|
assert.equal(result, 2 * arr.reduce((a, b) => a + b, 0))
|
|
|
- })
|
|
|
|
|
|
|
+ }),
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -42,21 +41,23 @@ describe('Source', function () {
|
|
|
})
|
|
})
|
|
|
.into(Sink.sum)
|
|
.into(Sink.sum)
|
|
|
.run()
|
|
.run()
|
|
|
- assert.equal(result, arr.length, "An element is missing")
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ assert.equal(result, arr.length, 'An element is missing')
|
|
|
|
|
+ }),
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should run ordered async', async () => {
|
|
it('should run ordered async', async () => {
|
|
|
await fc.assert(
|
|
await fc.assert(
|
|
|
fc.asyncProperty(fc.array(fc.integer()), fc.integer(1, 50), async (arr, concurrencyLevel) => {
|
|
fc.asyncProperty(fc.array(fc.integer()), fc.integer(1, 50), async (arr, concurrencyLevel) => {
|
|
|
- const timeout = () => (1 + (0 | (Math.random() * 4)))
|
|
|
|
|
|
|
+ const timeout = () => 1 + (0 | (Math.random() * 4))
|
|
|
const result = await Source.fromArray(arr)
|
|
const result = await Source.fromArray(arr)
|
|
|
- .mapAsync(concurrencyLevel, v => { return new Promise(resolve => setTimeout(() => resolve(v), timeout())) })
|
|
|
|
|
- .into(Sink.reduce<number, number[]>((acc, b) => ([...acc, b]), []))
|
|
|
|
|
|
|
+ .mapAsync(concurrencyLevel, v => {
|
|
|
|
|
+ return new Promise(resolve => setTimeout(() => resolve(v), timeout()))
|
|
|
|
|
+ })
|
|
|
|
|
+ .into(Sink.reduce<number, number[]>((acc, b) => [...acc, b], []))
|
|
|
.run()
|
|
.run()
|
|
|
assert.deepEqual(result, arr)
|
|
assert.deepEqual(result, arr)
|
|
|
- })
|
|
|
|
|
|
|
+ }),
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -64,36 +65,49 @@ describe('Source', function () {
|
|
|
await fc.assert(
|
|
await fc.assert(
|
|
|
fc.asyncProperty(fc.array(fc.integer()), fc.integer(1, 50), async (arr, concurrencyLevel) => {
|
|
fc.asyncProperty(fc.array(fc.integer()), fc.integer(1, 50), async (arr, concurrencyLevel) => {
|
|
|
const result = await Source.fromArray(arr)
|
|
const result = await Source.fromArray(arr)
|
|
|
- .mapAsyncUnordered(concurrencyLevel, v => { return new Promise(resolve => setTimeout(() => resolve(v * 2), 2)) })
|
|
|
|
|
|
|
+ .mapAsyncUnordered(concurrencyLevel, v => {
|
|
|
|
|
+ return new Promise(resolve => setTimeout(() => resolve(v * 2), 2))
|
|
|
|
|
+ })
|
|
|
.into(Sink.sum)
|
|
.into(Sink.sum)
|
|
|
.run()
|
|
.run()
|
|
|
assert.equal(result, 2 * arr.reduce((a, b) => a + b, 0))
|
|
assert.equal(result, 2 * arr.reduce((a, b) => a + b, 0))
|
|
|
- })
|
|
|
|
|
|
|
+ }),
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should filter elements', async () => {
|
|
it('should filter elements', async () => {
|
|
|
await fc.assert(
|
|
await fc.assert(
|
|
|
- fc.asyncProperty(fc.array(fc.integer()), async (arr) => {
|
|
|
|
|
|
|
+ fc.asyncProperty(fc.array(fc.integer()), async arr => {
|
|
|
const result = await Source.fromArray(arr)
|
|
const result = await Source.fromArray(arr)
|
|
|
.filter(n => n % 2 === 0)
|
|
.filter(n => n % 2 === 0)
|
|
|
.into(Sink.sum)
|
|
.into(Sink.sum)
|
|
|
.run()
|
|
.run()
|
|
|
assert.deepEqual(result % 2, 0)
|
|
assert.deepEqual(result % 2, 0)
|
|
|
- })
|
|
|
|
|
|
|
+ }),
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should throttle elements', async () => {
|
|
it('should throttle elements', async () => {
|
|
|
const before = Date.now()
|
|
const before = Date.now()
|
|
|
- const result = await Source.fromArray([1,2,3,4,5])
|
|
|
|
|
- .throttle(2, 10)
|
|
|
|
|
- .into(Sink.sum)
|
|
|
|
|
- .run()
|
|
|
|
|
|
|
+ const result = await Source.fromArray([1, 2, 3, 4, 5]).throttle(2, 10).into(Sink.sum).run()
|
|
|
const after = Date.now()
|
|
const after = Date.now()
|
|
|
const duration = after - before
|
|
const duration = after - before
|
|
|
assert.equal(result, 15)
|
|
assert.equal(result, 15)
|
|
|
- assert.ok(duration> 20, 'must at least be 20ms but was ' + duration)
|
|
|
|
|
|
|
+ assert.ok(duration > 20, 'must at least be 20ms but was ' + duration)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
-});
|
|
|
|
|
|
|
+ it('should run forEach correctly', async () => {
|
|
|
|
|
+ await fc.assert(
|
|
|
|
|
+ fc.asyncProperty(fc.array(fc.integer()), async arr => {
|
|
|
|
|
+ let acc = 0
|
|
|
|
|
+ await Source.fromArray(arr)
|
|
|
|
|
+ .into(Sink.forEach(n => (acc += n)))
|
|
|
|
|
+ .run()
|
|
|
|
|
+ assert.strictEqual(
|
|
|
|
|
+ acc,
|
|
|
|
|
+ arr.reduce((a, b) => a + b, 0),
|
|
|
|
|
+ )
|
|
|
|
|
+ }),
|
|
|
|
|
+ )
|
|
|
|
|
+ })
|
|
|
|
|
+})
|