core.testing / InMemoryMessagingNetwork / InMemoryMessaging

# InMemoryMessaging

`inner class InMemoryMessaging : MessagingService`

An InMemoryMessaging provides a MessagingService that isnt 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.

### Types

Handler | `inner class Handler : MessageHandlerRegistration`
InnerState | `inner class InnerState`

### Constructors

<init> | `InMemoryMessaging(manuallyPumped: Boolean, handle: Handle)` An InMemoryMessaging provides a MessagingService that isnt 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.

### Properties

backgroundThread | `val backgroundThread: Nothing?`
myAddress | `val myAddress: SingleMessageRecipient` Returns an address that refers to this node.
running | `var running: Boolean`
state | `val state: ThreadBox<InnerState>`

### Functions

addMessageHandler | `fun addMessageHandler(topic: String, executor: Executor?, callback: (Message, MessageHandlerRegistration) -> Unit): MessageHandlerRegistration` The provided function will be invoked for each received message whose topic matches the given string, on the given executor. The topic can be the empty string to match all messages.
createMessage | `fun createMessage(topic: String, data: ByteArray): Message` Returns the given (topic, data) pair as a newly created message object.
pump | `fun pump(block: Boolean): Boolean` Delivers a single message from the internal queue. If there are no messages waiting to be delivered and block is true, waits until one has been provided on a different thread via send. If block is false, the return result indicates whether a message was delivered or not.
removeMessageHandler | `fun removeMessageHandler(registration: MessageHandlerRegistration): Unit` Removes a handler given the object returned from addMessageHandler. The callback will no longer be invoked once this method has returned, although executions that are currently in flight will not be interrupted.

send

`fun send(message: Message, target: MessageRecipients): Unit`

Sends a message to the given receiver. The details of how receivers are identified is up to the messaging implementation: the type system provides an opaque high level view, with more fine grained control being available via type casting. Once this function returns the message is queued for delivery but not necessarily delivered: if the recipients are offline then the message could be queued hours or days later.

stop | `fun stop(): Unit`