Vsevolod Tolstopyatov | ea651aa | 2018-07-17 18:39:20 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
| 7 | import com.devexperts.dxlab.lincheck.* |
| 8 | import com.devexperts.dxlab.lincheck.annotations.* |
| 9 | import com.devexperts.dxlab.lincheck.paramgen.* |
| 10 | import com.devexperts.dxlab.lincheck.stress.* |
| 11 | import kotlinx.coroutines.experimental.* |
| 12 | import org.junit.* |
| 13 | import java.io.* |
| 14 | |
| 15 | @Param(name = "value", gen = IntGen::class, conf = "1:3") |
| 16 | class ChannelIsClosedLinearizabilityTest : TestBase() { |
| 17 | |
| 18 | private val lt = LinTesting() |
| 19 | private lateinit var channel: Channel<Int> |
| 20 | |
| 21 | @Reset |
Vsevolod Tolstopyatov | 732474f | 2018-07-20 11:36:20 +0300 | [diff] [blame] | 22 | fun resetChannel() { |
Vsevolod Tolstopyatov | ea651aa | 2018-07-17 18:39:20 +0300 | [diff] [blame] | 23 | channel = Channel() |
| 24 | } |
| 25 | |
| 26 | @Operation(runOnce = true) |
| 27 | fun send1(@Param(name = "value") value: Int) = lt.run("send1") { channel.send(value) } |
| 28 | |
| 29 | @Operation(runOnce = true) |
| 30 | fun send2(@Param(name = "value") value: Int) = lt.run("send2") { channel.send(value) } |
| 31 | |
| 32 | @Operation(runOnce = true) |
| 33 | fun receive1() = lt.run("receive1") { channel.receive() } |
| 34 | |
| 35 | @Operation(runOnce = true) |
| 36 | fun receive2() = lt.run("receive2") { channel.receive() } |
| 37 | |
| 38 | @Operation(runOnce = true) |
| 39 | fun close1() = lt.run("close1") { channel.close(IOException("close1")) } |
| 40 | |
| 41 | @Operation(runOnce = true) |
| 42 | fun isClosedForReceive() = lt.run("isClosedForReceive") { channel.isClosedForReceive } |
| 43 | |
| 44 | @Operation(runOnce = true) |
| 45 | fun isClosedForSend() = lt.run("isClosedForSend") { channel.isClosedForSend } |
| 46 | |
| 47 | @Test |
| 48 | fun testLinearizability() { |
| 49 | val options = StressOptions() |
| 50 | .iterations(100) |
| 51 | .invocationsPerIteration(1000 * stressTestMultiplier) |
| 52 | .addThread(1, 3) |
| 53 | .addThread(1, 3) |
| 54 | .addThread(1, 3) |
| 55 | .verifier(LinVerifier::class.java) |
Vsevolod Tolstopyatov | 7bd983f | 2018-07-25 12:36:23 +0300 | [diff] [blame^] | 56 | .injectExecution { actors, methods -> |
| 57 | actors[0].add(actorMethod(methods, "receive1")) |
| 58 | actors[0].add(actorMethod(methods, "receive2")) |
| 59 | actors[0].add(actorMethod(methods, "close1")) |
| 60 | |
| 61 | actors[1].add(actorMethod(methods, "send2")) |
| 62 | actors[1].add(actorMethod(methods, "send1")) |
| 63 | |
| 64 | actors[2].add(actorMethod(methods, "isClosedForSend")) |
| 65 | } |
| 66 | |
Vsevolod Tolstopyatov | ea651aa | 2018-07-17 18:39:20 +0300 | [diff] [blame] | 67 | LinChecker.check(ChannelIsClosedLinearizabilityTest::class.java, options) |
| 68 | } |
| 69 | } |