From 3fb8a61d3a9f82adf8f4eee2292330ed67caf84c Mon Sep 17 00:00:00 2001 From: Joseph Zuniga-Daly Date: Fri, 3 Jul 2020 13:15:34 +0100 Subject: [PATCH] Test custom serializers mapped to interfaces --- ...ckNetworkCustomSerializerCheckpointTest.kt | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/node/src/integration-test/kotlin/net/corda/node/MockNetworkCustomSerializerCheckpointTest.kt b/node/src/integration-test/kotlin/net/corda/node/MockNetworkCustomSerializerCheckpointTest.kt index 82ffc3aa07..2625575feb 100644 --- a/node/src/integration-test/kotlin/net/corda/node/MockNetworkCustomSerializerCheckpointTest.kt +++ b/node/src/integration-test/kotlin/net/corda/node/MockNetworkCustomSerializerCheckpointTest.kt @@ -46,6 +46,32 @@ class MockNetworkCustomSerializerCheckpointTest{ val actualReference = node.startFlow(TestFlowCheckingReferencesWork(expectedReference)).get() Assertions.assertThat(actualReference).isSameAs(expectedReference) + Assertions.assertThat(actualReference["one"]).isEqualTo(1) + } + + @Test(timeout = 300_000) + @Suspendable + fun `check serilization of interfaces`() { + val node = mockNetwork.createPartyNode() + val result = node.startFlow(TestFlowWithDifficultToSerializeLocalVariableAsInterface(5)).get() + Assertions.assertThat(result).isEqualTo(5) + } + + @StartableByRPC + class TestFlowWithDifficultToSerializeLocalVariableAsInterface(private val purchase: Int) : FlowLogic() { + @Suspendable + override fun call(): Int { + + // This object is difficult to serialize with Kryo + val difficultToSerialize: BrokenMapInterface = BrokenMapSerializedByInterfaceImpl() + difficultToSerialize.putAll(mapOf("foo" to purchase)) + + // Force a checkpoint + sleep(Duration.ofSeconds(0)) + + // Return value from deserialized object + return difficultToSerialize["foo"] ?: 0 + } } // Flows @@ -68,12 +94,12 @@ class MockNetworkCustomSerializerCheckpointTest{ } @StartableByRPC - class TestFlowCheckingReferencesWork(private val reference: T) : FlowLogic() { + class TestFlowCheckingReferencesWork(private val reference: BrokenMap) : FlowLogic>() { private val referenceField = reference @Suspendable - override fun call(): T { + override fun call(): BrokenMap { val ref = referenceField @@ -92,7 +118,7 @@ class MockNetworkCustomSerializerCheckpointTest{ // Broken Map // This map breaks the rules for the put method. Making the normal map serializer fail. - class BrokenMap : MutableMap{ + open class BrokenMapImpl : MutableMap{ private val map = HashMap() override val size: Int @@ -114,10 +140,31 @@ class MockNetworkCustomSerializerCheckpointTest{ override fun remove(key: K): V? = map.remove(key) } + // A class to test custom serializers applied to implementations + class BrokenMap : BrokenMapImpl() + + // An interface and implementation to test custom serializers applied to interface types + interface BrokenMapInterface : MutableMap + class BrokenMapSerializedByInterfaceImpl : BrokenMapImpl(), BrokenMapInterface + // Custom serializers @Suppress("unused") - class TestSerializer : + class TestInterfaceSerializer : + CheckpointCustomSerializer, HashMap> { + + override fun toProxy(obj: BrokenMapInterface): HashMap { + val proxy = HashMap() + return obj.toMap(proxy) + } + + override fun fromProxy(proxy: HashMap): BrokenMapInterface { + return BrokenMapSerializedByInterfaceImpl().also { it.putAll(proxy) } + } + } + + @Suppress("unused") + class TestClassSerializer : CheckpointCustomSerializer, HashMap> { override fun toProxy(obj: BrokenMap): HashMap {