Skip to content

Commit ebe18b4

Browse files
committed
Stress-testing infrastructure
Tests are run x10 longer with "-DstressTest" JVM option
1 parent 174c696 commit ebe18b4

12 files changed

+36
-28
lines changed

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/JobTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19-
import org.junit.Assert.*
19+
import org.junit.Assert.assertEquals
2020
import org.junit.Test
2121

22-
class JobTest {
22+
class JobTest : TestBase() {
2323
@Test
2424
fun testState() {
2525
val job = Job()
@@ -48,7 +48,7 @@ class JobTest {
4848
@Test
4949
fun testManyHandlers() {
5050
val job = Job()
51-
val n = 100
51+
val n = 100 * stressTestMultiplier
5252
val fireCount = IntArray(n)
5353
for (i in 0 until n) job.invokeOnCompletion { fireCount[i]++ }
5454
check(job.isActive)
@@ -66,7 +66,7 @@ class JobTest {
6666
@Test
6767
fun testUnregisterInHandler() {
6868
val job = Job()
69-
val n = 100
69+
val n = 100 * stressTestMultiplier
7070
val fireCount = IntArray(n)
7171
for (i in 0 until n) {
7272
var registration: Job.Registration? = null
@@ -90,7 +90,7 @@ class JobTest {
9090
@Test
9191
fun testManyHandlersWithUnregister() {
9292
val job = Job()
93-
val n = 100
93+
val n = 100 * stressTestMultiplier
9494
val fireCount = IntArray(n)
9595
val registrations = Array<Job.Registration>(n) { i -> job.invokeOnCompletion { fireCount[i]++ } }
9696
check(job.isActive)
@@ -105,7 +105,7 @@ class JobTest {
105105
@Test
106106
fun testExceptionsInHandler() {
107107
val job = Job()
108-
val n = 100
108+
val n = 100 * stressTestMultiplier
109109
val fireCount = IntArray(n)
110110
class TestException : Throwable()
111111
for (i in 0 until n) job.invokeOnCompletion {
@@ -123,7 +123,7 @@ class JobTest {
123123
@Test
124124
fun testMemoryRelease() {
125125
val job = Job()
126-
val n = 10_000_000
126+
val n = 10_000_000 * stressTestMultiplier
127127
var fireCount = 0
128128
for (i in 0 until n) job.invokeOnCompletion { fireCount++ }.unregister()
129129
}

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/TestBase.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import java.util.concurrent.atomic.AtomicInteger
2222
import java.util.concurrent.atomic.AtomicReference
2323

2424
open class TestBase {
25+
val isStressTest = System.getProperty("stressTest") != null
26+
val stressTestMultiplier = if (isStressTest) 10 else 1
27+
2528
var actionIndex = AtomicInteger()
2629
var finished = AtomicBoolean()
2730
var error = AtomicReference<Throwable>()

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/channels/ChannelAtomicCancelStressTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import java.util.concurrent.atomic.AtomicReference
2929
* Tests cancel atomicity for channel send & receive operations, including their select versions.
3030
*/
3131
@RunWith(Parameterized::class)
32-
class ChannelAtomicCancelStressTest(val kind: TestChannelKind) {
32+
class ChannelAtomicCancelStressTest(val kind: TestChannelKind) : TestBase() {
3333
companion object {
3434
@Parameterized.Parameters(name = "{0}")
3535
@JvmStatic
3636
fun params(): Collection<Array<Any>> = TestChannelKind.values().map { arrayOf<Any>(it) }
3737
}
3838

39-
val TEST_DURATION = 3000L
39+
val TEST_DURATION = 3000L * stressTestMultiplier
4040

4141
val channel = kind.create()
4242
val senderDone = ArrayChannel<Boolean>(1)

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/channels/ChannelSendReceiveStressTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ChannelSendReceiveStressTest(
3131
val kind: TestChannelKind,
3232
val nSenders: Int,
3333
val nReceivers: Int
34-
) {
34+
) : TestBase() {
3535
companion object {
3636
@Parameterized.Parameters(name = "{0}, nSenders={1}, nReceivers={2}")
3737
@JvmStatic
@@ -43,8 +43,8 @@ class ChannelSendReceiveStressTest(
4343
}
4444
}
4545

46-
val timeLimit = 30_000L // 30 sec
47-
val nEvents = 1_000_000
46+
val timeLimit = 30_000L * stressTestMultiplier // 30 sec
47+
val nEvents = 1_000_000 * stressTestMultiplier
4848

4949
val channel = kind.create()
5050
val sendersCompleted = AtomicInteger()

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/internal/LockFreeLinkedListAtomicStressTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.coroutines.experimental.internal
1818

19+
import kotlinx.coroutines.experimental.TestBase
1920
import org.junit.Assert.*
2021
import org.junit.Test
2122
import java.util.*
@@ -26,14 +27,15 @@ import kotlin.concurrent.thread
2627
* This stress test has 4 threads adding randomly to the list and them immediately undoing
2728
* this addition by remove, and 4 threads trying to remove nodes from two lists simultaneously (atomically).
2829
*/
29-
class LockFreeLinkedListAtomicStressTest {
30+
class LockFreeLinkedListAtomicStressTest : TestBase() {
3031
data class IntNode(val i: Int) : LockFreeLinkedListNode()
3132

33+
val TEST_DURATION = 5000L * stressTestMultiplier
34+
3235
val threads = mutableListOf<Thread>()
3336
val nLists = 4
3437
val nAdderThreads = 4
3538
val nRemoverThreads = 4
36-
val timeout = 5000L
3739
val completedAdder = AtomicInteger()
3840
val completedRemover = AtomicInteger()
3941

@@ -45,7 +47,7 @@ class LockFreeLinkedListAtomicStressTest {
4547

4648
@Test
4749
fun testStress() {
48-
val deadline = System.currentTimeMillis() + timeout
50+
val deadline = System.currentTimeMillis() + TEST_DURATION
4951
repeat(nAdderThreads) { threadId ->
5052
threads += thread(start = false, name = "adder-$threadId") {
5153
val rnd = Random()

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/internal/LockFreeLinkedListLongStressTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.coroutines.experimental.internal
1818

19+
import kotlinx.coroutines.experimental.TestBase
1920
import org.junit.Test
2021
import java.util.*
2122
import java.util.concurrent.atomic.AtomicInteger
@@ -27,12 +28,12 @@ import kotlin.coroutines.experimental.buildIterator
2728
* and 6 threads iterating and concurrently removing items. The resulting list that is being
2829
* stressed is long.
2930
*/
30-
class LockFreeLinkedListLongStressTest {
31+
class LockFreeLinkedListLongStressTest : TestBase() {
3132
data class IntNode(val i: Int) : LockFreeLinkedListNode()
3233
val list = LockFreeLinkedListHead()
3334

3435
val threads = mutableListOf<Thread>()
35-
val nAdded = 10_000_000
36+
val nAdded = 10_000_000 * stressTestMultiplier
3637
val nAddThreads = 4 // must be power of 2 (!!!)
3738
val nRemoveThreads = 6
3839
val removeProbability = 0.2

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/internal/LockFreeLinkedListShortStressTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.coroutines.experimental.internal
1818

19+
import kotlinx.coroutines.experimental.TestBase
1920
import org.junit.Assert.*
2021
import org.junit.Test
2122
import java.util.*
@@ -27,14 +28,15 @@ import kotlin.concurrent.thread
2728
* this addition by remove, and 4 threads removing first node. The resulting list that is being
2829
* stressed is very short.
2930
*/
30-
class LockFreeLinkedListShortStressTest {
31+
class LockFreeLinkedListShortStressTest : TestBase() {
3132
data class IntNode(val i: Int) : LockFreeLinkedListNode()
3233
val list = LockFreeLinkedListHead()
3334

35+
val TEST_DURATION = 5000L * stressTestMultiplier
36+
3437
val threads = mutableListOf<Thread>()
3538
val nAdderThreads = 6
3639
val nRemoverThreads = 4
37-
val timeout = 5000L
3840
val completedAdder = AtomicInteger()
3941
val completedRemover = AtomicInteger()
4042

@@ -44,7 +46,7 @@ class LockFreeLinkedListShortStressTest {
4446

4547
@Test
4648
fun testStress() {
47-
val deadline = System.currentTimeMillis() + timeout
49+
val deadline = System.currentTimeMillis() + TEST_DURATION
4850
repeat(nAdderThreads) { threadId ->
4951
threads += thread(start = false, name = "adder-$threadId") {
5052
val rnd = Random()

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/selects/SelectArrayChannelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class SelectArrayChannelTest : TestBase() {
226226
@Test
227227
fun testSelectSendResourceCleanup() = runBlocking<Unit> {
228228
val channel = ArrayChannel<Int>(1)
229-
val n = 10_000_000
229+
val n = 10_000_000 * stressTestMultiplier
230230
expect(1)
231231
channel.send(-1) // fill the buffer, so all subsequent sends cannot proceed
232232
repeat(n) { i ->
@@ -241,7 +241,7 @@ class SelectArrayChannelTest : TestBase() {
241241
@Test
242242
fun testSelectReceiveResourceCleanup() = runBlocking<Unit> {
243243
val channel = ArrayChannel<Int>(1)
244-
val n = 10_000_000
244+
val n = 10_000_000 * stressTestMultiplier
245245
expect(1)
246246
repeat(n) { i ->
247247
select {

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/selects/SelectMutexTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class SelectMutexTest : TestBase() {
7272

7373
@Test
7474
fun testSelectCancelledResourceRelease() = runBlocking<Unit> {
75-
val n = 1_000
75+
val n = 1_000 * stressTestMultiplier
7676
val mutex = Mutex(true) as MutexImpl // locked
7777
expect(1)
7878
repeat(n) { i ->

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/selects/SelectPhilosophersStressTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import kotlinx.coroutines.experimental.sync.Mutex
2121
import org.junit.Assert.assertTrue
2222
import org.junit.Test
2323

24-
class SelectPhilosophersStressTest {
25-
val TEST_DURATION = 3000L
24+
class SelectPhilosophersStressTest : TestBase() {
25+
val TEST_DURATION = 3000L * stressTestMultiplier
2626

2727
val n = 10 // number of philosophers
2828
val forks = Array<Mutex>(n) { Mutex() }

0 commit comments

Comments
 (0)