|
| 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.channels |
| 18 | + |
| 19 | +import com.devexperts.dxlab.lincheck.* |
| 20 | +import com.devexperts.dxlab.lincheck.annotations.* |
| 21 | +import com.devexperts.dxlab.lincheck.paramgen.* |
| 22 | +import com.devexperts.dxlab.lincheck.stress.* |
| 23 | +import kotlinx.coroutines.experimental.* |
| 24 | +import kotlinx.coroutines.experimental.selects.* |
| 25 | +import org.junit.* |
| 26 | +import java.io.* |
| 27 | + |
| 28 | +@Param(name = "value", gen = IntGen::class, conf = "1:3") |
| 29 | +class SelectChannelLinearizabilityTest : TestBase() { |
| 30 | + |
| 31 | + private val lt = LinTesting() |
| 32 | + private var capacity = 0 |
| 33 | + private lateinit var channel: Channel<Int> |
| 34 | + |
| 35 | + @Reset |
| 36 | + fun reset() { |
| 37 | + channel = Channel(capacity) |
| 38 | + } |
| 39 | + |
| 40 | + @Operation(runOnce = true) |
| 41 | + fun onSend1(@Param(name = "value") value: Int) = onSend(value, "onSend1") |
| 42 | + |
| 43 | + @Operation(runOnce = true) |
| 44 | + fun onSend2(@Param(name = "value") value: Int) = onSend(value, "onSend2") |
| 45 | + |
| 46 | + |
| 47 | + private fun onSend(value: Int, name: String): List<OpResult> { |
| 48 | + return lt.run(name) { |
| 49 | + select<Unit> { |
| 50 | + channel.onSend(value) {} |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Operation(runOnce = true) |
| 56 | + fun close1() = lt.run("close1") { channel.close(IOException("close1")) } |
| 57 | + |
| 58 | + @Operation(runOnce = true) |
| 59 | + fun close2() = lt.run("close2") { channel.close(IOException("close2")) } |
| 60 | + |
| 61 | + @Operation(runOnce = true) |
| 62 | + fun onReceive1() = onReceive("onReceive1") |
| 63 | + |
| 64 | + @Operation(runOnce = true) |
| 65 | + fun onReceive2() = onReceive("onReceive2") |
| 66 | + |
| 67 | + private fun onReceive(name: String): List<OpResult> { |
| 68 | + return lt.run(name) { |
| 69 | + select<Int> { |
| 70 | + channel.onReceive { |
| 71 | + it |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Operation(runOnce = true) |
| 78 | + fun onClose1() = onClose("onClose1") |
| 79 | + |
| 80 | + @Operation(runOnce = true) |
| 81 | + fun onClose2() = onClose("onClose2") |
| 82 | + |
| 83 | + private fun onClose(name: String): List<OpResult> { |
| 84 | + return lt.run(name) { |
| 85 | + select<Unit> { |
| 86 | + channel.onClose { |
| 87 | + try { |
| 88 | + val result = channel.offer(42) |
| 89 | + throw AssertionError("Offer in 'onClose' should throw, but instead returned $result") |
| 90 | + } catch (e: Exception) { |
| 91 | + // Should happen |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testRendezvousChannelLinearizability() { |
| 100 | + runTest(0) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun testArrayChannelLinearizability() { |
| 105 | + for (i in listOf(1, 2, 16)) { |
| 106 | + runTest(i) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun testConflatedChannelLinearizability() = runTest(Channel.CONFLATED) |
| 112 | + |
| 113 | + @Test |
| 114 | + fun testUnlimitedChannelLinearizability() = runTest(Channel.UNLIMITED) |
| 115 | + |
| 116 | + private fun runTest(capacity: Int) { |
| 117 | + this.capacity = capacity |
| 118 | + val options = StressOptions() |
| 119 | + .iterations(100) |
| 120 | + .invocationsPerIteration(200 * stressTestMultiplier) |
| 121 | + .addThread(1, 3) |
| 122 | + .addThread(1, 3) |
| 123 | + .addThread(1, 3) |
| 124 | + .addThread(1, 3) |
| 125 | + .verifier(LinVerifier::class.java) |
| 126 | + LinChecker.check(SelectChannelLinearizabilityTest::class.java, options) |
| 127 | + } |
| 128 | +} |
0 commit comments