Skip to content

Commit

Permalink
Add logging for tests and try a fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
fryderykhuang committed Jan 19, 2024
1 parent 6d1dea8 commit 4f9ff61
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 19 deletions.
4 changes: 2 additions & 2 deletions scripts/Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Push-Location $SolutionDir
$Env:Platform = 'x64'
foreach ($project in $Tests)
{
dotnet test -c Debug -a x64 -v n .\$project\$project.csproj
dotnet test -c Debug -a x64 --logger "console;verbosity=detailed" .\$project\$project.csproj
if (!$?) { throw }
dotnet test -c Release -a x64 -v n .\$project\$project.csproj
dotnet test -c Release -a x64 --logger "console;verbosity=detailed" .\$project\$project.csproj
if (!$?) { throw }
}

Expand Down
3 changes: 2 additions & 1 deletion src/NullGC.Collections.Tests/FixedCountValueDequeTTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NullGC.TestCommons;
using Xunit.Abstractions;

namespace NullGC.Collections.Tests;

Expand Down Expand Up @@ -126,7 +127,7 @@ public void GetNthFromHeadOrTail()
q.Dispose();
}

public FixedCountValueDequeTTests() : base(false)
public FixedCountValueDequeTTests(ITestOutputHelper logger) : base(logger, false)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="Xunit.Extensions.Ordering" Version="1.4.5"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
5 changes: 3 additions & 2 deletions src/NullGC.Collections.Tests/SlidingTimeWindowTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NullGC.Diagnostics;
using NullGC.TestCommons;
using Xunit.Abstractions;

namespace NullGC.Collections.Tests;

Expand All @@ -10,7 +11,7 @@ public void GeneralFacts()
{
var stw = new SlidingTimeWindow<int>(1000, 5, default);

while (Environment.TickCount64 % 1000 != 0) ;
while (Environment.TickCount64 % 1000 > 10) Thread.Yield();
var w = stw.Update(1);
Assert.True(w.Count == 1);
Assert.True(w.Buckets.Count == 1);
Expand Down Expand Up @@ -66,7 +67,7 @@ public void GeneralFacts()
stw.Dispose();
}

public SlidingTimeWindowTests() : base(false)
public SlidingTimeWindowTests(ITestOutputHelper logger) : base(logger, false)
{
}
}
3 changes: 2 additions & 1 deletion src/NullGC.Collections.Tests/ValueArrayTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using NullGC.TestCommons;
using Xunit.Abstractions;

namespace NullGC.Collections.Tests;

