Provide an API to register callback on app shutdown (#2402)

Provide an API to register callback on app shutdown.
This commit is contained in:
Ben Wyeth
2018-01-24 15:19:24 +00:00
committed by Mike Hearn
parent 3c0e006456
commit d17670c747
7 changed files with 78 additions and 12 deletions

View File

@ -31,13 +31,6 @@ import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.streams.toList
private fun checkQuasarAgent() {
if (!(ManagementFactory.getRuntimeMXBean().inputArguments.any { it.contains("quasar") })) {
throw IllegalStateException("No quasar agent")
}
}
@Ignore("Run these locally")
class NodePerformanceTests {
@StartableByRPC
@ -52,11 +45,6 @@ class NodePerformanceTests {
val averageMs: Double
)
@Before
fun before() {
checkQuasarAgent()
}
@Test
fun `empty flow per second`() {
driver(startNodesInProcess = true) {

View File

@ -0,0 +1,48 @@
package net.corda.node
import net.corda.core.node.ServiceHub
import net.corda.core.node.services.CordaService
import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.utilities.contextLogger
import net.corda.core.utilities.getOrThrow
import net.corda.testing.DUMMY_BANK_A_NAME
import net.corda.testing.driver.driver
import org.junit.Assert
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
class NodeUnloadHandlerTests {
companion object {
val latch = CountDownLatch(1)
}
@Test
fun `should be able to register run on stop lambda`() {
driver(startNodesInProcess = true, extraCordappPackagesToScan = listOf("net.corda.node"), isDebug = true) {
startNode(providedName = DUMMY_BANK_A_NAME).getOrThrow()
// just want to fall off the end of this for the mo...
}
Assert.assertTrue("Timed out waiting for AbstractNode to invoke the test service shutdown callback",latch.await(30, TimeUnit.SECONDS))
}
@CordaService
class RunOnStopTestService(serviceHub: ServiceHub) : SingletonSerializeAsToken() {
companion object {
private val log = contextLogger()
}
init {
serviceHub.registerUnloadHandler(this::shutdown)
}
fun shutdown() {
log.info("shutting down")
latch.countDown()
}
}
}

View File

@ -804,6 +804,11 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
}
override fun jdbcSession(): Connection = database.createSession()
// allows services to register handlers to be informed when the node stop method is called
override fun registerUnloadHandler(handler: () -> Unit) {
runOnStop += handler
}
}
}