Made NodeUnloadHandlerTests a unit test by using mock node (#3572)

This commit is contained in:
Shams Asari 2018-07-12 15:22:31 +01:00 committed by GitHub
parent 7f42a9b48c
commit ab08ce21f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,12 @@
package net.corda.node package net.corda.node.internal
import net.corda.core.internal.packageName
import net.corda.core.node.ServiceHub import net.corda.core.node.ServiceHub
import net.corda.core.node.services.CordaService import net.corda.core.node.services.CordaService
import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.utilities.contextLogger import net.corda.core.utilities.contextLogger
import net.corda.core.utilities.getOrThrow import net.corda.testing.node.internal.InternalMockNetwork
import net.corda.testing.core.DUMMY_BANK_A_NAME import org.junit.After
import net.corda.testing.driver.DriverParameters
import net.corda.testing.driver.driver
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
@ -15,16 +14,23 @@ import java.util.concurrent.TimeUnit
class NodeUnloadHandlerTests { class NodeUnloadHandlerTests {
companion object { companion object {
val latch = CountDownLatch(1) val registerLatch = CountDownLatch(1)
val shutdownLatch = CountDownLatch(1)
}
private val mockNet = InternalMockNetwork(cordappPackages = listOf(javaClass.packageName), notarySpecs = emptyList())
@After
fun cleanUp() {
mockNet.stopNodes()
} }
@Test @Test
fun `should be able to register run on stop lambda`() { fun `should be able to register run on stop lambda`() {
driver(DriverParameters(startNodesInProcess = true, extraCordappPackagesToScan = listOf("net.corda.node"), notarySpecs = emptyList())) { val node = mockNet.createNode()
startNode(providedName = DUMMY_BANK_A_NAME).getOrThrow() registerLatch.await() // Make sure the handler is registered on node start up
// just want to fall off the end of this for the mo... node.dispose()
} assertTrue("Timed out waiting for AbstractNode to invoke the test service shutdown callback", shutdownLatch.await(30, TimeUnit.SECONDS))
assertTrue("Timed out waiting for AbstractNode to invoke the test service shutdown callback", latch.await(30, TimeUnit.SECONDS))
} }
@Suppress("unused") @Suppress("unused")
@ -36,11 +42,12 @@ class NodeUnloadHandlerTests {
init { init {
serviceHub.registerUnloadHandler(this::shutdown) serviceHub.registerUnloadHandler(this::shutdown)
registerLatch.countDown()
} }
private fun shutdown() { private fun shutdown() {
log.info("shutting down") log.info("shutting down")
latch.countDown() shutdownLatch.countDown()
} }
} }
} }