Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.selects |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.sync.* |
| 9 | import org.junit.* |
| 10 | import org.junit.Assert.* |
| 11 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 12 | |
Roman Elizarov | ebe18b4 | 2017-02-28 17:50:55 +0300 | [diff] [blame] | 13 | class SelectPhilosophersStressTest : TestBase() { |
| 14 | val TEST_DURATION = 3000L * stressTestMultiplier |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 15 | |
| 16 | val n = 10 // number of philosophers |
| 17 | val forks = Array<Mutex>(n) { Mutex() } |
| 18 | |
| 19 | suspend fun eat(id: Int, desc: String) { |
| 20 | val left = forks[id] |
| 21 | val right = forks[(id + 1) % n] |
| 22 | while (true) { |
| 23 | val pair = selectUnbiased<Pair<Mutex, Mutex>> { |
| 24 | left.onLock(desc) { left to right } |
| 25 | right.onLock(desc) { right to left } |
| 26 | } |
| 27 | if (pair.second.tryLock(desc)) break |
| 28 | pair.first.unlock(desc) |
| 29 | pair.second.lock(desc) |
| 30 | if (pair.first.tryLock(desc)) break |
| 31 | pair.second.unlock(desc) |
| 32 | } |
| 33 | assertTrue(left.isLocked && right.isLocked) |
| 34 | // om, nom, nom --> eating!!! |
| 35 | right.unlock(desc) |
| 36 | left.unlock(desc) |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | fun testPhilosophers() = runBlocking<Unit> { |
Roman Elizarov | d3d335b | 2017-10-21 17:43:53 +0300 | [diff] [blame] | 41 | println("--- SelectPhilosophersStressTest") |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 42 | val timeLimit = System.currentTimeMillis() + TEST_DURATION |
| 43 | val philosophers = List<Deferred<Int>>(n) { id -> |
| 44 | async(CommonPool) { |
| 45 | val desc = "Philosopher $id" |
| 46 | var eatsCount = 0 |
| 47 | while (System.currentTimeMillis() < timeLimit) { |
| 48 | eat(id, desc) |
| 49 | eatsCount++ |
| 50 | yield() |
| 51 | } |
| 52 | println("Philosopher $id done, eats $eatsCount times") |
| 53 | eatsCount |
| 54 | } |
| 55 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 56 | val debugJob = launch(coroutineContext) { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 57 | delay(3 * TEST_DURATION) |
| 58 | println("Test is failing. Lock states are:") |
| 59 | forks.withIndex().forEach { (id, mutex) -> println("$id: $mutex") } |
| 60 | } |
| 61 | val eats = withTimeout(5 * TEST_DURATION) { philosophers.map { it.await() } } |
| 62 | debugJob.cancel() |
| 63 | eats.withIndex().forEach { (id, eats) -> |
| 64 | assertTrue("$id shall not starve", eats > 0) |
| 65 | } |
| 66 | } |
| 67 | } |