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
+7 -11
View File
@@ -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<Run>()
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() {