Exemple #1
0
def c_config():
    """
    Print the current configuration of barcode-server
    """
    from barcode_server.config import AppConfig

    config = AppConfig()
    click.echo(config.print(TomlFormatter()))
Exemple #2
0
def c_run():
    """
    Run the barcode-server
    """
    from barcode_server.barcode import BarcodeReader
    from barcode_server.config import AppConfig
    from barcode_server.webserver import Webserver

    signal.signal(signal.SIGINT, signal_handler)

    config = AppConfig()

    log_level = logging._nameToLevel.get(
        str(config.LOG_LEVEL.value).upper(), config.LOG_LEVEL.default)
    logging.getLogger("barcode_server").setLevel(log_level)

    barcode_reader = BarcodeReader(config)
    webserver = Webserver(config, barcode_reader)

    logging.debug("Starting...")

    # start prometheus server
    if config.STATS_PORT.value is not None:
        LOGGER.info("Starting statistics webserver...")
        start_http_server(config.STATS_PORT.value)

    tasks = asyncio.gather(webserver.start(), )

    loop.run_until_complete(tasks)
    loop.run_forever()
class TestBase(IsolatedAsyncioTestCase):
    from barcode_server.config import AppConfig
    from container_app_conf.source.yaml_source import YamlSource

    # load config from test folder
    config = AppConfig(singleton=True,
                       data_sources=[YamlSource("barcode_server", "./tests/")])
Exemple #4
0
class WebsocketNotifierTest(AioHTTPTestCase):
    from barcode_server.config import AppConfig
    from container_app_conf.source.yaml_source import YamlSource

    # load config from test folder
    config = AppConfig(singleton=True,
                       data_sources=[YamlSource("barcode_server", "./tests/")])

    webserver = None

    async def get_application(self):
        """
        Override the get_app method to return your application.
        """
        barcode_reader = MagicMock()
        self.webserver = Webserver(self.config, barcode_reader)
        app = self.webserver.create_app()
        runner = aiohttp.web.AppRunner(app)
        await runner.setup()
        site = aiohttp.web.TCPSite(runner,
                                   host=self.config.SERVER_HOST.value,
                                   port=self.config.SERVER_PORT.value)
        await site.start()
        return app

    # the unittest_run_loop decorator can be used in tandem with
    # the AioHTTPTestCase to simplify running
    # tests that are asynchronous
    @unittest_run_loop
    async def test_ws_connect_and_event(self):
        sample_event = create_barcode_event_mock("abcdefg")
        expected_json = barcode_event_to_json(sample_event)

        async with aiohttp.ClientSession() as session:
            async with session.ws_connect(
                    'http://127.0.0.1:9654/',
                    headers={
                        "X-Auth-Token": self.config.SERVER_API_TOKEN.value
                    }) as ws:
                asyncio.create_task(self.webserver.on_barcode(sample_event))
                async for msg in ws:
                    if msg.type == aiohttp.WSMsgType.BINARY:
                        if expected_json == msg.data:
                            await ws.close()
                            assert True
                            return
                        else:
                            assert False
                    else:
                        assert False

        assert False
 async def test_initialization(self):
     config = AppConfig()
     reader = BarcodeReader(config)
     self.assertIsNotNone(reader)
 def __init__(self):
     config = AppConfig()
     self.drop_event_queue_after = config.DROP_EVENT_QUEUE_AFTER.value
     self.retry_interval = config.RETRY_INTERVAL.value
     self.event_queue = asyncio.Queue()
     self.processor_task: Optional[Task] = None
class WebsocketNotifierTest(AioHTTPTestCase):
    from barcode_server.config import AppConfig
    from container_app_conf.source.yaml_source import YamlSource

    # load config from test folder
    config = AppConfig(singleton=True,
                       data_sources=[YamlSource("barcode_server", "./tests/")])

    webserver = None

    async def get_application(self):
        """
        Override the get_app method to return your application.
        """
        barcode_reader = MagicMock()
        self.webserver = Webserver(self.config, barcode_reader)
        app = self.webserver.create_app()
        runner = aiohttp.web.AppRunner(app)
        await runner.setup()
        site = aiohttp.web.TCPSite(runner,
                                   host=self.config.SERVER_HOST.value,
                                   port=self.config.SERVER_PORT.value)
        await site.start()
        return app

    # the unittest_run_loop decorator can be used in tandem with
    # the AioHTTPTestCase to simplify running
    # tests that are asynchronous
    @unittest_run_loop
    async def test_ws_connect_and_event(self):
        sample_event = create_barcode_event_mock("abcdefg")
        expected_json = barcode_event_to_json(sample_event)

        import uuid
        client_id = str(uuid.uuid4())

        async with aiohttp.ClientSession() as session:
            async with session.ws_connect(
                    'http://127.0.0.1:9654/',
                    headers={
                        const.Client_Id: client_id,
                        const.X_Auth_Token: self.config.SERVER_API_TOKEN.value
                    }) as ws:
                asyncio.create_task(self.webserver.on_barcode(sample_event))
                async for msg in ws:
                    if msg.type == aiohttp.WSMsgType.BINARY:
                        if expected_json == msg.data:
                            await ws.close()
                            assert True
                            return
                        else:
                            assert False
                    else:
                        assert False

        assert False

    @unittest_run_loop
    async def test_ws_reconnect_event_catchup(self):
        missed_event = create_barcode_event_mock("abcdefg")
        second_event = create_barcode_event_mock("123456")
        missed_event_json = barcode_event_to_json(missed_event)
        second_event_json = barcode_event_to_json(second_event)

        import uuid
        client_id = str(uuid.uuid4())

        # connect to the server once
        async with aiohttp.ClientSession() as session:
            async with session.ws_connect(
                    'http://127.0.0.1:9654/',
                    headers={
                        const.Client_Id: client_id,
                        const.X_Auth_Token: self.config.SERVER_API_TOKEN.value
                    }) as ws:
                await ws.close()

        # then emulate a barcode scan event
        asyncio.create_task(self.webserver.on_barcode(missed_event))

        await asyncio.sleep(0.1)

        # and then reconnect again, expecting the event in between
        async with aiohttp.ClientSession() as session:
            async with session.ws_connect(
                    'http://127.0.0.1:9654/',
                    headers={
                        const.Client_Id: client_id,
                        const.X_Auth_Token: self.config.SERVER_API_TOKEN.value
                    }) as ws:
                # emulate another event, while connected
                asyncio.create_task(self.webserver.on_barcode(second_event))

                missed_event_received = False
                async for msg in ws:
                    if msg.type == aiohttp.WSMsgType.BINARY:
                        if missed_event_json == msg.data:
                            if missed_event_received:
                                assert False
                            missed_event_received = True
                        elif second_event_json == msg.data:
                            if not missed_event_received:
                                assert False
                            await ws.close()
                            assert True
                            return
                        else:
                            assert False
                    else:
                        assert False

        assert False

    @unittest_run_loop
    async def test_ws_reconnect_drop_cache(self):
        missed_event = create_barcode_event_mock("abcdefg")
        second_event = create_barcode_event_mock("123456")
        missed_event_json = barcode_event_to_json(missed_event)
        second_event_json = barcode_event_to_json(second_event)

        import uuid
        client_id = str(uuid.uuid4())

        # connect to the server once
        async with aiohttp.ClientSession() as session:
            async with session.ws_connect(
                    'http://127.0.0.1:9654/',
                    headers={
                        const.Client_Id: client_id,
                        const.X_Auth_Token: self.config.SERVER_API_TOKEN.value
                    }) as ws:
                await ws.close()

        # then emulate a barcode scan event while not connected
        asyncio.create_task(self.webserver.on_barcode(missed_event))

        await asyncio.sleep(0.1)

        # and then reconnect again, passing the "drop cache" header, expecting only
        # the new live event
        async with aiohttp.ClientSession() as session:
            async with session.ws_connect(
                    'http://127.0.0.1:9654/',
                    headers={
                        const.Client_Id: client_id,
                        const.Drop_Event_Queue: "",
                        const.X_Auth_Token: self.config.SERVER_API_TOKEN.value
                    }) as ws:
                # emulate another event, while connected
                asyncio.create_task(self.webserver.on_barcode(second_event))

                async for msg in ws:
                    if msg.type == aiohttp.WSMsgType.BINARY:
                        if missed_event_json == msg.data:
                            assert False
                        elif second_event_json == msg.data:
                            await ws.close()
                            assert True
                            return
                        else:
                            assert False
                    else:
                        assert False

        assert False