InMemoryNetwork

class InMemoryNetwork(scope: CoroutineScope? = null, random: Random = Random(0))(source)

A simulated datagram network for tests: every InMemoryTransport attached to it gets a UDP-style address and exchanges datagrams with the others in memory.

By default delivery is perfect and immediate. conditions adds loss, duplication, latency and jitter (and so reordering); filter drops chosen datagrams deterministically, for tests such as "the first ACK is lost". tap sees every datagram and what happened to it.

Latency needs scope to run the delayed deliveries; under runTest pass backgroundScope, and the delays run on virtual time. Give the CoAP endpoints on the network testScheduler.timeSource as their CoapEndpointConfig.timeSource too: their timers wait on the dispatcher's clock and must read the same one. Randomness comes from random, so a seed reproduces a run.

val network = InMemoryNetwork(backgroundScope, Random(42))
network.conditions = LinkConditions(lossRate = 0.2, duplicationRate = 0.1)
val server = network.attach()
val client = network.attach()

Datagrams to an address nobody is attached to are dropped, as UDP does (no ICMP is simulated).

Constructors

Link copied to clipboard
constructor(scope: CoroutineScope? = null, random: Random = Random(0))

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Loss, duplication and delay applied to every datagram sent from now on.

Link copied to clipboard

Returns false to drop a datagram; applied before conditions.

Link copied to clipboard

Sees every datagram once, with its DatagramFate. Called on the sending coroutine.

Functions

Link copied to clipboard
suspend fun attach(address: Endpoint.Udp? = null, maxDatagramSize: Int = DEFAULT_MAX_DATAGRAM_SIZE): InMemoryTransport

Attaches a transport at address, by default the next free port on 127.0.0.1.

Link copied to clipboard
suspend fun spoof(from: Endpoint.Udp, to: Endpoint, bytes: ByteArray)

Sends bytes to to as if from from, whether or not a transport is attached there: a datagram with a forged source address, which a UDP receiver cannot tell from a real one (RFC 7252 §11.4). What the receiver sends back to an address nobody is attached to is dropped as DatagramFate.UNREACHABLE, as a spoofed source never answers. filter, conditions and tap apply as to every datagram. Flood tests use it to send from any number of addresses without a transport for each.