Пример #1
0
    def _send_data(
        self,
        data: Dict[Any, Any],
        result_id: Optional[str] = None,
        block: bool = False,
    ) -> AsyncResult:
        """
        Sends data to tdlib.

        If `block`is True, waits for the result
        """
        if '@extra' not in data:
            data['@extra'] = {}

        if not result_id and 'request_id' in data['@extra']:
            result_id = data['@extra']['request_id']

        async_result = AsyncResult(client=self, result_id=result_id)
        data['@extra']['request_id'] = async_result.id
        self._results[async_result.id] = async_result
        self._tdjson.send(data)
        async_result.request = data

        if block:
            async_result.wait(raise_exc=True)

        return async_result
Пример #2
0
    def test_wait_with_error_and_raise_exc(self):
        async_result = AsyncResult(client=None)
        async_result.error = True
        async_result.error_info = 'some_error'
        async_result._ready.set()

        with pytest.raises(RuntimeError):
            async_result.wait(timeout=0.1, raise_exc=True)
Пример #3
0
    def test_wait_with_timeout(self):
        ar = AsyncResult(client=None)
        try:
            ar.wait(timeout=0.01)
            raised = False
        except TimeoutError:
            raised = True

        assert raised is True
Пример #4
0
    def test_wait_with_error_and_raise_exc(self):
        ar = AsyncResult(client=None)
        ar.error = True
        ar.error_info = 'some_error'
        try:
            ar.wait(timeout=0.1, raise_exc=True)
            raised = False
        except RuntimeError:
            raised = True

        assert raised is True
Пример #5
0
    def _wait_authorization_result(self, result: AsyncResult) -> AuthorizationState:
        authorization_state = None
        if result:
            result.wait(raise_exc=True)

            if result.update is None:
                raise RuntimeError('Something wrong, the result update is None')

            if result.id == 'getAuthorizationState':
                authorization_state = result.update['@type']
            else:
                authorization_state = result.update['authorization_state']['@type']

        return AuthorizationState(authorization_state)
Пример #6
0
 def test_wait_with_error_and_without_raise_exc(self):
     ar = AsyncResult(client=None)
     ar.error = True
     ar.error_info = 'some_error'
     ar.wait(timeout=0.01)
Пример #7
0
 def test_wait_with_update(self):
     ar = AsyncResult(client=None)
     ar.update = '123'
     ar.wait(timeout=0.01)
Пример #8
0
 def test_wait_with_update(self):
     async_result = AsyncResult(client=None)
     async_result.update = '123'
     async_result._ready.set()
     async_result.wait(timeout=0.01)
Пример #9
0
    def test_wait_with_timeout(self):
        async_result = AsyncResult(client=None)

        with pytest.raises(TimeoutError):
            async_result.wait(timeout=0.01)
Пример #10
0
 def test_wait_with_error_and_without_raise_exc(self):
     async_result = AsyncResult(client=None)
     async_result.error = True
     async_result.error_info = 'some_error'
     async_result._ready.set()
     async_result.wait(timeout=0.01)