Skip to content

Commit e88bbd6

Browse files
committed
Provide toString implementation for limitedParallelism
1 parent 918b1cc commit e88bbd6

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

kotlinx-coroutines-core/common/src/internal/LimitedDispatcher.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ internal class LimitedDispatcher(
9595
}
9696
}
9797

98+
override fun toString() = "$dispatcher.limitedParallelism($parallelism)"
99+
98100
/**
99101
* A worker that polls the queue and runs tasks until there are no more of them.
100102
*
@@ -125,5 +127,4 @@ internal class LimitedDispatcher(
125127
}
126128
}
127129

128-
// Save a few bytecode ops
129130
internal fun Int.checkParallelism() = require(this >= 1) { "Expected positive parallelism level, but got $this" }

kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt

+5
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable {
184184
(this as Object).notifyAll()
185185
}
186186

187+
// User only for testing and nothing else
187188
internal val isThreadPresent
188189
get() = _thread != null
190+
191+
override fun toString(): String {
192+
return "DefaultExecutor"
193+
}
189194
}

kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ private object UnlimitedIoScheduler : CoroutineDispatcher() {
4949
if (parallelism >= MAX_POOL_SIZE) return this
5050
return super.limitedParallelism(parallelism)
5151
}
52+
53+
// This name only leaks to user code as part of .limitedParallelism machinery
54+
override fun toString(): String {
55+
return "Dispatchers.IO"
56+
}
5257
}
5358

5459
// Dispatchers.IO

kotlinx-coroutines-core/jvm/test/DispatchersToStringTest.kt

+8
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ class DispatchersToStringTest {
1111
assertEquals("Dispatchers.Main[missing]", Dispatchers.Main.toString())
1212
assertEquals("Dispatchers.Main[missing]", Dispatchers.Main.immediate.toString())
1313
}
14+
15+
@Test
16+
fun testLimitedParallelism() {
17+
assertEquals("Dispatchers.IO.limitedParallelism(1)", Dispatchers.IO.limitedParallelism(1).toString())
18+
assertEquals("Dispatchers.Default.limitedParallelism(2)", Dispatchers.Default.limitedParallelism(2).toString())
19+
// Not overridden at all, limited parallelism returns `this`
20+
assertEquals("DefaultExecutor", (DefaultDelay as CoroutineDispatcher).limitedParallelism(42).toString())
21+
}
1422
}

0 commit comments

Comments
 (0)