Run downloadOnly () synchronously on v4 causes a Thread weird error
Glide Version: 4.7.1
Integration libraries: OkHttp3
Device/Android Version: N/A (Unit tests with Robolectric)
Issue details / Repro steps / Use case background:
I've created an ImageDownloader class that uses Glide to download and cache some images in background using Rx:
class ImageDownloader
@Inject constructor(
private val requestManager: RequestManager
) {
fun download(questions: List<Question>): Observable<Unit> {
return questions.filter { it.hasImage }
.toObservable()
.map { it.imageUrl }
.flatMap { createImageRequest(it) }
.map { }
}
private fun createImageRequest(imageUrl: String): Observable<File> {
return Observable.fromFuture(
requestManager
.downloadOnly()
.load(imageUrl)
.submit()
)
}
}
So, I've draft an unit test to assert that images are in the app cache, using Robolectric:
@RunWith(RobolectricTestRunner::class)
class QuestionImageDownloaderTest {
private val application = RuntimeEnvironment.application
private val subject = QuestionImageDownloader(Glide.with(application))
private val dependencies by lazy { Dependencies() }
@Test
fun `when download images, its files should be on the internal storage`() {
subject.download(dependencies.questions).blockingLast()
val cacheDir = Glide.getPhotoCacheDir(application)!!
cacheDir.listFiles().forEach { println(it.path) }
}
private class Dependencies {
val questions = listOf(
Question(imageUrl = "https://cdn.pixabay.com/photo/2017/05/09/21/49/gecko-2299365_960_720.jpg"),
Question(imageUrl = "https://cdn.pixabay.com/photo/2016/10/11/18/34/black-1732419_960_720.png")
)
}
}
I expected to get a list of two file paths printed on the terminal, but instead I got the following crash:
java.lang.IllegalArgumentException: You must call this method on a background thread
at com.bumptech.glide.util.Util.assertBackgroundThread(Util.java:151)
at com.bumptech.glide.request.RequestFutureTarget.doGet(RequestFutureTarget.java:187)
at com.bumptech.glide.request.RequestFutureTarget.get(RequestFutureTarget.java:115)
at io.reactivex.internal.operators.observable.ObservableFromFuture.subscribeActual(ObservableFromFuture.java:41)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.subscribeInner(ObservableFlatMap.java:162)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(ObservableFlatMap.java:139)
at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:64)
at io.reactivex.internal.operators.observable.ObservableFromIterable$FromIterableDisposable.run(ObservableFromIterable.java:98)
at io.reactivex.internal.operators.observable.ObservableFromIterable.subscribeActual(ObservableFromIterable.java:58)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableFlatMap.subscribeActual(ObservableFlatMap.java:55)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.Observable.blockingLast(Observable.java:5121)
at com.appprova.data.mock.QuestionImageDownloaderTest.when download images, its files should be on the internal storage(QuestionImageDownloaderTest.kt:26)
Well... Ok... It's running on Android Main Thread simulated by Robolectric. So, maybe if I tell Robolectric that Main Thread is another Thread, it could work. I changed the Main Thread with RuntimeEnvironment.setMainThread(Thread()).
@Test
fun `when download images, its files should be on the internal storage`() {
RuntimeEnvironment.setMainThread(Thread())
subject.download(dependencies.questions).blockingLast()
val cacheDir = Glide.getPhotoCacheDir(application)!!
cacheDir.listFiles().forEach { println(it.path) }
}
And then, I had this another weird error:
java.lang.IllegalArgumentException: You must call this method on the main thread
at com.bumptech.glide.util.Util.assertMainThread(Util.java:142)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:594)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:587)
at com.bumptech.glide.RequestBuilder$1.run(RequestBuilder.java:747)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:141)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:32)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:120)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:319)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:156)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:145)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:127)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.enqueueMessage(Handler.java:662)
at android.os.Handler.sendMessageAtTime(Handler.java:631)
at android.os.Handler.sendMessageDelayed(Handler.java:601)
at android.os.Handler.post(Handler.java:357)
at com.bumptech.glide.RequestBuilder.submit(RequestBuilder.java:743)
at com.bumptech.glide.RequestBuilder.submit(RequestBuilder.java:722)
at com.appprova.data.mock.QuestionImageDownloader.createImageRequest(QuestionImageDownloader.kt:28)
at com.appprova.data.mock.QuestionImageDownloader.access$createImageRequest(QuestionImageDownloader.kt:10)
at com.appprova.data.mock.QuestionImageDownloader$download$3.apply(QuestionImageDownloader.kt:19)
at com.appprova.data.mock.QuestionImageDownloader$download$3.apply(QuestionImageDownloader.kt:10)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(ObservableFlatMap.java:121)
at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:64)
at io.reactivex.internal.operators.observable.ObservableFromIterable$FromIterableDisposable.run(ObservableFromIterable.java:98)
at io.reactivex.internal.operators.observable.ObservableFromIterable.subscribeActual(ObservableFromIterable.java:58)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableFlatMap.subscribeActual(ObservableFlatMap.java:55)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
at io.reactivex.Observable.subscribe(Observable.java:11442)
at io.reactivex.Observable.blockingLast(Observable.java:5121)
at com.appprova.data.mock.QuestionImageDownloaderTest.when download images, its files should be on the internal storage(QuestionImageDownloaderTest.kt:28)
So, I started to analyze RequestBuilder.into() function, and I find out some Thread assertions that are causing these errors.
I found on Glide V3 dos that it is possible to run the download synchronously, and this is what I want, because it will be executed on a Scheduler.io().
It is not possible on V4? How could I run it synchronously on my unit test?
You can do this with emulator tests where your test runs in a different thread than the application under test. Since you're writing what is more or less an integration test it might be worth considering.
For Robolectric it's a little more tricky. Right now Glide relies on posting results back to the UI thread when they finish, so it's not safe to start a background thread, call get(), and then block the main thread waiting for the background thread.
Glide does have some mocked synchronous tests, which might be helpful, see: https://github.com/bumptech/glide/blob/2dccb5c2fb265cbfa67b328cea30e922c67feb9e/library/test/src/test/java/com/bumptech/glide/GlideTest.java#L318.
For emulator tests, see: https://github.com/bumptech/glide/blob/2dccb5c2fb265cbfa67b328cea30e922c67feb9e/instrumentation/src/androidTest/java/com/bumptech/glide/LoadBitmapTest.java#L41
This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.
Hi, @sjudd! Thank you for your answer, and sorry for the late reply...
Sadly I did not success with mocked synchronous approach, and by now we have no emulator tests.
I think that I could not understand very well what is the scope of GlideTest.java that can be useful for my synchronous tests.
In particular, see: https://github.com/bumptech/glide/blob/2dccb5c2fb265cbfa67b328cea30e922c67feb9e/library/test/src/test/java/com/bumptech/glide/GlideTest.java#L127