-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rotorsoft
committed
Sep 29, 2024
1 parent
be12e85
commit 0ae4fff
Showing
4 changed files
with
183 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { dispose, Message, sleep } from "@rotorsoft/eventually"; | ||
import { PostgresMessageQueue } from ".."; | ||
|
||
const table = "message_queue_test"; | ||
const mq = PostgresMessageQueue(table); | ||
|
||
describe("message queue", () => { | ||
beforeAll(async () => { | ||
await mq.drop(); | ||
await mq.seed(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dispose()(); | ||
}); | ||
|
||
it("should enqueue and dequeue", async () => { | ||
await mq.enqueue([{ name: "a", stream: "test", data: { value: "1" } }]); | ||
await mq.enqueue([ | ||
{ name: "a", stream: "test", data: { value: "2" } }, | ||
{ name: "a", stream: "test", data: { value: "3" } }, | ||
{ name: "a", stream: "test", data: { value: "4" } } | ||
]); | ||
await mq.enqueue([{ name: "a", stream: "test", data: { value: "5" } }]); | ||
|
||
// should dequeue in order | ||
const messages: Message[] = []; | ||
await mq.dequeue( | ||
(message) => { | ||
messages.push(message); | ||
return Promise.resolve(); | ||
}, | ||
{ stream: "test" } | ||
); | ||
expect(messages.length).toBe(1); | ||
|
||
// should not dequeue if stream is locked | ||
await Promise.all([ | ||
// should lock the stream for 1 second | ||
mq.dequeue( | ||
async (message) => { | ||
await sleep(1000); | ||
messages.push(message); | ||
}, | ||
{ stream: "test" } | ||
), | ||
// the stream should be locked for 1 second, thus should not dequeue | ||
async () => { | ||
await sleep(300); | ||
await mq.dequeue( | ||
async (message) => { | ||
messages.push(message); | ||
return Promise.resolve(); | ||
}, | ||
{ stream: "test" } | ||
); | ||
expect(messages.length).toBe(1); | ||
} | ||
]); | ||
|
||
// should be able to dequeue again | ||
await mq.dequeue( | ||
(message) => { | ||
messages.push(message); | ||
return Promise.resolve(); | ||
}, | ||
{ stream: "test" } | ||
); | ||
expect(messages.length).toBe(3); | ||
}); | ||
}); |
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