generated from streamich/tpl-ts-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from streamich/concurrency-decorator
Concurrency decorator
- Loading branch information
Showing
4 changed files
with
138 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import {concurrency} from '../concurrencyDecorator'; | ||
import {tick} from '../tick'; | ||
|
||
test('can execute one function with limit 1', async () => { | ||
const res: number[] = []; | ||
class A { | ||
@concurrency(1) | ||
async create(value: number) { | ||
res.push(value); | ||
} | ||
} | ||
const a = new A(); | ||
await a.create(123); | ||
expect(res).toStrictEqual([123]); | ||
await a.create(456); | ||
expect(res).toStrictEqual([123, 456]); | ||
await Promise.all([a.create(1), a.create(2)]); | ||
expect(res).toStrictEqual([123, 456, 1, 2]); | ||
}); | ||
|
||
test('can execute one function with limit 10', async () => { | ||
const res: number[] = []; | ||
class A { | ||
@concurrency(10) | ||
async create(value: number) { | ||
res.push(value); | ||
} | ||
} | ||
const a = new A(); | ||
await a.create(123); | ||
expect(res).toStrictEqual([123]); | ||
await a.create(456); | ||
expect(res).toStrictEqual([123, 456]); | ||
await Promise.all([a.create(1)]); | ||
expect(res).toStrictEqual([123, 456, 1]); | ||
}); | ||
|
||
describe('limits concurrency to 1', () => { | ||
for (let i = 0; i < 10; i++) { | ||
test(`${i + 1}`, async () => { | ||
const res: number[] = []; | ||
class A { | ||
@concurrency(1) | ||
async create(value: number) { | ||
await tick(Math.round(Math.random() * 10) + 1); | ||
res.push(value); | ||
} | ||
} | ||
const a = new A(); | ||
await Promise.all([a.create(1), a.create(2), a.create(3), a.create(4), a.create(5)]); | ||
expect(res).toStrictEqual([1, 2, 3, 4, 5]); | ||
}); | ||
} | ||
}); | ||
|
||
describe('check concurrency in-flight', () => { | ||
for (let limit = 1; limit <= 6; limit++) { | ||
describe(`limits concurrency to ${limit}`, () => { | ||
for (let i = 0; i < 10; i++) { | ||
test(`${i + 1}`, async () => { | ||
const running: boolean[] = []; | ||
const assert = async () => { | ||
const count = running.filter(Boolean).length; | ||
if (count > limit) throw new Error('Too many running'); | ||
}; | ||
class A { | ||
@concurrency(limit) | ||
async create(index: number) { | ||
running[index] = false; | ||
return async () => { | ||
running[index] = true; | ||
await assert(); | ||
await tick(Math.round(Math.random() * 10) + 1); | ||
await assert(); | ||
running[index] = false; | ||
}; | ||
} | ||
} | ||
const a = new A(); | ||
const promises: Promise<any>[] = []; | ||
for (let i = 0; i < limit * 2; i++) { | ||
promises.push(a.create(i)); | ||
} | ||
await Promise.all(promises); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
describe('check execution order', () => { | ||
for (let limit = 1; limit <= 6; limit++) { | ||
describe(`limits concurrency to ${limit}`, () => { | ||
for (let i = 0; i < 10; i++) { | ||
test(`${i + 1}`, async () => { | ||
let expectedIndex: number = 0; | ||
class A { | ||
@concurrency(limit) | ||
async create(index: number) { | ||
return async () => { | ||
if (index !== expectedIndex) throw new Error('Wrong order'); | ||
expectedIndex++; | ||
await tick(Math.round(Math.random() * 10) + 1); | ||
}; | ||
} | ||
} | ||
const a = new A(); | ||
const promises: Promise<any>[] = []; | ||
for (let i = 0; i < limit * 2; i++) { | ||
promises.push(a.create(i)); | ||
} | ||
await Promise.all(promises); | ||
}); | ||
} | ||
}); | ||
} | ||
}); |
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,18 @@ | ||
import {concurrency as _concurrency} from './concurrency'; | ||
|
||
/** | ||
* A class method decorator that limits the concurrency of the method to the | ||
* given number of parallel executions. All invocations are queued and executed | ||
* in the order they were called. | ||
*/ | ||
export function concurrency<This, Args extends any[], Return>(limit: number) { | ||
return ( | ||
target: (this: This, ...args: Args) => Promise<Return>, | ||
context?: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Promise<Return>>, | ||
) => { | ||
const limiter = _concurrency(limit); | ||
return async function (this: This, ...args: Args): Promise<Return> { | ||
return limiter(async () => await target.call(this, ...args)); | ||
}; | ||
}; | ||
} |
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