From 687a992262a84a53d4ff56dd3eaebc44dda9c442 Mon Sep 17 00:00:00 2001 From: Andrzej Cichocki Date: Mon, 13 Nov 2017 18:28:54 +0000 Subject: [PATCH] CORDA-716 Make asContextEnv available to smoketesting (#2039) --- .../rpc/internal/KryoClientSerializationScheme.kt | 7 ++++++- .../kotlin/net/corda/smoketesting/NodeProcess.kt | 11 ++++++----- .../internal/CommonSerializationTestHelpers.kt | 15 +++++++++++++++ .../net/corda/testing/SerializationTestHelpers.kt | 11 +---------- 4 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 testing/test-common/src/main/kotlin/net/corda/testing/common/internal/CommonSerializationTestHelpers.kt diff --git a/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/KryoClientSerializationScheme.kt b/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/KryoClientSerializationScheme.kt index 0e830b3a99..eb9f22e937 100644 --- a/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/KryoClientSerializationScheme.kt +++ b/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/KryoClientSerializationScheme.kt @@ -2,6 +2,7 @@ package net.corda.client.rpc.internal import com.esotericsoftware.kryo.pool.KryoPool import net.corda.core.serialization.SerializationContext +import net.corda.core.serialization.internal.SerializationEnvironment import net.corda.core.serialization.internal.SerializationEnvironmentImpl import net.corda.core.serialization.internal.nodeSerializationEnv import net.corda.core.utilities.ByteSequence @@ -33,7 +34,11 @@ class KryoClientSerializationScheme : AbstractKryoSerializationScheme() { companion object { /** Call from main only. */ fun initialiseSerialization() { - nodeSerializationEnv = SerializationEnvironmentImpl( + nodeSerializationEnv = createSerializationEnv() + } + + fun createSerializationEnv(): SerializationEnvironment { + return SerializationEnvironmentImpl( SerializationFactoryImpl().apply { registerScheme(KryoClientSerializationScheme()) registerScheme(AMQPClientSerializationScheme()) diff --git a/testing/smoke-test-utils/src/main/kotlin/net/corda/smoketesting/NodeProcess.kt b/testing/smoke-test-utils/src/main/kotlin/net/corda/smoketesting/NodeProcess.kt index f2a9196eac..2634e295e0 100644 --- a/testing/smoke-test-utils/src/main/kotlin/net/corda/smoketesting/NodeProcess.kt +++ b/testing/smoke-test-utils/src/main/kotlin/net/corda/smoketesting/NodeProcess.kt @@ -10,6 +10,7 @@ import net.corda.core.utilities.NetworkHostAndPort import net.corda.core.utilities.loggerFor import net.corda.testing.common.internal.NetworkParametersCopier import net.corda.testing.common.internal.testNetworkParameters +import net.corda.testing.common.internal.asContextEnv import java.nio.file.Path import java.nio.file.Paths import java.time.Instant @@ -53,11 +54,11 @@ class NodeProcess( val javaPath: Path = Paths.get(System.getProperty("java.home"), "bin", "java") val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(systemDefault()) val defaultNetworkParameters = run { - // TODO withTestSerialization is in test-utils, which we don't have access to - KryoClientSerializationScheme.initialiseSerialization() - // There are no notaries in the network parameters for smoke test nodes. If this is required then we would - // need to introduce the concept of a "network" which predefines the notaries, like the driver and MockNetwork - NetworkParametersCopier(testNetworkParameters(emptyList())) + KryoClientSerializationScheme.createSerializationEnv().asContextEnv { + // There are no notaries in the network parameters for smoke test nodes. If this is required then we would + // need to introduce the concept of a "network" which predefines the notaries, like the driver and MockNetwork + NetworkParametersCopier(testNetworkParameters(emptyList())) + } } init { diff --git a/testing/test-common/src/main/kotlin/net/corda/testing/common/internal/CommonSerializationTestHelpers.kt b/testing/test-common/src/main/kotlin/net/corda/testing/common/internal/CommonSerializationTestHelpers.kt new file mode 100644 index 0000000000..4d26575e3a --- /dev/null +++ b/testing/test-common/src/main/kotlin/net/corda/testing/common/internal/CommonSerializationTestHelpers.kt @@ -0,0 +1,15 @@ +package net.corda.testing.common.internal + +import net.corda.core.serialization.internal.SerializationEnvironment +import net.corda.core.serialization.internal._contextSerializationEnv +import net.corda.core.serialization.internal._inheritableContextSerializationEnv + +fun SerializationEnvironment.asContextEnv(inheritable: Boolean = false, callable: (SerializationEnvironment) -> T): T { + val property = if (inheritable) _inheritableContextSerializationEnv else _contextSerializationEnv + property.set(this) + try { + return callable(this) + } finally { + property.set(null) + } +} diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt index 57139d6250..4920b9d940 100644 --- a/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt +++ b/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt @@ -8,6 +8,7 @@ import net.corda.node.serialization.KryoServerSerializationScheme import net.corda.nodeapi.internal.serialization.* import net.corda.nodeapi.internal.serialization.amqp.AMQPClientSerializationScheme import net.corda.nodeapi.internal.serialization.amqp.AMQPServerSerializationScheme +import net.corda.testing.common.internal.asContextEnv import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement @@ -32,16 +33,6 @@ fun withTestSerialization(inheritable: Boolean = false, callable: (Serializa return createTestSerializationEnv().asContextEnv(inheritable, callable) } -private fun SerializationEnvironment.asContextEnv(inheritable: Boolean, callable: (SerializationEnvironment) -> T): T { - val property = if (inheritable) _inheritableContextSerializationEnv else _contextSerializationEnv - property.set(this) - try { - return callable(this) - } finally { - property.set(null) - } -} - /** * For example your test class uses [SerializationEnvironmentRule] but you want to turn it off for one method. * Use sparingly, ideally a test class shouldn't mix serialization init mechanisms.