Beispiel #1
0
    def test_verify_n_receivers(self):
        n = 4
        addr = self.address()

        # connection should be ok
        denied = False
        try:
            br1 = BlockingConnection(addr)
        except ConnectionException:
            denied = True

        self.assertFalse(
            denied)  # assert if connections that should open did not open

        # n receivers OK
        try:
            r1 = br1.create_receiver(address="****YES_1of4***")
            r2 = br1.create_receiver(address="****YES_20f4****")
            r3 = br1.create_receiver(address="****YES_3of4****")
            r4 = br1.create_receiver(address="****YES_4of4****")
        except Exception:
            denied = True

        self.assertFalse(denied)  # n receivers should have worked

        # receiver n+1 should be denied
        try:
            r5 = br1.create_receiver("****NO****")
        except Exception:
            denied = True

        self.assertTrue(denied)  # receiver n+1 should have failed

        br1.close()
    def test_verify_n_receivers(self):
        n = 4
        addr = self.address()

        # connection should be ok
        denied = False
        try:
            br1 = BlockingConnection(addr)
        except ConnectionException:
            denied = True

        self.assertFalse(denied) # assert if connections that should open did not open

        # n receivers OK
        try:
            r1 = br1.create_receiver(address="****YES_1of4***")
            r2 = br1.create_receiver(address="****YES_20f4****")
            r3 = br1.create_receiver(address="****YES_3of4****")
            r4 = br1.create_receiver(address="****YES_4of4****")
        except Exception:
            denied = True

        self.assertFalse(denied) # n receivers should have worked

        # receiver n+1 should be denied
        try:
            r5 = br1.create_receiver("****NO****")
        except Exception:
            denied = True

        self.assertTrue(denied) # receiver n+1 should have failed

        br1.close()
