MPP: More common tests, using kotlin.test.assertTrue & runTest dsl
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/AtomicCancellationTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/AtomicCancellationTest.kt
index 14b2b53..6d43cc1 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/AtomicCancellationTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/AtomicCancellationTest.kt
@@ -19,33 +19,9 @@
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.selects.select
import kotlinx.coroutines.experimental.sync.Mutex
-import org.hamcrest.core.IsEqual
-import org.junit.Assert.assertThat
-import org.junit.Test
+import kotlin.test.*
-class AtomicCancellationTest: TestBase() {
- @Test
- fun testCancellableLaunch() = runBlocking {
- expect(1)
- val job = launch(coroutineContext) {
- expectUnreached() // will get cancelled before start
- }
- expect(2)
- job.cancel()
- finish(3)
- }
-
- @Test
- fun testAtomicLaunch() = runBlocking {
- expect(1)
- val job = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
- finish(4) // will execute even after it was cancelled
- }
- expect(2)
- job.cancel()
- expect(3)
- }
-
+class AtomicCancellationTest : TestBase() {
@Test
fun testLockAtomicCancel() = runBlocking {
expect(1)
@@ -74,7 +50,7 @@
"OK"
}
}
- assertThat(result, IsEqual("OK"))
+ assertEquals("OK", result)
expect(5) // should execute despite cancellation
}
expect(3)
@@ -94,7 +70,7 @@
expect(4) // should execute despite cancellation
}
expect(3)
- assertThat(channel.receive(), IsEqual(42)) // will schedule sender for further execution
+ assertEquals(42, channel.receive()) // will schedule sender for further execution
job.cancel() // cancel the job next
yield() // now yield
finish(5)
@@ -112,11 +88,11 @@
"OK"
}
}
- assertThat(result, IsEqual("OK"))
+ assertEquals("OK", result)
expect(5) // should execute despite cancellation
}
expect(3)
- assertThat(channel.receive(), IsEqual(42)) // will schedule sender for further execution
+ assertEquals(42, channel.receive()) // will schedule sender for further execution
job.cancel() // cancel the job next
yield() // now yield
finish(6)
@@ -128,7 +104,7 @@
val channel = Channel<Int>()
val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
expect(2)
- assertThat(channel.receive(), IsEqual(42)) // suspends
+ assertEquals(42, channel.receive()) // suspends
expect(4) // should execute despite cancellation
}
expect(3)
@@ -146,12 +122,12 @@
expect(2)
val result = select<String> { // suspends
channel.onReceive {
- assertThat(it, IsEqual(42))
+ assertEquals(42, it)
expect(4)
"OK"
}
}
- assertThat(result, IsEqual("OK"))
+ assertEquals("OK", result)
expect(5) // should execute despite cancellation
}
expect(3)
@@ -162,45 +138,17 @@
}
@Test
- fun testDeferredAwaitCancellable() = runBlocking {
- expect(1)
- val deferred = async(coroutineContext) { // deferred, not yet complete
- expect(4)
- "OK"
- }
- assertThat(deferred.isCompleted, IsEqual(false))
- var job: Job? = null
- launch(coroutineContext) { // will cancel job as soon as deferred completes
- expect(5)
- assertThat(deferred.isCompleted, IsEqual(true))
- job!!.cancel()
- }
- job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
- expect(2)
- try {
- deferred.await() // suspends
- expectUnreached() // will not execute -- cancelled while dispatched
- } finally {
- finish(7) // but will execute finally blocks
- }
- }
- expect(3) // continues to execute when job suspends
- yield() // to deferred & canceller
- expect(6)
- }
-
- @Test
fun testSelectDeferredAwaitCancellable() = runBlocking {
expect(1)
val deferred = async(coroutineContext) { // deferred, not yet complete
expect(4)
"OK"
}
- assertThat(deferred.isCompleted, IsEqual(false))
+ assertEquals(false, deferred.isCompleted)
var job: Job? = null
launch(coroutineContext) { // will cancel job as soon as deferred completes
expect(5)
- assertThat(deferred.isCompleted, IsEqual(true))
+ assertEquals(true, deferred.isCompleted)
job!!.cancel()
}
job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
@@ -220,43 +168,16 @@
}
@Test
- fun testJobJoinCancellable() = runBlocking {
- expect(1)
- val jobToJoin = launch(coroutineContext) { // not yet complete
- expect(4)
- }
- assertThat(jobToJoin.isCompleted, IsEqual(false))
- var job: Job? = null
- launch(coroutineContext) { // will cancel job as soon as jobToJoin completes
- expect(5)
- assertThat(jobToJoin.isCompleted, IsEqual(true))
- job!!.cancel()
- }
- job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
- expect(2)
- try {
- jobToJoin.join() // suspends
- expectUnreached() // will not execute -- cancelled while dispatched
- } finally {
- finish(7) // but will execute finally blocks
- }
- }
- expect(3) // continues to execute when job suspends
- yield() // to jobToJoin & canceller
- expect(6)
- }
-
- @Test
fun testSelectJobJoinCancellable() = runBlocking {
expect(1)
val jobToJoin = launch(coroutineContext) { // not yet complete
expect(4)
}
- assertThat(jobToJoin.isCompleted, IsEqual(false))
+ assertEquals(false, jobToJoin.isCompleted)
var job: Job? = null
launch(coroutineContext) { // will cancel job as soon as jobToJoin completes
expect(5)
- assertThat(jobToJoin.isCompleted, IsEqual(true))
+ assertEquals(true, jobToJoin.isCompleted)
job!!.cancel()
}
job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
@@ -274,6 +195,4 @@
yield() // to jobToJoin & canceller
expect(6)
}
-
-
}
\ No newline at end of file
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutinesTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutinesTest.kt
index b9cc888..29a03e0 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutinesTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutinesTest.kt
@@ -1,6 +1,6 @@
package kotlinx.coroutines.experimental
-import org.junit.Test
+import kotlin.test.*
class CoroutinesTest : TestBase() {
@Test
@@ -18,25 +18,6 @@
}
@Test
- fun testNotCancellableChildWithExceptionCancelled() = runTest(
- expected = { it is TestException }
- ) {
- expect(1)
- // CoroutineStart.ATOMIC makes sure it will not get cancelled for it starts executing
- val d = async(coroutineContext, start = CoroutineStart.ATOMIC) {
- finish(4)
- throwTestException() // will throw
- expectUnreached()
- }
- expect(2)
- // now cancel with some other exception
- d.cancel(IllegalArgumentException())
- // now await to see how it got crashed -- IAE should have been suppressed by TestException
- expect(3)
- d.await()
- }
-
- @Test
fun testCancelManyCompletedAttachedChildren() = runTest {
val parent = launch(coroutineContext) { /* do nothing */ }
val n = 10_000 * stressTestMultiplier
@@ -51,8 +32,5 @@
private fun throwTestException(): Unit = throw TestException()
- private class TestException : Exception {
- constructor(message: String): super(message)
- constructor(): super()
- }
+ private class TestException() : Exception()
}
\ No newline at end of file
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithContextTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithContextTest.kt
index 29f240e..ee26c07 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithContextTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithContextTest.kt
@@ -16,9 +16,7 @@
package kotlinx.coroutines.experimental
-import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.core.IsEqual
-import org.junit.Test
+import kotlin.test.*
class WithContextTest : TestBase() {
@Test
@@ -28,7 +26,7 @@
expect(2)
"OK"
}
- assertThat(result, IsEqual("OK"))
+ assertEquals("OK", result)
finish(3)
}
@@ -41,7 +39,7 @@
expect(3)
"OK"
}
- assertThat(result, IsEqual("OK"))
+ assertEquals("OK", result)
finish(4)
}
}
\ No newline at end of file
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullTest.kt
index e4619b8..6c43f76 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullTest.kt
@@ -16,68 +16,9 @@
package kotlinx.coroutines.experimental
-import org.hamcrest.core.IsEqual
-import org.hamcrest.core.IsNull
-import org.junit.Assert.assertThat
-import org.junit.Test
-import java.io.IOException
+import kotlin.test.*
class WithTimeoutOrNullTest : TestBase() {
- /**
- * Tests a case of no timeout and no suspension inside.
- */
- @Test
- fun testBasicNoSuspend() = runTest {
- expect(1)
- val result = withTimeoutOrNull(10_000) {
- expect(2)
- "OK"
- }
- assertThat(result, IsEqual("OK"))
- finish(3)
- }
-
- /**
- * Tests a case of no timeout and one suspension inside.
- */
- @Test
- fun testBasicSuspend() = runTest {
- expect(1)
- val result = withTimeoutOrNull(10_000) {
- expect(2)
- yield()
- expect(3)
- "OK"
- }
- assertThat(result, IsEqual("OK"))
- finish(4)
- }
-
- /**
- * Tests property dispatching of `withTimeoutOrNull` blocks
- */
- @Test
- fun testDispatch() = runTest {
- expect(1)
- launch(coroutineContext) {
- expect(4)
- yield() // back to main
- expect(7)
- }
- expect(2)
- // test that it does not yield to the above job when started
- val result = withTimeoutOrNull(1000) {
- expect(3)
- yield() // yield only now
- expect(5)
- "OK"
- }
- assertThat(result, IsEqual("OK"))
- expect(6)
- yield() // back to launch
- finish(8)
- }
-
@Test
fun testNullOnTimeout() = runTest {
expect(1)
@@ -87,7 +28,7 @@
expectUnreached()
"OK"
}
- assertThat(result, IsNull())
+ assertEquals(null, result)
finish(3)
}
@@ -103,13 +44,13 @@
}
"OK"
}
- assertThat(result, IsNull())
+ assertEquals(null, result)
finish(4)
}
@Test
fun testSuppressExceptionWithAnotherException() = runTest(
- expected = { it is IOException }
+ expected = { it is TestException }
) {
expect(1)
val result = withTimeoutOrNull(100) {
@@ -118,7 +59,7 @@
delay(1000)
} catch (e: CancellationException) {
finish(3)
- throw IOException(e)
+ throw TestException()
}
expectUnreached()
"OK"
@@ -126,56 +67,8 @@
expectUnreached()
}
- /**
- * Tests that a 100% CPU-consuming loop will react on timeout if it has yields.
- */
@Test
- fun testYieldBlockingWithTimeout() = runBlocking {
- expect(1)
- val result = withTimeoutOrNull(100) {
- while (true) {
- yield()
- }
- }
- assertThat(result, IsNull())
- finish(2)
- }
-
- @Test(expected = CancellationException::class)
- fun testInnerTimeoutTest() = runBlocking {
- withTimeoutOrNull(200) {
- withTimeout(100) {
- while (true) {
- yield()
- }
- }
- expectUnreached() // will timeout
- }
- expectUnreached() // will timeout
- }
-
- @Test
- fun testOuterTimeoutTest() = runBlocking {
- var counter = 0
- val result = withTimeoutOrNull(250) {
- while (true) {
- val inner = withTimeoutOrNull(100) {
- while (true) {
- yield()
- }
- }
- assertThat(inner, IsNull())
- counter++
- }
- }
- assertThat(result, IsNull())
- // under load counter may be equal to 1, so the check is lenient here
- println("Executed: $counter times")
- check(counter in 1..2)
- }
-
- @Test
- fun testOuterTimeoutFiredBeforeInner() = runBlocking<Unit> {
+ fun testOuterTimeoutFiredBeforeInner() = runTest {
val result = withTimeoutOrNull(100) {
Thread.sleep(200) // wait enough for outer timeout to fire
withContext(NonCancellable) { yield() } // give an event loop a chance to run and process that cancellation
@@ -186,6 +79,8 @@
expectUnreached() // should not be reached, because it is outer timeout
}
// outer timeout results in null
- assertThat(result, IsNull())
+ assertEquals(null, result)
}
+
+ private class TestException : Exception()
}
\ No newline at end of file
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullThreadDispatchTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullThreadDispatchTest.kt
index 6ffb153..df1387c 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullThreadDispatchTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullThreadDispatchTest.kt
@@ -16,11 +16,7 @@
package kotlinx.coroutines.experimental
-import org.hamcrest.core.IsEqual
-import org.hamcrest.core.IsNull
-import org.junit.After
-import org.junit.Assert
-import org.junit.Test
+import kotlin.test.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory
@@ -30,7 +26,7 @@
class WithTimeoutOrNullThreadDispatchTest : TestBase() {
var executor: ExecutorService? = null
- @After
+ @AfterTest
fun tearDown() {
executor?.shutdown()
}
@@ -78,7 +74,7 @@
val dispatcher = factory(ThreadFactory { Thread(it).also { thread = it } })
withContext(dispatcher) {
expect(2)
- Assert.assertThat(Thread.currentThread(), IsEqual(thread))
+ assertEquals(thread, Thread.currentThread())
val result = withTimeoutOrNull(100) {
try {
expect(3)
@@ -86,12 +82,12 @@
expectUnreached()
} catch (e: CancellationException) {
expect(4)
- Assert.assertThat(Thread.currentThread(), IsEqual(thread))
+ assertEquals(thread, Thread.currentThread())
throw e // rethrow
}
}
- Assert.assertThat(Thread.currentThread(), IsEqual(thread))
- Assert.assertThat(result, IsNull())
+ assertEquals(thread, Thread.currentThread())
+ assertEquals(null, result)
expect(5)
}
finish(6)
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutTest.kt
index aa51f23..a793a1e 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutTest.kt
@@ -14,70 +14,14 @@
* limitations under the License.
*/
+@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
+
package kotlinx.coroutines.experimental
-import org.hamcrest.core.IsEqual
-import org.junit.Assert.assertThat
-import org.junit.Test
+import kotlin.test.*
import java.io.IOException
class WithTimeoutTest : TestBase() {
- /**
- * Tests a case of no timeout and no suspension inside.
- */
- @Test
- fun testBasicNoSuspend() = runTest {
- expect(1)
- val result = withTimeout(10_000) {
- expect(2)
- "OK"
- }
- assertThat(result, IsEqual("OK"))
- finish(3)
- }
-
- /**
- * Tests a case of no timeout and one suspension inside.
- */
- @Test
- fun testBasicSuspend() = runTest {
- expect(1)
- val result = withTimeout(10_000) {
- expect(2)
- yield()
- expect(3)
- "OK"
- }
- assertThat(result, IsEqual("OK"))
- finish(4)
- }
-
- /**
- * Tests proper dispatching of `withTimeout` blocks
- */
- @Test
- fun testDispatch() = runTest {
- expect(1)
- launch(coroutineContext) {
- expect(4)
- yield() // back to main
- expect(7)
- }
- expect(2)
- // test that it does not yield to the above job when started
- val result = withTimeout(1000) {
- expect(3)
- yield() // yield only now
- expect(5)
- "OK"
- }
- assertThat(result, IsEqual("OK"))
- expect(6)
- yield() // back to launch
- finish(8)
- }
-
-
@Test
fun testExceptionOnTimeout() = runTest {
expect(1)
@@ -89,7 +33,7 @@
"OK"
}
} catch (e: CancellationException) {
- assertThat(e.message, IsEqual("Timed out waiting for 100 MILLISECONDS"))
+ assertEquals("Timed out waiting for 100 MILLISECONDS", e.message)
finish(3)
}
}
@@ -129,34 +73,4 @@
}
expectUnreached()
}
-
- /**
- * Tests that a 100% CPU-consuming loop will react on timeout if it has yields.
- */
- @Test(expected = CancellationException::class)
- fun testYieldBlockingWithTimeout() = runTest {
- withTimeout(100) {
- while (true) {
- yield()
- }
- }
- }
-
- /**
- * Tests that [withTimeout] waits for children coroutines to complete.
- */
- @Test
- fun testWithTimeoutChildWait() = runTest {
- expect(1)
- withTimeout(100) {
- expect(2)
- // launch child with timeout
- launch(coroutineContext) {
- expect(4)
- }
- expect(3)
- // now will wait for child before returning
- }
- finish(5)
- }
}
\ No newline at end of file
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutThreadDispatchTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutThreadDispatchTest.kt
index 806a30c..8529b8c 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutThreadDispatchTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutThreadDispatchTest.kt
@@ -16,10 +16,7 @@
package kotlinx.coroutines.experimental
-import org.hamcrest.core.IsEqual
-import org.junit.After
-import org.junit.Assert
-import org.junit.Test
+import kotlin.test.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory
@@ -29,7 +26,7 @@
class WithTimeoutThreadDispatchTest : TestBase() {
var executor: ExecutorService? = null
- @After
+ @AfterTest
fun tearDown() {
executor?.shutdown()
}
@@ -76,7 +73,7 @@
val dispatcher = factory(ThreadFactory { Thread(it).also { thread = it } })
withContext(dispatcher) {
expect(2)
- Assert.assertThat(Thread.currentThread(), IsEqual(thread))
+ assertEquals(thread, Thread.currentThread())
try {
withTimeout(100) {
try {
@@ -85,13 +82,13 @@
expectUnreached()
} catch (e: CancellationException) {
expect(4)
- Assert.assertThat(Thread.currentThread(), IsEqual(thread))
+ assertEquals(thread, Thread.currentThread())
throw e // rethrow
}
}
} catch (e: CancellationException) {
expect(5)
- Assert.assertThat(Thread.currentThread(), IsEqual(thread))
+ assertEquals(thread, Thread.currentThread())
}
expect(6)
}