Skip to content

Commit

Permalink
Added solutions for 'testing'
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Dec 29, 2020
1 parent 06b24a7 commit 419805f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
22 changes: 8 additions & 14 deletions test/tasks/Request4SuspendKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@ package tasks
import contributors.MockGithubService
import contributors.expectedResults
import contributors.testRequestData
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class Request4SuspendKtTest {
@Test
fun testSuspend() = runBlocking {
val startTime = System.currentTimeMillis()
fun testSuspend() = runBlockingTest {
val startTime = currentTime
val result = loadContributorsSuspend(MockGithubService, testRequestData)
Assert.assertEquals("Wrong result for 'loadContributorsSuspend'", expectedResults.users, result)
val totalTime = System.currentTimeMillis() - startTime
/*
// TODO: uncomment this assertion
val totalTime = currentTime - startTime
Assert.assertEquals(
"The calls run consequently, so the total virtual time should be 4000 ms: " +
"1000 for repos request plus (1000 + 1200 + 800) = 3000 for sequential contributors requests)",
"The calls run consequently," +
"so the total virtual time should be 4000 ms: ",
expectedResults.timeFromStart, totalTime
)
*/
Assert.assertTrue(
"The calls run consequently, so the total time should be around 4000 ms: " +
"1000 for repos request plus (1000 + 1200 + 800) = 3000 for sequential contributors requests)",
totalTime in expectedResults.timeFromStart..(expectedResults.timeFromStart + 500)
)
}
}
18 changes: 6 additions & 12 deletions test/tasks/Request5ConcurrentKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@ package tasks
import contributors.MockGithubService
import contributors.expectedConcurrentResults
import contributors.testRequestData
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class Request5ConcurrentKtTest {
@Test
fun testConcurrent() = runBlocking {
val startTime = System.currentTimeMillis()
fun testConcurrent() = runBlockingTest {
val startTime = currentTime
val result = loadContributorsConcurrent(MockGithubService, testRequestData)
Assert.assertEquals("Wrong result for 'loadContributorsConcurrent'", expectedConcurrentResults.users, result)
val totalTime = System.currentTimeMillis() - startTime
/*
// TODO: uncomment this assertion
val totalTime = currentTime - startTime
Assert.assertEquals(
"The calls run concurrently, so the total virtual time should be 2200 ms: " +
"1000 ms for repos request plus max(1000, 1200, 800) = 1200 ms for concurrent contributors requests)",
expectedConcurrentResults.timeFromStart, totalTime
)
*/
Assert.assertTrue(
"The calls run concurrently, so the total virtual time should be 2200 ms: " +
"1000 ms for repos request plus max(1000, 1200, 800) = 1200 ms for concurrent contributors requests)",
totalTime in expectedConcurrentResults.timeFromStart..(expectedConcurrentResults.timeFromStart + 500)
)
}
}
13 changes: 6 additions & 7 deletions test/tasks/Request6ProgressKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ package tasks
import contributors.MockGithubService
import contributors.progressResults
import contributors.testRequestData
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class Request6ProgressKtTest {
@Test
fun testProgress() = runBlocking {
val startTime = System.currentTimeMillis()
fun testProgress() = runBlockingTest {
val startTime = currentTime
var index = 0
loadContributorsProgress(MockGithubService, testRequestData) {
users, _ ->
val expected = progressResults[index++]
val time = System.currentTimeMillis() - startTime
/*
// TODO: uncomment this assertion
val time = currentTime - startTime
Assert.assertEquals("Expected intermediate result after virtual ${expected.timeFromStart} ms:",
expected.timeFromStart, time)
*/
Assert.assertEquals("Wrong intermediate result after $time:", expected.users, users)
}
}
Expand Down
13 changes: 6 additions & 7 deletions test/tasks/Request7ChannelsKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ package tasks
import contributors.MockGithubService
import contributors.concurrentProgressResults
import contributors.testRequestData
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class Request7ChannelsKtTest {
@Test
fun testChannels() = runBlocking {
val startTime = System.currentTimeMillis()
fun testChannels() = runBlockingTest {
val startTime = currentTime
var index = 0
loadContributorsChannels(MockGithubService, testRequestData) {
users, _ ->
val expected = concurrentProgressResults[index++]
val time = System.currentTimeMillis() - startTime
/*
// TODO: uncomment this assertion
val time = currentTime - startTime
Assert.assertEquals("Expected intermediate result after virtual ${expected.timeFromStart} ms:",
expected.timeFromStart, time)
*/
Assert.assertEquals("Wrong intermediate result after $time:", expected.users, users)
}
}
Expand Down

0 comments on commit 419805f

Please sign in to comment.