Roman Elizarov | c22a1c7 | 2018-05-30 21:58:02 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental.internal |
| 18 | |
| 19 | import kotlinx.atomicfu.* |
| 20 | import kotlinx.coroutines.experimental.* |
| 21 | import java.util.concurrent.* |
| 22 | import kotlin.concurrent.* |
| 23 | import kotlin.test.* |
| 24 | |
| 25 | // Tests many short queues to stress copy/resize |
| 26 | class LockFreeMPSCQueueStressTest : TestBase() { |
| 27 | private val nSeconds = 3 * stressTestMultiplier |
| 28 | private val nProducers = 4 |
| 29 | private val batchSize = 100 |
| 30 | |
| 31 | private val batch = atomic(0) |
| 32 | private val produced = atomic(0L) |
| 33 | private val consumed = atomic(0L) |
| 34 | private var expected = LongArray(nProducers) |
| 35 | |
| 36 | private var queue = atomic<LockFreeMPSCQueue<Item>?>(null) |
| 37 | private val done = atomic(0) |
| 38 | private val doneProducers = atomic(0) |
| 39 | |
| 40 | private val barrier = CyclicBarrier(nProducers + 2) |
| 41 | |
| 42 | private class Item(val producer: Int, val index: Long) |
| 43 | |
| 44 | @Test |
| 45 | fun testStress() { |
| 46 | val threads = mutableListOf<Thread>() |
| 47 | threads += thread(name = "Pacer", start = false) { |
| 48 | while (done.value == 0) { |
| 49 | queue.value = LockFreeMPSCQueue() |
| 50 | batch.value = 0 |
| 51 | doneProducers.value = 0 |
| 52 | barrier.await() // start consumers & producers |
| 53 | barrier.await() // await consumers & producers |
| 54 | } |
| 55 | queue.value = null |
| 56 | println("Pacer done") |
| 57 | barrier.await() // wakeup the rest |
| 58 | } |
| 59 | threads += thread(name = "Consumer", start = false) { |
| 60 | while (true) { |
| 61 | barrier.await() |
| 62 | val queue = queue.value ?: break |
| 63 | while (true) { |
| 64 | val item = queue.removeFirstOrNull() |
| 65 | if (item == null) { |
| 66 | if (doneProducers.value == nProducers && queue.isEmpty) break // that's it |
| 67 | continue // spin to retry |
| 68 | } |
| 69 | consumed.incrementAndGet() |
| 70 | val eItem = expected[item.producer]++ |
| 71 | if (eItem != item.index) error("Expected $eItem but got ${item.index} from Producer-${item.producer}") |
| 72 | } |
| 73 | barrier.await() |
| 74 | } |
| 75 | println("Consumer done") |
| 76 | } |
| 77 | val producers = List(nProducers) { producer -> |
| 78 | thread(name = "Producer-$producer", start = false) { |
| 79 | var index = 0L |
| 80 | while (true) { |
| 81 | barrier.await() |
| 82 | val queue = queue.value ?: break |
| 83 | while (true) { |
| 84 | if (batch.incrementAndGet() >= batchSize) break |
| 85 | check(queue.addLast(Item(producer, index++))) // never closed |
| 86 | produced.incrementAndGet() |
| 87 | } |
| 88 | doneProducers.incrementAndGet() |
| 89 | barrier.await() |
| 90 | } |
| 91 | println("Producer-$producer done") |
| 92 | } |
| 93 | } |
| 94 | threads += producers |
| 95 | threads.forEach { |
| 96 | it.setUncaughtExceptionHandler { t, e -> |
| 97 | System.err.println("Thread $t failed: $e") |
| 98 | e.printStackTrace() |
| 99 | done.value = 1 |
| 100 | error("Thread $t failed", e) |
| 101 | } |
| 102 | } |
| 103 | threads.forEach { it.start() } |
| 104 | for (second in 1..nSeconds) { |
| 105 | Thread.sleep(1000) |
| 106 | println("$second: produced=${produced.value}, consumed=${consumed.value}") |
| 107 | if (done.value == 1) break |
| 108 | } |
| 109 | done.value = 1 |
| 110 | threads.forEach { it.join() } |
| 111 | println("T: produced=${produced.value}, consumed=${consumed.value}") |
| 112 | assertEquals(produced.value, consumed.value) |
| 113 | } |
| 114 | } |