From e2f3e5da46f7f842d9b89858aa1aea88c58f10d4 Mon Sep 17 00:00:00 2001 From: Bernhard Liebl Date: Tue, 18 Jan 2022 12:13:12 +0100 Subject: [PATCH] adapted test cases to: - work with contextual from_bytes() - add regression test against original issue with buf release (see test_roundtrip_bytes_buffer) --- test/test_serialization.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/test/test_serialization.py b/test/test_serialization.py index 28aa5ec..2d04184 100644 --- a/test/test_serialization.py +++ b/test/test_serialization.py @@ -46,8 +46,8 @@ def test_roundtrip_bytes(all_types): test_regression.init_all_types(msg) message_bytes = msg.to_bytes() - msg = all_types.TestAllTypes.from_bytes(message_bytes) - test_regression.check_all_types(msg) + with all_types.TestAllTypes.from_bytes(message_bytes) as msg: + test_regression.check_all_types(msg) @pytest.mark.skipif( @@ -77,8 +77,8 @@ def test_roundtrip_bytes_mmap(all_types): f.seek(0) memory = mmap.mmap(f.fileno(), length) - msg = all_types.TestAllTypes.from_bytes(memory) - test_regression.check_all_types(msg) + with all_types.TestAllTypes.from_bytes(memory) as msg: + test_regression.check_all_types(msg) @pytest.mark.skipif( @@ -90,13 +90,17 @@ def test_roundtrip_bytes_buffer(all_types): b = msg.to_bytes() v = memoryview(b) - msg = all_types.TestAllTypes.from_bytes(v) - test_regression.check_all_types(msg) + try: + with all_types.TestAllTypes.from_bytes(v) as msg: + test_regression.check_all_types(msg) + finally: + v.release() def test_roundtrip_bytes_fail(all_types): with pytest.raises(TypeError): - all_types.TestAllTypes.from_bytes(42) + with all_types.TestAllTypes.from_bytes(42) as msg: + pass @pytest.mark.skipif( @@ -229,14 +233,14 @@ def test_from_bytes_traversal_limit(all_types): bld.init("structList", size) data = bld.to_bytes() - msg = all_types.TestAllTypes.from_bytes(data) - with pytest.raises(capnp.KjException): - for i in range(0, size): - msg.structList[i].uInt8Field == 0 + with all_types.TestAllTypes.from_bytes(data) as msg: + with pytest.raises(capnp.KjException): + for i in range(0, size): + msg.structList[i].uInt8Field == 0 - msg = all_types.TestAllTypes.from_bytes(data, traversal_limit_in_words=2 ** 62) - for i in range(0, size): - assert msg.structList[i].uInt8Field == 0 + with all_types.TestAllTypes.from_bytes(data, traversal_limit_in_words=2 ** 62) as msg: + for i in range(0, size): + assert msg.structList[i].uInt8Field == 0 def test_from_bytes_packed_traversal_limit(all_types):