Skip to content

Commit 419805f

Browse files
committed
Added solutions for 'testing'
1 parent 06b24a7 commit 419805f

4 files changed

+26
-40
lines changed

test/tasks/Request4SuspendKtTest.kt

+8-14
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,23 @@ package tasks
33
import contributors.MockGithubService
44
import contributors.expectedResults
55
import contributors.testRequestData
6-
import kotlinx.coroutines.runBlocking
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import kotlinx.coroutines.test.runBlockingTest
78
import org.junit.Assert
89
import org.junit.Test
910

11+
@OptIn(ExperimentalCoroutinesApi::class)
1012
class Request4SuspendKtTest {
1113
@Test
12-
fun testSuspend() = runBlocking {
13-
val startTime = System.currentTimeMillis()
14+
fun testSuspend() = runBlockingTest {
15+
val startTime = currentTime
1416
val result = loadContributorsSuspend(MockGithubService, testRequestData)
1517
Assert.assertEquals("Wrong result for 'loadContributorsSuspend'", expectedResults.users, result)
16-
val totalTime = System.currentTimeMillis() - startTime
17-
/*
18-
// TODO: uncomment this assertion
18+
val totalTime = currentTime - startTime
1919
Assert.assertEquals(
20-
"The calls run consequently, so the total virtual time should be 4000 ms: " +
21-
"1000 for repos request plus (1000 + 1200 + 800) = 3000 for sequential contributors requests)",
20+
"The calls run consequently," +
21+
"so the total virtual time should be 4000 ms: ",
2222
expectedResults.timeFromStart, totalTime
2323
)
24-
*/
25-
Assert.assertTrue(
26-
"The calls run consequently, so the total time should be around 4000 ms: " +
27-
"1000 for repos request plus (1000 + 1200 + 800) = 3000 for sequential contributors requests)",
28-
totalTime in expectedResults.timeFromStart..(expectedResults.timeFromStart + 500)
29-
)
3024
}
3125
}

test/tasks/Request5ConcurrentKtTest.kt

+6-12
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,23 @@ package tasks
33
import contributors.MockGithubService
44
import contributors.expectedConcurrentResults
55
import contributors.testRequestData
6-
import kotlinx.coroutines.runBlocking
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import kotlinx.coroutines.test.runBlockingTest
78
import org.junit.Assert
89
import org.junit.Test
910

11+
@OptIn(ExperimentalCoroutinesApi::class)
1012
class Request5ConcurrentKtTest {
1113
@Test
12-
fun testConcurrent() = runBlocking {
13-
val startTime = System.currentTimeMillis()
14+
fun testConcurrent() = runBlockingTest {
15+
val startTime = currentTime
1416
val result = loadContributorsConcurrent(MockGithubService, testRequestData)
1517
Assert.assertEquals("Wrong result for 'loadContributorsConcurrent'", expectedConcurrentResults.users, result)
16-
val totalTime = System.currentTimeMillis() - startTime
17-
/*
18-
// TODO: uncomment this assertion
18+
val totalTime = currentTime - startTime
1919
Assert.assertEquals(
2020
"The calls run concurrently, so the total virtual time should be 2200 ms: " +
2121
"1000 ms for repos request plus max(1000, 1200, 800) = 1200 ms for concurrent contributors requests)",
2222
expectedConcurrentResults.timeFromStart, totalTime
2323
)
24-
*/
25-
Assert.assertTrue(
26-
"The calls run concurrently, so the total virtual time should be 2200 ms: " +
27-
"1000 ms for repos request plus max(1000, 1200, 800) = 1200 ms for concurrent contributors requests)",
28-
totalTime in expectedConcurrentResults.timeFromStart..(expectedConcurrentResults.timeFromStart + 500)
29-
)
3024
}
3125
}

test/tasks/Request6ProgressKtTest.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@ package tasks
33
import contributors.MockGithubService
44
import contributors.progressResults
55
import contributors.testRequestData
6-
import kotlinx.coroutines.runBlocking
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import kotlinx.coroutines.test.runBlockingTest
78
import org.junit.Assert
89
import org.junit.Test
910

11+
@OptIn(ExperimentalCoroutinesApi::class)
1012
class Request6ProgressKtTest {
1113
@Test
12-
fun testProgress() = runBlocking {
13-
val startTime = System.currentTimeMillis()
14+
fun testProgress() = runBlockingTest {
15+
val startTime = currentTime
1416
var index = 0
1517
loadContributorsProgress(MockGithubService, testRequestData) {
1618
users, _ ->
1719
val expected = progressResults[index++]
18-
val time = System.currentTimeMillis() - startTime
19-
/*
20-
// TODO: uncomment this assertion
20+
val time = currentTime - startTime
2121
Assert.assertEquals("Expected intermediate result after virtual ${expected.timeFromStart} ms:",
2222
expected.timeFromStart, time)
23-
*/
2423
Assert.assertEquals("Wrong intermediate result after $time:", expected.users, users)
2524
}
2625
}

test/tasks/Request7ChannelsKtTest.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@ package tasks
33
import contributors.MockGithubService
44
import contributors.concurrentProgressResults
55
import contributors.testRequestData
6-
import kotlinx.coroutines.runBlocking
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import kotlinx.coroutines.test.runBlockingTest
78
import org.junit.Assert
89
import org.junit.Test
910

11+
@OptIn(ExperimentalCoroutinesApi::class)
1012
class Request7ChannelsKtTest {
1113
@Test
12-
fun testChannels() = runBlocking {
13-
val startTime = System.currentTimeMillis()
14+
fun testChannels() = runBlockingTest {
15+
val startTime = currentTime
1416
var index = 0
1517
loadContributorsChannels(MockGithubService, testRequestData) {
1618
users, _ ->
1719
val expected = concurrentProgressResults[index++]
18-
val time = System.currentTimeMillis() - startTime
19-
/*
20-
// TODO: uncomment this assertion
20+
val time = currentTime - startTime
2121
Assert.assertEquals("Expected intermediate result after virtual ${expected.timeFromStart} ms:",
2222
expected.timeFromStart, time)
23-
*/
2423
Assert.assertEquals("Wrong intermediate result after $time:", expected.users, users)
2524
}
2625
}

0 commit comments

Comments
 (0)