async def inner():
            self.log.debug('Starting server')
            server_future = asyncio.ensure_future(self.server.serve_forever())
            await self.server.when_started

            self.port = self.server.endpoints[0]['port']

            import ssl
            ssl_context = ssl.create_default_context(
                ssl.Purpose.SERVER_AUTH, cafile='hpfeeds/tests/testcert.crt')
            ssl_context.check_hostname = False

            self.log.debug('Creating client service')
            client = ClientSession('127.0.0.1',
                                   self.port,
                                   'test',
                                   'secret',
                                   ssl=ssl_context)
            client.subscribe('test-chan')

            # Wait till client connected
            await client.when_connected

            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_client_connections') == 1
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_made') == 1

            self.log.debug('Publishing test message')
            client.publish('test-chan', b'test message')

            self.log.debug('Waiting for read()')
            assert ('test', 'test-chan',
                    b'test message') == await client.read()

            # We would test this after call to subscribe, but need to wait until sure server has processed command
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_subscriptions', {
                    'ident': 'test',
                    'chan': 'test-chan'
                }) == 1

            # This will only have incremented when server has processed auth message
            # Test can only reliably assert this is the case after reading a message
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_ready', {'ident': 'test'}) == 1

            self.log.debug('Stopping client')
            await client.close()

            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_send_buffer_fill',
                {'ident': 'test'}) == 12
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_send_buffer_drain',
                {'ident': 'test'}) == 32

            self.log.debug('Stopping server')
            server_future.cancel()
            await server_future
Exemple #2
0
    async def distribute_queued(self, queue):
        logging.info(f"Starting to distribute to {self.broker}:{self.port}...")
        self.enabled = True

        if self.tls_enabled:
            # create default SSL context, which requires valid cert chain
            client = ClientSession(self.broker,
                                   self.port,
                                   self.identity,
                                   self.secret,
                                   ssl=self.tls_enabled)
        else:
            client = ClientSession(self.broker, self.port, self.identity,
                                   self.secret)

        while self.enabled or queue.qsize() > 0:
            # logging.info(f"Created distributor {self.broker}...")
            try:
                msg = await queue.get()
                msg_as_json = self.construct_hpfeeds_msg(msg)
                for c in self.channels:
                    client.publish(c, msg_as_json)

            except asyncio.CancelledError:
                self.enabled = False
                logging.debug(f"Distribution to {self.broker} cancelled")

        logging.debug(f"Stopped to distribute to {self.broker}...")
        async def inner():
            self.log.debug('Starting server')
            server_future = asyncio.ensure_future(self.server.serve_forever())

            self.log.debug('Creating client service')
            client = ClientSession('127.0.0.1', self.port, 'test', 'secret')

            # Wait till client connected
            await client.when_connected

            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_client_connections') == 1
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_made') == 1

            # Subscribe to a new thing after connection is up
            client.subscribe('test-chan')

            self.log.debug('Publishing test message')
            client.publish('test-chan', b'test message')

            self.log.debug('Waiting for read()')
            assert ('test', 'test-chan',
                    b'test message') == await client.read()

            # We would test this after call to subscribe, but need to wait until sure server has processed command
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_subscriptions', {
                    'ident': 'test',
                    'chan': 'test-chan'
                }) == 1

            # Unsubscribe while the connection is up
            client.unsubscribe('test-chan')

            # FIXME: How to test that did anything!

            # This will only have incremented when server has processed auth message
            # Test can only reliably assert this is the case after reading a message
            assert prometheus.REGISTRY.get_sample_value(
                'hpfeeds_broker_connection_ready', {'ident': 'test'}) == 1

            self.log.debug('Stopping client')
            await client.close()

            self.log.debug('Stopping server')
            server_future.cancel()
            await server_future
Exemple #4
0
    async def distribute_queued(self, queue):
        logging.info(f"Starting to distribute to {self.broker}...")
        self.enabled = True
        client = ClientSession(self.broker, self.port, self.identity, self.secret)

        while self.enabled or queue.qsize() > 0:

            try:
                msg = await queue.get()
                msg_as_json = self.construct_hpfeeds_msg(msg)
                for c in self.channels:
                    client.publish(c, msg_as_json)

            except asyncio.CancelledError:
                self.enabled = False
                logging.info(f"Distribution to {self.broker} cancelled")

        logging.info(f"Stopped to distribute to {self.broker}...")##