Skip to content

Commit a1ef099

Browse files
committed
Implement some tests for the new functionality
1 parent 63f72d9 commit a1ef099

File tree

77 files changed

+294
-212
lines changed

Some content is hidden

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

77 files changed

+294
-212
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

+18
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,22 @@ class RunBlockingTest : TestBase() {
194194
}
195195
}
196196
}
197+
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+
}
197213
}
214+
215+
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

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

33
import kotlinx.coroutines.testing.*
44
import org.junit.*
5+
import kotlin.test.Test
6+
import kotlin.test.assertTrue
7+
import kotlin.time.Duration.Companion.milliseconds
58

69
class RunBlockingJvmTest : TestBase() {
710
@Test
@@ -13,3 +16,5 @@ class RunBlockingJvmTest : TestBase() {
1316
rb.hashCode() // unused
1417
}
1518
}
19+
20+
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/src/Await.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlinx.coroutines.reactive.*
1515
*
1616
* @throws NoSuchElementException if the publisher does not emit any value
1717
*/
18-
public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T =
18+
suspend fun <T> Flow.Publisher<T>.awaitFirst(): T =
1919
FlowAdapters.toPublisher(this).awaitFirst()
2020

2121
/**
@@ -27,7 +27,7 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T =
2727
* If the [Job] of the current coroutine is cancelled while the suspending function is waiting, this
2828
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
2929
*/
30-
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T =
30+
suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T =
3131
FlowAdapters.toPublisher(this).awaitFirstOrDefault(default)
3232

3333
/**
@@ -38,7 +38,7 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T =
3838
* If the [Job] of the current coroutine is cancelled while the suspending function is waiting, this
3939
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
4040
*/
41-
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? =
41+
suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? =
4242
FlowAdapters.toPublisher(this).awaitFirstOrNull()
4343

4444
/**
@@ -50,7 +50,7 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? =
5050
* If the [Job] of the current coroutine is cancelled while the suspending function is waiting, this
5151
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
5252
*/
53-
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T =
53+
suspend fun <T> Flow.Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T =
5454
FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue)
5555

5656
/**
@@ -63,7 +63,7 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirstOrElse(defaultValue: () -> T)
6363
*
6464
* @throws NoSuchElementException if the publisher does not emit any value
6565
*/
66-
public suspend fun <T> Flow.Publisher<T>.awaitLast(): T =
66+
suspend fun <T> Flow.Publisher<T>.awaitLast(): T =
6767
FlowAdapters.toPublisher(this).awaitLast()
6868

6969
/**
@@ -77,5 +77,5 @@ public suspend fun <T> Flow.Publisher<T>.awaitLast(): T =
7777
* @throws NoSuchElementException if the publisher does not emit any value
7878
* @throws IllegalArgumentException if the publisher emits more than one value
7979
*/
80-
public suspend fun <T> Flow.Publisher<T>.awaitSingle(): T =
80+
suspend fun <T> Flow.Publisher<T>.awaitSingle(): T =
8181
FlowAdapters.toPublisher(this).awaitSingle()

Diff for: reactive/kotlinx-coroutines-jdk9/src/Publish.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.reactivestreams.FlowAdapters
2828
*
2929
* @throws IllegalArgumentException if the provided [context] contains a [Job] instance.
3030
*/
31-
public fun <T> flowPublish(
31+
fun <T> flowPublish(
3232
context: CoroutineContext = EmptyCoroutineContext,
3333
@BuilderInference block: suspend ProducerScope<T>.() -> Unit
3434
): Flow.Publisher<T> = FlowAdapters.toFlowPublisher(publish(context, block))

Diff for: reactive/kotlinx-coroutines-jdk9/src/ReactiveFlow.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.util.concurrent.Flow as JFlow
1919
* If any of the resulting flow transformations fails, the subscription is immediately cancelled and all the in-flight
2020
* elements are discarded.
2121
*/
22-
public fun <T : Any> JFlow.Publisher<T>.asFlow(): Flow<T> =
22+
fun <T : Any> JFlow.Publisher<T>.asFlow(): Flow<T> =
2323
FlowAdapters.toPublisher(this).asFlow()
2424

2525
/**
@@ -32,7 +32,7 @@ public fun <T : Any> JFlow.Publisher<T>.asFlow(): Flow<T> =
3232
* is used, so calls are performed from an arbitrary thread.
3333
*/
3434
@JvmOverloads // binary compatibility
35-
public fun <T : Any> Flow<T>.asPublisher(context: CoroutineContext = EmptyCoroutineContext): JFlow.Publisher<T> =
35+
fun <T : Any> Flow<T>.asPublisher(context: CoroutineContext = EmptyCoroutineContext): JFlow.Publisher<T> =
3636
FlowAdapters.toFlowPublisher(asReactivePublisher(context))
3737

3838
/**
@@ -41,5 +41,5 @@ public fun <T : Any> Flow<T>.asPublisher(context: CoroutineContext = EmptyCorout
4141
* If [action] throws an exception at some point, the subscription is cancelled, and the exception is rethrown from
4242
* [collect]. Also, if the publisher signals an error, that error is rethrown from [collect].
4343
*/
44-
public suspend inline fun <T> JFlow.Publisher<T>.collect(action: (T) -> Unit): Unit =
44+
suspend inline fun <T> JFlow.Publisher<T>.collect(action: (T) -> Unit): Unit =
4545
FlowAdapters.toPublisher(this).collect(action)

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)

0 commit comments

Comments
 (0)