From be8996260fa6e5ea86ffa679c2fd3f872a887d7b Mon Sep 17 00:00:00 2001 From: Alessandro Palumbo Date: Sat, 5 Oct 2019 09:09:12 +0200 Subject: [PATCH] feat(stepfunctions): Added test and validation for maxConcurrency, that has to be a positive integer --- .../aws-stepfunctions/lib/states/map.ts | 26 ++++++++- .../aws-stepfunctions/test/test.map.ts | 56 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/map.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/map.ts index 826da6419708c..3500952bfe3f1 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/map.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/map.ts @@ -80,7 +80,7 @@ export interface MapProps { export class Map extends State implements INextable { public readonly endStates: INextable[]; - private readonly maxConcurrency?: number; + private readonly maxConcurrency: number | undefined; private readonly itemsPath?: string; constructor(scope: cdk.Construct, id: string, props: MapProps = {}) { @@ -150,10 +150,30 @@ export class Map extends State implements INextable { * Validate this state */ protected validate(): string[] { + const validateMaxConcurrency = () => { + const maxConcurrency = this.maxConcurrency; + if (maxConcurrency === undefined) { + return; + } + + const isFloat = Math.floor(maxConcurrency) !== maxConcurrency; + + const isNotPositiveInteger = maxConcurrency < 0 || maxConcurrency > Number.MAX_SAFE_INTEGER; + + if (isFloat || isNotPositiveInteger) { + errors.push('maxConcurrency, if provided, has to be a positive integer'); + } + }; + + const errors: string[] = []; + if (!this.iteration) { - return ['Map state must have a non-empty iterator']; + errors.push('Map state must have a non-empty iterator'); } - return []; + + validateMaxConcurrency(); + + return errors; } private renderItemsPath(): any { diff --git a/packages/@aws-cdk/aws-stepfunctions/test/test.map.ts b/packages/@aws-cdk/aws-stepfunctions/test/test.map.ts index 1cdd9e2cabdc6..e6c66dca32945 100644 --- a/packages/@aws-cdk/aws-stepfunctions/test/test.map.ts +++ b/packages/@aws-cdk/aws-stepfunctions/test/test.map.ts @@ -73,6 +73,62 @@ export = {             app.synth();         }, /Map state must have a non-empty iterator/, 'A validation was expected'); +        test.done(); +    }, + 'synth fails when maxConcurrency is a float'(test: Test) { +        test.throws(() => { +            const app = new cdk.App(); +            const stack = new cdk.Stack(app, 'my-stack'); + +            const map = new stepfunctions.Map(stack, 'Map State', { +                maxConcurrency: 1.2, +                itemsPath: stepfunctions.Data.stringAt('$.inputForMap') +            }); +            map.iterator(new stepfunctions.Pass(stack, 'Pass State')); + +            new stepfunctions.StateGraph(map, 'Test Graph'); + +            app.synth(); +        }, /maxConcurrency, if provided, has to be a positive integer/); + +        test.done(); +    }, +    'synth fails when maxConcurrency is a negative integer'(test: Test) { + +        test.throws(() => { +            const app = new cdk.App(); +            const stack = new cdk.Stack(app, 'my-stack'); + +            const map = new stepfunctions.Map(stack, 'Map State', { +                maxConcurrency: -1, +                itemsPath: stepfunctions.Data.stringAt('$.inputForMap') +            }); +            map.iterator(new stepfunctions.Pass(stack, 'Pass State')); + +            new stepfunctions.StateGraph(map, 'Test Graph'); + +            app.synth(); +        }, /maxConcurrency, if provided, has to be a positive integer/); + +        test.done(); +    }, +    'synth fails when maxConcurrency is too big to be an integer'(test: Test) { + +        const app = new cdk.App(); +        const stack = new cdk.Stack(app, 'my-stack'); + +        const map = new stepfunctions.Map(stack, 'Map State', { +            maxConcurrency: Number.MAX_VALUE, +            itemsPath: stepfunctions.Data.stringAt('$.inputForMap') +        }); +        map.iterator(new stepfunctions.Pass(stack, 'Pass State')); + +        new stepfunctions.StateGraph(map, 'Test Graph'); + +        test.throws(() => { +            app.synth(); +        }, /maxConcurrency, if provided, has to be a positive integer/); +         test.done();     } };