diff --git a/src/main/kotlin/core/messaging/InMemoryNetwork.kt b/src/main/kotlin/core/messaging/InMemoryNetwork.kt index bf7795b6d4..377f9dc4c0 100644 --- a/src/main/kotlin/core/messaging/InMemoryNetwork.kt +++ b/src/main/kotlin/core/messaging/InMemoryNetwork.kt @@ -25,8 +25,8 @@ import javax.annotation.concurrent.ThreadSafe import kotlin.concurrent.thread /** - * An in-memory network allows you to manufacture [Node]s for a set of participants. Each - * [Node] maintains a queue of messages it has received, and a background thread that dispatches + * An in-memory network allows you to manufacture [InMemoryNode]s for a set of participants. Each + * [InMemoryNode] maintains a queue of messages it has received, and a background thread that dispatches * messages one by one to registered handlers. Alternatively, a messaging system may be manually pumped, in which * case no thread is created and a caller is expected to force delivery one at a time (this is useful for unit * testing). @@ -34,26 +34,26 @@ import kotlin.concurrent.thread @ThreadSafe public class InMemoryNetwork { private var counter = 0 // -1 means stopped. - private val handleNodeMap = HashMap() + private val handleNodeMap = HashMap() // All messages are kept here until the messages are pumped off the queue by a caller to the node class. // Queues are created on-demand when a message is sent to an address: the receiving node doesn't have to have // been created yet. If the node identified by the given handle has gone away/been shut down then messages // stack up here waiting for it to come back. The intent of this is to simulate a reliable messaging network. private val messageQueues = HashMap>() - val nodes: List @Synchronized get() = handleNodeMap.values.toList() + val nodes: List @Synchronized get() = handleNodeMap.values.toList() /** * Creates a node and returns the new object that identifies its location on the network to senders, and the - * [Node] that the recipient/in-memory node uses to receive messages and send messages itself. + * [InMemoryNode] that the recipient/in-memory node uses to receive messages and send messages itself. * - * If [manuallyPumped] is set to true, then you are expected to call the [Node.pump] method on the [Node] + * If [manuallyPumped] is set to true, then you are expected to call the [InMemoryNode.pump] method on the [InMemoryNode] * in order to cause the delivery of a single message, which will occur on the thread of the caller. If set to false * then this class will set up a background thread to deliver messages asynchronously, if the handler specifies no * executor. */ @Synchronized - fun createNode(manuallyPumped: Boolean): Pair> { + fun createNode(manuallyPumped: Boolean): Pair> { check(counter >= 0) { "In memory network stopped: please recreate. "} val builder = createNodeWithID(manuallyPumped, counter) as Builder counter++ @@ -62,7 +62,7 @@ public class InMemoryNetwork { } /** Creates a node at the given address: useful if you want to recreate a node to simulate a restart */ - fun createNodeWithID(manuallyPumped: Boolean, id: Int): MessagingServiceBuilder { + fun createNodeWithID(manuallyPumped: Boolean, id: Int): MessagingServiceBuilder { return Builder(manuallyPumped, Handle(id)) } @@ -103,10 +103,10 @@ public class InMemoryNetwork { messageQueues.clear() } - inner class Builder(val manuallyPumped: Boolean, val id: Handle) : MessagingServiceBuilder { - override fun start(): ListenableFuture { + inner class Builder(val manuallyPumped: Boolean, val id: Handle) : MessagingServiceBuilder { + override fun start(): ListenableFuture { synchronized(this@InMemoryNetwork) { - val node = Node(manuallyPumped, id) + val node = InMemoryNode(manuallyPumped, id) handleNodeMap[id] = node return Futures.immediateFuture(node) } @@ -122,7 +122,7 @@ public class InMemoryNetwork { private var timestampingAdvert: LegallyIdentifiableNode? = null @Synchronized - fun setupTimestampingNode(manuallyPumped: Boolean): Pair { + fun setupTimestampingNode(manuallyPumped: Boolean): Pair { check(timestampingAdvert == null) val (handle, builder) = createNode(manuallyPumped) val node = builder.start().get() @@ -134,13 +134,13 @@ public class InMemoryNetwork { } /** - * An [Node] provides a [MessagingService] that isn't backed by any kind of network or disk storage + * An [InMemoryNode] provides a [MessagingService] that isn't backed by any kind of network or disk storage * system, but just uses regular queues on the heap instead. It is intended for unit testing and developer convenience * when all entities on 'the network' are being simulated in-process. * * An instance can be obtained by creating a builder and then using the start method. */ - inner class Node(private val manuallyPumped: Boolean, private val handle: Handle): MessagingService { + inner class InMemoryNode(private val manuallyPumped: Boolean, private val handle: Handle): MessagingService { inner class Handler(val executor: Executor?, val topic: String, val callback: (Message, MessageHandlerRegistration) -> Unit) : MessageHandlerRegistration @GuardedBy("this") diff --git a/src/test/kotlin/core/messaging/InMemoryMessagingTests.kt b/src/test/kotlin/core/messaging/InMemoryMessagingTests.kt index 5d4b523095..e6cdc5ae43 100644 --- a/src/test/kotlin/core/messaging/InMemoryMessagingTests.kt +++ b/src/test/kotlin/core/messaging/InMemoryMessagingTests.kt @@ -21,10 +21,10 @@ import kotlin.test.assertFalse import kotlin.test.assertTrue open class TestWithInMemoryNetwork { - val nodes: MutableMap = HashMap() + val nodes: MutableMap = HashMap() lateinit var network: InMemoryNetwork - fun makeNode(inBackground: Boolean = false): Pair { + fun makeNode(inBackground: Boolean = false): Pair { // The manuallyPumped = true bit means that we must call the pump method on the system in order to val (address, builder) = network.createNode(!inBackground) val node = builder.start().get() diff --git a/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt b/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt index 375e0db3d1..42a08dbae8 100644 --- a/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt +++ b/src/test/kotlin/core/node/TimestamperNodeServiceTest.kt @@ -27,8 +27,8 @@ import kotlin.test.assertFailsWith import kotlin.test.assertTrue class TimestamperNodeServiceTest : TestWithInMemoryNetwork() { - lateinit var myNode: Pair - lateinit var serviceNode: Pair + lateinit var myNode: Pair + lateinit var serviceNode: Pair lateinit var service: TimestamperNodeService val ptx = TransactionBuilder().apply {