def test_signalling(self): """ Test connecting to the server, binding a queue to the signalling exchange and signalling the outcome of a job. """ # Create the exchange and bind a queue to it conn, ch = signalling.connect() qname = signalling.declare_and_bind_queue(0, ('succeeded',)) # Send a message signalling.signal_job_outcome(0, 'succeeded') messages = [] def callback(msg): messages.append(msg) ch.basic_cancel(msg.consumer_tag) ch.basic_consume(qname, callback=callback) while ch.callbacks: ch.wait() ch.close() conn.close() self.assertEqual(1, len(messages))
def test_log_configuration(self): """Test that the AMQP log configuration is consistent""" conn, ch = signalling.connect() # match messages of any level related to any job qname = signalling.declare_and_bind_queue('*', '*') # now there is a queue, send the log messages from Java root_logger = jpype.JClass("org.apache.log4j.Logger").getRootLogger() root_logger.debug('Java debug message') root_logger.info('Java info message') root_logger.warn('Java warn message') root_logger.error('Java error message') root_logger.fatal('Java fatal message') # and from Python root_log = logging.getLogger() root_log.debug('Python %s message', 'debug') root_log.info('Python %s message', 'info') root_log.warning('Python %s message', 'warn') root_log.error('Python %s message', 'error') root_log.critical('Python %s message', 'fatal') # process the messages messages = [] def consume(msg): messages.append(msg) if len(messages) == 10: ch.basic_cancel(msg.consumer_tag) self.consume_messages(conn, ch, qname, consume) self.assertEquals(10, len(messages)) # check message order for source in ['Java', 'Python']: for level in ['debug', 'info', 'warn', 'error', 'fatal']: routing_key = 'log.%s.123' % level fragment = '%s %s message' % (source, level) contained = filter(lambda msg: fragment in msg.body, messages) self.assertEquals( 1, len(contained), '"%s" contained in "%s"' % (fragment, contained)) recv_routing_key = contained[0].delivery_info['routing_key'] self.assertEquals( routing_key, recv_routing_key, '%s %s routing key: expected %s got %s' % ( source, level, routing_key, recv_routing_key)) # check process name in messages for i, msg in enumerate(messages): self.assertTrue(' ->UnitTestProcess<- ' in msg.body, 'process name in %d-th log entry "%s"' % ( i, msg.body))
def declare_signalling_exchange(): """ Ensure the signalling exchange exists. It is safe to call this function multiple times, even if the exchange already exists. On the other hand, if rabbitmq has just been restarted, the exchange will not exists and calling this function is required before tests using the amqp logging (for example the hazard_unittest which runs a complete job). """ # connecting will implicitly create the exchange if it doesn't exits yet conn, chn = signalling.connect() chn.close() conn.close()