Skip to content

Commit d9e4af9

Browse files
committed
Fixed PlayService task test
- Use kotlin.test - Avoid using OutOfMemoryError in test
1 parent e9224ac commit d9e4af9

File tree

1 file changed

+27
-29
lines changed
  • integration/kotlinx-coroutines-play-services/test

1 file changed

+27
-29
lines changed

integration/kotlinx-coroutines-play-services/test/TaskTest.kt

+27-29
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package kotlinx.coroutines.tasks
66

77
import com.google.android.gms.tasks.*
88
import kotlinx.coroutines.*
9-
import org.hamcrest.core.*
109
import org.junit.*
10+
import org.junit.Test
1111
import java.util.concurrent.locks.*
1212
import kotlin.concurrent.*
13+
import kotlin.test.*
1314

1415
class TaskTest : TestBase() {
1516
@Before
@@ -26,7 +27,7 @@ class TaskTest : TestBase() {
2627
}
2728
expect(3)
2829
val task = deferred.asTask()
29-
Assert.assertThat(task.await(), IsEqual("OK"))
30+
assertEquals("OK", task.await())
3031
finish(4)
3132
}
3233

@@ -39,7 +40,7 @@ class TaskTest : TestBase() {
3940
}
4041
expect(2)
4142
val task = deferred.asTask()
42-
Assert.assertThat(task.await(), IsEqual("OK"))
43+
assertEquals("OK", task.await())
4344
finish(4)
4445
}
4546

@@ -53,23 +54,20 @@ class TaskTest : TestBase() {
5354
try {
5455
runTest { task.await() }
5556
} catch (e: Exception) {
56-
Assert.assertTrue(e is CancellationException)
57-
Assert.assertTrue(task.isCanceled)
57+
assertTrue(e is CancellationException)
58+
assertTrue(task.isCanceled)
5859
}
5960
}
6061

6162
@Test
6263
fun testThrowingAsTask() {
6364
val deferred = GlobalScope.async {
64-
throw OutOfMemoryError()
65+
throw TestException("Fail")
6566
}
6667

6768
val task = deferred.asTask()
68-
try {
69-
runTest { task.await() }
70-
} catch (e: RuntimeExecutionException) {
71-
Assert.assertFalse(task.isSuccessful)
72-
Assert.assertTrue(e.cause?.cause is OutOfMemoryError)
69+
runTest(expected = { it is TestException }) {
70+
task.await()
7371
}
7472
}
7573

@@ -81,47 +79,47 @@ class TaskTest : TestBase() {
8179
lock.withLock { 42 }
8280
}.asDeferred()
8381

84-
Assert.assertFalse(deferred.isCompleted)
82+
assertFalse(deferred.isCompleted)
8583
lock.unlock()
8684

87-
Assert.assertEquals(42, deferred.await())
88-
Assert.assertTrue(deferred.isCompleted)
85+
assertEquals(42, deferred.await())
86+
assertTrue(deferred.isCompleted)
8987
}
9088

9189
@Test
9290
fun testTaskAsDeferred() = runTest {
9391
val deferred = Tasks.forResult(42).asDeferred()
94-
Assert.assertEquals(42, deferred.await())
92+
assertEquals(42, deferred.await())
9593
}
9694

9795
@Test
9896
fun testCancelledTaskAsDeferred() = runTest {
9997
val deferred = Tasks.forCanceled<Int>().asDeferred()
10098

101-
Assert.assertTrue(deferred.isCancelled)
99+
assertTrue(deferred.isCancelled)
102100
try {
103101
deferred.await()
104-
Assert.fail("deferred.await() should be cancelled")
102+
fail("deferred.await() should be cancelled")
105103
} catch (e: Exception) {
106-
Assert.assertTrue(e is CancellationException)
104+
assertTrue(e is CancellationException)
107105
}
108106
}
109107

110108
@Test
111109
fun testFailedTaskAsDeferred() = runTest {
112110
val deferred = Tasks.forException<Int>(TestException("something went wrong")).asDeferred()
113111

114-
Assert.assertTrue(deferred.isCancelled && deferred.isCompleted)
112+
assertTrue(deferred.isCancelled && deferred.isCompleted)
115113
val completionException = deferred.getCompletionExceptionOrNull()!!
116-
Assert.assertTrue(completionException is TestException)
117-
Assert.assertEquals("something went wrong", completionException.message)
114+
assertTrue(completionException is TestException)
115+
assertEquals("something went wrong", completionException.message)
118116

119117
try {
120118
deferred.await()
121-
Assert.fail("deferred.await() should throw an exception")
119+
fail("deferred.await() should throw an exception")
122120
} catch (e: Exception) {
123-
Assert.assertTrue(e is TestException)
124-
Assert.assertEquals("something went wrong", e.message)
121+
assertTrue(e is TestException)
122+
assertEquals("something went wrong", e.message)
125123
}
126124
}
127125

@@ -133,16 +131,16 @@ class TaskTest : TestBase() {
133131
lock.withLock { throw TestException("something went wrong") }
134132
}.asDeferred()
135133

136-
Assert.assertFalse(deferred.isCompleted)
134+
assertFalse(deferred.isCompleted)
137135
lock.unlock()
138136

139137
try {
140138
deferred.await()
141-
Assert.fail("deferred.await() should throw an exception")
139+
fail("deferred.await() should throw an exception")
142140
} catch (e: Exception) {
143-
Assert.assertTrue(e is TestException)
144-
Assert.assertEquals("something went wrong", e.message)
145-
Assert.assertSame(e, deferred.getCompletionExceptionOrNull())
141+
assertTrue(e is TestException)
142+
assertEquals("something went wrong", e.message)
143+
assertSame(e, deferred.getCompletionExceptionOrNull())
146144
}
147145
}
148146

0 commit comments

Comments
 (0)