Fix race in IntegrationTestingTutorial. (#594)

This commit is contained in:
Andrzej Cichocki 2017-04-27 09:15:12 +01:00 committed by GitHub
parent 3fcb773a31
commit b3894fa38a
4 changed files with 11 additions and 22 deletions

View File

@ -275,11 +275,7 @@ class TransientProperty<out T>(private val initializer: () -> T) {
@Transient private var v: T? = null
@Synchronized
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
if (v == null)
v = initializer()
return v!!
}
operator fun getValue(thisRef: Any?, property: KProperty<*>) = v ?: initializer().also { v = it }
}
/**

View File

@ -65,7 +65,7 @@ object LogHelper {
/**
* May fail to restore the original level due to unavoidable race if called by multiple threads.
*/
inline fun <T> withLevel(logName: String, levelName: String, block: () -> T) = let {
inline fun <T> withLevel(logName: String, levelName: String, block: () -> T) = run {
val level = Level.valueOf(levelName)
val oldLevel = LogManager.getLogger(logName).level
Configurator.setLevel(logName, level)

View File

@ -60,7 +60,7 @@ class IntegrationTestingTutorial {
// START 4
val issueRef = OpaqueBytes.of(0)
val futures = Stack<ListenableFuture<*>>()
for (i in 1 .. 10) {
(1..10).map { i ->
thread {
futures.push(aliceProxy.startFlow(::CashIssueFlow,
i.DOLLARS,
@ -69,14 +69,12 @@ class IntegrationTestingTutorial {
notary.nodeInfo.notaryIdentity
).returnValue)
}
}
while (!futures.empty()) {
futures.pop().getOrThrow()
}
}.forEach(Thread::join) // Ensure the stack of futures is populated.
futures.forEach { it.getOrThrow() }
bobVaultUpdates.expectEvents {
parallel(
(1 .. 10).map { i ->
(1..10).map { i ->
expect(
match = { update: Vault.Update ->
(update.produced.first().state.data as Cash.State).amount.quantity == i * 100L
@ -90,13 +88,13 @@ class IntegrationTestingTutorial {
// END 4
// START 5
for (i in 1 .. 10) {
for (i in 1..10) {
bobProxy.startFlow(::CashPaymentFlow, i.DOLLARS, alice.nodeInfo.legalIdentity).returnValue.getOrThrow()
}
aliceVaultUpdates.expectEvents {
sequence(
(1 .. 10).map { i ->
(1..10).map { i ->
expect { update: Vault.Update ->
println("Alice got vault update of $update")
assertEquals((update.produced.first().state.data as Cash.State).amount.quantity, i * 100L)

View File

@ -87,13 +87,8 @@ class FlowStateMachineImpl<R>(override val id: StateMachineRunId,
@Transient private var _resultFuture: SettableFuture<R>? = SettableFuture.create<R>()
/** This future will complete when the call method returns. */
override val resultFuture: ListenableFuture<R> get() {
return _resultFuture ?: run {
val f = SettableFuture.create<R>()
_resultFuture = f
return f
}
}
override val resultFuture: ListenableFuture<R>
get() = _resultFuture ?: SettableFuture.create<R>().also { _resultFuture = it }
// This state IS serialised, as we need it to know what the fiber is waiting for.
internal val openSessions = HashMap<Pair<FlowLogic<*>, Party>, FlowSession>()
@ -456,7 +451,7 @@ private data class FlowHandleImpl<A>(
}
@CordaSerializable
private data class FlowProgressHandleImpl<A> (
private data class FlowProgressHandleImpl<A>(
override val id: StateMachineRunId,
override val returnValue: ListenableFuture<A>,
override val progress: Observable<String>) : FlowProgressHandle<A> {