Tune parameters of time-sensitive tests

Lessen CPU load on the main thread.
Remove fragile checks.
This commit is contained in:
Ilya Gorbunov
2020-04-07 16:04:42 +03:00
parent b049a55cca
commit fc9ac67980
2 changed files with 10 additions and 15 deletions
@@ -26,11 +26,10 @@ class TimerTest {
latch.countDown() latch.countDown()
if (latch.count == 0L) this.cancel() 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 elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAt)
val expectedAtLeast = 180L
val expectedRange = 100L..500L assertTrue(elapsed > expectedAtLeast, "Expected elapsed ($elapsed ms) to be at least $expectedAtLeast ms")
assertTrue(elapsed in expectedRange, "Expected elapsed ($elapsed ms) to fit in range $expectedRange")
assertSame(task, callbackTask) assertSame(task, callbackTask)
} }
} }
+7 -11
View File
@@ -90,35 +90,31 @@ class LazyJVMTest {
@Test fun publishOnceLazy() { @Test fun publishOnceLazy() {
val counter = AtomicInteger(0) val counter = AtomicInteger(0)
val initialized = AtomicBoolean(false) val coreCount = Runtime.getRuntime().availableProcessors()
val threads = 3 val threads = (coreCount / 2).coerceIn(2..3)
val values = Random().let { r -> List(threads) { 50 + r.nextInt(50) } } 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<Run>() val runs = ConcurrentLinkedQueue<Run>()
val initializer = { val initializer = {
val id = counter.getAndIncrement() val id = counter.getAndIncrement()
val value = values[id] val value = values[id]
runs += Run(value, initialized.get()) runs += Run(id, value)
Thread.sleep(value.toLong()) Thread.sleep(value.toLong())
initialized.set(true)
value value
} }
val lazy = lazy(LazyThreadSafetyMode.PUBLICATION, initializer) val lazy = lazy(LazyThreadSafetyMode.PUBLICATION, initializer)
val barrier = CyclicBarrier(threads) val barrier = CyclicBarrier(threads)
val accessThreads = List(threads) { thread { barrier.await(); lazy.value } } 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() } accessThreads.forEach { it.join() }
assertEquals(threads, counter.get()) assertEquals(threads, counter.get())
assertEquals(result, lazy.value, "Value must not change after isInitialized is set: $lazy, runs: $runs") assertEquals(result, lazy.value, "Value must not change after isInitialized is set: $lazy, runs: $runs")
assertTrue(runs.any { it.value == result }, "Unexpected lazy result value: $result, runs: $runs")
runs.forEach {
assertFalse(it.initialized, "Expected uninitialized on all initializer executions, runs: $runs")
}
} }
@Test fun publishOnceLazyRace() { @Test fun publishOnceLazyRace() {