Beispiel #3
0
    def test_inter_router_protocol_trace(self):
        qd_manager = QdManager(self, self.address)
        # Turn off trace logging on all connections for Router B.
        qd_manager.update("org.apache.qpid.dispatch.log", {"enable": "info+"},
                          name="log/DEFAULT")

        # Get the connection id of the inter-router connection
        results = qd_manager.query("org.apache.qpid.dispatch.connection")
        conn_id = None
        for result in results:
            if result[u'role'] == u'inter-router':
                conn_id = result[u'identity']

        # Turn on trace logging for the inter-router connection
        qd_manager.update("org.apache.qpid.dispatch.connection",
                          {"enableProtocolTrace": "true"},
                          identity=conn_id)

        # Create a receiver and make sure the MAU update is seen on the inter-router connection log
        TEST_ADDR_1 = "EnableConnectionLevelProtocolTraceTest1"
        conn_2 = BlockingConnection(self.address)
        blocking_receiver_1 = conn_2.create_receiver(address=TEST_ADDR_1)

        # Give some time for the MAU to go over the inter-router link
        time.sleep(2)
        logs = qd_manager.get_log()
        mau_found = False
        for log in logs:
            if u'PROTOCOL' in log[0]:
                if "@transfer" in log[2] and TEST_ADDR_1 in log[
                        2] and "MAU" in log[2]:
                    mau_found = True
                    break

        self.assertTrue(mau_found)

        # Turn off trace logging for the inter-router connection
        qd_manager.update("org.apache.qpid.dispatch.connection",
                          {"enableProtocolTrace": "no"},
                          identity=conn_id)

        # Create a receiver and make sure the MAU update is NOT seen on the inter-router connection log
        TEST_ADDR_2 = "EnableConnectionLevelProtocolTraceTest2"
        conn_1 = BlockingConnection(self.address)
        blocking_receiver_2 = conn_1.create_receiver(address=TEST_ADDR_2)

        time.sleep(1)

        logs = qd_manager.get_log()
        mau_found = False
        for log in logs:
            if u'PROTOCOL' in log[0]:
                if "@transfer" in log[2] and TEST_ADDR_2 in log[
                        2] and "MAU" in log[2]:
                    mau_found = True
                    break

        self.assertFalse(mau_found)
        conn_1.close()
        conn_2.close()
    def test_forwarding_fanout(self):
        """
        Verify bindings that do not have a key receive all messages
        """
        config = [
            ('exchange', {
                'address': 'AddressF',
                'name': 'ExchangeF'
            }),
            ('binding', {
                'name': 'binding1',
                'exchangeName': 'ExchangeF',
                'bindingKey': 'pattern',
                'nextHopAddress': 'nextHop1'
            }),
            # two bindings w/o key
            ('binding', {
                'name': 'binding2',
                'exchangeName': 'ExchangeF',
                'nextHopAddress': 'nextHop2'
            }),
            ('binding', {
                'name': 'binding3',
                'exchangeName': 'ExchangeF',
                'nextHopAddress': 'nextHop3'
            })
        ]

        for meth in ['amqp', 'mqtt']:
            config[0][1]['matchMethod'] = meth
            router = self._create_router('A', config)

            # create clients for message transfer
            conn = BlockingConnection(router.addresses[0])
            sender = conn.create_sender(address="AddressF",
                                        options=AtMostOnce())
            nhop1 = conn.create_receiver(address="nextHop1", credit=100)
            nhop2 = conn.create_receiver(address="nextHop2", credit=100)
            nhop3 = conn.create_receiver(address="nextHop3", credit=100)

            # send message with subject "nope"
            # should arrive at nextHop2 & 3 only
            sender.send(Message(subject='nope', body='A'))
            self.assertEqual('A', nhop2.receive(timeout=TIMEOUT).body)
            self.assertEqual('A', nhop3.receive(timeout=TIMEOUT).body)

            # send message with subject "pattern"
            # forwarded to all bindings:
            sender.send(Message(subject='pattern', body='B'))
            self.assertEqual('B', nhop1.receive(timeout=TIMEOUT).body)
            self.assertEqual('B', nhop2.receive(timeout=TIMEOUT).body)
            self.assertEqual('B', nhop3.receive(timeout=TIMEOUT).body)

            conn.close()
            router.teardown()
    def test_max_frame_default(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxFrameDefault.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # if frame size not set then a default is used
            self.assertTrue(" max-frame-size=16384" in open_lines[0])
    def test_max_frame_small(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxFrameSmall.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # if frame size <= 512 proton set min of 512
            self.assertTrue(" max-frame-size=512" in open_lines[0])
    def test_max_sessions(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxSessions.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # channel-max is 10
            self.assertTrue(" channel-max=9" in open_lines[0])
Beispiel #8
0
    def test_max_sessions_large(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with open('../setUpClass/MaxSessionsLarge.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # channel-max is 0. Should get proton default 32767
            self.assertIn(" channel-max=32767", open_lines[0])
Beispiel #9
0
    def test_inter_router_protocol_trace(self):
        qd_manager = QdManager(self, self.address)

        # The router already has trace logging turned on for all connections.
        # Get the connection id of the inter-router connection
        results = qd_manager.query("org.apache.qpid.dispatch.connection")
        conn_id = None
        for result in results:
            if result['role'] == 'inter-router':
                conn_id = result['identity']

        # Turn off trace logging for the inter-router connection. This update command is run async by the router
        # so we need to sleep a bit before the operation is actually completed.
        qd_manager.update("org.apache.qpid.dispatch.connection",
                          {"enableProtocolTrace": "false"},
                          identity=conn_id)
        time.sleep(1)

        num_transfers = self._get_transfer_frame_count(conn_id)

        # Create a receiver. This will send an MAU update to the other router but we should not see any of that
        # in the log since the trace logging for the inter-router connection has been turned off.
        TEST_ADDR_1 = "EnableConnectionLevelProtocolTraceTest1"
        conn_2 = BlockingConnection(self.address)
        conn_2.create_receiver(address=TEST_ADDR_1)
        # Give some time for the MAU to go over the inter-router connection.
        time.sleep(2)
        num_transfers_after_update = self._get_transfer_frame_count(conn_id)

        # Since there will be no transfer frames printed in the log, there should be no more new transfers in the
        # log file.
        self.assertEqual(num_transfers_after_update, num_transfers)

        # Turn on trace logging for the inter-router connection
        qd_manager.update("org.apache.qpid.dispatch.connection",
                          {"enableProtocolTrace": "yes"},
                          identity=conn_id)

        # Create a receiver and make sure the MAU update is NOT seen on the inter-router connection log
        TEST_ADDR_2 = "EnableConnectionLevelProtocolTraceTest2"
        conn_1 = BlockingConnection(self.address)
        conn_1.create_receiver(address=TEST_ADDR_2)

        # Give time for the MAU to be generated.
        time.sleep(2)

        num_transfers_after_update = self._get_transfer_frame_count(conn_id)

        # Since we have now turned on trace logging for the inter-router connection, we should see
        # additional transfer frames in the log and we check that here.
        self.assertGreater(num_transfers_after_update, num_transfers)
        conn_1.close()
        conn_2.close()
    def test_max_frame_max_session_zero(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxFrameMaxSessionFramesZero.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # max-frame gets set to protocol min
            self.assertTrue(' max-frame-size=512,' in open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window is defaulted to 2^31-1
            self.assertTrue(" incoming-window=2147483647," in begin_lines[0])
    def test_max_frame_max_session_too_big(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxFrameMaxSessionFramesTooBig.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # max-frame is from the config
            self.assertTrue(' max-frame-size=1000000,' in open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window is truncated
            self.assertTrue(" incoming-window=2147," in begin_lines[0])
            warning_lines = [s for s in log_lines if "requested maxSessionFrames truncated from 5000000 to 2147" in s]
            self.assertTrue(len(warning_lines) == 1)
    def test_custom_annotations_match(self):
        """
        The linkRoute on Routers C and B is set to org.apache.
        Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'.
        Sends a message with custom annotations to org.apache via router QDR.C and makes sure that the message was successfully
        routed (using full address matching) and received using pre-created links that were created as a
        result of specifying addresses in the linkRoute attribute('org.apache.'). Make sure custom annotations arrived as well.
        """
        hello_world_3 = "Hello World_3!"
        # Connects to listener #2 on QDR.C
        addr = self.routers[2].addresses[0]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache
        blocking_receiver = blocking_connection.create_receiver(address="org.apache")

        apply_options = AtMostOnce()

        # Sender to  to org.apache
        blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options)
        msg = Message(body=hello_world_3)
        annotations = {'custom-annotation': '1/Custom_Annotation'}
        msg.annotations = annotations

        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_3, received_message.body)
        self.assertEqual(received_message.annotations, annotations)

        blocking_connection.close()
    def run(self):
        try:
            ssl = SSLDomain(SSLDomain.MODE_CLIENT)
            ssl.set_credentials(str(self.options.accountPublicKey),
                                str(self.options.accountPrivateKey), str(""))
            ssl.set_trusted_ca_db(str(self.options.brokerPublicKey))
            ssl.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME,
                                        trusted_CAs=str(
                                            self.options.brokerPublicKey))

            connection = BlockingConnection(self.address,
                                            ssl_domain=ssl,
                                            heartbeat=60000)
            receiver = connection.create_receiver(self.broadcast_address,
                                                  credit=self.capacity)

            while True:
                received_message = None

                try:
                    received_message = receiver.receive(
                        timeout=self.options.timeout)
                except Timeout, e:
                    print("-I- No message received for ", self.options.timeout,
                          " seconds")
                    break

                self.message_counter += 1
                print("-I- Received broadcast message: " +
                      received_message.body)
                receiver.accept()

            print("-I- " + str(self.message_counter) + " messages received")

            connection.close()
    def test_max_frame_max_session_frames__max_sessions_default(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxFrameMaxSessionFrames.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # max-frame is from the config
            self.assertTrue(' max-frame-size=2048,' in open_lines[0])
            # channel-max is default
            self.assertTrue(" channel-max=32767" in open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window is from the config
            self.assertTrue(" incoming-window=10," in begin_lines[0] )
    def run(self):
        try:
            ssl = SSLDomain(SSLDomain.MODE_CLIENT)
            ssl.set_credentials(str(self.options.accountPublicKey), str(self.options.accountPrivateKey), str(""))
            ssl.set_trusted_ca_db(str(self.options.brokerPublicKey))
            ssl.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME, trusted_CAs=str(self.options.brokerPublicKey))

            connection = BlockingConnection(self.address, ssl_domain=ssl, heartbeat=60000)
            receiver = connection.create_receiver(self.broadcast_address, credit=self.capacity)

            while True:
                received_message = None

                try:
                    received_message = receiver.receive(timeout=self.options.timeout)
                except Timeout, e:
                    print("-I- No message received for ", self.options.timeout, " seconds")
                    break

                self.message_counter += 1
                print("-I- Received broadcast message: " + received_message.body)
                receiver.accept()

            print("-I- " + str(self.message_counter) + " messages received")

            connection.close()
    def test_max_session_frames_default(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        if skip_test:
            return self.skipTest("Test skipped on non-64 bit architectures")
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxSessionFramesDefault.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # if frame size not set then a default is used
            self.assertTrue(" max-frame-size=16384" in open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window is from the config
            self.assertTrue(" incoming-window=2147483647," in begin_lines[0])
Beispiel #17
0
    def test_max_session_frames_default(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        if skip_test:
            return self.skipTest("Test skipped on non-64 bit architectures")
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with  open('../setUpClass/MaxSessionFramesDefault.log', 'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # if frame size not set then a default is used
            self.assertTrue(" max-frame-size=16384" in open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window is from the config
            self.assertTrue(" incoming-window=2147483647," in begin_lines[0])
    def test_custom_annotations_match(self):
        """
        The linkRoute on Routers C and B is set to org.apache.
        Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'.
        Sends a message with custom annotations to org.apache via router QDR.C and makes sure that the message was successfully
        routed (using full address matching) and received using pre-created links that were created as a
        result of specifying addresses in the linkRoute attribute('org.apache.'). Make sure custom annotations arrived as well.
        """
        hello_world_3 = "Hello World_3!"
        # Connects to listener #2 on QDR.C
        addr = self.routers[2].addresses[0]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache
        blocking_receiver = blocking_connection.create_receiver(address="org.apache")

        apply_options = AtMostOnce()

        # Sender to  to org.apache
        blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options)
        msg = Message(body=hello_world_3)
        annotations = {'custom-annotation': '1/Custom_Annotation'}
        msg.annotations = annotations

        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_3, received_message.body)
        self.assertEqual(received_message.annotations, annotations)

        blocking_connection.close()
    def test_forwarding_fanout(self):
        """
        Verify bindings that do not have a key receive all messages
        """
        config = [
            ('exchange', {'address': 'AddressF',
                          'name': 'ExchangeF'}),
            ('binding', {'name':           'binding1',
                         'exchangeName':   'ExchangeF',
                         'bindingKey':     'pattern',
                         'nextHopAddress': 'nextHop1'}),
            # two bindings w/o key
            ('binding', {'name':           'binding2',
                         'exchangeName':   'ExchangeF',
                         'nextHopAddress': 'nextHop2'}),
            ('binding', {'name':           'binding3',
                         'exchangeName':   'ExchangeF',
                         'nextHopAddress': 'nextHop3'})
        ]

        for meth in ['amqp', 'mqtt']:
            config[0][1]['matchMethod'] = meth
            router = self._create_router('A', config)

            # create clients for message transfer
            conn = BlockingConnection(router.addresses[0])
            sender = conn.create_sender(address="AddressF", options=AtMostOnce())
            nhop1 = conn.create_receiver(address="nextHop1", credit=100)
            nhop2 = conn.create_receiver(address="nextHop2", credit=100)
            nhop3 = conn.create_receiver(address="nextHop3", credit=100)

            # send message with subject "nope"
            # should arrive at nextHop2 & 3 only
            sender.send(Message(subject='nope', body='A'))
            self.assertEqual('A', nhop2.receive(timeout=TIMEOUT).body)
            self.assertEqual('A', nhop3.receive(timeout=TIMEOUT).body)

            # send message with subject "pattern"
            # forwarded to all bindings:
            sender.send(Message(subject='pattern', body='B'))
            self.assertEqual('B', nhop1.receive(timeout=TIMEOUT).body)
            self.assertEqual('B', nhop2.receive(timeout=TIMEOUT).body)
            self.assertEqual('B', nhop3.receive(timeout=TIMEOUT).body)

            conn.close()
            router.teardown()
Beispiel #20
0
    def test_multiple_log_file(self):
        blocking_connection = BlockingConnection(self.address)

        TEST_ADDRESS = "test_multiple_log_file"

        blocking_receiver = blocking_connection.create_receiver(address=TEST_ADDRESS)
        blocking_sender = blocking_connection.create_sender(address=TEST_ADDRESS, options=apply_options)

        TEST_MSG = "LOGTEST"
        msg = Message(body=TEST_MSG)
        blocking_sender.send(msg)
        received_message = blocking_receiver.receive()
        self.assertEqual(TEST_MSG, received_message.body)
        server_log_found = True
        all_server_logs = True
        try:
            with open(self.router.outdir + '/test-router-server.log', 'r') as server_log:
                for line in server_log:
                    parts = line.split(" ")
                    if (parts[3] != "SERVER"):
                        all_server_logs = False
                        break
        except:
            server_log_found = False

        self.assertTrue(all_server_logs)
        self.assertTrue(server_log_found)

        protocol_log_found = True
        all_protocol_logs = True
        try:
            with open(self.router.outdir + '/test-router-protocol.log', 'r') as protocol_log:
                for line in protocol_log:
                    parts = line.split(" ")
                    if (parts[3] != "PROTOCOL"):
                        all_protocol_logs = False
                        break
        except:
            protocol_log_found = False

        self.assertTrue(protocol_log_found)
        self.assertTrue(all_protocol_logs)

        core_router_log_found = True
        all_core_router_logs = True
        try:
            with open(self.router.outdir + '/test-router-core.log', 'r') as core_log:
                for line in core_log:
                    parts = line.split(" ")
                    if parts[3] != "ROUTER_CORE" and parts[3] != "ROUTER":
                        all_core_router_logs = False
                        break

        except:
            core_router_log_found = False

        self.assertTrue(core_router_log_found)
        self.assertTrue(all_core_router_logs)
Beispiel #21
0
 def create_sender_receiver(self, test_address, test_msg, blocking_connection=None):
     if not blocking_connection:
         blocking_connection = BlockingConnection(self.address)
     blocking_receiver = blocking_connection.create_receiver(address=test_address)
     blocking_sender = blocking_connection.create_sender(address=test_address, options=apply_options)
     msg = Message(body=test_msg)
     blocking_sender.send(msg)
     received_message = blocking_receiver.receive()
     self.assertEqual(test_msg, received_message.body)
    def test_resumable_receiver_disallowed(self):
        addr = self.routers[1].addresses[0]

        connection = BlockingConnection(addr)
        try:
            receiver = connection.create_receiver(
                address="org.apache", options=[DurableSubscription()])
            self.fail("link should have been detached")
        except LinkDetached, e:
            None
    def test_resumable_receiver_disallowed(self):
        addr = self.routers[1].addresses[0]

        connection = BlockingConnection(addr)
        try:
            receiver = connection.create_receiver(address="org.apache", options=[DurableSubscription()])
            self.fail("link should have been detached")
        except LinkDetached:
            pass
        connection.close()
Beispiel #24
0
    def test_max_frame_max_session_zero(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with open('../setUpClass/MaxFrameMaxSessionFramesZero.log',
                  'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # max-frame gets set to protocol min
            self.assertIn(' max-frame-size=512,', open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window should be 2^31-1 (64-bit) or
            # (2^31-1) / max-frame-size (32-bit)
            is_64bits = sys.maxsize > 2**32
            expected = " incoming-window=2147483647," if is_64bits else \
                (" incoming-window=%d," % int(2147483647 / 512))
            self.assertIn(expected, begin_lines[0])
    def test_yy_query_many_links(self):
        # This test will fail without the fix for DISPATCH-974
        c = BlockingConnection(self.address())
        count = 0
        links = []
        COUNT = 5000

        ADDRESS_SENDER = "examples-sender"
        ADDRESS_RECEIVER = "examples-receiver"

        # This loop creates 5000 consumer and 5000 producer links with
        # different addresses
        while True:
            count += 1
            r = c.create_receiver(ADDRESS_RECEIVER + str(count))
            links.append(r)
            s = c.create_sender(ADDRESS_SENDER + str(count))
            links.append(c)
            if count == COUNT:
                break

        # Try fetching all 10,000 addresses
        # This qdmanage query command would fail without the fix
        # for DISPATCH-974
        query_command = 'QUERY --type=org.apache.qpid.dispatch.router.address'
        outs = json.loads(self.run_qdmanage(query_command))

        sender_addresses = 0
        receiver_addresses = 0

        for out in outs:
            if ADDRESS_SENDER in out['name']:
                sender_addresses += 1
            if ADDRESS_RECEIVER in out['name']:
                receiver_addresses += 1

        self.assertEqual(sender_addresses, COUNT)
        self.assertEqual(receiver_addresses, COUNT)

        query_command = 'QUERY --type=link'
        outs = json.loads(self.run_qdmanage(query_command))

        out_links = 0
        in_links = 0

        for out in outs:
            if out.get('owningAddr'):
                if ADDRESS_SENDER in out['owningAddr']:
                    in_links += 1
                if ADDRESS_RECEIVER in out['owningAddr']:
                    out_links += 1

        self.assertEqual(out_links, COUNT)
        self.assertEqual(in_links, COUNT)
Beispiel #26
0
    def test_yy_query_many_links(self):
        # This test will fail without the fix for DISPATCH-974
        c = BlockingConnection(self.address())
        count = 0
        links = []
        COUNT = 5000

        ADDRESS_SENDER = "examples-sender"
        ADDRESS_RECEIVER = "examples-receiver"

        # This loop creates 5000 consumer and 5000 producer links with
        # different addresses
        while True:
            count += 1
            r = c.create_receiver(ADDRESS_RECEIVER + str(count))
            links.append(r)
            s = c.create_sender(ADDRESS_SENDER + str(count))
            links.append(c)
            if count == COUNT:
                break

        # Try fetching all 10,000 addresses
        # This qdmanage query command would fail without the fix
        # for DISPATCH-974
        query_command = 'QUERY --type=org.apache.qpid.dispatch.router.address'
        outs = json.loads(self.run_qdmanage(query_command))

        sender_addresses = 0
        receiver_addresses = 0

        for out in outs:
            if ADDRESS_SENDER in out['name']:
                sender_addresses += 1
            if ADDRESS_RECEIVER in out['name']:
                receiver_addresses += 1

        self.assertEqual(sender_addresses, COUNT)
        self.assertEqual(receiver_addresses, COUNT)

        query_command = 'QUERY --type=link'
        outs = json.loads(self.run_qdmanage(query_command))

        out_links = 0
        in_links = 0

        for out in outs:
            if out.get('owningAddr'):
                if ADDRESS_SENDER in out['owningAddr']:
                    in_links += 1
                if ADDRESS_RECEIVER in out['owningAddr']:
                    out_links += 1

        self.assertEqual(out_links, COUNT)
        self.assertEqual(in_links, COUNT)
    def test_max_frame_max_session_too_big(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with open('../setUpClass/MaxFrameMaxSessionFramesTooBig.log',
                  'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # max-frame is from the config
            self.assertTrue(' max-frame-size=1000000,' in open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window is truncated
            self.assertTrue(" incoming-window=2147," in begin_lines[0])
            warning_lines = [s for s in log_lines if "(warning)" in s]
            self.assertTrue(len(warning_lines) == 1)
            self.assertTrue(
                "requested maxSessionFrames truncated from 5000000 to 2147" in
                warning_lines[0])
Beispiel #28
0
    def test_max_session_frames_default(self):
        # Set up a connection to get the Open and a receiver to get a Begin frame in the log
        bc = BlockingConnection(self.router.addresses[0])
        bc.create_receiver("xxx")
        bc.close()

        with open('../setUpClass/MaxSessionFramesDefault.log',
                  'r') as router_log:
            log_lines = router_log.read().split("\n")
            open_lines = [s for s in log_lines if "-> @open" in s]
            # if frame size not set then a default is used
            self.assertIn(" max-frame-size=16384", open_lines[0])
            begin_lines = [s for s in log_lines if "-> @begin" in s]
            # incoming-window should be 2^31-1 (64-bit) or
            # (2^31-1) / max-frame-size (32-bit)
            is_64bits = sys.maxsize > 2**32
            expected = " incoming-window=2147483647," if is_64bits else \
                (" incoming-window=%d," % int(2147483647 / 16384))
            #self.assertIn(expected, begin_lines[0], "Expected:'%s' not found in '%s'" % (expected, begin_lines[0]))
            self.assertIn(expected, begin_lines[0])
Beispiel #29
0
    def get_next_message_on_queue(self) -> Message:
        """
        Gets the next message from the queue
        :return: Message read from queue
        """
        connection = BlockingConnection(self.queue_url, user=self.username, password=self.password)
        receiver = connection.create_receiver(self.queue_name)
        message = receiver.receive(timeout=30)
        receiver.accept()
        connection.close()

        return message
    def test_aaa_partial_link_route_match(self):
        """
        The linkRoutePattern on Routers C and B is set to org.apache.
        Creates a receiver listening on the address 'org.apache.dev' and a sender that sends to address 'org.apache.dev'.
        Sends a message to org.apache.dev via router QDR.C and makes sure that the message was successfully
        routed (using partial address matching) and received using pre-created links that were created as a
        result of specifying addresses in the linkRoutePattern attribute('org.apache.').
        """
        hello_world_1 = "Hello World_1!"

        # Connects to listener #2 on QDR.C
        addr = self.routers[2].addresses[1]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache.dev
        blocking_receiver = blocking_connection.create_receiver(address="org.apache.dev")

        apply_options = AtMostOnce()

        # Sender to  to org.apache.dev
        blocking_sender = blocking_connection.create_sender(address="org.apache.dev", options=apply_options)
        msg = Message(body=hello_world_1)
        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_1, received_message.body)

        # Connect to the router acting like the broker (QDR.A) and check the deliveriesIngress and deliveriesEgress
        local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT)
        self.assertEqual(u'QDR.A', local_node.query(type='org.apache.qpid.dispatch.router',
                                                    attribute_names=['routerId']).results[0][0])

        self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address',
                                            name='router.address/M0org.apache.dev').deliveriesEgress,
                         "deliveriesEgress is wrong")
        self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address',
                                            name='router.address/M0org.apache.dev').deliveriesIngress,
                         "deliveriesIngress is wrong")

        # There should be 4 links -
        # 1. outbound receiver link on org.apache.dev
        # 2. inbound sender link on blocking_sender
        # 3. inbound link to the $management
        # 4. outbound link to $management
        # self.assertEqual(4, len()
        self.assertEquals(4, len(local_node.query(type='org.apache.qpid.dispatch.router.link').results))

        #blocking_receiver.close()
        blocking_connection.close()
Beispiel #31
0
 def drain(self):
     """
     Drain the queue to prevent test failures caused by previous failing tests not ack'ing all of their messages
     """
     connection = BlockingConnection(self.queue_url, user=self.username, password=self.password)
     receiver = connection.create_receiver(self.queue_name)
     try:
         while True:
             receiver.receive(timeout=1)
             receiver.accept()
     except Timeout:
         pass
     finally:
         connection.close()
Beispiel #32
0
    def test_full_link_route_match(self):
        """
        The linkRoute on Routers C and B is set to org.apache.
        Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'.
        Sends a message to org.apache via router QDR.C and makes sure that the message was successfully
        routed (using full address matching) and received using pre-created links that were created as a
        result of specifying addresses in the linkRoute attribute('org.apache.').
        """
        hello_world_3 = "Hello World_3!"
        # Connects to listener #2 on QDR.C
        addr = self.routers[2].addresses[0]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache
        blocking_receiver = blocking_connection.create_receiver(
            address="org.apache")

        apply_options = AtMostOnce()

        # Sender to  to org.apache
        blocking_sender = blocking_connection.create_sender(
            address="org.apache", options=apply_options)
        msg = Message(body=hello_world_3)
        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_3, received_message.body)

        local_node = Node.connect(self.routers[0].addresses[0],
                                  timeout=TIMEOUT)

        # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms
        # that the message was link routed
        self.assertEqual(
            1,
            local_node.read(type='org.apache.qpid.dispatch.router.address',
                            name='M0org.apache').deliveriesEgress)

        self.assertEqual(
            1,
            local_node.read(type='org.apache.qpid.dispatch.router.address',
                            name='M0org.apache').deliveriesIngress)

        blocking_connection.close()
Beispiel #33
0
    def test_full_link_route_match_1(self):
        """
        This test is pretty much the same as the previous test (test_full_link_route_match) but the connection is
        made to router QDR.B instead of QDR.C and we expect the message to be link routed successfully.
        """
        hello_world_4 = "Hello World_4!"
        addr = self.routers[1].addresses[0]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache
        blocking_receiver = blocking_connection.create_receiver(
            address="org.apache")

        apply_options = AtMostOnce()

        # Sender to  to org.apache
        blocking_sender = blocking_connection.create_sender(
            address="org.apache", options=apply_options)

        msg = Message(body=hello_world_4)
        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_4, received_message.body)

        local_node = Node.connect(self.routers[0].addresses[0],
                                  timeout=TIMEOUT)

        # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms
        # that the message was link routed
        self.assertEqual(
            1,
            local_node.read(type='org.apache.qpid.dispatch.router.address',
                            name='M0org.apache').deliveriesEgress)

        self.assertEqual(
            1,
            local_node.read(type='org.apache.qpid.dispatch.router.address',
                            name='M0org.apache').deliveriesIngress)

        blocking_connection.close()
Beispiel #34
0
    def test_router_core_logger(self):
        blocking_connection = BlockingConnection(self.address)

        TEST_ADDRESS = "test_multiple_log_file"

        blocking_receiver = blocking_connection.create_receiver(
            address=TEST_ADDRESS)
        blocking_sender = blocking_connection.create_sender(
            address=TEST_ADDRESS, options=apply_options)

        TEST_MSG_BODY = "LOGTEST"
        msg = Message(body=TEST_MSG_BODY)
        blocking_sender.send(msg)
        received_message = blocking_receiver.receive()
        self.assertEqual(TEST_MSG_BODY, received_message.body)
        qd_manager = QdManager(self, self.address)
        logs = qd_manager.get_log()

        router_core_found = False
        for log in logs:
            if 'ROUTER_CORE' in log[0]:
                router_core_found = True
                break

        self.assertTrue(router_core_found)

        core_log_file_found = True
        all_lines_router_core = True
        try:
            # Before the fix to DISPATCH-1575, this file will not be
            # created because the router core module was logging to the ROUTER
            # module instead of the ROUTER_CORE module.
            with open(self.router.outdir + '/test-router-core.log',
                      'r') as core_log:
                for line in core_log:
                    # Every line in the file must log to the router core module.
                    if "ROUTER_CORE" not in line:
                        all_lines_router_core = False
                        break
        except:
            core_log_file_found = False

        self.assertTrue(core_log_file_found)
        self.assertTrue(all_lines_router_core)
    def test_full_link_route_match(self):
        """
        The linkRoutePattern on Routers C and B is set to org.apache.
        Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'.
        Sends a message to org.apache via router QDR.C and makes sure that the message was successfully
        routed (using full address matching) and received using pre-created links that were created as a
        result of specifying addresses in the linkRoutePattern attribute('org.apache.').
        """
        hello_world_3 = "Hello World_3!"
        # Connects to listener #2 on QDR.C
        addr = self.routers[2].addresses[1]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache
        blocking_receiver = blocking_connection.create_receiver(address="org.apache")

        apply_options = AtMostOnce()

        # Sender to  to org.apache
        blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options)
        msg = Message(body=hello_world_3)
        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_3, received_message.body)

        local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT)

        # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms
        # that the message was link routed
        self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address',
                                            name='router.address/M0org.apache').deliveriesEgress,
                         "deliveriesEgress is wrong")

        self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address',
                                            name='router.address/M0org.apache').deliveriesIngress,
                         "deliveriesIngress is wrong")

        #blocking_receiver.close()
        blocking_connection.close()
Beispiel #36
0
class SyncRequestClient(IncomingMessageHandler):
    """
    Implementation of the synchronous request-responce (aka RPC) pattern.
    Create an instance and call invoke() to send a request and wait for a response.
    """
    def __init__(self, url, timeout=None):
        """
        @param url: a proton.Url or a URL string of the form 'host:port/path'
            host:port is used to connect, path is used to identify the remote messaging endpoint.
        """
        super(SyncRequestClient, self).__init__()
        self.connection = BlockingConnection(Url(url).defaults(),
                                             timeout=timeout)
        self.sender = self.connection.create_sender(url.path)
        # dynamic=true generates a unique address dynamically for this receiver.
        # credit=1 because we want to receive 1 response message initially.
        self.receiver = self.connection.create_receiver(None,
                                                        dynamic=True,
                                                        credit=1,
                                                        handler=self)
        self.response = None

    def invoke(self, request):
        """Send a request, wait for and return the response"""
        request.reply_to = self.reply_to
        self.sender.send(request)
        self.connection.wait(lambda: self.response, msg="Waiting for response")
        response = self.response
        self.response = None  # Ready for next response.
        self.receiver.flow(1)  # Set up credit for the next response.
        return response

    @property
    def reply_to(self):
        """Return the dynamic address of our receiver."""
        return self.receiver.remote_source.address

    def on_message(self, event):
        """Called when we receive a message for our receiver."""
        self.response = event.message  # Store the response

    def close(self):
        self.connection.close()
Beispiel #37
0
    def test_verify_n_receivers(self):
        n = 4
        addr = self.address()
        br1 = BlockingConnection(addr)

        # n receivers OK
        br1.create_receiver(address="****YES_1of4***")
        br1.create_receiver(address="****YES_20f4****")
        br1.create_receiver(address="****YES_3of4****")
        br1.create_receiver(address="****YES_4of4****")

        # receiver n+1 should be denied
        self.assertRaises(LinkDetached, br1.create_receiver, "****NO****")

        br1.close()
    def test_full_link_route_match_1(self):
        """
        This test is pretty much the same as the previous test (test_full_link_route_match) but the connection is
        made to router QDR.B instead of QDR.C and we expect the message to be link routed successfully.
        """
        hello_world_4 = "Hello World_4!"
        addr = self.routers[2].addresses[0]

        blocking_connection = BlockingConnection(addr)

        # Receive on org.apache
        blocking_receiver = blocking_connection.create_receiver(address="org.apache")

        apply_options = AtMostOnce()

        # Sender to  to org.apache
        blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options)

        msg = Message(body=hello_world_4)
        # Send a message
        blocking_sender.send(msg)

        received_message = blocking_receiver.receive()

        self.assertEqual(hello_world_4, received_message.body)

        local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT)

        # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms
        # that the message was link routed
        self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address',
                                            name='M0org.apache').deliveriesEgress,
                         "deliveriesEgress is wrong")

        self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address',
                                            name='M0org.apache').deliveriesIngress,
                         "deliveriesIngress is wrong")

        blocking_connection.close()
