Skip to content

Commit 0e7efab

Browse files
committed
Move most tests in the test module to common code
1 parent d78e8ad commit 0e7efab

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

kotlinx-coroutines-test/jvm/test/TestCoroutineDispatcherOrderTest.kt renamed to kotlinx-coroutines-test/common/test/TestCoroutineDispatcherOrderTest.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
import kotlinx.atomicfu.*
56
import kotlinx.coroutines.*
67
import kotlinx.coroutines.test.*
78
import kotlin.test.*
89

9-
class TestCoroutineDispatcherOrderTest : TestBase() {
10+
class TestCoroutineDispatcherOrderTest {
11+
12+
private val actionIndex = atomic(0)
13+
private val finished = atomic(false)
14+
15+
private fun expect(index: Int) {
16+
val wasIndex = actionIndex.incrementAndGet()
17+
// println("expect($index), wasIndex=$wasIndex")
18+
check(index == wasIndex) { "Expecting action index $index but it is actually $wasIndex" }
19+
}
20+
21+
private fun finish(index: Int) {
22+
expect(index)
23+
check(!finished.getAndSet(true)) { "Should call 'finish(...)' at most once" }
24+
}
1025

1126
@Test
1227
fun testAdvanceTimeBy_progressesOnEachDelay() {

kotlinx-coroutines-test/jvm/test/TestRunBlockingOrderTest.kt renamed to kotlinx-coroutines-test/common/test/TestRunBlockingOrderTest.kt

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
import kotlinx.atomicfu.*
56
import kotlinx.coroutines.*
67
import kotlinx.coroutines.test.*
78
import kotlin.test.*
89

9-
class TestRunBlockingOrderTest : TestBase() {
10+
class TestRunBlockingOrderTest {
11+
12+
private val actionIndex = atomic(0)
13+
private val finished = atomic(false)
14+
15+
private fun expect(index: Int) {
16+
val wasIndex = actionIndex.incrementAndGet()
17+
// println("expect($index), wasIndex=$wasIndex")
18+
check(index == wasIndex) { "Expecting action index $index but it is actually $wasIndex" }
19+
}
20+
21+
private fun finish(index: Int) {
22+
expect(index)
23+
check(!finished.getAndSet(true)) { "Should call 'finish(...)' at most once" }
24+
}
25+
1026
@Test
1127
fun testLaunchImmediate() = runBlockingTest {
1228
expect(1)

kotlinx-coroutines-test/common/test/TestRunBlockingTest.kt

-22
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,6 @@ class TestRunBlockingTest {
7373
}
7474
}
7575

76-
@Test
77-
fun kryak() {
78-
class ClassUnderTest(private val mainScope: CoroutineScope, private val bgDispatcher: CoroutineDispatcher) {
79-
fun doSomething() {
80-
mainScope.launch {
81-
val data = withContext(bgDispatcher) {
82-
delay(1000)
83-
42
84-
}
85-
println(data)
86-
}
87-
}
88-
}
89-
90-
val testBgDispatcher = TestCoroutineDispatcher()
91-
val testScope = TestCoroutineScope()
92-
val subject = ClassUnderTest(testScope, testBgDispatcher)
93-
94-
subject.doSomething()
95-
testScope.advanceUntilIdle()
96-
}
97-
9876
@Test
9977
fun whenUsingTimeout_triggersWhenWaiting() {
10078
assertFailsWith<TimeoutCancellationException> {

kotlinx-coroutines-test/jvm/test/TestDispatchersTest.kt

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
import kotlinx.atomicfu.*
56
import kotlinx.coroutines.*
67
import kotlinx.coroutines.test.*
78
import kotlin.coroutines.*
89
import kotlin.test.*
910

10-
class TestDispatchersTest : TestBase() {
11+
class TestDispatchersTest {
12+
private val actionIndex = atomic(0)
13+
private val finished = atomic(false)
14+
15+
private fun expect(index: Int) {
16+
val wasIndex = actionIndex.incrementAndGet()
17+
println("expect($index), wasIndex=$wasIndex")
18+
check(index == wasIndex) { "Expecting action index $index but it is actually $wasIndex" }
19+
}
20+
21+
private fun finish(index: Int) {
22+
expect(index)
23+
check(!finished.getAndSet(true)) { "Should call 'finish(...)' at most once" }
24+
}
1125

1226
@BeforeTest
1327
fun setUp() {
1428
Dispatchers.resetMain()
1529
}
1630

1731
@Test
18-
fun testSelfSet() = runTest {
32+
fun testSelfSet() {
1933
assertFailsWith<IllegalArgumentException> { Dispatchers.setMain(Dispatchers.Main) }
2034
}
2135

2236
@Test
23-
fun testImmediateDispatcher() = runTest {
37+
fun testImmediateDispatcher() = runBlockingTest {
2438
Dispatchers.setMain(ImmediateDispatcher())
2539
expect(1)
2640
withContext(Dispatchers.Main) {
@@ -41,7 +55,7 @@ class TestDispatchersTest : TestBase() {
4155
return false
4256
}
4357

44-
override fun dispatch(context: CoroutineContext, block: Runnable) = expectUnreached()
58+
override fun dispatch(context: CoroutineContext, block: Runnable) = throw RuntimeException("Shouldn't be reached")
4559
}
4660

4761
private inner class RegularDispatcher : CoroutineDispatcher() {

kotlinx-coroutines-test/npm/package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@
1919
"coroutines",
2020
"JetBrains",
2121
"test"
22-
],
23-
"dependencies": {
24-
"kotlinx-coroutines-core": "~$version"
25-
}
26-
}
22+
]
23+
}

0 commit comments

Comments
 (0)