Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure some functions run on the background thread. #9

Closed
levibostian opened this issue Aug 27, 2018 · 2 comments
Closed

Make sure some functions run on the background thread. #9

levibostian opened this issue Aug 27, 2018 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@levibostian
Copy link
Owner

I was creating integration tests the other day and when I tried to test observe() in an OnlineRepository subclass of mine, I was having a lot of exceptions.

Here is a gist of my test:

    @Test
    fun observeCachedData_data() {
        val foo = listOf(
                FooModel(id = 1)
        )
        insertFoo(foo)

        val body = GetFooResponse("", listOf())
        val result = Result.response(Response.success(body))
        `when`(service.getFoo(null)).thenReturn(
                Single.just(result).subscribeOn(Schedulers.io()) // Run on background thread as a DB save is triggered.
        )
        `when`(responseProcessor.process(result)).thenReturn(ResponseProcessor.ProcessedResult<GetFooResponse>(null, "", body))

        repository.loadDataRequirements = FooRepository.GetRequirements(null)
        repository.observe()
                .subscribeOn(Schedulers.io())
                .test()
                .awaitDone(500, TimeUnit.MILLISECONDS)
                .assertValue {
                    it.data!!.size == 1
                }
    }

Looking above, I was able to fix my test with the line: Single.just(result).subscribeOn(Schedulers.io()) // Run on background thread as a DB save is triggered. from my mock. I had to make sure that fetchFreshData() was called on a background thread. Why? Because in the OnlineRepository source code I notice that the thread that is subscribed on in fetchFreshData() determines what thread saveData() is called on.

This is not right. A subclass of OnlineRepository should always know what thread something is executed on.

@levibostian
Copy link
Owner Author

For this issue, I will be following the same patterns that Teller-iOS is using. The guarantee that certain functions of an OnlineRepository subclass are called by certain threads.

@levibostian levibostian added this to the 0.1.0-alpha milestone Feb 8, 2019
@levibostian levibostian added the enhancement New feature or request label Feb 8, 2019
@levibostian
Copy link
Owner Author

Change added to this branch. Will be merged in soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant