summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-network/src/test
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-06-19 12:58:07 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-06-21 15:16:27 +0200
commitf28cc9964ad1ca1c074331ed54053d469e7373e5 (patch)
treea099bf8125bc35e3c1067521a8362531c9b0f88a /opendc-simulator/opendc-simulator-network/src/test
parentba32561e4b0e00c00d528df615a58e396e0fddc0 (diff)
simulator: Add core module for network simulation
This change adds a basic framework as basis for network simulation support in OpenDC. It is modelled similarly to the power system that has been added recently.
Diffstat (limited to 'opendc-simulator/opendc-simulator-network/src/test')
-rw-r--r--opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkLinkTest.kt89
-rw-r--r--opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSinkTest.kt137
2 files changed, 226 insertions, 0 deletions
diff --git a/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkLinkTest.kt b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkLinkTest.kt
new file mode 100644
index 00000000..3480c9df
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkLinkTest.kt
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2021 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.simulator.network
+
+import io.mockk.mockk
+import org.junit.jupiter.api.Assertions.*
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+
+/**
+ * Test suite for [SimNetworkLink] class.
+ */
+class SimNetworkLinkTest {
+ @Test
+ fun testContainsLeft() {
+ val left = mockk<SimNetworkPort>()
+ val right = mockk<SimNetworkPort>()
+
+ val link = SimNetworkLink(left, right)
+ assertTrue(left in link)
+ }
+
+ @Test
+ fun testContainsRight() {
+ val left = mockk<SimNetworkPort>()
+ val right = mockk<SimNetworkPort>()
+
+ val link = SimNetworkLink(left, right)
+ assertTrue(right in link)
+ }
+
+ @Test
+ fun testContainsNone() {
+ val left = mockk<SimNetworkPort>()
+ val right = mockk<SimNetworkPort>()
+ val none = mockk<SimNetworkPort>()
+
+ val link = SimNetworkLink(left, right)
+ assertFalse(none in link)
+ }
+
+ @Test
+ fun testOppositeLeft() {
+ val left = mockk<SimNetworkPort>()
+ val right = mockk<SimNetworkPort>()
+
+ val link = SimNetworkLink(left, right)
+ assertEquals(right, link.opposite(left))
+ }
+
+ @Test
+ fun testOppositeRight() {
+ val left = mockk<SimNetworkPort>()
+ val right = mockk<SimNetworkPort>()
+
+ val link = SimNetworkLink(left, right)
+ assertEquals(left, link.opposite(right))
+ }
+
+ @Test
+ fun testOppositeNone() {
+ val left = mockk<SimNetworkPort>()
+ val right = mockk<SimNetworkPort>()
+ val none = mockk<SimNetworkPort>()
+
+ val link = SimNetworkLink(left, right)
+ assertThrows<IllegalArgumentException> { link.opposite(none) }
+ }
+}
diff --git a/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSinkTest.kt b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSinkTest.kt
new file mode 100644
index 00000000..b8c4b00d
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSinkTest.kt
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2021 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.simulator.network
+
+import io.mockk.every
+import io.mockk.mockk
+import io.mockk.spyk
+import io.mockk.verify
+import org.junit.jupiter.api.Assertions.*
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertDoesNotThrow
+import org.junit.jupiter.api.assertThrows
+import org.opendc.simulator.core.runBlockingSimulation
+import org.opendc.simulator.resources.*
+import org.opendc.simulator.resources.consumer.SimWorkConsumer
+
+/**
+ * Test suite for the [SimNetworkSink] class.
+ */
+class SimNetworkSinkTest {
+ @Test
+ fun testInitialState() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+
+ assertFalse(sink.isConnected)
+ assertNull(sink.link)
+ assertEquals(100.0, sink.capacity)
+ }
+
+ @Test
+ fun testDisconnectIdempotent() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+
+ assertDoesNotThrow { sink.disconnect() }
+ assertFalse(sink.isConnected)
+ }
+
+ @Test
+ fun testConnectCircular() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+
+ assertThrows<IllegalArgumentException> {
+ sink.connect(sink)
+ }
+ }
+
+ @Test
+ fun testConnectAlreadyConnectedTarget() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+ val source = mockk<SimNetworkPort>(relaxUnitFun = true)
+ every { source.isConnected } returns true
+
+ assertThrows<IllegalStateException> {
+ sink.connect(source)
+ }
+ }
+
+ @Test
+ fun testConnectAlreadyConnected() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+ val source1 = Source(interpreter)
+
+ val source2 = mockk<SimNetworkPort>(relaxUnitFun = true)
+
+ every { source2.isConnected } returns false
+
+ sink.connect(source1)
+ assertThrows<IllegalStateException> {
+ sink.connect(source2)
+ }
+ }
+
+ @Test
+ fun testConnect() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+ val source = spyk(Source(interpreter))
+ val consumer = source.consumer
+
+ sink.connect(source)
+
+ assertTrue(sink.isConnected)
+ assertTrue(source.isConnected)
+
+ verify { source.createConsumer() }
+ verify { consumer.onEvent(any(), SimResourceEvent.Start) }
+ }
+
+ @Test
+ fun testDisconnect() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+ val source = spyk(Source(interpreter))
+ val consumer = source.consumer
+
+ sink.connect(source)
+ sink.disconnect()
+
+ assertFalse(sink.isConnected)
+ assertFalse(source.isConnected)
+
+ verify { consumer.onEvent(any(), SimResourceEvent.Exit) }
+ }
+
+ private class Source(interpreter: SimResourceInterpreter) : SimNetworkPort() {
+ val consumer = spyk(SimWorkConsumer(Double.POSITIVE_INFINITY, utilization = 0.8))
+
+ public override fun createConsumer(): SimResourceConsumer = consumer
+
+ override val provider: SimResourceProvider = SimResourceSource(0.0, interpreter)
+ }
+}