-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use onIdle to avoid a race in FlowTests
Glide's executor thread notifies the flow in a loop while holding a lock on the resource. When it finishes, it releases the lock. If the flow wins the race and runs before Glide's executor releases the lock, the resource will not be recycled in the remainder of the test method. If the executor wins the race and releases the lock first, then the resource will be recycled in the rest of the test method. To fix this race, I've used Espresso's onIdle and idling resources, similar to the compose rule.
- Loading branch information
Showing
4 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...tion/ktx/src/test/java/com/bumptech/glide/load/engine/executor/GlideIdlingResourceInit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.bumptech.glide.load.engine.executor | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.espresso.IdlingRegistry | ||
import androidx.test.espresso.idling.concurrent.IdlingThreadPoolExecutor | ||
import com.bumptech.glide.Glide | ||
import com.bumptech.glide.GlideBuilder | ||
import java.util.concurrent.LinkedBlockingQueue | ||
import java.util.concurrent.TimeUnit | ||
|
||
object GlideIdlingResources { | ||
|
||
fun initGlide(builder: GlideBuilder? = null) { | ||
val registry = IdlingRegistry.getInstance() | ||
val executor = | ||
IdlingThreadPoolExecutor( | ||
"glide_test_thread", | ||
/* corePoolSize = */ 1, | ||
/* maximumPoolSize = */ 1, | ||
/* keepAliveTime = */ 1, | ||
TimeUnit.SECONDS, | ||
LinkedBlockingQueue() | ||
) { | ||
Thread(it) | ||
} | ||
val glideExecutor = GlideExecutor(executor) | ||
Glide.init( | ||
ApplicationProvider.getApplicationContext(), | ||
(builder ?: GlideBuilder()) | ||
.setSourceExecutor(glideExecutor) | ||
.setAnimationExecutor(glideExecutor) | ||
.setDiskCacheExecutor(glideExecutor) | ||
) | ||
registry.register(executor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters