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
    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
Esempio n. 3
0
 async def _send_event(self, event: BarcodeEvent):
     json = barcode_event_to_json(event)
     async with Client(hostname=self.host,
                       port=self.port,
                       username=self.user,
                       password=self.password,
                       client_id=self.client_id) as client:
         await client.publish(self.topic, json, self.qos, self.retain)
         LOGGER.debug(f"Notified {self.host}:{self.port}: {event.barcode}")
Esempio n. 4
0
 async def _send_event(self, event: BarcodeEvent):
     json = barcode_event_to_json(event)
     async with aiohttp.ClientSession() as session:
         async with session.request(self.method,
                                    self.url,
                                    headers=self.headers,
                                    data=json) as resp:
             resp.raise_for_status()
         LOGGER.debug(f"Notified {self.url}: {event.barcode}")
Esempio n. 5
0
    def test_json(self):
        date_str = "2020-08-03T10:00:00+00:00"

        input_device = Mock()
        input_device.name = "Barcode Scanner"
        input_device.path = "/dev/input/event2"
        input_device.info.vendor = 1
        input_device.info.product = 2

        date = datetime.fromisoformat(str(date_str))
        barcode = "4006824000970"

        event = BarcodeEvent(input_device, barcode, date)
        event_json = str(barcode_event_to_json(event))

        self.assertIn(date_str, event_json)
        self.assertIn(input_device.path, event_json)
        self.assertIn(barcode, event_json)
        self.assertIsNotNone(event_json)
Esempio n. 6
0
 async def _send_event(self, event: BarcodeEvent):
     json = barcode_event_to_json(event)
     await self.websocket.send_bytes(json)