blob: f4cd7df68277d9158a7902f1e278fcc6e479ef7a [file] [log] [blame]
Vsevolod Tolstopyatovea651aa2018-07-17 18:39:20 +03001/*
2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5package kotlinx.coroutines.experimental.channels
6
7import com.devexperts.dxlab.lincheck.*
8import com.devexperts.dxlab.lincheck.annotations.*
9import com.devexperts.dxlab.lincheck.paramgen.*
10import com.devexperts.dxlab.lincheck.stress.*
11import kotlinx.coroutines.experimental.*
12import org.junit.*
13import java.io.*
14
15@Param(name = "value", gen = IntGen::class, conf = "1:3")
16class ChannelIsClosedLinearizabilityTest : TestBase() {
17
18 private val lt = LinTesting()
19 private lateinit var channel: Channel<Int>
20
21 @Reset
Vsevolod Tolstopyatov732474f2018-07-20 11:36:20 +030022 fun resetChannel() {
Vsevolod Tolstopyatovea651aa2018-07-17 18:39:20 +030023 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 Tolstopyatov7bd983f2018-07-25 12:36:23 +030056 .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 Tolstopyatovea651aa2018-07-17 18:39:20 +030067 LinChecker.check(ChannelIsClosedLinearizabilityTest::class.java, options)
68 }
69}