Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"vitest": "^3.2.1"
},
"dependencies": {
"rhea": "^3.0.4"
"rhea": "github:amqp/rhea"
}
}
38 changes: 31 additions & 7 deletions src/delivery_context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Delivery, Receiver } from "rhea"
import { Delivery, MessageAnnotations, Receiver } from "rhea"

export interface DeliveryContext {
accept(): void
discard(): void
requeue(): void
discard(annotations?: MessageAnnotations): void
requeue(annotations?: MessageAnnotations): void
}

export class AmqpDeliveryContext implements DeliveryContext {
Expand All @@ -18,15 +18,39 @@ export class AmqpDeliveryContext implements DeliveryContext {
this.delivery.accept()
}

discard(): void {
discard(annotations?: MessageAnnotations): void {
if (this.receiverLink.is_closed()) throw new Error("Receiver link is closed")
if (!annotations) {
this.delivery.reject()
return
}

this.delivery.reject()
this.discardWithAnnotations(annotations)
}

requeue(): void {
private discardWithAnnotations(annotations: MessageAnnotations): void {
this.delivery.modified({
delivery_failed: true,
undeliverable_here: true,
message_annotations: annotations,
})
}

requeue(annotations?: MessageAnnotations): void {
if (this.receiverLink.is_closed()) throw new Error("Receiver link is closed")
if (!annotations) {
this.delivery.release()
return
}

this.requeueWithAnnotations(annotations)
}

this.delivery.release()
private requeueWithAnnotations(annotations: MessageAnnotations): void {
this.delivery.modified({
delivery_failed: false,
undeliverable_here: false,
message_annotations: annotations,
})
}
}
12 changes: 6 additions & 6 deletions test/e2e/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe("Consumer", () => {
})
})

test.skip("consumer can discard a message with annotations in a queue", async () => {
test("consumer can discard a message with annotations in a queue", async () => {
const publisher = await connection.createPublisher({ queue: { name: discardQueueName } })
const expectedBody = "ciao"
await publisher.publish(
Expand All @@ -240,7 +240,7 @@ describe("Consumer", () => {
const consumer = await connection.createConsumer({
queue: { name: discardQueueName },
messageHandler: (context) => {
context.discard()
context.discard({ "x-opt-annotation-key": "annotation-value" })
},
})
consumer.start()
Expand Down Expand Up @@ -296,7 +296,7 @@ describe("Consumer", () => {
})
})

test.skip("consumer can requeue a message with annotations in a queue", async () => {
test("consumer can requeue a message with annotations in a queue", async () => {
let toRequeue = true
const messages: Message[] = []
const consumer = await connection.createConsumer({
Expand All @@ -305,7 +305,7 @@ describe("Consumer", () => {
messages.push(message)
if (toRequeue) {
toRequeue = false
context.requeue()
context.requeue({ "x-opt-annotation-key": "annotation-value" })
return
}
context.accept()
Expand All @@ -327,7 +327,7 @@ describe("Consumer", () => {
expect(messages[0].message_annotations!["x-opt-annotation-key"]).toBeUndefined()
expect(messages[0].message_annotations!["x-delivery-count"]).toBeUndefined()
expect(messages[1].message_annotations!["x-opt-annotation-key"]).toEqual("annotation-value")
expect(messages[1].message_annotations!["x-delivery-count"]).toEqual("1")
expect(messages[1].message_annotations!["x-delivery-count"]).toEqual(1)
})
})
}, 15000)
})