class ReceiverThread(threading.Thread):
    def __init__(self, _id, address=ADDRESS, domain=None):
        super(ReceiverThread, self).__init__()
        self._id = _id
        self.address = address
        self.domain = domain
        self.running = True
        self.nr = 0
        self.max = RECEIVERS

    def connect(self):
        self.conn = BlockingConnection(ROUTER_ADDRESS,
                                       ssl_domain=self.domain,
                                       heartbeat=HEARTBEAT)

    def run(self):
        self.connect()
        for x in xrange(self.max):
            name = '%s.%s' % (self.address, x)
            self.recv = self.conn.create_receiver(name,
                                                  name=str(uuid4()),
                                                  dynamic=False,
                                                  options=None)
            print("created: ", name)
    def run(self):
        try:
            ssl = SSLDomain(SSLDomain.MODE_CLIENT)
            ssl.set_credentials(str(self.options.accountPublicKey), str(self.options.accountPrivateKey), str(""))
            ssl.set_trusted_ca_db(str(self.options.brokerPublicKey))
            ssl.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME, trusted_CAs=str(self.options.brokerPublicKey))

            connection = BlockingConnection(self.address, ssl_domain=ssl, heartbeat=60000)
            receiver = connection.create_receiver(self.response_address)
            sender = connection.create_sender(self.request_address)

            message = Message(body="<FIXML>...</FIXML>", reply_to=self.reply_adress)
            print("-I- Sending request message: " + message.body)
            sender.send(message);

            try:
                received_message = receiver.receive(timeout=self.options.timeout)
                print("-I- Received response message: " + received_message.body)
                self.message_counter += 1
                receiver.accept()
            except Timeout, e:
                print("-I- No message received for ", self.options.timeout, " seconds")

            connection.close()
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from __future__ import print_function
from proton import Message
from proton.utils import BlockingConnection
from proton.handlers import IncomingMessageHandler

