Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/idempotency/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"@aws-lambda-powertools/testing-utils": "file:../testing",
"@aws-sdk/client-dynamodb": "^3.975.0",
"@aws-sdk/lib-dynamodb": "^3.975.0",
"@aws/durable-execution-sdk-js": "^1.0.2",
"aws-sdk-client-mock": "^4.1.0"
}
}
23 changes: 23 additions & 0 deletions packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { setTimeout } from 'node:timers/promises';
import {
type DurableContext,
withDurableExecution,
} from '@aws/durable-execution-sdk-js';
import { Logger } from '@aws-lambda-powertools/logger';
import type { Context } from 'aws-lambda';
import { IdempotencyConfig } from '../../src/IdempotencyConfig.js';
Expand Down Expand Up @@ -111,3 +115,22 @@ export const handlerLambda = makeIdempotent(
}),
}
);

export const handlerDurable = withDurableExecution(
makeIdempotent(
async (event: { foo: string }, context: DurableContext) => {
context.configureLogger({ customLogger: logger });

logger.info('Processing event', { foo: event.foo });

await context.wait({ seconds: 1 });

logger.info('After wait');

return event.foo;
},
{
persistenceStore: dynamoDBPersistenceLayer,
}
)
);
51 changes: 51 additions & 0 deletions packages/idempotency/tests/e2e/makeIdempotent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@aws-lambda-powertools/testing-utils';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
import { Duration } from 'aws-cdk-lib';
import { AttributeType } from 'aws-cdk-lib/aws-dynamodb';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js';
Expand Down Expand Up @@ -77,6 +78,25 @@ describe('Idempotency E2E tests, wrapper function usage', () => {
}
);

let functionNameDurable: string;
let tableNameDurable: string;
new IdempotencyTestNodejsFunctionAndDynamoTable(
testStack,
{
function: {
entry: lambdaFunctionCodeFilePath,
handler: 'handlerDurable',
durableConfig: {
executionTimeout: Duration.minutes(5),
retentionPeriod: Duration.days(1),
},
},
},
{
nameSuffix: 'durable',
}
);

const ddb = new DynamoDBClient({});

beforeAll(async () => {
Expand All @@ -94,6 +114,8 @@ describe('Idempotency E2E tests, wrapper function usage', () => {
testStack.findAndGetStackOutputValue('handlerFn');
tableNameLambdaHandler =
testStack.findAndGetStackOutputValue('handlerTable');
functionNameDurable = testStack.findAndGetStackOutputValue('durableFn');
tableNameDurable = testStack.findAndGetStackOutputValue('durableTable');
});

it('when called twice with the same payload, it returns the same result', async () => {
Expand Down Expand Up @@ -309,6 +331,35 @@ describe('Idempotency E2E tests, wrapper function usage', () => {
expect(functionLogs[1]).toHaveLength(0);
});

it('calls an idempotent durable function and always returns the same result when called multiple times', async () => {
// Prepare
const payload = {
foo: 'bar',
};
const payloadHash = createHash('md5')
.update(JSON.stringify(payload))
.digest('base64');

// Act
await invokeFunction({
functionName: `${functionNameDurable}:$LATEST`,
times: 2,
invocationMode: 'SEQUENTIAL',
payload,
});

// Assess
const idempotencyRecords = await ddb.send(
new ScanCommand({
TableName: tableNameDurable,
})
);
expect(idempotencyRecords.Items?.length).toEqual(1);
expect(idempotencyRecords.Items?.[0].id).toEqual(
`${functionNameDurable}#${payloadHash}`
);
});

afterAll(async () => {
if (!process.env.DISABLE_TEARDOWN) {
await testStack.destroy();
Expand Down
Loading
Loading