CORDA-716 Make asContextEnv available to smoketesting ()

This commit is contained in:
Andrzej Cichocki 2017-11-13 18:28:54 +00:00 committed by GitHub
parent c583af8f4b
commit 687a992262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 16 deletions
client/rpc/src/main/kotlin/net/corda/client/rpc/internal
testing
smoke-test-utils/src/main/kotlin/net/corda/smoketesting
test-common/src/main/kotlin/net/corda/testing/common/internal
test-utils/src/main/kotlin/net/corda/testing

View File

@ -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())

View File

@ -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 {

View File

@ -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 <T> 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)
}
}

View File

@ -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 <T> withTestSerialization(inheritable: Boolean = false, callable: (Serializa
return createTestSerializationEnv().asContextEnv(inheritable, callable)
}
private fun <T> 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.