conn = BlockingConnection("localhost:5672")
receiver = conn.create_receiver("examples")
sender = conn.create_sender("examples")
sender.send(Message(body="Hello World!"));
msg = receiver.receive(timeout=30)
print(msg.body)
receiver.accept()
conn.close()

    def test_normal_receiver_allowed(self):
        addr = self.routers[1].addresses[0]

        connection = BlockingConnection(addr)
        receiver = connection.create_receiver(address="org.apache")
        connection.close()
Beispiel #43
0
class Connection(BaseConnection):
    """
    Proton connection.
    """

    __metaclass__ = ThreadSingleton

    @staticmethod
    def ssl_domain(connector):
        """
        Get the ssl domain using the broker settings.
        :param connector: A broker.
        :type connector: Connector
        :return: The populated domain.
        :rtype: SSLDomain
        :raise: SSLException
        :raise: ValueError
        """
        domain = None
        if connector.use_ssl():
            connector.ssl.validate()
            domain = SSLDomain(SSLDomain.MODE_CLIENT)
            domain.set_trusted_ca_db(connector.ssl.ca_certificate)
            domain.set_credentials(
                connector.ssl.client_certificate,
                connector.ssl.client_key or connector.ssl.client_certificate, None)
            if connector.ssl.host_validation:
                mode = SSLDomain.VERIFY_PEER_NAME
            else:
                mode = SSLDomain.VERIFY_PEER
            domain.set_peer_authentication(mode)
        return domain

    def __init__(self, url):
        """
        :param url: The connector url.
        :type url: str
        """
        super(Connection, self).__init__(url)
        self._impl = None

    def is_open(self):
        """
        Get whether the connection has been opened.
        :return: True if open.
        :rtype bool
        """
        return self._impl is not None

    @retry(ConnectionException, SSLException)
    def open(self):
        """
        Open a connection to the broker.
        """
        if self.is_open():
            # already open
            return
        connector = Connector.find(self.url)
        domain = self.ssl_domain(connector)
        log.info('open: %s', connector)
        self._impl = BlockingConnection(
            connector.url.canonical,
            heartbeat=connector.heartbeat,
            ssl_domain=domain)
        log.info('opened: %s', self.url)

    def sender(self, address):
        """
        Get a message sender for the specified address.
        :param address: An AMQP address.
        :type address: str
        :return: A sender.
        :rtype: proton.utils.BlockingSender
        """
        name = utf8(uuid4())
        return self._impl.create_sender(address, name=name)

    def receiver(self, address=None, dynamic=False):
        """
        Get a message receiver for the specified address.
        :param address: An AMQP address.
        :type address: str
        :param dynamic: Indicates link address is dynamically assigned.
        :type dynamic: bool
        :return: A receiver.
        :rtype: proton.utils.BlockingReceiver
        """
        options = None
        name = utf8(uuid4())
        if dynamic:
            # needed by dispatch router
            options = DynamicNodeProperties({'x-opt-qd.address': unicode(address)})
            address = None
        return self._impl.create_receiver(address, name=name, dynamic=dynamic, options=options)

    def close(self):
        """
        Close the connection.
        """
        connection = self._impl
        self._impl = None
        try:
            connection.close()
            log.info('closed: %s', self.url)
        except Exception, pe:
            log.debug(utf8(pe))
    def test_forwarding(self):
        """
        Simple forwarding over a single 0-10 exchange
        """
        config = [
            ('exchange', {'address': 'Address1',
                          'name': 'Exchange1',
                          'matchMethod': 'amqp'}),
            # two different patterns, same next hop:
            ('binding', {'name':           'binding1',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a.*',
                         'nextHopAddress': 'nextHop1'}),
            ('binding', {'name':           'binding2',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a.b',
                         'nextHopAddress': 'nextHop1'}),
            # duplicate patterns, different next hops:
            ('binding', {'name':           'binding3',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a.c.#',
                         'nextHopAddress': 'nextHop1'}),
            ('binding', {'name': 'binding4',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a.c.#',
                         'nextHopAddress': 'nextHop2'}),
            # match for nextHop2 only
            ('binding', {'name':           'binding5',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a.b.c',
                         'nextHopAddress': 'nextHop2'})
        ]
        router = self._create_router('A', config)

        # create clients for message transfer
        conn = BlockingConnection(router.addresses[0])
        sender = conn.create_sender(address="Address1", options=AtMostOnce())
        nhop1 = conn.create_receiver(address="nextHop1", credit=100)
        nhop2 = conn.create_receiver(address="nextHop2", credit=100)

        # verify initial metrics
        self._validate_exchange(router, name='Exchange1',
                                bindingCount=5,
                                receivedCount=0,
                                droppedCount=0,
                                forwardedCount=0,
                                divertedCount=0)

        for b in range(5):
            self._validate_binding(router,
                                   name='binding%s' % (b + 1),
                                   matchedCount=0)

        # send message with subject "a.b"
        # matches (binding1, binding2)
        # forwarded to NextHop1 only
        sender.send(Message(subject='a.b', body='A'))
        self.assertEqual('A', nhop1.receive(timeout=TIMEOUT).body)

        # send message with subject "a.c"
        # matches (bindings 1,3,4)
        # ->  NextHop1, NextHop2
        sender.send(Message(subject='a.c', body='B'))
        self.assertEqual('B', nhop1.receive(timeout=TIMEOUT).body)
        self.assertEqual('B', nhop2.receive(timeout=TIMEOUT).body)

        # send message with subject "a.c.d"
        # matches bindings 3,4
        # -> NextHop1, NextHop2
        sender.send(Message(subject='a.c.d', body='C'))
        self.assertEqual('C', nhop1.receive(timeout=TIMEOUT).body)
        self.assertEqual('C', nhop2.receive(timeout=TIMEOUT).body)

        # send message with subject "x.y.z"
        # no binding match - expected to drop
        # not forwarded
        sender.send(Message(subject='x.y.z', body=["I am Noone"]))

        # send message with subject "a.b.c"
        # matches binding5
        # -> NextHop2
        sender.send(Message(subject='a.b.c', body='D'))
        self.assertEqual('D', nhop2.receive(timeout=TIMEOUT).body)

        # ensure there are no more messages on either hop:

        self.assertRaises(Timeout, nhop1.receive, timeout=0.25)
        self.assertRaises(Timeout, nhop2.receive, timeout=0.25)

        # validate counters
        self._validate_binding(router, name='binding1',
                               matchedCount=2)
        self._validate_binding(router, name='binding2',
                               matchedCount=1)
        self._validate_binding(router, name='binding3',
                               matchedCount=2)
        self._validate_binding(router, name='binding4',
                               matchedCount=2)
        self._validate_binding(router, name='binding5',
                               matchedCount=1)
        self._validate_exchange(router, name="Exchange1",
                                receivedCount=5,
                                forwardedCount=4,
                                divertedCount=0,
                                droppedCount=1)
        conn.close()
    def test_forwarding_mqtt(self):
        """
        Simple forwarding over a single mqtt exchange
        """
        config = [
            ('exchange', {'address':          'Address2',
                          'name':             'Exchange1',
                          'matchMethod':      'mqtt',
                          'alternateAddress': 'altNextHop'}),

            ('binding', {'name':           'binding1',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a/b',
                         'nextHopAddress': 'nextHop1'}),
            ('binding', {'name':           'binding2',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'a/+',
                         'nextHopAddress': 'nextHop2'}),
            ('binding', {'name':           'binding3',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'c/#',
                         'nextHopAddress': 'nextHop1'}),
            ('binding', {'name':           'binding4',
                         'exchangeName':   'Exchange1',
                         'bindingKey':     'c/b',
                         'nextHopAddress': 'nextHop2'}),
        ]
        router = self._create_router('B', config)

        # create clients for message transfer
        conn = BlockingConnection(router.addresses[0])
        sender = conn.create_sender(address="Address2", options=AtMostOnce())
        nhop1 = conn.create_receiver(address="nextHop1", credit=100)
        nhop2 = conn.create_receiver(address="nextHop2", credit=100)
        alt = conn.create_receiver(address="altNextHop", credit=100)

        # send message with subject "a.b"
        # matches (binding1, binding2)
        # forwarded to NextHop1, NextHop2
        sender.send(Message(subject='a/b', body='A'))
        self.assertEqual('A', nhop1.receive(timeout=TIMEOUT).body)
        self.assertEqual('A', nhop2.receive(timeout=TIMEOUT).body)

        # send message with subject "a/c"
        # matches binding2
        # ->  NextHop2
        sender.send(Message(subject='a/c', body='B'))
        self.assertEqual('B', nhop2.receive(timeout=TIMEOUT).body)

        # send message with subject "c/b"
        # matches bindings 3,4
        # -> NextHop1, NextHop2
        sender.send(Message(subject='c/b', body='C'))
        self.assertEqual('C', nhop1.receive(timeout=TIMEOUT).body)
        self.assertEqual('C', nhop2.receive(timeout=TIMEOUT).body)

        # send message with subject "c/b/dee/eee"
        # matches binding3
        # -> NextHop1
        sender.send(Message(subject='c/b/dee/eee', body='D'))
        self.assertEqual('D', nhop1.receive(timeout=TIMEOUT).body)

        # send message with subject "x.y.z"
        # no binding match
        # -> alternate
        sender.send(Message(subject='x.y.z', body="?"))
        self.assertEqual('?', alt.receive(timeout=TIMEOUT).body)

        # ensure there are no more messages on either hop:

        self.assertRaises(Timeout, nhop1.receive, timeout=0.25)
        self.assertRaises(Timeout, nhop2.receive, timeout=0.25)
        self.assertRaises(Timeout, alt.receive, timeout=0.25)

        # validate counters
        self._validate_binding(router, name='binding1',
                               matchedCount=1)
        self._validate_binding(router, name='binding2',
                               matchedCount=2)
        self._validate_binding(router, name='binding3',
                               matchedCount=2)
        self._validate_binding(router, name='binding4',
                               matchedCount=1)
        self._validate_exchange(router, name="Exchange1",
                                receivedCount=5,
                                forwardedCount=5,
                                divertedCount=1,
                                droppedCount=0)
        conn.close()
