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

integration/kotlinx-coroutines-guava/test/ListenableFutureTest.kt

Lines changed: 1 addition & 0 deletions
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() {
Lines changed: 17 additions & 0 deletions
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+
}

kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt

Lines changed: 19 additions & 1 deletion
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

kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt

Lines changed: 3 additions & 2 deletions
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()

kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt

Lines changed: 3 additions & 2 deletions
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

kotlinx-coroutines-core/jvm/test/JobChildStressTest.kt

Lines changed: 1 addition & 0 deletions
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.

kotlinx-coroutines-core/jvm/test/ReusableCancellableContinuationInvariantStressTest.kt

Lines changed: 0 additions & 1 deletion
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

kotlinx-coroutines-core/jvm/test/RunBlockingJvmTest.kt

Lines changed: 2 additions & 0 deletions
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()

kotlinx-coroutines-core/jvm/test/ThreadLocalStressTest.kt

Lines changed: 2 additions & 3 deletions
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
}

kotlinx-coroutines-core/jvm/test/UnconfinedConcurrentStressTest.kt

Lines changed: 1 addition & 0 deletions
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() {

0 commit comments

Comments
 (0)