def test_sendNow(self): consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumerNow(consumer) for data in range(50, 60): routing_key.sendNow(data) received_data = consumer.receive(timeout=2) self.assertEqual(data, received_data)
def test_abort(self): consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumerNow(consumer) for data in range(90, 100): routing_key.send(data) # Messages sent using send() are forgotten on abort. transaction.abort() self.assertRaises(QueueEmpty, consumer.receive, timeout=2)
def test_receive(self): consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumerNow(consumer) for data in range(55, 65): routing_key.sendNow(data) self.assertEqual(data, consumer.receive(timeout=2)) # All the messages received were consumed. self.assertRaises(QueueEmpty, consumer.receive, timeout=2) # New connections to the queue see an empty queue too. consumer.session.disconnect() consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumerNow(consumer) self.assertRaises(QueueEmpty, consumer.receive, timeout=2)
def test_send(self): consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumerNow(consumer) for data in range(90, 100): routing_key.send(data) routing_key.sendNow('sync') # There is nothing in the queue except the sync we just sent. self.assertEqual('sync', consumer.receive(timeout=2)) # Messages get sent on commit transaction.commit() for data in range(90, 100): self.assertEqual(data, consumer.receive()) # There are no more messages. They have all been consumed. routing_key.sendNow('sync') self.assertEqual('sync', consumer.receive(timeout=2))
def test_associateConsumerNow(self): # associateConsumerNow() associates the consumer right away. consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumerNow(consumer) routing_key.sendNow('now') routing_key.send('later') # There is already something in the queue. self.assertEqual('now', consumer.receive(timeout=2)) transaction.commit() # Now that the transaction has been committed there is another item in # the queue. self.assertEqual('later', consumer.receive(timeout=2))
def test_associateConsumer(self): # associateConsumer() only associates the consumer at transaction # commit time. However, order is preserved. consumer = RabbitQueue(global_session, next(queue_names)) routing_key = RabbitRoutingKey(global_session, next(key_names)) routing_key.associateConsumer(consumer) # The session is still not connected. self.assertFalse(global_session.is_connected) routing_key.sendNow('now') routing_key.send('later') # The queue is not found because the consumer has not yet been # associated with the routing key and the queue declared. self.assertRaises(QueueNotFound, consumer.receive, timeout=2) transaction.commit() # Now that the transaction has been committed, the consumer is # associated, and receives the deferred message. self.assertEqual('later', consumer.receive(timeout=2))
def test_does_not_connect_session_immediately(self): # RabbitRoutingKey does not connect the session until necessary. RabbitRoutingKey(global_session, next(key_names)) self.assertFalse(global_session.is_connected)
def test_interface(self): routing_key = RabbitRoutingKey(global_session, next(key_names)) self.assertThat(routing_key, Provides(IMessageProducer))