def test_conninfo(self): c = BrokerConnection(userid=None, transport="amqplib") self.assertRaises(KeyError, c.connect) c = BrokerConnection(hostname=None, transport="amqplib") self.assertRaises(KeyError, c.connect) c = BrokerConnection(password=None, transport="amqplib") self.assertRaises(KeyError, c.connect)
def test_db_port(self): c1 = BrokerConnection(port=None, transport=Transport).channel() self.assertEqual(c1.client.port, Transport.default_port) c1.close() c2 = BrokerConnection(port=9999, transport=Transport).channel() self.assertEqual(c2.client.port, 9999) c2.close()
def test_start__consume_messages(self): class _QoS(object): prev = 3 value = 4 def update(self): self.prev = self.value class _Consumer(MyKombuConsumer): iterations = 0 def reset_connection(self): if self.iterations >= 1: raise KeyError("foo") init_callback = Mock() l = _Consumer(self.ready_queue, self.eta_schedule, self.logger, send_events=False, init_callback=init_callback) l.task_consumer = Mock() l.broadcast_consumer = Mock() l.qos = _QoS() l.connection = BrokerConnection() l.iterations = 0 def raises_KeyError(limit=None): l.iterations += 1 if l.qos.prev != l.qos.value: l.qos.update() if l.iterations >= 2: raise KeyError("foo") l.consume_messages = raises_KeyError with self.assertRaises(KeyError): l.start() self.assertTrue(init_callback.call_count) self.assertEqual(l.iterations, 1) self.assertEqual(l.qos.prev, l.qos.value) init_callback.reset_mock() l = _Consumer(self.ready_queue, self.eta_schedule, self.logger, send_events=False, init_callback=init_callback) l.qos = _QoS() l.task_consumer = Mock() l.broadcast_consumer = Mock() l.connection = BrokerConnection() l.consume_messages = Mock(side_effect=socket.error("foo")) with self.assertRaises(socket.error): l.start() self.assertTrue(init_callback.call_count) self.assertTrue(l.consume_messages.call_count)
def test_db_values(self): c1 = BrokerConnection(virtual_host=1, transport=Transport).channel() self.assertEqual(c1.client.db, 1) c2 = BrokerConnection(virtual_host="1", transport=Transport).channel() self.assertEqual(c2.client.db, 1) c3 = BrokerConnection(virtual_host="/1", transport=Transport).channel() self.assertEqual(c3.client.db, 1) self.assertRaises( BrokerConnection(virtual_host="/foo", transport=Transport).channel)
def test_url_parser(self): from kombu.transport import mongodb from pymongo.errors import ConfigurationError raise SkipTest( "Test is functional: it actually connects to mongod") class Transport(mongodb.Transport): Connection = MockConnection url = "mongodb://" c = BrokerConnection(url, transport=Transport).connect() client = c.channels[0].client self.assertEquals(client.name, "kombu_default") self.assertEquals(client.connection.host, "127.0.0.1") url = "mongodb://localhost" c = BrokerConnection(url, transport=Transport).connect() client = c.channels[0].client self.assertEquals(client.name, "kombu_default") url = "mongodb://localhost/dbname" c = BrokerConnection(url, transport=Transport).connect() client = c.channels[0].client self.assertEquals(client.name, "dbname") url = "mongodb://localhost,example.org:29017/dbname" c = BrokerConnection(url, transport=Transport).connect() client = c.channels[0].client nodes = client.connection.nodes self.assertEquals(len(nodes), 2) self.assertTrue(("example.org", 29017) in nodes) self.assertEquals(client.name, "dbname") # Passing options breaks kombu's _init_params method # url = "mongodb://localhost,localhost2:29017/dbname?safe=true" # c = BrokerConnection(url, transport=Transport).connect() # client = c.channels[0].client url = "mongodb://*****:*****@localhost/dbname" c = BrokerConnection(url, transport=Transport).connect() # Assuming there's no user 'username' with password 'password' # configured in mongodb # Needed, otherwise the error would be rose before # the assertRaises is called def get_client(): c.channels[0].client self.assertRaises(ConfigurationError, get_client)
def test_default_port(self): class Transport(amqplib.Transport): Connection = MockConnection c = BrokerConnection(port=None, transport=Transport).connect() self.assertEqual(c["host"], "127.0.0.1:%s" % (Transport.default_port, ))
def test_url_parser(self): try: import sqlalchemy # noqa except ImportError: raise SkipTest("sqlalchemy not installed") with patch("kombu.transport.sqlalchemy.Channel._open"): url = "sqlalchemy+sqlite://celerydb.sqlite" BrokerConnection(url).connect() url = "sqla+sqlite://celerydb.sqlite" BrokerConnection(url).connect() # Should prevent regression fixed by f187ccd url = "sqlb+sqlite://celerydb.sqlite" with self.assertRaises(KeyError): BrokerConnection(url).connect()
def init_rabbit_mq(self): """ This function will attempt to connect to RabbitMQ Server and if successful return 'True'. Returns 'False' otherwise. """ self.logger.info("Initializing RabbitMQ stuff") try: schedule_exchange = Exchange("airtime-media-monitor", "direct", durable=True, auto_delete=True) schedule_queue = Queue("media-monitor", exchange=schedule_exchange, key="filesystem") self.connection = BrokerConnection( self.config.cfg["rabbitmq_host"], self.config.cfg["rabbitmq_user"], self.config.cfg["rabbitmq_password"], self.config.cfg["rabbitmq_vhost"]) channel = self.connection.channel() consumer = Consumer(channel, schedule_queue) consumer.register_callback(self.handle_message) consumer.consume() except Exception, e: self.logger.error(e) return False
def init_rabbit_mq(self): self.logger.info("Initializing RabbitMQ stuff") try: self.logger.info("rabbitmq_host: " + self.config["rabbitmq_host"]) self.logger.info("rabbitmq_user: "******"rabbitmq_user"]) self.logger.info("rabbitmq_password: "******"rabbitmq_password"]) self.logger.info("rabbitmq_vhost: " + self.config["rabbitmq_vhost"]) """""" schedule_exchange = \ Exchange("airtime-pypo", "direct", durable=True, auto_delete=True) schedule_queue = \ Queue("pypo-fetch", exchange=schedule_exchange, key="foo") connection = BrokerConnection(self.config["rabbitmq_host"], \ self.config["rabbitmq_user"], \ self.config["rabbitmq_password"], \ self.config["rabbitmq_vhost"]) channel = connection.channel() self.simple_queue = SimpleQueue(channel, schedule_queue) """ connection = Connection('amqp://*****:*****@172.16.82.1:5672//pypox') self.simple_queue = connection.SimpleQueue('pypo-fetch') #message = simple_queue.get(block=True, timeout=1) """ except Exception, e: self.logger.error(e) return False
def init_rabbit_mq(self): try: self.logger.info("Initializing RabbitMQ message consumer...") schedule_exchange = Exchange("airtime-media-monitor", "direct", durable=True, auto_delete=True) schedule_queue = Queue("media-monitor", exchange=schedule_exchange, key="filesystem") self.connection = BrokerConnection(self.cfg["rabbitmq_host"], self.cfg["rabbitmq_user"], self.cfg["rabbitmq_password"], self.cfg["rabbitmq_vhost"]) channel = self.connection.channel() self.simple_queue = SimpleQueue(channel, schedule_queue) self.logger.info("Initialized RabbitMQ consumer.") except Exception as e: self.logger.info("Failed to initialize RabbitMQ consumer") self.logger.error(e) return False return True
def test_default_port(self): class Transport(pyamqplib.Transport): Connection = dict c = BrokerConnection(port=None, transport=Transport).connect() self.assertEqual(c["host"], "localhost:%s" % (Transport.default_port, ))
def test_connection_errors(self): class MyTransport(Transport): connection_errors = (KeyError, ValueError) conn = BrokerConnection(transport=MyTransport) self.assertTupleEqual(conn.connection_errors, (KeyError, ValueError))
def test_publish__consume(self): connection = BrokerConnection(transport=Transport) channel = connection.channel() producer = Producer(channel, self.exchange, routing_key="test_Redis") consumer = Consumer(channel, self.queue) producer.publish({"hello2": "world2"}) _received = [] def callback(message_data, message): _received.append(message_data) message.ack() consumer.register_callback(callback) consumer.consume() self.assertIn(channel, channel.connection.cycle._channels) try: connection.drain_events(timeout=1) self.assertTrue(_received) self.assertRaises(socket.timeout, connection.drain_events, timeout=0.01) finally: channel.close()
def _do_test(): conn = BrokerConnection(transport=Transport) chan = conn.channel() self.assertTrue(chan.Client) self.assertTrue(chan.ResponseError) self.assertTrue(conn.transport.connection_errors) self.assertTrue(conn.transport.channel_errors)
def test_close_disconnects(self): c = BrokerConnection(transport=Transport).channel() conn1 = c.client.connection conn2 = c.subclient.connection c.close() self.assertTrue(conn1.disconnected) self.assertTrue(conn2.disconnected)
def test_custom_port(self): class Transport(pyamqplib.Transport): Connection = MockConnection c = BrokerConnection(port=1337, transport=Transport).connect() self.assertEqual(c["host"], "localhost:1337")
def _rabbit_configured(self): # Data message should be forwarded to AMQP try: with BrokerConnection(settings.BROKER_URL) as conn: c = conn.connect() return c.connected except socket.error: return False
def test_parse_generated_as_uri(self): conn = BrokerConnection(self.url) info = conn.info() for k, v in self.expected.items(): self.assertEqual(info[k], v) # by default almost the same- no password self.assertEqual(conn.as_uri(), self.nopass) self.assertEqual(conn.as_uri(include_password=True), self.url)
def setup_rabbit_mq_channel(self): service_exchange = Exchange(self.acord_control_exchange, "topic", durable=False) # connections/channels connection = BrokerConnection(self.rabbit_host, self.rabbit_user, self.rabbit_password) logging.info("Connection to RabbitMQ server successful") channel = connection.channel() # produce self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info') self.publish = connection.ensure(self.producer, self.producer.publish, errback=self.errback, max_retries=3)
def get_connection(self): """ Return a connection instance configured as described in the settings file. """ transport = settings.MESSAGE_BROKER['transport'] transport_options = settings.MESSAGE_BROKER.get("options", {}) return BrokerConnection(transport=transport, **transport_options)
def setup(self): #Note: not a setup_class method, not to conflict with AbstractTestFixture's setup self._mock_rabbit_connection = BrokerConnection("pyamqp://*****:*****@localhost:5672") self._connections = {self._mock_rabbit_connection} self._exchange = Exchange('navitia', durable=True, delivry_mode=2, type='topic') self._mock_rabbit_connection.connect() #wait for the cnx to run the test self._wait_for_rabbitmq_cnx()
def setUp(self): self.c = BrokerConnection(transport="memory") self.e = Exchange("test_transport_memory") self.q = Queue("test_transport_memory", exchange=self.e, routing_key="test_transport_memory") self.q2 = Queue("test_transport_memory2", exchange=self.e, routing_key="test_transport_memory2")
def test_revive_when_channel_is_connection(self): p = self.connection.Producer() p.exchange = Mock() new_conn = BrokerConnection("memory://") defchan = new_conn.default_channel p.revive(new_conn) self.assertIs(p.channel, defchan) p.exchange.revive.assert_called_with(defchan)
def setup(self): self.mock_chaos_connection = BrokerConnection( "pyamqp://*****:*****@localhost:5672") self._connections = {self.mock_chaos_connection} self._exchange = Exchange('navitia', durable=True, delivry_mode=2, type='topic') self.mock_chaos_connection.connect()
def setup_rabbit_mq_channel(self): service_exchange = Exchange(cfg.CONF.udpservice.acord_control_exchange, "topic", durable=False) rabbit_host = cfg.CONF.udpservice.rabbit_hosts rabbit_user = cfg.CONF.udpservice.rabbit_userid rabbit_password = cfg.CONF.udpservice.rabbit_password # connections/channels connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password) print 'Connection to RabbitMQ server successful' channel = connection.channel() # produce self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')
def init_rabbit_mq(self): self.logger.info("Initializing RabbitMQ stuff") try: schedule_exchange = Exchange("airtime-pypo", "direct", durable=True, auto_delete=True) schedule_queue = Queue("pypo-fetch", exchange=schedule_exchange, key="foo") connection = BrokerConnection(config["rabbitmq_host"], config["rabbitmq_user"], config["rabbitmq_password"], config["rabbitmq_vhost"]) channel = connection.channel() self.simple_queue = SimpleQueue(channel, schedule_queue) except Exception, e: self.logger.error(e) return False
def test_close_survives_connerror(self): class _CustomError(Exception): pass class MyTransport(Transport): connection_errors = (_CustomError, ) def close_connection(self, connection): raise _CustomError("foo") conn = BrokerConnection(transport=MyTransport) conn.connect() conn.close() self.assertTrue(conn._closed)
def setup_rabbit_mq_channel(): global producer global rabbit_user, rabbit_password, rabbit_host, vcpeservice_rabbit_exchange,cpe_publisher_id vcpeservice_exchange = Exchange(vcpeservice_rabbit_exchange, "topic", durable=False) # connections/channels connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password) logger.info('Connection to RabbitMQ server successful') channel = connection.channel() # produce producer = Producer(channel, exchange=vcpeservice_exchange, routing_key='notifications.info') p = subprocess.Popen('hostname', shell=True, stdout=subprocess.PIPE) (hostname, error) = p.communicate() cpe_publisher_id = cpe_publisher_id + '_on_' + hostname logger.info('cpe_publisher_id=%s',cpe_publisher_id)
def setUp(self): class Mailbox(pidbox.Mailbox): def _collect(self, *args, **kwargs): return "COLLECTED" self.mailbox = Mailbox("test_pidbox") self.connection = BrokerConnection(transport="memory") self.state = {"var": 1} self.handlers = {"mymethod": self._handler} self.bound = self.mailbox(self.connection) self.default_chan = self.connection.channel() self.node = self.bound.Node("test_pidbox", state=self.state, handlers=self.handlers, channel=self.default_chan)
def setup_rabbit_mq_channel(self): ceilometer_exchange = Exchange(self.rabbit_exchange, "topic", durable=False) # connections/channels connection = BrokerConnection(self.rabbit_host, self.rabbit_user, self.rabbit_password) LOG.info( "BroadViewPublisher: Connection to RabbitMQ server successful") channel = connection.channel() # produce self._producer = Producer(channel, exchange=ceilometer_exchange, routing_key='notifications.info') self._publish = connection.ensure(self._producer, self._producer.publish, errback=self.errback, max_retries=3)