示例#1
0
    def loads_key(self,
                  typ: Optional[ModelArg],
                  key: Optional[bytes],
                  *,
                  serializer: CodecArg = None) -> K:
        """Deserialize message key.

        Arguments:
            typ: Model to use for deserialization.
            key: Serialized key.

            serializer: Codec to use for this value.  If not set
               the default will be used (:attr:`key_serializer`).
        """
        if key is None:
            if typ is not None and issubclass(typ, ModelT):
                raise KeyDecodeError(f'Expected {typ!r}, received {key!r}!')
            return key
        serializer = serializer or self.key_serializer
        try:
            payload = self._loads(serializer, key)
            return cast(K, self._prepare_payload(typ, payload))
        except MemoryError:
            raise
        except Exception as exc:
            raise KeyDecodeError(str(exc)).with_traceback(
                sys.exc_info()[2]) from exc
示例#2
0
 async def test_decode__decode_error_callback(self, *, topic, message):
     exc = KeyDecodeError()
     topic.app.serializers.loads_key = Mock(side_effect=exc)
     topic.on_key_decode_error = AsyncMock()
     topic._compile_decode()
     await topic.decode(message, propagate=False)
     topic.on_key_decode_error.assert_called_once_with(exc, message)
示例#3
0
 async def test_decode__decode_error_propagate(self, *, topic, message):
     exc = KeyDecodeError()
     topic.app.serializers.loads_key = Mock(side_effect=exc)
     topic._compile_decode()
     with pytest.raises(type(exc)):
         await topic.decode(message, propagate=True)