class JMSClient(object):
    """Class JMSClient
    """

    _mh = None
    _client = None
    _host = None
    _port = None
    _user = None
    _passw = None
    _verbose = None
    _is_connected = None

    def __init__(self, verbose=False):
        """Class constructor

        Called when the object is initialized

        Args:                   
           verbose (bool): verbose mode

        """

        try:

            self._mh = MasterHead.get_head()

            self._verbose = verbose
            if (self._verbose):
                basicConfig()
                getLogger().setLevel(DEBUG)

        except ProtonException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())

    @property
    def client(self):
        """ AMQP client property getter """

        return self._client

    @property
    def host(self):
        """ server host property getter """

        return self._host

    @property
    def port(self):
        """ server port property getter """

        return self._port

    @property
    def user(self):
        """ username property getter """

        return self._user

    @property
    def passw(self):
        """ user password property getter """

        return self._passw

    @property
    def verbose(self):
        """ verbose mode property getter """

        return self._verbose

    @property
    def is_connected(self):
        """ is_connected property getter """

        return self._is_connected

    def connect(self, host, port=5672, user=None, passw=None, timeout=10):
        """Method connects to server

        Args:
           host (str): hostname
           port (str): port
           user (str): username
           passw (str): password
           timeout (int): timeout

        Returns:
           bool: result

        Raises:
           event: jms_before_connect
           event: jms_after_connected            

        """

        try:

            msg = 'host:{0}, port:{1}, user:{2}, passw:{3}, timeout:{4}'.format(
                host, port, user, passw, timeout)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_connecting', msg), self._mh.fromhere())

            ev = event.Event(
                'jms_before_connect', host, port, user, passw, timeout)
            if (self._mh.fire_event(ev) > 0):
                host = ev.argv(0)
                port = ev.argv(1)
                user = ev.argv(2)
                passw = ev.argv(3)
                timeout = ev.argv(4)

            self._host = host
            self._port = port
            self._user = user
            self._passw = passw

            if (ev.will_run_default()):
                if (self._user == None):
                    self._client = BlockingConnection(
                        'amqp://{0}:{1}'.format(self._host, self._port), timeout=timeout)
                else:
                    self._client = BlockingConnection('amqp://{0}:{1}@{2}:{3}'.format(self._user, self._passw,
                                                                                      self._host, self._port), timeout=timeout)
                self._is_connected = True

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_connected'), self._mh.fromhere())
            ev = event.Event('jms_after_connect')
            self._mh.fire_event(ev)

            return True

        except ProtonException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def disconnect(self):
        """Method disconnects from server

        Args:   
           none        

        Returns:
           bool: result

        """

        try:

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_disconnecting'), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return False
            else:
                self._client.close()
                self._is_connected = False
                self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                    'htk_jms_disconnected'), self._mh.fromhere())
                return True

        except ProtonException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def send(self, destination_name, message, destination_type='queue', headers={}):
        """Method sends message

        JMS headers - JMSDeliveryMode, JMSPriority, JMSExpiration, JMSType, JMSMessageID,
                      JMSDestination, JMSReplyTo, JMSCorrelationID, JMSTimestamp

        Args:
           destination_name (str): queue|topic name
           message (str): message
           destination_type (str): queue|topic
           headers (dict): JMS headers, key - title, value - string

        Returns:
           bool: result

        Raises:
           event: jms_before_send
           event: jms_after_send             

        """

        try:

            msg = 'destination_name:{0}, message:{1}, destination_type:{2}, headers:{3}'.format(
                destination_name, message, destination_type, headers)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_sending_msg', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return False

            ev = event.Event(
                'jms_before_send', destination_name, message, destination_type, headers)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                message = ev.argv(1)
                destination_type = ev.argv(2)
                headers = ev.argv(3)

            if (ev.will_run_default()):
                sender = self._client.create_sender(
                    '{0}://{1}'.format(destination_type, destination_name))

                headers_new = {}
                for key, value in headers.items():
                    if (key in mapping):
                        headers_new[mapping[key]] = value

                sender.send(Message(body=message, properties=headers_new))
                sender.close()

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_sent'), self._mh.fromhere())
            ev = event.Event('jms_after_send')
            self._mh.fire_event(ev)

            return True

        except ProtonException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def receive(self, destination_name, cnt=1):
        """Method receives messages

        Args:
           destination_name (str): queue name
           cnt (int): count of messages

        Returns:
           list:  messages as dictionary {'message', JMS headers}

        Raises:
           event: jms_before_receive
           event: jms_after_receive             

        """

        try:

            msg = 'destination_name:{0}, count:{1}'.format(
                destination_name, cnt)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_receiving_msg', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return None

            ev = event.Event('jms_before_receive', destination_name, cnt)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                cnt = ev.argv(1)

            if (ev.will_run_default()):
                receiver = self._client.create_receiver(
                    'queue://{0}'.format(destination_name))

                msgs = []
                i = 0
                while (i < cnt):
                    try:
                        msg = receiver.receive(timeout=1)
                        receiver.accept()
                        msgs.append(msg)
                        i = i + 1
                    except Timeout:
                        break

                messages = []
                for msg in msgs:

                    message = {}
                    message['message'] = msg.body
                    for key, value in msg.properties.items():
                        if (key in mapping.values()):
                            message[
                                list(mapping.keys())[list(mapping.values()).index(key)]] = value

                    messages.append(message)

                receiver.close()

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_received', len(messages)), self._mh.fromhere())
            ev = event.Event('jms_after_receive')
            self._mh.fire_event(ev)

            return messages

        except ProtonException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return None

    def browse(self, destination_name, cnt=100, jms_correlation_id=None, jms_type=None):
        """Method browses queue

        Args:
           destination_name (str): queue name
           cnt (int): count of messages
           jms_correlation_id (str): requested JMSCorrelationID
           jms_type (str): requested JMSType

        Returns:
           list: messages as dictionary {'message', JMS headers}

        Raises:
           event: jms_before_browse
           event: jms_after_browse              

        """

        try:

            msg = 'destination_name:{0}, count:{1}, jms_correlation_id:{2}, jms_type:{3}'.format(
                destination_name, cnt, jms_correlation_id, jms_type)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_browsing', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return None

            ev = event.Event('jms_before_browse', destination_name, cnt)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                cnt = ev.argv(1)
                jms_correlation_id = ev.argv(2)
                jms_type = ev.argv(3)

            if (ev.will_run_default()):
                receiver = self._client.create_receiver(
                    'queue://{0}'.format(destination_name), options=Copy())

                msgs = []
                i = 0
                while (i < cnt):
                    try:
                        msg = receiver.receive(timeout=1)
                        receiver.accept()
                        correlation_id = None
                        type = None
                        for key, value in msg.properties.items():
                            if (key == 'properties.correlation-id'):
                                correlation_id = value
                            elif (key == 'message-annotations.x-opt-jms-type'):
                                type = value

                        if ((jms_correlation_id == None or jms_correlation_id == correlation_id) and
                                (jms_type == None or jms_type == type)):
                            msgs.append(msg)
                            i = i + 1

                    except Timeout:
                        break

                receiver.close()

                messages = []
                for msg in msgs:

                    message = {}
                    message['message'] = msg.body
                    for key, value in msg.properties.items():
                        if (key in mapping.values()):
                            message[
                                list(mapping.keys())[list(mapping.values()).index(key)]] = value

                    messages.append(message)

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_received', len(messages)), self._mh.fromhere())
            ev = event.Event('jms_after_browse')
            self._mh.fire_event(ev)

            return messages

        except ProtonException as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return None
    def test_zzz_qdmanage_delete_link_route(self):
        """
        We are deleting the link route using qdmanage short name. This should be the last test to run
        """

        # First delete linkRoutes on QDR.B
        local_node = Node.connect(self.routers[1].addresses[0], timeout=TIMEOUT)
        result_list = local_node.query(type='org.apache.qpid.dispatch.router.config.linkRoute').results

        identity_1 = result_list[0][1]
        identity_2 = result_list[1][1]
        identity_3 = result_list[2][1]
        identity_4 = result_list[3][1]

        cmd = 'DELETE --type=linkRoute --identity=' + identity_1
        self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0])

        cmd = 'DELETE --type=linkRoute --identity=' + identity_2
        self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0])

        cmd = 'DELETE --type=linkRoute --identity=' + identity_3
        self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0])

        cmd = 'DELETE --type=linkRoute --identity=' + identity_4
        self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0])

        cmd = 'QUERY --type=linkRoute'
        out = self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0])
        self.assertEquals(out.rstrip(), '[]')

        # linkRoutes now gone on QDR.B but remember that it still exist on QDR.C
        # We will now try to create a receiver on address org.apache.dev on QDR.C.
        # Since the linkRoute on QDR.B is gone, QDR.C
        # will not allow a receiver to be created since there is no route to destination.

        # Connects to listener #2 on QDR.C
        addr = self.routers[2].addresses[1]

        # Now delete linkRoutes on QDR.C to eradicate linkRoutes completely
        local_node = Node.connect(addr, timeout=TIMEOUT)
        result_list = local_node.query(type='org.apache.qpid.dispatch.router.config.linkRoute').results

        identity_1 = result_list[0][1]
        identity_2 = result_list[1][1]
        identity_3 = result_list[2][1]
        identity_4 = result_list[3][1]

        cmd = 'DELETE --type=linkRoute --identity=' + identity_1
        self.run_qdmanage(cmd=cmd, address=addr)

        cmd = 'DELETE --type=linkRoute --identity=' + identity_2
        self.run_qdmanage(cmd=cmd, address=addr)

        cmd = 'DELETE --type=linkRoute --identity=' + identity_3
        self.run_qdmanage(cmd=cmd, address=addr)

        cmd = 'DELETE --type=linkRoute --identity=' + identity_4
        self.run_qdmanage(cmd=cmd, address=addr)

        cmd = 'QUERY --type=linkRoute'
        out = self.run_qdmanage(cmd=cmd, address=addr)
        self.assertEquals(out.rstrip(), '[]')

        blocking_connection = BlockingConnection(addr, timeout=3)

        # Receive on org.apache.dev (this address used to be linkRouted but not anymore since we deleted linkRoutes
        # on both QDR.C and QDR.B)
        blocking_receiver = blocking_connection.create_receiver(address="org.apache.dev")

        apply_options = AtMostOnce()
        hello_world_1 = "Hello World_1!"
        # Sender to org.apache.dev
        blocking_sender = blocking_connection.create_sender(address="org.apache.dev", options=apply_options)
        msg = Message(body=hello_world_1)

        # Send a message
        blocking_sender.send(msg)
        received_message = blocking_receiver.receive(timeout=5)
        self.assertEqual(hello_world_1, received_message.body)
    def test_yy_query_many_links(self):
        # This test will fail without the fix for DISPATCH-974
        c = BlockingConnection(self.router.addresses[0])
        count = 0
        links = []
        COUNT = 5000

        ADDRESS_SENDER = "examples-sender"
        ADDRESS_RECEIVER = "examples-receiver"

        # This loop creates 5000 consumer and 5000 producer links
        while True:
            count += 1
            r = c.create_receiver(ADDRESS_RECEIVER + str(count))
            links.append(r)
            s = c.create_sender(ADDRESS_SENDER + str(count))
            links.append(c)
            if count == COUNT:
                break

        # Now we run qdstat command and check if we got back details
        # about all the 10000 links
        # We do not specify the limit which means unlimited
        # which means get everything that is there.
        outs = self.run_qdstat(['--links'])
        out_list = outs.split("\n")

        out_links = 0
        in_links = 0
        for out in out_list:
            if "endpoint  in" in out and ADDRESS_SENDER in out:
                in_links += 1
            if "endpoint  out" in out and ADDRESS_RECEIVER in out:
                out_links += 1

        self.assertEqual(in_links, COUNT)
        self.assertEqual(out_links, COUNT)

        # Run qdstat with a limit more than 10,000
        outs = self.run_qdstat(['--links', '--limit=15000'])
        out_list = outs.split("\n")

        out_links = 0
        in_links = 0
        for out in out_list:
            if "endpoint  in" in out and ADDRESS_SENDER in out:
                in_links += 1
            if "endpoint  out" in out and ADDRESS_RECEIVER in out:
                out_links += 1

        self.assertEqual(in_links, COUNT)
        self.assertEqual(out_links, COUNT)


        # Run qdstat with a limit less than 10,000
        outs = self.run_qdstat(['--links', '--limit=2000'])
        out_list = outs.split("\n")

        links = 0
        for out in out_list:
            if "endpoint" in out and "examples" in out:
                links += 1

        self.assertEqual(links, 2000)

        # Run qdstat with a limit of 700 because 700
        # is the maximum number of rows we get per request
        outs = self.run_qdstat(['--links', '--limit=700'])
        out_list = outs.split("\n")

        links = 0
        for out in out_list:
            if "endpoint" in out and "examples" in out:
                links += 1

        self.assertEqual(links, 700)

        # Run qdstat with a limit of 500 because 700
        # is the maximum number of rows we get per request
        # and we want to try something less than 700
        outs = self.run_qdstat(['--links', '--limit=500'])
        out_list = outs.split("\n")

        links = 0
        for out in out_list:
            if "endpoint" in out and "examples" in out:
                links += 1

        self.assertEqual(links, 500)


        # This test would fail without the fix for DISPATCH-974
        outs = self.run_qdstat(['--address'])
        out_list = outs.split("\n")

        sender_addresses = 0
        receiver_addresses = 0
        for out in out_list:
            if ADDRESS_SENDER in out:
                sender_addresses += 1
            if ADDRESS_RECEIVER in out:
                receiver_addresses += 1

        self.assertEqual(sender_addresses, COUNT)
        self.assertEqual(receiver_addresses, COUNT)

        c.close()