Skip to content

Commit 70a0d02

Browse files
address PR comments
1 parent a75180b commit 70a0d02

9 files changed

Lines changed: 65 additions & 36 deletions

File tree

.gitleaksignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ e12407e09151898bfd8d049d57eee9db9977d56b:.github/copilot-instructions.md:generic
2323
debc75a97cfe551a69fd1e8694be483213322a9d:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10
2424
793b2ab7a3e698f5848fbe24d2718f88664ec0fc:.npmrc:npm-access-token:8
2525
793b2ab7a3e698f5848fbe24d2718f88664ec0fc:.npmrc:github-pat:6
26-
777eb4047ad06b9e939a292ee18664a0ffee4f29:tests/resources/prepared-letter.json:generic-api-key:4
2726
d005112adcfd286c3bef076214836dbb2fe8d0b5:.npmrc:github-pat:7
2827
d005112adcfd286c3bef076214836dbb2fe8d0b5:.npmrc:npm-access-token:9
2928
4fa1923947bbff2387218d698d766cbb7c121a0f:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10

package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
import fs from "node:fs";
22
import path from "node:path";
3-
import { envName } from "tests/constants/api-constants";
3+
import { UPSERT_LETTER_LAMBDA_ARN } from "tests/constants/api-constants";
44
import { getLambdaEnv, updateLambdaEnv } from "tests/helpers/aws-lambda-helper";
55
import { logger } from "tests/helpers/pino-logger";
66

7-
const ORIGINAL_ENV = path.join(__dirname, ".lambda-env.json");
8-
const UPSERT_LETTER_LAMBDA_ARN = `arn:aws:lambda:eu-west-2:820178564574:function:nhs-${envName}-supapi-upsertletter`;
7+
const ORIGINAL_ENV = path.join(
8+
__dirname,
9+
"..",
10+
"..",
11+
"resources",
12+
"lambda-env.json",
13+
);
914

1015
export default async function performanceSetup() {
11-
const currentEnv = await getLambdaEnv(UPSERT_LETTER_LAMBDA_ARN);
12-
if (Object.keys(currentEnv).length === 0) {
13-
return;
14-
}
16+
try {
17+
const currentEnv = await getLambdaEnv(UPSERT_LETTER_LAMBDA_ARN);
18+
if (Object.keys(currentEnv).length === 0) {
19+
throw new Error(
20+
`unable to obtain configuration environment for Lambda function ${UPSERT_LETTER_LAMBDA_ARN}. Exiting performance-setup`,
21+
);
22+
}
23+
24+
// Persist original env for teardown
25+
fs.writeFileSync(ORIGINAL_ENV, JSON.stringify(currentEnv, null, 2));
1526

16-
// Persist original env for teardown
17-
fs.writeFileSync(ORIGINAL_ENV, JSON.stringify(currentEnv, null, 2));
27+
// if original env not persisted throw error, as we can't restore the env during teardown
28+
if (!fs.existsSync(ORIGINAL_ENV)) {
29+
throw new Error(`Setup failed: original Lambda env was not persisted at ${ORIGINAL_ENV}, so the Lambda environment was NOT stored.
30+
Try running the test again`);
31+
}
1832

19-
await updateLambdaEnv(UPSERT_LETTER_LAMBDA_ARN, {
20-
...currentEnv,
21-
LETTER_TTL_HOURS: "1",
22-
});
33+
await updateLambdaEnv(UPSERT_LETTER_LAMBDA_ARN, {
34+
...currentEnv,
35+
LETTER_TTL_HOURS: "1",
36+
});
2337

24-
logger.info("LETTER_TTL_HOURS set to 1");
38+
logger.info("LETTER_TTL_HOURS set to 1");
39+
} catch (error) {
40+
throw new Error(`Performance tests setup failed with error: ${error}`);
41+
}
2542
}
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
import fs from "node:fs";
2-
import { envName } from "tests/constants/api-constants";
2+
import { UPSERT_LETTER_LAMBDA_ARN } from "tests/constants/api-constants";
33
import { updateLambdaEnv } from "tests/helpers/aws-lambda-helper";
44
import path from "node:path";
55
import { logger } from "tests/helpers/pino-logger";
66

7-
const ORIGINAL_ENV = path.join(__dirname, ".lambda-env.json");
8-
const UPSERT_LETTER_LAMBDA_ARN = `arn:aws:lambda:eu-west-2:820178564574:function:nhs-${envName}-supapi-upsertletter`;
7+
const ORIGINAL_ENV = path.join(
8+
__dirname,
9+
"..",
10+
"..",
11+
"resources",
12+
"lambda-env.json",
13+
);
914

