A small service publishes receipts one message per record. After a recent change, the behavior became unstable: when one message fails, the queue sometimes receives extra data, and the caller sees a different error than the one that actually occurred during sending.
ReceiptPublisher#publish_all method accepts an array of hashes with the keys :id and :payload.broker.open_session;"receipt:<id>:<payload>";id to the result array published_ids.on_error: :collect:
failures must include a hash with the id, the error class, and the error message.on_error: :raise is passed:
Run the example below and compare the result with the expected behavior:
broker = MemoryBroker.new(fail_on_send: [2], fail_on_close: [2])
publisher = ReceiptPublisher.new(broker)
publisher.publish_all(
[
{ id: 1, payload: "ok" },
{ id: 2, payload: "boom" },
{ id: 3, payload: "later" }
],
on_error: :collect
)
broker.delivered
# there should be no duplicates for the same receipt here
And separately the mode that re-raises the error:
broker = MemoryBroker.new(fail_on_send: [2], fail_on_close: [2])
publisher = ReceiptPublisher.new(broker)
publisher.publish_all(
[
{ id: 1, payload: "ok" },
{ id: 2, payload: "boom" }
],
on_error: :raise
)
:collect mode:
broker.delivered, each successful receipt appears exactly once;:raise mode:
3.3Fiber schedulerMemoryBroker contract:
open_session returns an object with send_message(String) and close methodsfail_on_send and fail_on_close are arrays of id values for which the corresponding operation raises BrokerErrorreceipt:<id>:<payload>Use line comments to explain the problem, its impact, the fix and how to test it.