def test_sendMappingInfoCallsSendMessage(self):
     """Checks if sendMappingInfo calls sendMessage."""
     mock = MessMock()
     mbroker = MessBro(None, None, "")
     mbroker.sendMessage = mock.mockMethodWithOneParam
     self.assertFalse(mock.mockCalled)
     mbroker.sendMappingInfo()
     self.assertTrue(mock.mockCalled)
    def test_NodeInfoStoresNodeData(self):
        """Checks if node info data are stored in messageBroker instance."""

        message = {"messagetype" : "nodeinfo",
                   "data" : [{"nodeid" : "testnodeid", "description" : "testdescr"}]}
        mbroker = MessBro(None, None, "")
        mbroker.nodeInfo(message)
        self.assertEqual(message["data"], mbroker._hardware)
 def test_sendMappingInfo_CreatesMessage(self):
     """Checks if sendMappingInfo creates correct message type"""
     mock = MessMock()
     mbroker = MessBro(None, None, "")
     mbroker.sendMessage = mock.mockMethodWithOneParam
     mbroker.sendMappingInfo()
     message = json.loads(mock.param.encode('utf-8'))
     self.assertTrue(message["messagetype"] == 'MappingInfo')
     self.assertEqual(message["data"], [])
    def test_MessageTypeSendMappingInfoIsHandled(self):
        """Checks if message type getMappingInfo calls sendMappingInfo."""
        mock = MessMock()

        message = {"messagetype" : "getMappingInfo"}
        mbroker = MessBro(None, None, "")
        mbroker.sendMappingInfo = mock.mockMethodWithOutParams
        self.assertFalse(mock.mockCalled)
        mbroker.parseJsonMessage(message)
        self.assertTrue(mock.mockCalled)
    def test_NodeInfoMessage(self):
        """MessageBroker handles messageType nodeinfo."""
        mock = MessMock()

        message = {"messagetype" : "nodeinfo",
                   "data" : {}}
        mbroker = MessBro(None, None, "")
        mbroker.nodeInfo = mock.mockNodeInfo
        mbroker.parseJsonMessage(message)
        self.assertTrue(mock.mockNodeInfoCalled)
        self.assertEqual(message, mock.message)
Example #6
0
def google_testtopic():
    # Tests run on a test topic for simple communication testing of the adapter

    #Consuming messages
    message_broker = MessageBroker(type='google')
    #consumer = message_broker.generate_consumer(type='google',topic_name='test',gcallback=callback)
    #try:
    #    consumer.result()
    #except KeyboardInterrupt:
    #    consumer.cancel()

    #Message publishing
    publisher = message_broker.generate_producer(type='google')
    message_broker.publish_message(type='google',producer_instance=publisher,topic_name='test',value=b"test?")
    def test_sendPappingInfo_sendsNodeInfos(self):
        """Checks if sendMappingInfo creates message containing node infos."""
        mock = MessMock()
        hardwareInfo = {"hardwareInfo" : None}
        mockHardwareClient = NodeMock("hardware", hardwareInfo)
        mockBrowserClient = NodeMock("browser", None)

        mbroker = MessBro(None, None, "")
        # setup client items
        MessageBroker.clients.append(mockHardwareClient)
        MessageBroker.clients.append(mockBrowserClient)
        mbroker.sendMessage = mock.mockMethodWithOneParam
        mbroker.sendMappingInfo()
        message = json.loads(mock.param.encode('utf-8'))
        # check if message contains node data
        self.assertTrue(message["messagetype"] == "MappingInfo")
        self.assertTrue(message["data"][0] == hardwareInfo)
        self.assertEqual(len(message["data"]), 1)
Example #8
0
def kafka_testtopic():
    #Tests run on a test topic for simple communication testing of the adapter

    #Consume
    running = True
    message_broker = MessageBroker(type='kafka')
    consumer = message_broker.generate_consumer(type='kafka', topic_name='test', auto_offset_reset='earliest',
                                               bootstrap_servers='localhost:9092', api_version=10)
    try:
        consumer.subscribe("test")
        while running:
            msg = consumer.poll(timeout_ms=10000000)
            if msg is None: continue

            for val in iter(msg.values()):
                print(val[0].value)
    finally:
        consumer.close()