1015
export default async function performanceTeardown() {
11-
if (!fs.existsSync(ORIGINAL_ENV)) {
12-
return;
13-
}
16+
try {
17+
if (!fs.existsSync(ORIGINAL_ENV)) {
18+
throw new Error(`Teardown failed: original Lambda env file not found at ${ORIGINAL_ENV}, so the Lambda environment was NOT restored.
19+
Either run the test again, or manually set the Original Lambda environment (default LETTER_TLL_HOURS=12960)`);
20+
}
1421

15-
const originalEnv = JSON.parse(fs.readFileSync(ORIGINAL_ENV, "utf8"));
22+
const originalEnv = JSON.parse(fs.readFileSync(ORIGINAL_ENV, "utf8"));
1623

17-
await updateLambdaEnv(UPSERT_LETTER_LAMBDA_ARN, originalEnv);
18-
fs.unlinkSync(ORIGINAL_ENV);
24+
const response = await updateLambdaEnv(
25+
UPSERT_LETTER_LAMBDA_ARN,
26+
originalEnv,
27+
);
28+
if (response.$metadata.httpStatusCode !== 200) {
29+
throw new Error(`Teardown failed: with code: ${response.$metadata.httpStatusCode}, while updating Lambda configuration environment from the file ${ORIGINAL_ENV} for function ${UPSERT_LETTER_LAMBDA_ARN}
30+
Either run the test again, or update the configuration manually.`);
31+
}
1932

20-
logger.info("Original value of LETTER_TTL_HOURS restored");
33+
fs.unlinkSync(ORIGINAL_ENV);
34+
logger.info("Original value of LETTER_TTL_HOURS restored");
35+
} catch (error) {
36+
throw new Error(`Performance tests teardown failed with error: ${error}`);
37+
}
2138
}

tests/constants/api-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export const LETTERSTABLENAME = `nhs-${envName}-supapi-letters`;
88
export const SUPPLIERID = "TestSupplier1";
99
export const MI_ENDPOINT = "mi";
1010
export const SUPPLIERTABLENAME = `nhs-${envName}-supapi-suppliers`;
11+
export const UPSERT_LETTER_LAMBDA_ARN = `arn:aws:lambda:eu-west-2:820178564574:function:nhs-${envName}-supapi-upsertletter`;

tests/helpers/aws-kinesis-helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import { AWS_REGION } from "tests/constants/api-constants";
1212
export const kinesisClient = new KinesisClient({ region: AWS_REGION });
1313

1414
/**
15-
* Wait for a matching record on a Kinesis stream.
15+
* Get all records from a given start time for the specified Kinesis Stream.
16+
* Any records before the given start time will be ignored
1617
*
1718
* @param streamARN - existing Kinesis stream ARN
18-
* @param opts - optional settings
19+
* @param startTimeMs - Start Time in milliseconds
1920
*/
2021
export async function retrieveKinesisRecordsAtTimestamp(
2122
streamARN: string,

tests/helpers/aws-lambda-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function updateLambdaEnv(
2424
functionArn: string,
2525
variables: Record<string, string>,
2626
) {
27-
await lambdaClient.send(
27+
return await lambdaClient.send(
2828
new UpdateFunctionConfigurationCommand({
2929
FunctionName: functionArn,
3030
Environment: { Variables: variables },

tests/performance/testCases/send-prepared-letter-request.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { retrieveKinesisRecordsAtTimestamp } from "tests/helpers/aws-kinesis-hel
88
import { logger } from "tests/helpers/pino-logger";
99
import { envName } from "tests/constants/api-constants";
1010
import PREPARED_LETTER from "../../resources/prepared-letter.json";
11+
import { assert } from "console";
1112

1213
test.describe("Performance test checking how long it takes letter requests from the EventSub SNS to reach the Letters Database ", () => {
1314
test("send 2500 letter requests", async () => {

tests/resources/prepared-letter.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"data": {
33
"campaignId": "campaign_456",
4-
"clientId": "00f3b388-bbe9-41c9-9e76-052d37ee8988",
4+
"clientId": "testClientId",
55
"createdAt": "2025-08-28T08:45:00.000Z",
66
"domainId": "letter1",
77
"letterVariantId": "lv1",

0 commit comments

Comments
 (0)