Skip to content

Commit 75c22c4

Browse files
svtkmerfemor
authored andcommitted
Added solutions for 'testing'
1 parent a6879e9 commit 75c22c4

File tree

4 files changed

+30
-40
lines changed

4 files changed

+30
-40
lines changed

test/tasks/Request4SuspendKtTest.kt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@ 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.currentTime
8+
import kotlinx.coroutines.test.runTest
79
import org.junit.Assert
810
import org.junit.Test
911

12+
@OptIn(ExperimentalCoroutinesApi::class)
1013
class Request4SuspendKtTest {
1114
@Test
12-
fun testSuspend() = runBlocking {
13-
val startTime = System.currentTimeMillis()
15+
fun testSuspend() = runTest {
16+
val startTime = currentTime
1417
val result = loadContributorsSuspend(MockGithubService, testRequestData)
1518
Assert.assertEquals("Wrong result for 'loadContributorsSuspend'", expectedResults.users, result)
16-
val totalTime = System.currentTimeMillis() - startTime
17-
/*
18-
// TODO: uncomment this assertion
19+
val totalTime = currentTime - startTime
1920
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)",
21+
"The calls run consequently," +
22+
"so the total virtual time should be 4000 ms: ",
2223
expectedResults.timeFromStart, totalTime
2324
)
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-
)
3025
}
3126
}

test/tasks/Request5ConcurrentKtTest.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@ 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.currentTime
8+
import kotlinx.coroutines.test.runTest
79
import org.junit.Assert
810
import org.junit.Test
911

12+
@OptIn(ExperimentalCoroutinesApi::class)
1013
class Request5ConcurrentKtTest {
1114
@Test
12-
fun testConcurrent() = runBlocking {
13-
val startTime = System.currentTimeMillis()
15+
fun testConcurrent() = runTest {
16+
val startTime = currentTime
1417
val result = loadContributorsConcurrent(MockGithubService, testRequestData)
1518
Assert.assertEquals("Wrong result for 'loadContributorsConcurrent'", expectedConcurrentResults.users, result)
16-
val totalTime = System.currentTimeMillis() - startTime
17-
/*
18-
// TODO: uncomment this assertion
19+
val totalTime = currentTime - startTime
1920
Assert.assertEquals(
2021
"The calls run concurrently, so the total virtual time should be 2200 ms: " +
2122
"1000 ms for repos request plus max(1000, 1200, 800) = 1200 ms for concurrent contributors requests)",
2223
expectedConcurrentResults.timeFromStart, totalTime
2324
)
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-
)
3025
}
3126
}

test/tasks/Request6ProgressKtTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ 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.currentTime
8+
import kotlinx.coroutines.test.runTest
79
import org.junit.Assert
810
import org.junit.Test
911

12+
@OptIn(ExperimentalCoroutinesApi::class)
1013
class Request6ProgressKtTest {
1114
@Test
12-
fun testProgress() = runBlocking {
13-
val startTime = System.currentTimeMillis()
15+
fun testProgress() = runTest {
16+
val startTime = currentTime
1417
var index = 0
1518
loadContributorsProgress(MockGithubService, testRequestData) {
1619
users, _ ->
1720
val expected = progressResults[index++]
18-
val time = System.currentTimeMillis() - startTime
19-
/*
20-
// TODO: uncomment this assertion
21+
val time = currentTime - startTime
2122
Assert.assertEquals("Expected intermediate result after virtual ${expected.timeFromStart} ms:",
2223
expected.timeFromStart, time)
23-
*/
2424
Assert.assertEquals("Wrong intermediate result after $time:", expected.users, users)
2525
}
2626
}

test/tasks/Request7ChannelsKtTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ 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.currentTime
8+
import kotlinx.coroutines.test.runTest
79
import org.junit.Assert
810
import org.junit.Test
911

12+
@OptIn(ExperimentalCoroutinesApi::class)
1013
class Request7ChannelsKtTest {
1114
@Test
12-
fun testChannels() = runBlocking {
13-
val startTime = System.currentTimeMillis()
15+
fun testChannels() = runTest {
16+
val startTime = currentTime
1417
var index = 0
1518
loadContributorsChannels(MockGithubService, testRequestData) {
1619
users, _ ->
1720
val expected = concurrentProgressResults[index++]
18-
val time = System.currentTimeMillis() - startTime
19-
/*
20-
// TODO: uncomment this assertion
21+
val time = currentTime - startTime
2122
Assert.assertEquals("Expected intermediate result after virtual ${expected.timeFromStart} ms:",
2223
expected.timeFromStart, time)
23-
*/
2424
Assert.assertEquals("Wrong intermediate result after $time:", expected.users, users)
2525
}
2626
}

0 commit comments

Comments
 (0)