mirror of
https://github.com/corda/corda.git
synced 2024-12-24 23:26:48 +00:00
CORDA-716 Make asContextEnv available to smoketesting (#2039)
This commit is contained in:
parent
c583af8f4b
commit
687a992262
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
@ -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())
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user