Skip to content

Commit 1189eec

Browse files
committed
Add test
1 parent bcc1e4b commit 1189eec

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.Loader;
7+
using Xunit;
8+
9+
public static class Runtime_100437
10+
{
11+
[Fact]
12+
public int TestCollectibleEmptyArrayNotInFrozenHeap()
13+
{
14+
WeakReference[] wrs = new WeakReference[10];
15+
for (int i = 0; i < wrs.Length; i++)
16+
{
17+
var alc = new MyAssemblyLoadContext();
18+
var a = alc.LoadFromAssemblyPath(typeof(Program).Assembly.Location);
19+
wrs[i] = (WeakReference)a.GetType("Runtime_100437").GetMethod("Work").Invoke(null, null);
20+
GC.Collect();
21+
}
22+
23+
int result = 0;
24+
foreach (var wr in wrs)
25+
{
26+
// This is testing that the empty array from Work(), if collected, should not have been allocated on the Frozen heap
27+
// otherwise it will result in a random crash.
28+
result += wr.Target?.ToString()?.GetHashCode() ?? 0;
29+
}
30+
return result;
31+
}
32+
33+
public static WeakReference Work()
34+
{
35+
return new WeakReference(Array.Empty<Program>());
36+
}
37+
38+
private class MyAssemblyLoadContext : AssemblyLoadContext
39+
{
40+
public MyAssemblyLoadContext()
41+
: base(isCollectible: true)
42+
{
43+
}
44+
}
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)