2323import com .bumptech .glide .load .engine .bitmap_recycle .LruBitmapPool ;
2424import com .bumptech .glide .load .engine .cache .LruResourceCache ;
2525import com .bumptech .glide .load .engine .cache .MemoryCacheAdapter ;
26+ import com .bumptech .glide .load .engine .executor .GlideExecutor ;
27+ import com .bumptech .glide .load .engine .executor .MockGlideExecutor ;
2628import com .bumptech .glide .request .RequestListener ;
2729import com .bumptech .glide .request .target .Target ;
2830import com .bumptech .glide .test .ConcurrencyHelper ;
@@ -45,18 +47,31 @@ public class LoadBitmapTest {
4547
4648 private final ConcurrencyHelper concurrency = new ConcurrencyHelper ();
4749 private Context context ;
50+ private GlideBuilder glideBuilder ;
4851
4952 @ Before
5053 public void setUp () {
5154 MockitoAnnotations .initMocks (this );
5255 context = InstrumentationRegistry .getTargetContext ();
56+
57+ // Clearing the future here can race with clearing the EngineResource held on to by EngineJob
58+ // while it's notifying callbacks. Forcing all executors to use the same thread avoids the race
59+ // by making our clear and EngineJob's clear run on the same thread.
60+ GlideExecutor mainThreadExecutor = MockGlideExecutor .newMainThreadExecutor ();
61+ glideBuilder =
62+ new GlideBuilder ()
63+ .setSourceExecutor (mainThreadExecutor )
64+ .setDiskCacheExecutor (mainThreadExecutor )
65+ .setAnimationExecutor (mainThreadExecutor );
5366 }
5467
5568 @ Test
5669 public void clearFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBitmap () {
57- Glide .init (context , new GlideBuilder ()
58- .setMemoryCache (new MemoryCacheAdapter ())
59- .setBitmapPool (new BitmapPoolAdapter ()));
70+ Glide .init (
71+ context ,
72+ new GlideBuilder ()
73+ .setMemoryCache (new MemoryCacheAdapter ())
74+ .setBitmapPool (new BitmapPoolAdapter ()));
6075 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
6176 Target <Drawable > target =
6277 concurrency .wait (
@@ -74,9 +89,11 @@ public void clearFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBi
7489
7590 @ Test
7691 public void transformFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBitmap () {
77- Glide .init (context , new GlideBuilder ()
78- .setMemoryCache (new MemoryCacheAdapter ())
79- .setBitmapPool (new BitmapPoolAdapter ()));
92+ Glide .init (
93+ context ,
94+ new GlideBuilder ()
95+ .setMemoryCache (new MemoryCacheAdapter ())
96+ .setBitmapPool (new BitmapPoolAdapter ()));
8097 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
8198 concurrency .wait (
8299 GlideApp .with (context )
@@ -90,9 +107,11 @@ public void transformFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecyc
90107
91108 @ Test
92109 public void clearFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap () {
93- Glide .init (context , new GlideBuilder ()
94- .setMemoryCache (new MemoryCacheAdapter ())
95- .setBitmapPool (new BitmapPoolAdapter ()));
110+ Glide .init (
111+ context ,
112+ new GlideBuilder ()
113+ .setMemoryCache (new MemoryCacheAdapter ())
114+ .setBitmapPool (new BitmapPoolAdapter ()));
96115 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
97116 Target <Drawable > target =
98117 concurrency .wait (
@@ -109,9 +128,11 @@ public void clearFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap() {
109128
110129 @ Test
111130 public void transformFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap () {
112- Glide .init (context , new GlideBuilder ()
113- .setMemoryCache (new MemoryCacheAdapter ())
114- .setBitmapPool (new BitmapPoolAdapter ()));
131+ Glide .init (
132+ context ,
133+ new GlideBuilder ()
134+ .setMemoryCache (new MemoryCacheAdapter ())
135+ .setBitmapPool (new BitmapPoolAdapter ()));
115136 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
116137 concurrency .wait (
117138 GlideApp .with (context )
@@ -124,9 +145,11 @@ public void transformFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap()
124145
125146 @ Test
126147 public void clearFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitmap () {
127- Glide .init (context , new GlideBuilder ()
128- .setMemoryCache (new MemoryCacheAdapter ())
129- .setBitmapPool (new BitmapPoolAdapter ()));
148+ Glide .init (
149+ context ,
150+ new GlideBuilder ()
151+ .setMemoryCache (new MemoryCacheAdapter ())
152+ .setBitmapPool (new BitmapPoolAdapter ()));
130153 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
131154 Target <Bitmap > target =
132155 concurrency .wait (
@@ -144,9 +167,11 @@ public void clearFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitm
144167
145168 @ Test
146169 public void transformFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitmap () {
147- Glide .init (context , new GlideBuilder ()
148- .setMemoryCache (new MemoryCacheAdapter ())
149- .setBitmapPool (new BitmapPoolAdapter ()));
170+ Glide .init (
171+ context ,
172+ new GlideBuilder ()
173+ .setMemoryCache (new MemoryCacheAdapter ())
174+ .setBitmapPool (new BitmapPoolAdapter ()));
150175 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
151176 concurrency .wait (
152177 GlideApp .with (context )
@@ -161,9 +186,11 @@ public void transformFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycle
161186 @ Test
162187 public void loadFromRequestManager_withBitmap_doesNotLoadFromDiskCache () {
163188 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
164- Glide .init (context , new GlideBuilder ()
165- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
166- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
189+ Glide .init (
190+ context ,
191+ glideBuilder
192+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
193+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
167194 Target <Drawable > target =
168195 concurrency .wait (
169196 GlideApp .with (context )
@@ -200,9 +227,11 @@ public void run() {
200227 @ Test
201228 public void loadFromRequestBuilder_asDrawable_withBitmap_doesNotLoadFromDiskCache () {
202229 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
203- Glide .init (context , new GlideBuilder ()
204- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
205- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
230+ Glide .init (
231+ context ,
232+ glideBuilder
233+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
234+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
206235 Target <Drawable > target =
207236 concurrency .wait (
208237 GlideApp .with (context )
@@ -240,9 +269,11 @@ public void run() {
240269 @ Test
241270 public void loadFromRequestBuilder_asDrawable_withBitmapAndStrategyBeforeLoad_notFromCache () {
242271 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
243- Glide .init (context , new GlideBuilder ()
244- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
245- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
272+ Glide .init (
273+ context ,
274+ glideBuilder
275+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
276+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
246277 Target <Drawable > target =
247278 concurrency .wait (
248279 GlideApp .with (context )
@@ -281,9 +312,11 @@ public void run() {
281312 @ Test
282313 public void loadFromRequestBuilder_asBitmap_withBitmap_doesNotLoadFromDiskCache () {
283314 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
284- Glide .init (context , new GlideBuilder ()
285- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
286- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
315+ Glide .init (
316+ context ,
317+ glideBuilder
318+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
319+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
287320 Target <Bitmap > target =
288321 concurrency .wait (
289322 GlideApp .with (context )
@@ -322,9 +355,12 @@ public void run() {
322355 @ Test
323356 public void loadFromRequestBuilder_asBitmap_withBitmapAndStrategyBeforeLoad_notFromCache () {
324357 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), ResourceIds .raw .canonical );
325- Glide .init (context , new GlideBuilder ()
326- .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
327- .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
358+ Glide .init (
359+ context ,
360+ glideBuilder
361+ .setMemoryCache (new LruResourceCache (Util .getBitmapByteSize (bitmap ) * 10 ))
362+ .setBitmapPool (new LruBitmapPool (Util .getBitmapByteSize (bitmap ) * 10 )));
363+
328364 Target <Bitmap > target =
329365 concurrency .wait (
330366 GlideApp .with (context )
0 commit comments