summaryrefslogtreecommitdiff
path: root/opendc-common
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-common')
-rw-r--r--opendc-common/build.gradle.kts2
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/DispatcherCoroutineDispatcher.kt23
-rw-r--r--opendc-common/src/test/kotlin/org/opendc/common/DispatcherCoroutineDispatcherTest.kt44
-rw-r--r--opendc-common/src/test/kotlin/org/opendc/common/util/PacerTest.kt35
4 files changed, 62 insertions, 42 deletions
diff --git a/opendc-common/build.gradle.kts b/opendc-common/build.gradle.kts
index f1c22f61..e0524f3c 100644
--- a/opendc-common/build.gradle.kts
+++ b/opendc-common/build.gradle.kts
@@ -23,7 +23,7 @@
group = "org.opendc"
description = "Common functionality used across OpenDC modules"
-/* Build configuration */
+// Build configuration
plugins {
`kotlin-library-conventions`
}
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/DispatcherCoroutineDispatcher.kt b/opendc-common/src/main/kotlin/org/opendc/common/DispatcherCoroutineDispatcher.kt
index 63744ef9..d33e370d 100644
--- a/opendc-common/src/main/kotlin/org/opendc/common/DispatcherCoroutineDispatcher.kt
+++ b/opendc-common/src/main/kotlin/org/opendc/common/DispatcherCoroutineDispatcher.kt
@@ -37,20 +37,33 @@ import kotlin.coroutines.CoroutineContext
*/
@OptIn(InternalCoroutinesApi::class)
internal class DispatcherCoroutineDispatcher(private val dispatcher: Dispatcher) : CoroutineDispatcher(), Delay, DispatcherProvider {
- override fun dispatch(context: CoroutineContext, block: Runnable) {
+ override fun dispatch(
+ context: CoroutineContext,
+ block: Runnable,
+ ) {
block.run()
}
- override fun dispatchYield(context: CoroutineContext, block: Runnable) {
+ override fun dispatchYield(
+ context: CoroutineContext,
+ block: Runnable,
+ ) {
dispatcher.schedule(block)
}
@OptIn(ExperimentalCoroutinesApi::class)
- override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
+ override fun scheduleResumeAfterDelay(
+ timeMillis: Long,
+ continuation: CancellableContinuation<Unit>,
+ ) {
dispatcher.schedule(timeMillis, CancellableContinuationRunnable(continuation) { resumeUndispatched(Unit) })
}
- override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
+ override fun invokeOnTimeout(
+ timeMillis: Long,
+ block: Runnable,
+ context: CoroutineContext,
+ ): DisposableHandle {
val handle = dispatcher.scheduleCancellable(timeMillis, block)
return DisposableHandle { handle.cancel() }
}
@@ -67,7 +80,7 @@ internal class DispatcherCoroutineDispatcher(private val dispatcher: Dispatcher)
*/
private class CancellableContinuationRunnable<T>(
@JvmField val continuation: CancellableContinuation<T>,
- private val block: CancellableContinuation<T>.() -> Unit
+ private val block: CancellableContinuation<T>.() -> Unit,
) : Runnable {
override fun run() = continuation.block()
}
diff --git a/opendc-common/src/test/kotlin/org/opendc/common/DispatcherCoroutineDispatcherTest.kt b/opendc-common/src/test/kotlin/org/opendc/common/DispatcherCoroutineDispatcherTest.kt
index 01b3d2fc..43faba64 100644
--- a/opendc-common/src/test/kotlin/org/opendc/common/DispatcherCoroutineDispatcherTest.kt
+++ b/opendc-common/src/test/kotlin/org/opendc/common/DispatcherCoroutineDispatcherTest.kt
@@ -36,44 +36,46 @@ import org.opendc.simulator.kotlin.runSimulation
* Test suite for [DispatcherCoroutineDispatcher].
*/
class DispatcherCoroutineDispatcherTest {
-
/**
* Tests if a dispatcher yields the correct time
*/
@Test
- fun testYield() = runSimulation {
- withContext(dispatcher.asCoroutineDispatcher()) {
- val startTime = dispatcher.currentTime
- yield()
- assertEquals(startTime, dispatcher.currentTime)
+ fun testYield() =
+ runSimulation {
+ withContext(dispatcher.asCoroutineDispatcher()) {
+ val startTime = dispatcher.currentTime
+ yield()
+ assertEquals(startTime, dispatcher.currentTime)
+ }
}
- }
/**
* Tests if a dispatcher correctly delays
*/
@Test
- fun testDelay() = runSimulation {
- withContext(dispatcher.asCoroutineDispatcher()) {
- val startTime = dispatcher.currentTime
- delay(10)
- assertEquals(startTime + 10, dispatcher.currentTime)
+ fun testDelay() =
+ runSimulation {
+ withContext(dispatcher.asCoroutineDispatcher()) {
+ val startTime = dispatcher.currentTime
+ delay(10)
+ assertEquals(startTime + 10, dispatcher.currentTime)
+ }
}
- }
/**
* Tests if a dispatcher correctly times out
*/
@Test
- fun testTimeout() = runSimulation {
- withContext(dispatcher.asCoroutineDispatcher()) {
- assertThrows<TimeoutCancellationException> {
- withTimeout(10) {
- delay(1000)
+ fun testTimeout() =
+ runSimulation {
+ withContext(dispatcher.asCoroutineDispatcher()) {
+ assertThrows<TimeoutCancellationException> {
+ withTimeout(10) {
+ delay(1000)
+ }
}
- }
- assertEquals(10, dispatcher.currentTime)
+ assertEquals(10, dispatcher.currentTime)
+ }
}
- }
}
diff --git a/opendc-common/src/test/kotlin/org/opendc/common/util/PacerTest.kt b/opendc-common/src/test/kotlin/org/opendc/common/util/PacerTest.kt
index 3235b046..539403b9 100644
--- a/opendc-common/src/test/kotlin/org/opendc/common/util/PacerTest.kt
+++ b/opendc-common/src/test/kotlin/org/opendc/common/util/PacerTest.kt
@@ -39,9 +39,10 @@ class PacerTest {
var count = 0
runSimulation {
- val pacer = Pacer(dispatcher, /*quantum*/ 100) {
- count++
- }
+ val pacer =
+ Pacer(dispatcher, 100) {
+ count++
+ }
pacer.enqueue()
}
@@ -54,9 +55,10 @@ class PacerTest {
var count = 0
runSimulation {
- val pacer = Pacer(dispatcher, /*quantum*/ 100) {
- count++
- }
+ val pacer =
+ Pacer(dispatcher, 100) {
+ count++
+ }
pacer.enqueue()
pacer.enqueue()
@@ -72,9 +74,10 @@ class PacerTest {
var count = 0
runSimulation {
- val pacer = Pacer(dispatcher, /*quantum*/ 100) {
- count++
- }
+ val pacer =
+ Pacer(dispatcher, 100) {
+ count++
+ }
pacer.enqueue()
pacer.cancel()
@@ -90,9 +93,10 @@ class PacerTest {
var count = 0
runSimulation {
- val pacer = Pacer(dispatcher, /*quantum*/ 100) {
- count++
- }
+ val pacer =
+ Pacer(dispatcher, 100) {
+ count++
+ }
assertFalse(pacer.isPending)
assertDoesNotThrow { pacer.cancel() }
@@ -108,9 +112,10 @@ class PacerTest {
var count = 0
runSimulation {
- val pacer = Pacer(dispatcher, /*quantum*/ 100) {
- count++
- }
+ val pacer =
+ Pacer(dispatcher, 100) {
+ count++
+ }
pacer.enqueue()
delay(100)