Skip to content
Merged
Changes from 2 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
51 changes: 16 additions & 35 deletions packages/batch/tests/helpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
SQSRecord,
} from 'aws-lambda';

const sqsRecordHandler = (record: SQSRecord): string => {
const baseSqsHandler = (record: SQSRecord): string => {
const body = record.body;
if (body.includes('fail')) {
throw Error('Failed to process record.');
Expand All @@ -14,16 +14,11 @@ const sqsRecordHandler = (record: SQSRecord): string => {
return body;
};

const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> => {
const body = record.body;
if (body.includes('fail')) {
throw Error('Failed to process record.');
}
const sqsRecordHandler = baseSqsHandler;
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> =>
Promise.resolve(baseSqsHandler(record));

return body;
};

const kinesisRecordHandler = (record: KinesisStreamRecord): string => {
const baseKinesisHandler = (record: KinesisStreamRecord): string => {
const body = record.kinesis.data;
if (body.includes('fail')) {
throw Error('Failed to process record.');
Expand All @@ -32,18 +27,12 @@ const kinesisRecordHandler = (record: KinesisStreamRecord): string => {
return body;
};

const kinesisRecordHandler = baseKinesisHandler;
const asyncKinesisRecordHandler = async (
record: KinesisStreamRecord
): Promise<string> => {
const body = record.kinesis.data;
if (body.includes('fail')) {
throw Error('Failed to process record.');
}
): Promise<string> => Promise.resolve(baseKinesisHandler(record));

return body;
};

const dynamodbRecordHandler = (record: DynamoDBRecord): object => {
const baseDynamodbHandler = (record: DynamoDBRecord): object => {
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
if (body.S?.includes('fail')) {
throw Error('Failed to process record.');
Expand All @@ -52,18 +41,17 @@ const dynamodbRecordHandler = (record: DynamoDBRecord): object => {
return body;
};

const dynamodbRecordHandler = baseDynamodbHandler;
const asyncDynamodbRecordHandler = async (
record: DynamoDBRecord
): Promise<object> => {
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
if (body.S?.includes('fail')) {
throw Error('Failed to process record.');
}

return body;
return Promise.resolve(baseDynamodbHandler(record));
};

const handlerWithContext = (record: SQSRecord, context: Context): string => {
const baseHandlerWithContext = (
record: SQSRecord,
context: Context
): string => {
try {
if (context.getRemainingTimeInMillis() === 0) {
throw Error('No time remaining.');
Expand All @@ -75,19 +63,12 @@ const handlerWithContext = (record: SQSRecord, context: Context): string => {
return record.body;
};

const handlerWithContext = baseHandlerWithContext;
const asyncHandlerWithContext = async (
record: SQSRecord,
context: Context
): Promise<string> => {
try {
if (context.getRemainingTimeInMillis() === 0) {
throw Error('No time remaining.');
}
} catch {
throw Error(`Context possibly malformed. Displaying context:\n${context}`);
}

return Promise.resolve(record.body);
return Promise.resolve(baseHandlerWithContext(record, context));
};

export {
Expand Down
Loading