def main():
    
    try:

        parser = argparse.ArgumentParser()
        parser.add_argument("start", help="Start the MessageBroker ") 
        parser.add_argument("--mode", help="Which mode should the message broker run as?", default='producer', choices=['consumer', 'producer', 'formatter', 'cleaner'])
        parser.add_argument("--log-level", help="What level should the logger be run at ", default='ERROR')
        parser.add_argument("--log-file", help="Specify what file should the logger write to", default='/var/log/mb/mb.log')
        parser.add_argument("--rabbitmq-host", help="Which RabbitMQ server should the MessageBroker connect to ", default='localhost')
        parser.add_argument("--rabbitmq-port", help="Which port the RabbitMQ server is listening on ", default=5672)
        parser.add_argument("--rabbitmq-user", help="Which user to connect to RabbitMQ with", default="guest")
        parser.add_argument("--rabbitmq-pass", help="Which password to use to connect to the RabbitMQ instance", default="guest")
        parser.add_argument("--rabbitmq-audit-queue", help="Which queue is iRODS publishing to ", default='audit_messages')
        parser.add_argument("--disable-keymap", help="Disable keymapping for debugging purposes.", default=False)
        parser.add_argument("--redis-host", help="Provide a redis host to connect to for intermediate message storage", default="localhost")
        parser.add_argument("--redis-port", help="Provide alternate Redis port", default=6379)
        parser.add_argument("--redis-db", help="Select the Redis logical database having the specified zero-based numeric index.", default=0)
        parser.add_argument("--config-file", help="Provide a config file for the MessageBroker", default='config.yml')

        # get the provided arguments for this instance of the MessageBroker
        args = parser.parse_args()
        
        # instantiate our MessageBroker class with the provided arguments
        mb = MessageBroker(args)

        # Start the MessageBroker (mb start)
        mb.start()

    except KeyboardInterrupt:
        print "Message Broker is going down now!"
        sys.exit(1)
    
    except Exception, e:
        tb = traceback.format_exc()
        if 'DEBUG' not in os.environ:
            logger.error(str(e))
        else:
            print tb
        class_category = classifier.classify_image(img)
        print(class_category)
        publisher = message_broker.generate_producer(type='kafka')
        tosend = bytes([class_category])
        message_broker.publish_message(producer_instance=publisher,
                                       topic_name="classes",
                                       key='parsed',
                                       value=tosend,
                                       type='kafka')


if __name__ == '__main__':
    #specify arks for kafka or google
    # Listening to images to classify
    type = sys.argv[1]
    message_broker = MessageBroker(type=type)

    if type == "google":
        future = message_broker.generate_consumer(type='google',
                                                  topic_name='images',
                                                  gcallback=classify_google)
        try:
            future.result()
        except KeyboardInterrupt:
            future.cancel()

    if type == "kafka":
        running = True
        consumer = message_broker.generate_consumer(
            type='kafka',
            topic_name='images',
 def test_MessageBrokerNullMessage(self):
     mbroker = MessBro(None, None, "")
     message = {}
     mbroker.parseJsonMessage(None)
Example #12
0
def classify_images(type):
    X_test, y_test = load_mnist('Data/', kind='t10k')
    if type == "google":
        message_broker = MessageBroker("google")

        # Listening to print out classes
        consumer = message_broker.generate_consumer(type='google', topic_name='classes', gcallback=callback)

        # Sending images
        publisher = message_broker.generate_producer(type='google')
        for img in X_test:
            print("Sending an image.")
            message = img.tobytes()
            message_broker.publish_message(type='google', producer_instance=publisher, topic_name='images', value=message)
            time.sleep(10)

        try:
           consumer.result()
        except KeyboardInterrupt:
           consumer.cancel()

    if type == 'kafka':
        # Sending images
        message_broker = MessageBroker(type='kafka')
        for img in X_test:
            print("Sending an image.")
            producer = message_broker.generate_producer(type="kafka")
            message_broker.publish_message(producer_instance=producer, topic_name="images", key='parsed',
                                           value=img.tobytes(), type='kafka')
            time.sleep(10)