Skip to content

Commit

Permalink
Merge pull request #4880 from sjudd:dont_launch_flow_clear
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 469917836
  • Loading branch information
glide-copybara-robot committed Aug 25, 2022
2 parents 13ca8bc + aee590a commit 2be5834
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ private fun <ResourceT : Any> RequestBuilder<ResourceT>.flow(
val target = FlowTarget(this, size)
requestBuilder.intoDirect(target)
awaitClose {
launch {
requestManager.clear(target)
}
requestManager.clear(target)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.Key
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.engine.cache.MemoryCache
import com.bumptech.glide.load.engine.executor.GlideExecutor
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.google.common.truth.Correspondence
import com.google.common.truth.IterableSubject
import com.google.common.truth.Truth.assertThat
import java.io.File
import java.util.concurrent.atomic.AtomicReference
import kotlin.reflect.KClass
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -336,6 +339,35 @@ class FlowsTest {
)
}

@Test
fun flow_onClose_clearsTarget() = runTest {
val inCache = AtomicReference<com.bumptech.glide.load.engine.Resource<*>?>()
Glide.init(context, GlideBuilder().setMemoryCache(object : MemoryCache {
override fun getCurrentSize(): Long = 0
override fun getMaxSize(): Long = 0
override fun setSizeMultiplier(multiplier: Float) {}
override fun remove(key: Key): com.bumptech.glide.load.engine.Resource<*>? {return null}
override fun setResourceRemovedListener(listener: MemoryCache.ResourceRemovedListener) {}
override fun clearMemory() {}
override fun trimMemory(level: Int) {}

override fun put(
key: Key,
resource: com.bumptech.glide.load.engine.Resource<*>?,
): com.bumptech.glide.load.engine.Resource<*>? {
inCache.set(resource)
return null
}
}))
val data = Glide.with(context)
.load(newImageFile())
.flow(100, 100)
.firstLoad()
.toList()
assertThat(data).isNotEmpty()
assertThat(inCache.get()).isNotNull()
}

// Avoid race conditions where the main request finishes first by making sure they execute
// sequentially using a single threaded executor.
private fun makeGlideSingleThreadedToOrderThumbnailRequests() {
Expand Down

0 comments on commit 2be5834

Please sign in to comment.