Skip to content

Commit 80ef17e

Browse files
committed
Implement some tests for the new functionality
1 parent 22c5391 commit 80ef17e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+159
-77
lines changed

Diff for: integration/kotlinx-coroutines-guava/test/ListenableFutureTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.junit.Test
99
import java.util.concurrent.*
1010
import java.util.concurrent.CancellationException
1111
import java.util.concurrent.atomic.*
12+
import kotlinx.coroutines.testing.CountDownLatch
1213
import kotlin.test.*
1314

1415
class ListenableFutureTest : TestBase() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kotlinx.coroutines
2+
3+
import kotlin.test.*
4+
import kotlinx.coroutines.testing.*
5+
6+
class DefaultDelayTest: TestBase() {
7+
@Test
8+
fun testDelayOnUnconfined() = runTest {
9+
val latch = CountDownLatch(1)
10+
launch(Dispatchers.Unconfined) {
11+
delay(1)
12+
latch.await()
13+
}
14+
delay(10)
15+
latch.countDown()
16+
}
17+
}

Diff for: kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt

+19-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class RunBlockingTest : TestBase() {
100100
}
101101
}
102102
expectUnreached()
103-
} catch (e: CancellationException) {
103+
} catch (_: CancellationException) {
104104
finish(4)
105105
}
106106
}
@@ -195,6 +195,22 @@ class RunBlockingTest : TestBase() {
195195
}
196196
}
197197

198+
/** Tests that tasks scheduled on a closed `runBlocking` event loop get processed in an I/O thread. */
199+
@OptIn(ExperimentalStdlibApi::class)
200+
@Test
201+
fun testLeakedEventLoopGetsProcessedInIO() {
202+
val dispatcher = runBlocking {
203+
coroutineContext[CoroutineDispatcher.Key]
204+
}!!
205+
runBlocking {
206+
GlobalScope.launch(dispatcher) {
207+
assertTrue(runningOnIoThread())
208+
delay(1.milliseconds)
209+
assertTrue(runningOnIoThread())
210+
}.join()
211+
}
212+
}
213+
198214
/** Will not compile if [runBlocking] doesn't have the "runs exactly once" contract. */
199215
@Test
200216
fun testContract() {
@@ -205,3 +221,5 @@ class RunBlockingTest : TestBase() {
205221
rb.hashCode() // unused
206222
}
207223
}
224+
225+
internal expect fun runningOnIoThread(): Boolean

Diff for: kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package kotlinx.coroutines
22

33
import kotlinx.coroutines.testing.*
44
import org.junit.Test
5-
import java.util.concurrent.*
65
import kotlin.coroutines.*
76
import kotlin.test.*
7+
import java.util.concurrent.Executors
8+
import java.util.concurrent.RejectedExecutionException
89

