def test_invalid_topic_keys(self):
        """ Check that message with a 'bad' routing_key does not match the queue's binding_key. """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        ROOT_KEY = 'feeder'

        # Set binding key for the queue that is created via setup_producer().
        cfg = manager.outgoing_cfg._replace(binding_key=ROOT_KEY+'.*')

        with manager.setup_producer(cfg=cfg) as producer:
            routing_key = 'FEEDER' + '.test_invalid_topic_keys'
            producer.publish(body=self.message, routing_key=routing_key, headers={'title_number': 'DN1'})
            logger.debug(self.message)

        # Attempt to consume message from outgoing exchange; should time out.
        try:
            self.consume(cfg=cfg)
        except socket.timeout:
            pass
        except Exception as e:
            raise
    def test_incoming_queue(self):
        """ Basic check of 'incoming' message via default direct exchange """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        producer = manager.setup_producer(cfg=manager.incoming_cfg)
        producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
        logger.info("Put message, exchange: {}, {}".format(self.message, producer.exchange))

        producer.close()

        self.consume()

        self.assertEqual(self.message, self.payload)
    def test_stored_incoming_message(self):
        """ Store message in INCOMING queue, then consume later and check it. """

        self.app.terminate()

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from source .
        with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
            producer.publish(body=self.message, headers={'title_number': 'DN1'})
            logger.debug(self.message)

        self.app.start()

        # Consume message from outgoing exchange.
        self.consume(cfg=manager.outgoing_cfg)

        self.assertEqual(self.message, self.payload)
    def test_stored_outgoing_message(self):
        """ Store message in OUTGOING queue, then consume later and check it. """

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from source .
        with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
            producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
            logger.debug(self.message)

        # Kill application; wait long enough for message to be stored.
        # N.B.: 1 second may be insufficient, for a full coverage check during testing.
        self.app.join(timeout=5)
        self.app.terminate()

        # Consume message from outgoing exchange.
        self.consume(cfg=manager.outgoing_cfg)

        self.assertEqual(self.message, self.payload)
    def test_broken_connection(self):
        """ Attempt 'publish' via closed connection, which is subsequently restored. """

        self.message = make_message()

        # Send a message to 'incoming' exchange - i.e. as if from source .
        with manager.setup_producer(cfg=manager.incoming_cfg) as producer:

            producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})

            # Kill connection to broker.
            producer.connection.close()

        # Block (wait) until app times out or terminates.
        self.app.join(timeout=5)

        # Consume message from outgoing exchange; this will establish another connection.
        self.consume(cfg=manager.outgoing_cfg)

        self.assertEqual(self.message, self.payload)
    def test_end_to_end(self, count=1):
        """ Send message from dummy "System Of Record", then consume and check it. """

        # Send a message to 'incoming' exchange - i.e. as if from source .
        # import pdb; pdb.set_trace()
        with manager.setup_producer(cfg=manager.incoming_cfg) as producer:
            for n in range(count):

                # Message to be sent.
                self.message = make_message()

                producer.publish(body=self.message, routing_key=manager.incoming_cfg.queue, headers={'title_number': 'DN1'})
                logger.debug(self.message)

                # Wait long enough message to be processed.
                self.app.join(timeout=1)

                # Consume message from outgoing exchange, via callback.
                self.consume(cfg=manager.outgoing_cfg)

                self.assertEqual(self.message, self.payload)
    def test_valid_topic_keys(self):
        """ Check that message with a suitable routing_key matches corresponding binding_key. """

        # We don't need the app to be running for this test.
        self.app.terminate()

        self.message = make_message()

        ROOT_KEY = 'feeder'

        # Set binding key for the queue that is created via setup_producer().
        cfg = manager.outgoing_cfg._replace(binding_key=ROOT_KEY+'.*')

        with manager.setup_producer(cfg=cfg) as producer:
            routing_key = ROOT_KEY + '.test_valid_topic_keys'
            producer.publish(body=self.message, routing_key=routing_key, headers={'title_number': 'DN1'})
            logger.debug(self.message)

        # Consume message from outgoing exchange.
        self.consume(cfg=cfg)

        self.assertEqual(self.message, self.payload)