Skip to content

Commit 4e73794

Browse files
committed
expand the test coverage of the orchestration script a bit
1 parent 1d60b95 commit 4e73794

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

packages/postDatedLambda/src/orchestration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {enrichMessagesWithExistingRecords} from "./databaseClient"
55
import {receivePostDatedSQSMessages, reportQueueStatus, handleProcessedMessages} from "./sqs"
66
import {BatchProcessingResult, PostDatedSQSMessage} from "./types"
77

8-
const MAX_QUEUE_RUNTIME = 14 * 60 * 1000 // 14 minutes, to avoid Lambda timeout issues (timeout is 15 minutes)
8+
export const MAX_QUEUE_RUNTIME = 14 * 60 * 1000 // 14 minutes, to avoid Lambda timeout issues (timeout is 15 minutes)
99
const MIN_RECEIVED_THRESHOLD = 3 // If fewer than this number of messages are received, consider the queue empty
1010

1111
/**

packages/postDatedLambda/tests/testOrchestration.test.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ jest.unstable_mockModule("../src/sqs", () => {
3434
import {Logger} from "@aws-lambda-powertools/logger"
3535

3636
import {createMockPostModifiedDataItem} from "./testUtils"
37-
import {PostDatedSQSMessage} from "../src/types"
37+
import {BatchProcessingResult, PostDatedSQSMessage} from "../src/types"
3838

3939
// Import the orchestration module after mocking dependencies
40-
const {processMessages} = await import("../src/orchestration")
40+
const {processMessages, processPostDatedQueue} = await import("../src/orchestration")
4141

4242
const logger = new Logger({serviceName: "postDatedLambdaTEST"})
4343

@@ -71,4 +71,72 @@ describe("orchestration", () => {
7171
expect(result.immaturePrescriptionUpdates).toHaveLength(0)
7272
})
7373
})
74+
75+
describe("processPostDatedQueue", () => {
76+
beforeEach(() => {
77+
jest.clearAllMocks()
78+
})
79+
80+
it("should process the SQS queue correctly", async () => {
81+
const mockMessages: Array<PostDatedSQSMessage> = [
82+
{MessageId: "1", Body: "Message 1", prescriptionData: createMockPostModifiedDataItem({})},
83+
{MessageId: "2", Body: "Message 2", prescriptionData: createMockPostModifiedDataItem({})}
84+
]
85+
86+
const mockEnrichedMessages = mockMessages.map((message) => ({
87+
...message,
88+
existingRecords: []
89+
}))
90+
91+
mockReceivePostDatedSQSMessages.mockReturnValueOnce(mockMessages)
92+
mockEnrichMessagesWithExistingRecords.mockReturnValueOnce(mockEnrichedMessages)
93+
mockProcessMessage.mockReturnValue(true)
94+
95+
await processPostDatedQueue(logger)
96+
97+
expect(mockReceivePostDatedSQSMessages).toHaveBeenCalledWith(logger)
98+
expect(mockReportQueueStatus).not.toHaveBeenCalled()
99+
expect(mockHandleProcessedMessages).toHaveBeenCalled()
100+
const [res, lg] =
101+
mockHandleProcessedMessages.mock.calls[0] as [BatchProcessingResult, Logger]
102+
expect(lg).toBe(logger)
103+
expect(res.maturedPrescriptionUpdates).toHaveLength(mockMessages.length)
104+
expect(res.immaturePrescriptionUpdates).toHaveLength(0)
105+
expect(res.maturedPrescriptionUpdates.map((message) => message.MessageId)).toEqual(
106+
mockMessages.map((message) => message.MessageId)
107+
)
108+
expect(mockProcessMessage).toHaveBeenCalledTimes(mockMessages.length)
109+
})
110+
111+
it("Should stop processing if the max runtime is exceeded", async () => {
112+
jest.useFakeTimers()
113+
const mockMessages: Array<PostDatedSQSMessage> = [
114+
{MessageId: "1", Body: "Message 1", prescriptionData: createMockPostModifiedDataItem({})},
115+
{MessageId: "2", Body: "Message 2", prescriptionData: createMockPostModifiedDataItem({})},
116+
{MessageId: "3", Body: "Message 3", prescriptionData: createMockPostModifiedDataItem({})},
117+
{MessageId: "4", Body: "Message 4", prescriptionData: createMockPostModifiedDataItem({})},
118+
{MessageId: "5", Body: "Message 5", prescriptionData: createMockPostModifiedDataItem({})},
119+
{MessageId: "6", Body: "Message 6", prescriptionData: createMockPostModifiedDataItem({})}
120+
]
121+
122+
mockReceivePostDatedSQSMessages.mockReturnValue(mockMessages)
123+
mockEnrichMessagesWithExistingRecords.mockReturnValue(
124+
mockMessages.map((message) => ({
125+
...message,
126+
existingRecords: []
127+
}))
128+
)
129+
const {MAX_QUEUE_RUNTIME} = await import("../src/orchestration")
130+
mockProcessMessage.mockImplementation(async () => {
131+
// Overrun by a second
132+
jest.advanceTimersByTime(MAX_QUEUE_RUNTIME + 1000)
133+
return true
134+
})
135+
136+
await processPostDatedQueue(logger)
137+
138+
expect(mockReportQueueStatus).toHaveBeenCalled()
139+
jest.useRealTimers()
140+
})
141+
})
74142
})

0 commit comments

Comments
 (0)