Skip to content

Commit

Permalink
test exc
Browse files Browse the repository at this point in the history
  • Loading branch information
jjaakola-aiven committed Jul 24, 2024
1 parent a226610 commit a63043f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 67 deletions.
14 changes: 7 additions & 7 deletions karapace/protobuf/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ def writer_process(
# Writing happens in the forked process, catch is broad so exception will get communicated
# back to calling process.
except Exception as bare_exception: # pylint: disable=broad-exception-caught
try:
raise ProtobufTypeException(writer_schema, datum) from bare_exception
except ProtobufTypeException as protobuf_exception:
writer_queue.put(protobuf_exception)
raise protobuf_exception
except BaseException as base_exception: # pylint: disable=broad-exception-caught
writer_queue.put(base_exception)
#try:
# raise ProtobufTypeException(writer_schema, datum) from bare_exception
#except ProtobufTypeException as protobuf_exception:
# writer_queue.put(protobuf_exception)
# raise protobuf_exception
writer_queue.put(bare_exception)
raise bare_exception
writer_queue.put(class_instance.SerializeToString())


Expand Down
60 changes: 0 additions & 60 deletions tests/unit/test_protobuf_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,63 +215,3 @@ async def test_happy_flow_references_two(default_config_path: Path):
assert 1 in serializer.ids_to_schemas

assert mock_protobuf_registry_client.method_calls == [call.get_schema("top"), call.get_schema_for_id(1)]


async def test_serialization_fails(default_config_path: Path):
mock_protobuf_registry_client = Mock()
get_latest_schema_future = asyncio.Future()
get_latest_schema_future.set_result(
(1, ParsedTypedSchema.parse(SchemaType.PROTOBUF, trim_margin(schema_protobuf)), Versioner.V(1))
)
mock_protobuf_registry_client.get_schema.return_value = get_latest_schema_future

serializer = await make_ser_deser(default_config_path, mock_protobuf_registry_client)
with pytest.raises(InvalidMessageSchema):
schema = await serializer.get_schema_for_subject("top")
await serializer.serialize(schema, test_fail_objects_protobuf[0])

assert mock_protobuf_registry_client.method_calls == [call.get_schema("top")]
mock_protobuf_registry_client.reset_mock()

with pytest.raises(InvalidMessageSchema):
schema = await serializer.get_schema_for_subject("top")
await serializer.serialize(schema, test_fail_objects_protobuf[1])

assert mock_protobuf_registry_client.method_calls == [call.get_schema("top")]


async def test_deserialization_fails(default_config_path: Path):
mock_protobuf_registry_client = Mock()

deserializer = await make_ser_deser(default_config_path, mock_protobuf_registry_client)
invalid_header_payload = struct.pack(">bII", 1, 500, 500)
with pytest.raises(InvalidMessageHeader):
await deserializer.deserialize(invalid_header_payload)

assert mock_protobuf_registry_client.method_calls == []
mock_protobuf_registry_client.reset_mock()

# wrong schema id (500)
invalid_data_payload = struct.pack(">bII", START_BYTE, 500, 500)
with pytest.raises(InvalidPayload):
await deserializer.deserialize(invalid_data_payload)

assert mock_protobuf_registry_client.method_calls == [call.get_schema_for_id(500)]


async def test_deserialization_fails2(default_config_path: Path):
mock_protobuf_registry_client = Mock()

deserializer = await make_ser_deser(default_config_path, mock_protobuf_registry_client)
invalid_header_payload = struct.pack(">bII", 1, 500, 500)
with pytest.raises(InvalidMessageHeader):
await deserializer.deserialize(invalid_header_payload)

assert mock_protobuf_registry_client.method_calls == []
mock_protobuf_registry_client.reset_mock()

enc_bytes = b"\x00\x00\x00\x00\x01\x00\x02\x05\0x12" # wrong schema data (2)
with pytest.raises(InvalidPayload):
await deserializer.deserialize(enc_bytes)

assert mock_protobuf_registry_client.method_calls == [call.get_schema_for_id(1)]

0 comments on commit a63043f

Please sign in to comment.