From fc9ac679808b06f8089b24b4bc4117ccfc812d42 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 7 Apr 2020 16:04:42 +0300 Subject: [PATCH] Tune parameters of time-sensitive tests Lessen CPU load on the main thread. Remove fragile checks. --- .../stdlib/jvm/test/concurrent/TimerTest.kt | 7 +++---- libraries/stdlib/jvm/test/utils/LazyJVMTest.kt | 18 +++++++----------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/libraries/stdlib/jvm/test/concurrent/TimerTest.kt b/libraries/stdlib/jvm/test/concurrent/TimerTest.kt index 914f192c2ce..04f177b49cd 100644 --- a/libraries/stdlib/jvm/test/concurrent/TimerTest.kt +++ b/libraries/stdlib/jvm/test/concurrent/TimerTest.kt @@ -26,11 +26,10 @@ class TimerTest { latch.countDown() if (latch.count == 0L) this.cancel() } - if (!latch.await(1500, TimeUnit.MILLISECONDS)) throw TimeoutException() + if (!latch.await(2500, TimeUnit.MILLISECONDS)) throw TimeoutException() val elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAt) - - val expectedRange = 100L..500L - assertTrue(elapsed in expectedRange, "Expected elapsed ($elapsed ms) to fit in range $expectedRange") + val expectedAtLeast = 180L + assertTrue(elapsed > expectedAtLeast, "Expected elapsed ($elapsed ms) to be at least $expectedAtLeast ms") assertSame(task, callbackTask) } } \ No newline at end of file diff --git a/libraries/stdlib/jvm/test/utils/LazyJVMTest.kt b/libraries/stdlib/jvm/test/utils/LazyJVMTest.kt index 7d2a5683c40..e6cf2f53e2f 100644 --- a/libraries/stdlib/jvm/test/utils/LazyJVMTest.kt +++ b/libraries/stdlib/jvm/test/utils/LazyJVMTest.kt @@ -90,35 +90,31 @@ class LazyJVMTest { @Test fun publishOnceLazy() { val counter = AtomicInteger(0) - val initialized = AtomicBoolean(false) - val threads = 3 - val values = Random().let { r -> List(threads) { 50 + r.nextInt(50) } } + val coreCount = Runtime.getRuntime().availableProcessors() + val threads = (coreCount / 2).coerceIn(2..3) + val values = Random().let { r -> List(threads) { 100 + r.nextInt(50) } } - data class Run(val value: Int, val initialized: Boolean) + data class Run(val id: Int, val value: Int) val runs = ConcurrentLinkedQueue() val initializer = { val id = counter.getAndIncrement() val value = values[id] - runs += Run(value, initialized.get()) + runs += Run(id, value) Thread.sleep(value.toLong()) - initialized.set(true) value } val lazy = lazy(LazyThreadSafetyMode.PUBLICATION, initializer) val barrier = CyclicBarrier(threads) val accessThreads = List(threads) { thread { barrier.await(); lazy.value } } - val result = run { while (!lazy.isInitialized()) /* wait */; lazy.value } + val result = run { while (!lazy.isInitialized()) Thread.sleep(1); lazy.value } accessThreads.forEach { it.join() } assertEquals(threads, counter.get()) assertEquals(result, lazy.value, "Value must not change after isInitialized is set: $lazy, runs: $runs") - - runs.forEach { - assertFalse(it.initialized, "Expected uninitialized on all initializer executions, runs: $runs") - } + assertTrue(runs.any { it.value == result }, "Unexpected lazy result value: $result, runs: $runs") } @Test fun publishOnceLazyRace() {