910
class ExecutorsTest : TestBase() {
1011
private fun checkThreadName(prefix: String) {
@@ -45,7 +46,7 @@ class ExecutorsTest : TestBase() {
4546

4647
@Test
4748
fun testConvertedDispatcherToExecutor() {
48-
val executor: ExecutorService = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") }
49+
val executor = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") }
4950
val dispatcher: CoroutineDispatcher = executor.asCoroutineDispatcher()
5051
assertSame(executor, dispatcher.asExecutor())
5152
executor.shutdown()

Diff for: kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import org.junit.*
55
import org.junit.Test
66
import org.junit.runner.*
77
import org.junit.runners.*
8-
import java.util.concurrent.*
8+
import java.util.concurrent.Executors
99
import kotlin.coroutines.*
1010
import kotlin.test.*
11+
import kotlin.time.Duration.Companion.seconds
1112

1213
@RunWith(Parameterized::class)
1314
class FailingCoroutinesMachineryTest(
@@ -139,7 +140,7 @@ class FailingCoroutinesMachineryTest(
139140
}
140141

141142
private fun checkException() {
142-
latch.await(2, TimeUnit.SECONDS)
143+
latch.await(2.seconds)
143144
val e = caught
144145
assertNotNull(e)
145146
// First condition -- failure in context element

Diff for: kotlinx-coroutines-core/jvm/test/JobChildStressTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.coroutines.testing.*
44
import java.util.concurrent.*
55
import java.util.concurrent.atomic.*
66
import kotlin.test.*
7+
import kotlinx.coroutines.testing.CountDownLatch
78

89
/**
910
* Testing the procedure of attaching a child to the parent job.

Diff for: kotlinx-coroutines-core/jvm/test/ReusableCancellableContinuationInvariantStressTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package kotlinx.coroutines
22

33
import kotlinx.coroutines.testing.*
44
import org.junit.Test
5-
import java.util.concurrent.CountDownLatch
65
import java.util.concurrent.atomic.AtomicReference
76
import kotlin.coroutines.*
87

Diff for: kotlinx-coroutines-core/jvm/test/RunBlockingJvmTest.kt

+2
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,5 @@ class RunBlockingJvmTest : TestBase() {
182182
return result.get().getOrThrow()
183183
}
184184
}
185+
186+
internal actual fun runningOnIoThread(): Boolean = Thread.currentThread().isIoDispatcherThread()

Diff for: kotlinx-coroutines-core/jvm/test/ThreadLocalStressTest.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package kotlinx.coroutines
22

33
import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.sync.*
5-
import java.util.concurrent.*
65
import kotlin.coroutines.*
76
import kotlin.coroutines.intrinsics.*
87
import kotlin.test.*
@@ -139,7 +138,7 @@ class ThreadLocalStressTest : TestBase() {
139138
cancel()
140139
semaphore.acquire()
141140
}
142-
} catch (e: CancellationException) {
141+
} catch (_: CancellationException) {
143142
// Ignore cancellation
144143
}
145144
}
@@ -154,7 +153,7 @@ class ThreadLocalStressTest : TestBase() {
154153
cancel()
155154
semaphore.acquire()
156155
}
157-
} catch (e: CancellationException) {
156+
} catch (_: CancellationException) {
158157
// Ignore cancellation
159158
}
160159
}

Diff for: kotlinx-coroutines-core/jvm/test/UnconfinedConcurrentStressTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.coroutines.testing.*
44
import org.junit.*
55
import org.junit.Test
66
import java.util.concurrent.*
7+
import kotlinx.coroutines.testing.CountDownLatch
78
import kotlin.test.*
89

910
class UnconfinedConcurrentStressTest : TestBase() {

Diff for: kotlinx-coroutines-core/jvm/test/channels/InvokeOnCloseStressTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.*
55
import org.junit.*
66
import org.junit.Test
7-
import java.util.concurrent.*
87
import java.util.concurrent.atomic.*
98
import kotlin.coroutines.*
109
import kotlin.test.*

Diff for: kotlinx-coroutines-core/jvm/test/jdk8/future/FutureTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlin.concurrent.withLock
1414
import kotlin.coroutines.*
1515
import kotlin.reflect.*
1616
import kotlin.test.*
17+
import kotlinx.coroutines.testing.CountDownLatch
1718

1819
class FutureTest : TestBase() {
1920
@Before

Diff for: kotlinx-coroutines-core/jvm/test/scheduling/CoroutineSchedulerOversubscriptionTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package kotlinx.coroutines.scheduling
22

33
import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.*
5-
import org.junit.Test
65
import java.util.concurrent.*
76
import java.util.concurrent.atomic.AtomicInteger
7+
import kotlinx.coroutines.testing.CountDownLatch
8+
import kotlin.test.Test
89

910
class CoroutineSchedulerOversubscriptionTest : TestBase() {
1011

Diff for: kotlinx-coroutines-core/jvm/test/scheduling/CoroutineSchedulerStressTest.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import kotlinx.coroutines.testing.*
44
import kotlinx.atomicfu.*
55
import kotlinx.coroutines.*
66
import kotlinx.coroutines.internal.*
7-
import org.junit.*
8-
import org.junit.Test
9-
import java.util.concurrent.*
7+
import java.util.concurrent.ConcurrentHashMap
108
import java.util.concurrent.atomic.*
119
import kotlin.coroutines.*
1210
import kotlin.test.*
@@ -27,7 +25,7 @@ class CoroutineSchedulerStressTest : TestBase() {
2725
private val processed = AtomicInteger(0)
2826
private val finishLatch = CountDownLatch(1)
2927

30-
@After
28+
@AfterTest
3129
fun tearDown() {
3230
dispatcher.close()
3331
}

Diff for: kotlinx-coroutines-core/jvm/test/scheduling/CoroutineSchedulerTest.kt

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package kotlinx.coroutines.scheduling
22

33
import kotlinx.coroutines.testing.*
4-
import org.junit.Test
5-
import java.lang.Runnable
6-
import java.util.concurrent.*
74
import kotlin.coroutines.*
85
import kotlin.test.*
96

Diff for: kotlinx-coroutines-core/jvm/test/scheduling/DefaultDispatchersTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.coroutines.*
55
import org.junit.Test
66
import java.util.concurrent.*
77
import java.util.concurrent.atomic.*
8+
import kotlinx.coroutines.testing.CountDownLatch
89
import kotlin.test.*
910

1011
class DefaultDispatchersTest : TestBase() {

Diff for: kotlinx-coroutines-core/jvm/test/scheduling/WorkQueueStressTest.kt

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package kotlinx.coroutines.scheduling
22

33
import kotlinx.coroutines.testing.*
4-
import kotlinx.coroutines.*
54
import org.junit.*
65
import org.junit.Test
7-
import java.util.concurrent.*
86
import kotlin.concurrent.*
97
import kotlin.jvm.internal.*
108
import kotlin.test.*

Diff for: kotlinx-coroutines-core/native/src/Dispatchers.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kotlinx.coroutines
22

33
import kotlin.coroutines.*
4+
import kotlin.native.concurrent.Worker
45

56

67
public actual object Dispatchers {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package kotlinx.coroutines
2+
3+
import kotlin.native.concurrent.ObsoleteWorkersApi
4+
import kotlin.native.concurrent.Worker
5+
6+
@OptIn(ObsoleteWorkersApi::class)
7+
internal actual fun runningOnIoThread(): Boolean = Worker.current.name.startsWith("Dispatchers.IO")

Diff for: kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import kotlinx.coroutines.debug.internal.*
66
import org.junit.Test
77
import java.util.concurrent.*
88
import kotlin.test.*
9+
import kotlinx.coroutines.testing.CountDownLatch
910

1011
class RunningThreadStackMergeTest : DebugTestBase() {
1112

Diff for: reactive/kotlinx-coroutines-jdk9/test/FlowAsPublisherTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class FlowAsPublisherTest : TestBase() {
8282
try {
8383
expect(1)
8484
publisher.awaitFirstOrNull()
85-
} catch (e: CancellationException) {
85+
} catch (_: CancellationException) {
8686
expect(3)
8787
}
8888
finish(4)

Diff for: reactive/kotlinx-coroutines-reactive/test/CancelledParentAttachTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.*
66
import org.junit.*
77

88

9-
class CancelledParentAttachTest : TestBase() {;
9+
class CancelledParentAttachTest : TestBase() {
1010

1111
@Test
1212
fun testFlow() = runTest {

Diff for: reactive/kotlinx-coroutines-reactive/test/FlowAsPublisherTest.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.*
55
import kotlinx.coroutines.CancellationException
66
import kotlinx.coroutines.flow.*
7-
import org.junit.Test
87
import org.reactivestreams.*
9-
import java.util.concurrent.*
108
import kotlin.test.*
119

1210
class FlowAsPublisherTest : TestBase() {
@@ -157,7 +155,7 @@ class FlowAsPublisherTest : TestBase() {
157155
try {
158156
expect(1)
159157
publisher.awaitFirstOrNull()
160-
} catch (e: CancellationException) {
158+
} catch (_: CancellationException) {
161159
expect(3)
162160
}
163161
finish(4)

Diff for: reactive/kotlinx-coroutines-reactive/test/IntegrationTest.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class IntegrationTest(
161161
}
162162
}.let {
163163
assertTrue("Expected the message to contain '$message', got '${it.message}'") {
164-
it.message?.contains(message) ?: false
164+
it.message?.contains(message) == true
165165
}
166166
}
167167
}
@@ -203,7 +203,7 @@ class IntegrationTest(
203203
} catch (e: NoSuchElementException) {
204204
// intentionally blank
205205
}
206-
}.let { assertTrue(it.message?.contains("onSubscribe") ?: false) }
206+
}.let { assertTrue(it.message?.contains("onSubscribe") == true) }
207207
}
208208

209209
@Test
@@ -222,4 +222,3 @@ class IntegrationTest(
222222
}
223223

224224
}
225-

Diff for: reactive/kotlinx-coroutines-reactive/test/PublishTest.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import kotlinx.coroutines.channels.*
77
import kotlinx.coroutines.flow.*
88
import kotlinx.coroutines.sync.*
99
import kotlinx.coroutines.testing.exceptions.*
10-
import org.junit.Test
1110
import org.reactivestreams.*
12-
import java.util.concurrent.*
1311
import kotlin.test.*
1412

1513
class PublishTest : TestBase() {
@@ -137,7 +135,7 @@ class PublishTest : TestBase() {
137135
try {
138136
expect(2)
139137
publisher.awaitFirstOrNull()
140-
} catch (e: CancellationException) {
138+
} catch (_: CancellationException) {
141139
expect(5)
142140
}
143141
finish(6)
@@ -229,7 +227,7 @@ class PublishTest : TestBase() {
229227
val result: ChannelResult<Unit> = producerScope!!.trySend(1)
230228
val e = result.exceptionOrNull()!!
231229
assertIs<CancellationException>(e, "The actual error: $e")
232-
assertTrue(producerScope!!.isClosedForSend)
230+
assertTrue(producerScope.isClosedForSend)
233231
assertTrue(result.isFailure)
234232
}
235233
finish(7)
@@ -247,7 +245,7 @@ class PublishTest : TestBase() {
247245
pub.collect {
248246
throw TestException()
249247
}
250-
} catch (e: TestException) {
248+
} catch (_: TestException) {
251249
finish(3)
252250
}
253251
}

Diff for: reactive/kotlinx-coroutines-reactive/test/ReactiveStreamTckTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class ReactiveStreamTckTest : TestBase() {
1616
}
1717

1818
@DataProvider(name = "dispatchers")
19-
public fun dispatchers(): Array<Array<Any>> = Dispatcher.values().map { arrayOf<Any>(it) }.toTypedArray()
19+
fun dispatchers(): Array<Array<Any>> = Dispatcher.values().map { arrayOf<Any>(it) }.toTypedArray()
2020

2121

22-
public class ReactiveStreamTckTestSuite(
22+
class ReactiveStreamTckTestSuite(
2323
private val dispatcher: Dispatcher
2424
) : PublisherVerification<Long>(TestEnvironment(500, 500)) {
2525

@@ -34,7 +34,7 @@ class ReactiveStreamTckTest : TestBase() {
3434
}
3535

3636
@Test
37-
public override fun optional_spec105_emptyStreamMustTerminateBySignallingOnComplete() {
37+
override fun optional_spec105_emptyStreamMustTerminateBySignallingOnComplete() {
3838
throw SkipException("Skipped")
3939
}
4040

Diff for: reactive/kotlinx-coroutines-reactor/test/FlowAsFluxTest.kt

-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.*
55
import kotlinx.coroutines.flow.*
66
import kotlinx.coroutines.reactive.*
7-
import org.junit.Test
87
import org.reactivestreams.*
98
import reactor.core.publisher.*
109
import reactor.util.context.Context
11-
import java.util.concurrent.*
1210
import kotlin.test.*
1311

1412
@Suppress("ReactiveStreamsSubscriberImplementation")

0 commit comments

Comments
 (0)