Fix initializer6 test

This commit is contained in:
Alexander Shabalin
2020-03-26 13:21:56 +03:00
parent 7295cc0f39
commit b419101f1f
@@ -9,7 +9,7 @@ import kotlin.test.*
import kotlin.native.concurrent.* import kotlin.native.concurrent.*
val aWorkerId = 2 val aWorkerId = AtomicInt(0)
val bWorkersCount = 3 val bWorkersCount = 3
val aWorkerUnlocker = AtomicInt(0) val aWorkerUnlocker = AtomicInt(0)
@@ -18,13 +18,13 @@ val bWorkerUnlocker = AtomicInt(0)
object A { object A {
init { init {
// Must be called by aWorker only. // Must be called by aWorker only.
assertEquals(aWorkerId, Worker.current.id) assertEquals(aWorkerId.value, Worker.current.id)
// Only allow b workers to run, when a worker has started initialization. // Only allow b workers to run, when a worker has started initialization.
bWorkerUnlocker.increment() bWorkerUnlocker.increment()
// Only proceed with initialization, when all b workers have started executing. // Only proceed with initialization, when all b workers have started executing.
while (aWorkerUnlocker.value < bWorkersCount) {} while (aWorkerUnlocker.value < bWorkersCount) {}
// And now wait a bit, to increase probability of races. // And now wait a bit, to increase probability of races.
Worker.current.park(1000 * 1000L) Worker.current.park(100 * 1000L)
} }
val a = produceA() val a = produceA()
val b = produceB() val b = produceB()
@@ -32,21 +32,20 @@ object A {
fun produceA(): String { fun produceA(): String {
// Must've been called by aWorker only. // Must've been called by aWorker only.
assertEquals(aWorkerId, Worker.current.id) assertEquals(aWorkerId.value, Worker.current.id)
return "A" return "A"
} }
fun produceB(): String { fun produceB(): String {
// Must've been called by aWorker only. // Must've been called by aWorker only.
assertEquals(aWorkerId, Worker.current.id) assertEquals(aWorkerId.value, Worker.current.id)
// Also check that it's ok to get A.a while initializing A.b. // Also check that it's ok to get A.a while initializing A.b.
return "B+${A.a}" return "B+${A.a}"
} }
@Test fun runTest() { @Test fun runTest() {
val aWorker = Worker.start() val aWorker = Worker.start()
// This test relies on aWorkerId value. aWorkerId.value = aWorker.id
assertEquals(aWorkerId, aWorker.id)
val bWorkers = Array(bWorkersCount, { _ -> Worker.start() }) val bWorkers = Array(bWorkersCount, { _ -> Worker.start() })
val aFuture = aWorker.execute(TransferMode.SAFE, {}, { val aFuture = aWorker.execute(TransferMode.SAFE, {}, {