Ejemplo n.º 1
0
    async def test_worker_authenticate_connect_requests_work_and_disconnect(
            self, worker_auth, worker_headers, supervisor
    ):
        worker_peer = 'worker-peer'
        await worker_auth.authenticate(worker_headers)
        await supervisor.handle_message(
            peer=worker_peer,
            sender=sentinel.sender,
            message=IncomingMessage.from_raw({
                'path': 'worker_connected'
            })
        )
        await supervisor.handle_message(
            peer=worker_peer,
            sender=sentinel.sender,
            message=IncomingMessage.from_raw({
                'path': 'worker_requests_work'
            })
        )
        response = await supervisor.handle_request_and_catch_exceptions(
            Request(
                peer=worker_peer,
                sender=sentinel.sender,
                message=IncomingMessage.from_raw({
                    'path': 'worker_disconnected'
                })
            )
        )

        assert response is None
Ejemplo n.º 2
0
    def test_valid_raw_message_1(self):
        raw_message = {'path': 'some-path'}

        message = IncomingMessage.from_raw(raw_message)

        assert message.path == 'some-path'
        assert message.body == {}
Ejemplo n.º 3
0
    def test_valid_raw_message_2(self):
        raw_message = {'path': 'some-path', 'body': {'key': 'value'}}

        message = IncomingMessage.from_raw(raw_message)

        assert message.path == 'some-path'
        assert message.body == {'key': 'value'}
Ejemplo n.º 4
0
 async def onClose(self, wasClean, code, reason):
     log.info('Connection was closed. Reason: %s, peer: %s', reason,
              self.peer)
     await self.supervisor.handle_message(
         sender=self,
         peer=self.peer,
         message=IncomingMessage(path='worker_disconnected'),
     )
Ejemplo n.º 5
0
    async def test_when_input_matches_schema_no_error_should_be_raised(self):
        data = {'work_id': 1, 'status': 'DONE', 'output': 'some output'}
        request = Request(
            message=IncomingMessage(path='something', body=data),
            sender=Mock(),
            peer=Mock(),
        )

        result = await SomeClass().method(request)

        assert result == data
Ejemplo n.º 6
0
    async def test_input_does_not_match_schema_error_should_be_raised(self):
        request = Request(
            message=IncomingMessage(
                path='lala',
                body={},
            ),
            sender=Mock(),
            peer=Mock(),
        )
        with pytest.raises(exceptions.ValidationError) as exc:
            await SomeClass().method(request)

        assert exc.value.data == {
            'work_id': ['This field is required.'],
            'status': ['This field is required.'],
        }
Ejemplo n.º 7
0
 async def onMessage(self, payload, isBinary):
     try:
         raw_message = self.deserializer.deserialize(payload)
         log.info('New message: %s from: %s',
                  clear_passwords_from_message(raw_message), self.peer)
         message = IncomingMessage.from_raw(raw_message)
     except ValidationError as exc:
         log.exception(exc)
         self.sendClose(code=3400, reason=exc.data)
     except Exception as exc:
         log.exception(exc)
         self.sendClose(code=3400, reason='Message is not in json _format')
     else:
         await self.supervisor.handle_message(
             sender=self,
             peer=self.peer,
             message=message,
         )
Ejemplo n.º 8
0
 def get_message(self, message_body=None):
     return IncomingMessage(
         path=self.PATH,
         body=message_body
     )
Ejemplo n.º 9
0
    def test_invalid_raw_message_2(self):
        raw_message = {'path': None, 'body': 'some-body'}

        with pytest.raises(ValidationError):
            IncomingMessage.from_raw(raw_message)