Testing for an edge case

So now it's time to throw away our assumptions and write a mock DataSource that will retrieve from an external application faster than from the DB. Let's add another implementation of Datasource in the same SampleAppFT. We will call it MockSlowDbDataSource for lack of a better name:

// Mock a datasource that retrieves the data in a different order
class MockSlowDbDataSource: DataSource {
// Mock getting the name from the database
override fun getNameAsync(id: Int) = async {
delay(1000)
"Susan Calvin"
}

// Mock getting the age from the cache
override fun getAgeAsync(id: Int) = async {
delay(500)
Calendar.getInstance().get(Calendar.YEAR) - 1982
}

// Mock getting the profession from an external system
override fun getProfessionAsync(id: Int) = async {
delay(200)
"Robopsychologist"
}
}

We will complete our second test using it:

@Test
fun testOppositeOrder() = runBlocking {
val manager = UserManager(MockSlowDbDataSource())

val user = manager.getUser(10)
assertTrue { user.name == "Susan Calvin" }
assertTrue { user.age == Calendar.getInstance().get(Calendar.YEAR) - 1982 }
assertTrue { user.profession == "Robopsychologist" }
}

If we run this test, we will make the application crash:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.129.194