From c9d3f7bb0cd7a971a2caab758b4ecc38372c7338 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Fri, 24 Jun 2022 14:02:23 +0200 Subject: [PATCH] ConcurrentWeakMap-related minor improvements (#3346) --- .../jvm/src/debug/internal/ConcurrentWeakMap.kt | 3 +-- kotlinx-coroutines-core/jvm/test/TestBase.kt | 3 ++- .../internal/ConcurrentWeakMapOperationStressTest.kt | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt index b0b2660517..b02eac6397 100644 --- a/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt @@ -10,11 +10,10 @@ import java.lang.ref.* // This is very limited implementation, not suitable as a generic map replacement. // It has lock-free get and put with synchronized rehash for simplicity (and better CPU usage on contention) -@OptIn(ExperimentalStdlibApi::class) @Suppress("UNCHECKED_CAST") internal class ConcurrentWeakMap( /** - * Weak reference queue is needed when a small key is mapped to a large value and we need to promptly release a + * Weak reference queue is needed when a small key is mapped to a large value, and we need to promptly release a * reference to the value when the key was already disposed. */ weakRefQueue: Boolean = false diff --git a/kotlinx-coroutines-core/jvm/test/TestBase.kt b/kotlinx-coroutines-core/jvm/test/TestBase.kt index f089241c63..ce94d33acc 100644 --- a/kotlinx-coroutines-core/jvm/test/TestBase.kt +++ b/kotlinx-coroutines-core/jvm/test/TestBase.kt @@ -236,8 +236,9 @@ public actual open class TestBase(private var disableOutCheck: Boolean) { if (expected != null) { if (!expected(e)) error("Unexpected exception: $e", e) - } else + } else { throw e + } } finally { if (ex == null && expected != null) error("Exception was expected but none produced") } diff --git a/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt index 49e6cccd77..63b5838442 100644 --- a/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt @@ -34,7 +34,7 @@ class ConcurrentWeakMapOperationStressTest : TestBase() { var generationOffset = 0L while (!stop.value) { val kvs = (generationOffset + batchSize * index until generationOffset + batchSize * (index + 1)) - .associateBy({ Key(it) }, { it * it }) + .associateBy({ Key(it) }, { it * it }) generationOffset += batchSize * nThreads for ((k, v) in kvs) { assertEquals(null, m.put(k, v)) @@ -45,8 +45,8 @@ class ConcurrentWeakMapOperationStressTest : TestBase() { for ((k, v) in kvs) { assertEquals(v, m.remove(k)) } - for ((k, v) in kvs) { - assertEquals(null, m.get(k)) + for ((k, _) in kvs) { + assertEquals(null, m[k]) } count.incrementAndGet() } @@ -68,6 +68,6 @@ class ConcurrentWeakMapOperationStressTest : TestBase() { } stop.value = true threads.forEach { it.join() } - assertEquals(0, m.size) + assertEquals(0, m.size, "Unexpected map state: $m") } -} \ No newline at end of file +}