summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-network/src/test
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-10-25 14:53:54 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-10-25 14:53:54 +0200
commitaa9b32f8cd1467e9718959f400f6777e5d71737d (patch)
treeb88bbede15108c6855d7f94ded4c7054df186a72 /opendc-simulator/opendc-simulator-network/src/test
parenteb0e0a3bc557c05a70eead388797ab850ea87366 (diff)
parentb7a71e5b4aa77b41ef41deec2ace42b67a5a13a7 (diff)
merge: Integrate v2.1 progress into public repository
This pull request integrates the changes planned for the v2.1 release of OpenDC into the public Github repository in order to sync the progress of both repositories.
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
-rw-r--r--opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt77
3 files changed, 303 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..14d22162
--- /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.flow.*
+import org.opendc.simulator.flow.source.FixedFlowSource
+
+/**
+ * Test suite for the [SimNetworkSink] class.
+ */
+class SimNetworkSinkTest {
+ @Test
+ fun testInitialState() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+
+ assertFalse(sink.isConnected)
+ assertNull(sink.link)
+ assertEquals(100.0, sink.capacity)
+ }
+
+ @Test
+ fun testDisconnectIdempotent() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+
+ assertDoesNotThrow { sink.disconnect() }
+ assertFalse(sink.isConnected)
+ }
+
+ @Test
+ fun testConnectCircular() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+
+ assertThrows<IllegalArgumentException> {
+ sink.connect(sink)
+ }
+ }
+
+ @Test
+ fun testConnectAlreadyConnectedTarget() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+ val source = mockk<SimNetworkPort>(relaxUnitFun = true)
+ every { source.isConnected } returns true
+
+ assertThrows<IllegalStateException> {
+ sink.connect(source)
+ }
+ }
+
+ @Test
+ fun testConnectAlreadyConnected() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+ val source1 = Source(engine)
+
+ val source2 = mockk<SimNetworkPort>(relaxUnitFun = true)
+
+ every { source2.isConnected } returns false
+
+ sink.connect(source1)
+ assertThrows<IllegalStateException> {
+ sink.connect(source2)
+ }
+ }
+
+ @Test
+ fun testConnect() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+ val source = spyk(Source(engine))
+ val consumer = source.consumer
+
+ sink.connect(source)
+
+ assertTrue(sink.isConnected)
+ assertTrue(source.isConnected)
+
+ verify { source.createConsumer() }
+ verify { consumer.onStart(any(), any()) }
+ }
+
+ @Test
+ fun testDisconnect() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+ val source = spyk(Source(engine))
+ val consumer = source.consumer
+
+ sink.connect(source)
+ sink.disconnect()
+
+ assertFalse(sink.isConnected)
+ assertFalse(source.isConnected)
+
+ verify { consumer.onStop(any(), any(), any()) }
+ }
+
+ private class Source(engine: FlowEngine) : SimNetworkPort() {
+ val consumer = spyk(FixedFlowSource(Double.POSITIVE_INFINITY, utilization = 0.8))
+
+ public override fun createConsumer(): FlowSource = consumer
+
+ override val provider: FlowConsumer = FlowSink(engine, 0.0)
+ }
+}
diff --git a/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt
new file mode 100644
index 00000000..62e54ffb
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt
@@ -0,0 +1,77 @@
+/*
+ * 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.spyk
+import io.mockk.verify
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+import org.opendc.simulator.core.runBlockingSimulation
+import org.opendc.simulator.flow.*
+import org.opendc.simulator.flow.source.FixedFlowSource
+
+/**
+ * Test suite for the [SimNetworkSwitchVirtual] class.
+ */
+class SimNetworkSwitchVirtualTest {
+ @Test
+ fun testConnect() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+ val source = spyk(Source(engine))
+ val switch = SimNetworkSwitchVirtual(engine)
+ val consumer = source.consumer
+
+ switch.newPort().connect(sink)
+ switch.newPort().connect(source)
+
+ assertTrue(sink.isConnected)
+ assertTrue(source.isConnected)
+
+ verify { source.createConsumer() }
+ verify { consumer.onStart(any(), any()) }
+ }
+
+ @Test
+ fun testConnectClosedPort() = runBlockingSimulation {
+ val engine = FlowEngine(coroutineContext, clock)
+ val sink = SimNetworkSink(engine, capacity = 100.0)
+ val switch = SimNetworkSwitchVirtual(engine)
+
+ val port = switch.newPort()
+ port.close()
+
+ assertThrows<IllegalStateException> {
+ port.connect(sink)
+ }
+ }
+
+ private class Source(engine: FlowEngine) : SimNetworkPort() {
+ val consumer = spyk(FixedFlowSource(Double.POSITIVE_INFINITY, utilization = 0.8))
+
+ public override fun createConsumer(): FlowSource = consumer
+
+ override val provider: FlowConsumer = FlowSink(engine, 0.0)
+ }
+}