Skip to content

Commit

Permalink
Close stream instead of dispose and fix unit test (#3705)
Browse files Browse the repository at this point in the history
* Close stream instead of dispose and fix unit test

* - cleanup
  • Loading branch information
PureWeen authored Dec 8, 2021
1 parent a1e46d7 commit b1f0687
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/Core/src/ImageSources/Android/Glide/PassThroughModelLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bumptech.Glide.Load.Data;
using Bumptech.Glide.Load.Model;
using Bumptech.Glide.Signature;
using Java.IO;

namespace Microsoft.Maui.BumptechGlide
{
Expand Down Expand Up @@ -32,8 +33,17 @@ public DataFetcher(Java.Lang.Object model)

public void Cancel() { }

public void Cleanup() =>
_model?.Dispose();
public void Cleanup()
{
if (_model is InputStream inputStream)
{
try
{
inputStream.Close();
}
catch (IOException) { }
}
}

public void LoadData(Priority priority, IDataFetcherDataCallback callback) =>
callback.OnDataReady(_model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,34 @@ public async Task CustomReleasingImageSourceReturnsDifferentBitmap()
result2.Dispose();
}

static async Task<bool> TryCollectFile(string bitmapFile)
async Task<bool> TryCollectFile(string bitmapFile)
{
var collected = false;

for (var i = 0; i < GCCollectRetries; i++)
for (var i = 0; i < GCCollectRetries && !collected; i++)
{
GC.Collect();
await WaitForGC();

try
{
// the OnlyRetrieveFromCache means that if it is not already loaded, then throw
await Glide
.With(Platform.DefaultContext)
.Load(bitmapFile, Platform.DefaultContext)
.SetOnlyRetrieveFromCache(true)
.SetDiskCacheStrategy(DiskCacheStrategy.None)
.SubmitAsync(Platform.DefaultContext);
_ = await Glide
.With(Platform.DefaultContext)
.Load(bitmapFile, Platform.DefaultContext)
.SetOnlyRetrieveFromCache(true)
.SetDiskCacheStrategy(DiskCacheStrategy.None)
.SubmitAsync(Platform.DefaultContext);
}
catch (ExecutionException ex) when (ex.Cause is GlideException)
{
// no-op becasue we are waiting for this
collected = true;
}
catch(GlideException)
{
// no-op becasue we are waiting for this
collected = true;
}
}

return collected;
Expand Down
9 changes: 9 additions & 0 deletions src/Core/tests/DeviceTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ protected Task InvokeOnMainThreadAsync(Func<Task> action) =>

public Task<T> InvokeOnMainThreadAsync<T>(Func<Task<T>> func) =>
TestDispatcher.Current.DispatchAsync(func);

protected async Task WaitForGC()
{
GC.Collect();
GC.WaitForPendingFinalizers();
await Task.Delay(10);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
32 changes: 32 additions & 0 deletions src/TestUtils/src/DeviceTests/RepeatAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit.Sdk;

namespace Microsoft.Maui.DeviceTests
{
/// <summary>
/// Usage Example
/// [Theory]
/// [Repeat(100)]
/// public async Task TheSameImageSourceReturnsTheSameBitmap(int _)
/// </summary>
public sealed class RepeatAttribute : DataAttribute
{
readonly int _count;

public RepeatAttribute(int count)
{
this._count = count;
}

public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
foreach (var iterationNumber in Enumerable.Range(start: 1, count: this._count))
{
yield return new object[] { iterationNumber };
}
}
}
}

0 comments on commit b1f0687

Please sign in to comment.