Skip to content

Commit a8a5399

Browse files
committed
feat: add launcher session mode
1 parent f673c4f commit a8a5399

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ In your gradle:
2929
The JUnit5 extension will automatically launch, setup VNodes and (re)deploy CPIs
3030
to the Combined Worker as needed by default. The config exposes three modes:
3131

32-
- SHARED: Default, described above.
32+
- SHARED: Default, described above. Leaves the worker running on finish.
33+
- PER_LAUNCHER: Will force a fresh Combined worker for the current JUnit LauncherSession. Stops the worker on finish.
3334
- PER_CLASS: Will force a fresh Combined worker for the current Test Class.
3435
- NONE: Completely disables automation for the Combined Worker to enable manual or external management.
3536

@@ -41,15 +42,14 @@ and expose them as a `NodeHandles` parameter to your test methods. Each node has
4142
utility methods like `waitForFlow` that can be used to initiate flows and wait for a final flow status.
4243

4344
```kotlin
44-
import com.fasterxml.jackson.databind.ObjectMapper
4545
import com.github.manosbatsis.corda5.testutils.integration.junit5.CombinedWorkerMode
4646
import com.github.manosbatsis.corda5.testutils.integration.junit5.Corda5NodesConfig
4747
import com.github.manosbatsis.corda5.testutils.integration.junit5.Corda5NodesExtension
4848
import com.github.manosbatsis.corda5.testutils.integration.junit5.nodehandles.NodeHandles
4949
import com.github.manosbatsis.corda5.testutils.rest.client.model.FlowRequest
50-
import net.corda.v5.base.types.MemberX500Name
5150
import org.junit.jupiter.api.Test
5251
import org.junit.jupiter.api.extension.ExtendWith
52+
import kotlin.test.assertEquals
5353
import kotlin.test.assertTrue
5454

5555
// Add the Corda5 nodes extension
@@ -73,24 +73,26 @@ open class DemoApplicationTests {
7373

7474
// The Corda5NodesExtension provides the NodeHandles
7575
@Test
76-
fun recordingFlowTests(nodeHandles: NodeHandles) {
76+
fun workFlowTests(nodeHandles: NodeHandles) {
7777
// Get node handles
7878
val aliceNode = nodeHandles.getByCommonName("Alice")
7979
val bobNode = nodeHandles.getByCommonName("Bob")
8080

81-
// Call flow
82-
val myFlowArgs = MyFlowArgs(aliceNode.memberX500Name, bobNode.memberX500Name)
83-
val createdStatus = aliceNode.waitForFlow(
81+
// Create flow args
82+
val flowArgs = MyFirstFlowStartArgs(bobNode.memberX500Name)
83+
// Call Flow
84+
val response = aliceNode.waitForFlow(
8485
FlowRequest(
85-
flowClass = MyFlow::class.java,
86-
requestBody = myFlowArgs,
87-
// Either String or the type
88-
// (here MyFlowResult) marshaled to string by the flow,
89-
flowResultClass = MyFlowResult::class.java
86+
flowClass = MyFirstFlow::class.java,
87+
requestBody = flowArgs,
88+
flowResultClass = Message::class.java
9089
)
9190
)
92-
// Check flow status
93-
assertTrue(createdStatus.isSuccess())
91+
92+
// Check status and deserialized flow result
93+
assertTrue(response.isSuccess())
94+
val expectedMessage = Message(bobNode.memberX500Name, "Hello Alice, best wishes from Bob")
95+
assertEquals(expectedMessage, response.flowResult)
9496
}
9597
}
9698
```

integration-junit5/src/main/kotlin/com/github/manosbatsis/corda5/testutils/integration/junit5/AbstractCorda5Extension.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ abstract class AbstractCorda5Extension : BeforeAllCallback, AfterAllCallback, Ju
3333
val incrementedCallbackCount = extensionContext.root.getStore(GLOBAL).getOrDefault(allCallbackCounterKey, Int::class.java, 0)
3434
.plus(1)
3535
extensionContext.root.getStore(GLOBAL).put(allCallbackCounterKey, incrementedCallbackCount)
36-
if (incrementedCallbackCount == 1) clearNodeHandles()
36+
if (incrementedCallbackCount == 1 && config.combinedWorkerMode != CombinedWorkerMode.SHARED) clearNodeHandles()
3737
initNodeHandles()
3838
}
3939
}
@@ -45,7 +45,8 @@ abstract class AbstractCorda5Extension : BeforeAllCallback, AfterAllCallback, Ju
4545
val decrementedCallbackCount = extensionContext.root.getStore(GLOBAL).get(allCallbackCounterKey, Int::class.java)
4646
.minus(1)
4747
extensionContext.root.getStore(GLOBAL).put(allCallbackCounterKey, decrementedCallbackCount)
48-
if (decrementedCallbackCount == 0 || config.combinedWorkerMode == CombinedWorkerMode.PER_CLASS)
48+
val launcherFinished = decrementedCallbackCount == 0 && config.combinedWorkerMode == CombinedWorkerMode.PER_LAUNCHER
49+
if (launcherFinished || config.combinedWorkerMode == CombinedWorkerMode.PER_CLASS)
4950
clearNodeHandles()
5051
}
5152
}

integration-junit5/src/main/kotlin/com/github/manosbatsis/corda5/testutils/integration/junit5/Corda5NodesConfig.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ fun gradleRootDir() {
1717

1818
enum class CombinedWorkerMode {
1919
/**
20-
* Default. Will launch, setup VNodes and (re)deploy to the Combined Worker
21-
* as needed only once and reuse it for all tests.
20+
* Default. Will start, setup VNodes and (re)deploy to the Combined Worker
21+
* as needed only once for all tests. Leaves the worker running.
2222
*/
2323
SHARED,
2424

2525
/**
26-
* Will launch, setup VNodes and (re)deploy to the Combined Worker
26+
* Will re-launch, setup VNodes and (re)deploy to the Combined Worker
2727
* as needed for each individual test class.
2828
*/
2929
PER_CLASS,
3030

31+
/**
32+
* Will re-launch, setup VNodes and (re)deploy to the Combined Worker
33+
* as needed for every JUnit LauncherSession. Stops the worker on finish.
34+
*/
35+
PER_LAUNCHER,
36+
3137
/**
3238
* Completely disables automation for the Combined Worker to enable manual or external management.
3339
*/

integration-junit5/src/main/kotlin/com/github/manosbatsis/corda5/testutils/integration/junit5/nodehandles/NodeHandlesHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class NodeHandlesHelper(
3737
when (config.combinedWorkerMode) {
3838
CombinedWorkerMode.PER_CLASS ->
3939
reset().also { nodeHandlesCache = buildNodeHandles() }
40-
40+
CombinedWorkerMode.PER_LAUNCHER ->
41+
if (nodeHandlesCache == null) nodeHandlesCache = buildNodeHandles()
4142
CombinedWorkerMode.SHARED ->
4243
if (nodeHandlesCache == null) nodeHandlesCache = buildNodeHandles()
43-
4444
CombinedWorkerMode.NONE ->
4545
nodeHandlesCache = nodeHandles(nodesClient.nodes().virtualNodes)
4646
}

0 commit comments

Comments
 (0)