Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close stream instead of dispose and fix unit test #3705

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Comment on lines +22 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to mention is the Mono runtime team had us use this class:

https://github.com/xamarin/java.interop/blob/bc5bcf4f0ef07aab898f2643d2a25f66512d98ed/tests/Java.Interop-Tests/Java.Interop/FinalizerHelpers.cs

It fills the stack 128 methods deep, to make tests able to wait on the GC more reliably.

But I think you could probably wait and see if these tests fail randomly down the road.

}
}
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 };
}
}
}
}