public class ValueArrayTests : AssertMemoryAllFreedBase
{
public ValueArrayTests() : base(false)
public ValueArrayTests(ITestOutputHelper logger) : base(logger, false)
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/NullGC.Collections.Tests/ValueLinkedListTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Runtime.CompilerServices;
using NullGC.Linq;
using NullGC.TestCommons;
using Xunit.Abstractions;

namespace NullGC.Collections.Tests;

public class ValueLinkedListTests : AssertMemoryAllFreedBase
{
public ValueLinkedListTests() : base(false)
public ValueLinkedListTests(ITestOutputHelper logger) : base(logger, false)
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/NullGC.Collections.Tests/ValueListTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using NullGC.TestCommons;
using Xunit.Abstractions;

namespace NullGC.Collections.Tests;

public class ValueListTests : AssertMemoryAllFreedBase
{
public ValueListTests() : base(false, true)
public ValueListTests(ITestOutputHelper logger) : base(logger, false, true)
{
}

Expand Down
4 changes: 0 additions & 4 deletions src/NullGC.Linq.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
global using Xunit;

using NullGC.TestCommons;
using Xunit.Extensions.Ordering;

//[assembly: AssemblyFixture(typeof(DefaultAllocatorContextFixture))]


5 changes: 3 additions & 2 deletions src/NullGC.Linq.Tests/LinqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NullGC.Allocators;
using NullGC.Collections;
using NullGC.TestCommons;
using Xunit.Abstractions;

namespace NullGC.Linq.Tests;

Expand All @@ -28,7 +29,7 @@ public override void Dispose()
private readonly int _bigStructMin;
private readonly int _smallStructMin;

public LinqTests() : base(false)
public LinqTests(ITestOutputHelper logger) : base(logger, false)
{
_emptyArray = ValueArray<int>.Empty;
_valList1 = new ValueList<int>(0) {7, 0, 4, 5, 6, 1, 2, 3, 8, 9};
Expand Down Expand Up @@ -148,7 +149,7 @@ public void OrderByMulti_LinqRef_SingleSorter()
public void OrderByMulti_LinqRef_MultiSorter()
{
Assert.Equal(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
_valList1.LinqRef().OrderBy(OrderBy.Ascending((in int x) => x,OrderBy.Descending((in int x)=>x))));
_valList1.LinqRef().OrderBy(OrderBy.Ascending((in int x) => x, OrderBy.Descending((in int x) => x))));
}

// [Fact]
Expand Down
1 change: 0 additions & 1 deletion src/NullGC.Linq.Tests/NullGC.Linq.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<PackageReference Include="LinqGen.Generator" Version="0.3.1"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="Xunit.Extensions.Ordering" Version="1.4.5"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
9 changes: 6 additions & 3 deletions src/NullGC.TestCommons/AssertMemoryAllFreedBase.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using NullGC.Allocators;
using NullGC.Collections.Extensions;
using Xunit;
using Xunit.Abstractions;

namespace NullGC.TestCommons;

[Collection("AllocatorContext")]
public abstract class AssertMemoryAllFreedBase : IDisposable, IClassFixture<DefaultAllocatorContextFixture>
public abstract class AssertMemoryAllFreedBase : TestBase, IDisposable, IClassFixture<DefaultAllocatorContextFixture>
{
private readonly bool _scoped;
private readonly IMemoryAllocationTrackable _memTrackable;
private readonly IMemoryAllocationTrackable _allocTrackable;

protected IMemoryAllocationTrackable AllocTracker => _allocTrackable;

protected AssertMemoryAllFreedBase(bool scoped, bool uncached = false)
protected AssertMemoryAllFreedBase(ITestOutputHelper logger, bool scoped, bool uncached = false) : base(logger)
{
AllocatorContext.ClearProvidersAndAllocations();

Expand All @@ -22,7 +24,8 @@ protected AssertMemoryAllFreedBase(bool scoped, bool uncached = false)
else if (!uncached)
AllocatorContextInitializer.SetupDefaultUnscopedAllocationContext(out _allocTrackable, out _memTrackable);
else
AllocatorContextInitializer.SetupDefaultUncachedUnscopedAllocationContext(out _allocTrackable, out _memTrackable);
AllocatorContextInitializer.SetupDefaultUncachedUnscopedAllocationContext(out _allocTrackable,
out _memTrackable);
}

public virtual void Dispose()
Expand Down
15 changes: 15 additions & 0 deletions src/NullGC.TestCommons/Extensions/XUnitExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using Xunit.Abstractions;

namespace NullGC.TestCommons.Extensions;

public static class XUnitExtensions
{
public static bool TryGetITest(this ITestOutputHelper o, out ITest test)
{
var type = o.GetType();
var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty);
test = (ITest) testMember?.GetValue(o)!;
return test is not null;
}
}
22 changes: 22 additions & 0 deletions src/NullGC.TestCommons/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NullGC.TestCommons.Extensions;
using Xunit.Abstractions;

namespace NullGC.TestCommons;

public abstract class TestBase : IDisposable
{
private readonly ITestOutputHelper _logger;

protected TestBase(ITestOutputHelper logger)
{
_logger = logger;
if (logger.TryGetITest(out var test)) logger.WriteLine($"Starting test '{test.DisplayName}'");
else throw new InvalidOperationException();
}

public void Dispose()
{
if (_logger.TryGetITest(out var test)) _logger.WriteLine($"Finished test '{test.DisplayName}'");
else throw new InvalidOperationException();
}
}

0 comments on commit 4f9ff61

Please sign in to comment.