Skip to content

Commit

Permalink
feat(stepfunctions): Added test and validation for maxConcurrency, th…
Browse files Browse the repository at this point in the history
…at has to be a positive integer
  • Loading branch information
apalumbo committed Oct 5, 2019
1 parent 91eb65f commit be89962
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/@aws-cdk/aws-stepfunctions/lib/states/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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 {
Expand Down
56 changes: 56 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/test/test.map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'(testTest) {
        test.throws(() => {
            const app = new cdk.App();
            const stack = new cdk.Stack(app, 'my-stack');

            const map = new stepfunctions.Map(stack, 'Map State', {
                maxConcurrency1.2,
                itemsPathstepfunctions.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'(testTest) {

        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,
                itemsPathstepfunctions.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'(testTest) {

        const app = new cdk.App();
        const stack = new cdk.Stack(app, 'my-stack');

        const map = new stepfunctions.Map(stack, 'Map State', {
            maxConcurrencyNumber.MAX_VALUE,
            itemsPathstepfunctions.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();
    }
};
Expand Down

0 comments on commit be89962

Please sign in to comment.