-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new option
saveTimestamps
lets to attach sent timestamps to e…
…vents that have been successfully sent
- Loading branch information
1 parent
00d1fcb
commit 45f3b9c
Showing
6 changed files
with
134 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/hermes-mongodb/test/events-with-sent-timestamps.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
|
||
import { OutboxConsumersCollectionName, OutboxMessagesCollectionName } from '@arturwojnar/hermes' | ||
import { expect, jest } from '@jest/globals' | ||
import { ObjectId } from 'mongodb' | ||
import nodeTimersPromises from 'node:timers/promises' | ||
import { createOutboxConsumer } from '../src' | ||
import { type MedicineAdded, type MedicineEvent } from './events' | ||
import { mongodb } from './mongodb' | ||
|
||
test('If option `saveTimestamps` is on, then sent events are marked with timestamps', async () => { | ||
const publishEventStub = jest.fn<() => Promise<void>>().mockResolvedValue(undefined) | ||
|
||
await mongodb(async (db, client, onDispose) => { | ||
const messagesCollection = db.collection(OutboxMessagesCollectionName) | ||
const consumersCollection = db.collection(OutboxConsumersCollectionName) | ||
const outbox = createOutboxConsumer<MedicineEvent>({ | ||
client, | ||
db, | ||
publish: publishEventStub, | ||
shouldDisposeOnSigterm: false, | ||
saveTimestamps: true, | ||
now: () => new Date('2024-04-03 15:00:00'), | ||
}) | ||
const event: MedicineAdded = { | ||
name: 'MedicineAdded', | ||
data: { | ||
medicineId: 'med1', | ||
patientId: 'patient99', | ||
}, | ||
} | ||
|
||
const stop = await outbox.start() | ||
onDispose(stop) | ||
|
||
expect(await messagesCollection.find().toArray()).toEqual([]) | ||
expect(await consumersCollection.find().toArray()).toEqual([ | ||
{ | ||
_id: expect.any(ObjectId), | ||
lastProcessedId: null, | ||
resumeToken: null, | ||
lastUpdatedAt: null, | ||
createdAt: expect.any(Date), | ||
partitionKey: 'default', | ||
}, | ||
]) | ||
|
||
await outbox.publish(event) | ||
|
||
await nodeTimersPromises.setTimeout(200) | ||
|
||
const messages = await messagesCollection.find().toArray() | ||
expect(messages).toEqual([ | ||
{ | ||
_id: expect.any(ObjectId), | ||
occurredAt: expect.any(Date), | ||
data: event, | ||
partitionKey: 'default', | ||
sentAt: new Date('2024-04-03 15:00:00'), | ||
}, | ||
]) | ||
expect(messages[0].sentAt) | ||
expect(await consumersCollection.find().toArray()).toEqual([ | ||
{ | ||
_id: expect.any(ObjectId), | ||
lastProcessedId: messages[0]._id, | ||
resumeToken: expect.anything(), | ||
lastUpdatedAt: expect.any(Date), | ||
createdAt: expect.any(Date), | ||
partitionKey: 'default', | ||
}, | ||
]) | ||
}) | ||
}) |