Skip to content

Commit

Permalink
Fix test problem with BDN >= 0.13.2.1930
Browse files Browse the repository at this point in the history
BDN >= 0.13.2.1930 uses the Arm64 disassembler package Gee.External.Capstone 2.2.0. Unlike BDN itself, the package is not strong-named, therfore causing a ReflectionTypeLoadException on NetFx (strong-named assembly required).
See dotnet/BenchmarkDotNet#2127
Closes #139
  • Loading branch information
mawosoft committed Oct 9, 2022
1 parent 3685f44 commit b1d5113
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
30 changes: 30 additions & 0 deletions tests/Mawosoft.Extensions.BenchmarkDotNet.Tests/BDNVersionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) 2021-2022 Matthias Wolf, Mawosoft.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
Expand Down Expand Up @@ -30,5 +33,32 @@ void CheckAssembly(Assembly assembly, bool nightly)
Assert.Equal(nightly, revision != 0);
}
}

[Fact]
public void BDNAssemblyGetTypes()
{
List<Type> types = new();
Assembly assembly = typeof(BenchmarkCase).Assembly;
try
{
types.AddRange(assembly.GetTypes());
}
catch (ReflectionTypeLoadException rtle)
{
// BDN >= 0.13.2.1930 uses the Arm64 disassembler package Gee.External.Capstone 2.2.0. Unlike BDN itself,
// the package is not strong-named, therfore causing a ReflectionTypeLoadException on NetFx (strong-named assembly required).
_testOutput.WriteLine(rtle.Message);
_testOutput.WriteLine($"# of LoaderExceptions: {rtle.LoaderExceptions.Length}");
_testOutput.WriteLine(string.Join(Environment.NewLine, rtle.LoaderExceptions.GroupBy(e => e?.Message, (k, g) => $"{g.Count()}x {k}")));
types.AddRange(rtle.Types.Where(t => t != null).Select(t => t!));
#if NETFRAMEWORK
Assert.True(assembly.GetName().Version >= new Version("0.13.2.1930"), "ReflectionTypeLoadException only tolerated for BDN >= 0.13.2.1930");
#else
Assert.True(false, "ReflectionTypeLoadException only tolerated on .NET Framework.");
#endif

}
Assert.NotEmpty(types);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Columns;
using Xunit;
using Xunit.Abstractions;

using static Mawosoft.Extensions.BenchmarkDotNet.ColumnCategoryExtensions;

namespace Mawosoft.Extensions.BenchmarkDotNet.Tests
{
public class ColumnCategoryExtensionsTests
{
private static IEnumerable<Type> GetTypesWrapper(Assembly assembly)
{
List<Type> types = new();
try
{
types.AddRange(assembly.GetTypes());
}
catch (ReflectionTypeLoadException rtle)
{
types.AddRange(rtle.Types.Where(t => t != null).Select(t => t!));

}
return types;
}

[Fact]
public void ToExtended_ExistingCategoriesMatch()
{
Expand Down Expand Up @@ -49,7 +64,7 @@ public GetExtendedColumnCategory_TheoryData()
static bool predicate(Type t) => t.IsClass && !t.IsAbstract && typeof(IColumn).IsAssignableFrom(t);

IEnumerable<Type> columnTypes =
typeof(IColumn).Assembly.GetTypes().Where(predicate)
GetTypesWrapper(typeof(IColumn).Assembly).Where(predicate)
.Concat(typeof(ColumnCategoryExtensions).Assembly.GetTypes().Where(predicate)
.Distinct());

Expand Down Expand Up @@ -140,10 +155,10 @@ private class GetExtendedColumnCategories_TheoryData : TheoryData<IColumnProvide
// to discover any new additions not covered by test.
public GetExtendedColumnCategories_TheoryData()
{
Func<Type, bool> predicate = t => t.IsClass && !t.IsAbstract && typeof(IColumnProvider).IsAssignableFrom(t);
static bool predicate(Type t) => t.IsClass && !t.IsAbstract && typeof(IColumnProvider).IsAssignableFrom(t);

IEnumerable<Type> providerTypes =
typeof(IColumn).Assembly.GetTypes().Where(predicate)
GetTypesWrapper(typeof(IColumnProvider).Assembly).Where(predicate)
.Concat(typeof(ColumnCategoryExtensions).Assembly.GetTypes().Where(predicate)
.Distinct());

Expand Down

0 comments on commit b1d5113

Please sign in to comment.