Closed r01k closed 5 years ago
The reason why this is happening here is because without publisher confirmations, Message.publish is fire and forget at the protocol level. The exception is raised when RabbitMQ closes the channel remotely. It's an awkward side-effect of the protocol.
If you enable publisher confirmations and perform mandatory publishing, it raises where you would expect it. The following test passes:
class Issue119TestCase(unittest.TestCase):
def test_exception_is_raised(self):
conn = rabbitpy.Connection(os.environ['RABBITMQ_URL'])
channel = conn.channel()
channel.enable_publisher_confirms()
msg = rabbitpy.Message(channel, body_value='hello')
with self.assertRaises(exceptions.AMQPNotFound):
msg.publish(exchange='invalid', mandatory=True)
If I purposefully pause and wait for a potential channel close after publishing, non mandatory publishing without publisher confirmations will suffer a pretty big performance degradation.
Unfortunately I can't think of a good way to force the behavior you are expecting with negatively impacting performance. It's not a bug in the library as much as it is a behavior in the protocol.
Using publisher confirmations will result in the behavior you expect, but will come with a performance tradeoff.
Publishing to a non-existing exchange does not raise exception if the channel is not used as a context manager:
If the channel is inside a with block the exception does come up: