Browse Source
* Remove dotMemory Unit from LeakTests * Nuke: run leak tests normallypull/20211/head
committed by
GitHub
14 changed files with 464 additions and 455 deletions
@ -1,5 +0,0 @@ |
|||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<ItemGroup> |
|||
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.2.20220510" /> |
|||
</ItemGroup> |
|||
</Project> |
|||
File diff suppressed because it is too large
@ -1,70 +1,76 @@ |
|||
using System; |
|||
using System.Diagnostics.CodeAnalysis; |
|||
using Avalonia.Controls; |
|||
using Avalonia.Controls.ApplicationLifetimes; |
|||
using Avalonia.Reactive; |
|||
using Avalonia.Threading; |
|||
using Avalonia.UnitTests; |
|||
using JetBrains.dotMemoryUnit; |
|||
using Xunit; |
|||
using Xunit.Abstractions; |
|||
|
|||
namespace Avalonia.LeakTests; |
|||
|
|||
internal class ViewModelForDisposingTest |
|||
{ |
|||
~ViewModelForDisposingTest() { ; } |
|||
[SuppressMessage("ReSharper", "EmptyDestructor", Justification = "Needed for test")] |
|||
~ViewModelForDisposingTest() { } |
|||
} |
|||
|
|||
[DotMemoryUnit(FailIfRunWithoutSupport = false)] |
|||
public class DataContextTests |
|||
public class DataContextTests : ScopedTestBase |
|||
{ |
|||
public DataContextTests(ITestOutputHelper atr) |
|||
{ |
|||
DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Window_DataContext_Disposed_After_Window_Close_With_Lifetime() |
|||
{ |
|||
static IDisposable Run() |
|||
static IDisposable Run(out WeakReference weakDataContext) |
|||
{ |
|||
var unitTestApp = UnitTestApplication.Start(TestServices.StyledWindow); |
|||
var lifetime = new ClassicDesktopStyleApplicationLifetime(); |
|||
lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown; |
|||
var window = new Window { DataContext = new ViewModelForDisposingTest() }; |
|||
var viewModel = new ViewModelForDisposingTest(); |
|||
var window = new Window { DataContext = viewModel }; |
|||
window.Show(); |
|||
window.Close(); |
|||
|
|||
return Disposable.Create(lifetime, lt => lt.Shutdown()) |
|||
var disposable = Disposable.Create(lifetime, lt => lt.Shutdown()) |
|||
.DisposeWith(new CompositeDisposable(lifetime, unitTestApp)); |
|||
|
|||
weakDataContext = new WeakReference(viewModel); |
|||
return disposable; |
|||
} |
|||
|
|||
using var _ = Run(); |
|||
// Process all Loaded events to free control reference(s)
|
|||
Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); |
|||
GC.Collect(); |
|||
using var _ = Run(out var weakDataContext); |
|||
Assert.True(weakDataContext.IsAlive); |
|||
|
|||
CollectGarbage(); |
|||
|
|||
dotMemory.Check(m => Assert.Equal(0, |
|||
m.GetObjects(o => o.Type.Is<ViewModelForDisposingTest>()).ObjectsCount)); |
|||
Assert.False(weakDataContext.IsAlive); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Window_DataContext_Disposed_After_Window_Close_Without_Lifetime() |
|||
{ |
|||
static void Run() |
|||
static void Run(out WeakReference weakDataContext) |
|||
{ |
|||
using var _ = UnitTestApplication.Start(TestServices.StyledWindow); |
|||
var window = new Window { DataContext = new ViewModelForDisposingTest() }; |
|||
var viewModel = new ViewModelForDisposingTest(); |
|||
var window = new Window { DataContext = viewModel }; |
|||
window.Show(); |
|||
window.Close(); |
|||
|
|||
weakDataContext = new WeakReference(viewModel); |
|||
} |
|||
|
|||
Run(); |
|||
Run(out var weakDataContext); |
|||
Assert.True(weakDataContext.IsAlive); |
|||
|
|||
CollectGarbage(); |
|||
|
|||
Assert.False(weakDataContext.IsAlive); |
|||
} |
|||
|
|||
private static void CollectGarbage() |
|||
{ |
|||
// Process all Loaded events to free control reference(s)
|
|||
Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); |
|||
GC.Collect(); |
|||
|
|||
dotMemory.Check(m => Assert.Equal(0, |
|||
m.GetObjects(o => o.Type.Is<ViewModelForDisposingTest>()).ObjectsCount)); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,16 @@ |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.LeakTests; |
|||
|
|||
/// <summary>
|
|||
/// Use on leak tests where objects are somehow kept rooted in debug mode.
|
|||
/// </summary>
|
|||
internal sealed class ReleaseFactAttribute : FactAttribute |
|||
{ |
|||
public ReleaseFactAttribute() |
|||
{ |
|||
#if DEBUG
|
|||
Skip = "Only runs in Release mode"; |
|||
#endif
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue