Skip to content

Commit 747138a

Browse files
authored
Fixes a bug in .NET 6.0 preview (and possibly all preview releases) w… (#61)
* Fixes a bug in .NET 6.0 preview (and possibly all preview releases) where the runtime version cannot be determined. * Let's try running the tests in .NET 6.0 in github.. * Fucking tabs.. * Need to include prerelease
1 parent 13ae303 commit 747138a

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

.github/workflows/run-tests.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ jobs:
1717
uses: actions/setup-dotnet@v1
1818
with:
1919
dotnet-version: 5.0.x
20+
- name: Setup .NET Core 6.0
21+
uses: actions/setup-dotnet@v1
22+
with:
23+
dotnet-version: '6.0.x'
24+
include-prerelease: true
2025
- uses: actions/checkout@v1
2126
# This test constantly passes localy (windows + linux) but fails in the test environment. Don't have the time/ inclination to figure out why this is right now..
2227
- run: dotnet test -c "Debug" --filter Name!=When_blocking_work_is_executed_on_the_thread_pool_then_thread_pool_delays_are_measured

src/prometheus-net.DotNetRuntime.Tests/EventListening/TestHelpers.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,30 @@ public class TestHelpers
1212
{
1313
public static EventWrittenEventArgs CreateEventWrittenEventArgs(int eventId, DateTime? timestamp = null, params object[] payload)
1414
{
15-
var args = (EventWrittenEventArgs)typeof(EventWrittenEventArgs).CreateInstance(new []{ typeof(EventSource)}, Flags.NonPublic | Flags.Instance, new object[] { null});
16-
args.SetPropertyValue("EventId", eventId);
15+
EventWrittenEventArgs args;
16+
var bindFlags = Flags.NonPublic | Flags.Instance;
17+
18+
// In .NET 6.0, they changed the signature of these constructors- handle this annoyance
19+
if (typeof(EventWrittenEventArgs).GetConstructors(bindFlags).Any(x => x.GetParameters().Length == 1))
20+
{
21+
args = (EventWrittenEventArgs)typeof(EventWrittenEventArgs).CreateInstance(new[] { typeof(EventSource)}, Flags.NonPublic | Flags.Instance, new object[] { null });
22+
args.SetPropertyValue("EventId", eventId);
23+
}
24+
else
25+
{
26+
args = (EventWrittenEventArgs)typeof(EventWrittenEventArgs).CreateInstance(new[] { typeof(EventSource), typeof(int) }, Flags.NonPublic | Flags.Instance, new object[] { null, eventId });
27+
}
28+
1729
args.SetPropertyValue("Payload", new ReadOnlyCollection<object>(payload));
1830

1931
if (timestamp.HasValue)
2032
{
2133
args.SetPropertyValue("TimeStamp", timestamp.Value);
2234
}
23-
35+
2436
return args;
2537
}
26-
38+
2739
public static EventWrittenEventArgs CreateCounterEventWrittenEventArgs(params (string key, object val)[] payload)
2840
{
2941
var counterPayload = payload.ToDictionary(k => k.key, v => v.val);
@@ -41,14 +53,11 @@ public static EventAssertion<T> ArrangeEventAssertion<T>(Action<Action<T>> wireU
4153
public class EventAssertion<T>
4254
{
4355
private Action<T> _handler;
44-
56+
4557
public EventAssertion(Action<Action<T>> wireUp)
4658
{
47-
_handler = e =>
48-
{
49-
History.Add(e);
50-
};
51-
59+
_handler = e => { History.Add(e); };
60+
5261
wireUp(_handler);
5362
}
5463

src/prometheus-net.DotNetRuntime.Tests/prometheus-net.DotNetRuntime.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
<Import Project="../Common.csproj" />
33

44
<PropertyGroup>
5-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
5+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
<RootNamespace>Prometheus.DotNetRuntime.Tests</RootNamespace>
88
<Platforms>AnyCPU</Platforms>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
<LangVersion>10</LangVersion>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/prometheus-net.DotNetRuntime/EventListening/EventParserTypes.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ internal static IEnumerable<Type> GetEventParsers(Assembly fromAssembly)
8787
internal static Lazy<Version> CurrentRuntimeVerison = new Lazy<Version>(() =>
8888
{
8989
var split = RuntimeInformation.FrameworkDescription.Split(' ');
90-
91-
if (Version.TryParse(split[split.Length - 1], out var version))
90+
if (split.Length < 2)
91+
return null;
92+
93+
var versionPart = split[^1];
94+
// Handle preview version strings, e.g. .NET 6.0.0-preview.7.21377.19.
95+
var hyphenIndex = versionPart.IndexOf('-');
96+
if (hyphenIndex > -1)
97+
versionPart = versionPart.Substring(0, hyphenIndex);
98+
99+
if (Version.TryParse(versionPart, out var version))
92100
return new Version(version.Major, version.Minor);
93101

94102
return null;

src/prometheus-net.DotNetRuntime/ListenerRegistration.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public static ListenerRegistration Create<T>(CaptureLevel level, Func<IServicePr
2626
if (!supportedLevels.Contains(eventLevel))
2727
throw new UnsupportedEventParserLevelException(typeof(T), level, supportedLevels);
2828

29-
3029
if (!EventParserTypes.AreEventsSupportedByRuntime(typeof(T)))
3130
throw new UnsupportedEventParserRuntimeException(typeof(T));
32-
3331

3432
return new ListenerRegistration(eventLevel, typeof(T), sp => (object)factory(sp));
3533
}

0 commit comments

Comments
 (0)