Repository: dotnet/runtime
File: src/tests/async/capacity/capacity.cs (C#)

Code before:
using Xunit;

public class CapacityTests
{
    [Fact]
    public static void TestLargeClassWithIntMethods()
    {
        // Scenario 1: allocate an instance of a class with 40000 methods that return int
        // The allocation should succeed
        var instance = new LargeClassWithIntMethods();
        Assert.NotNull(instance);
    }

    [Fact]
    public static void TestLargeClassWithTaskMethods_Success()
    {
        // Scenario 2: allocate an instance of a class with 32750 methods that return Task
        // The allocation should succeed
        var instance = new LargeClassWithTaskMethods_Success();
        Assert.NotNull(instance);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static object AllocLargeClassWithTaskMethods_Exception()
    {
        var instance = new LargeClassWithTaskMethods_Exception();
        return instance;
    }

    [Fact]
    public static void TestLargeClassWithTaskMethods_Exception()
    {
        // Scenario 3: make a call to a method that allocates an instance of a class with 32763 methods that return Task
        // The call should throw an exception
        Assert.Throws<TypeLoadException>(() =>
        {
            AllocLargeClassWithTaskMethods_Exception();
        });
    }
}


Review comment (suggestion):
This test is testing CoreCLR specific implementation limitation. Is it going to pass on Native AOT? If not, it should be disabled there.

Code after:
using System.Reflection.Emit;
using System.Threading.Tasks;
using Xunit;

public class CapacityTests
{
    [Fact]
    public static void TestLargeClassWithIntMethods()
    {
        // Scenario 1: allocate an instance of a class with 40000 methods that return int
        // The allocation should succeed
        var instance = CreateTypeWithMethods("LargeClassWithIntMethods", 40000, typeof(int));
        Assert.NotNull(instance);
    }

    [Fact]
    public static void TestLargeClassWithTaskMethods_Success()
    {
        // Scenario 2: allocate an instance of a class with 32750 methods that return Task
        // The allocation should succeed
        var instance = CreateTypeWithMethods("LargeClassWithTaskMethods_Success", 32750, typeof(Task));
        Assert.NotNull(instance);
    }

    [Fact]
    public static void TestLargeClassWithTaskMethods_Exception()
    {
        // Scenario 3: make a call to a method that allocates an instance of a class with 32763 methods that return Task
        // The call should throw an exception
        Assert.Throws<TypeLoadException>(() =>
        {
            CreateTypeWithMethods("LargeClassWithTaskMethods_Exception", 32763, typeof(Task));
        });
    }

    private static object CreateTypeWithMethods(string typeName, int methodCount, Type returnType)
    {
        AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(
            new AssemblyName("DynamicCapacityAssembly_" + typeName),
            AssemblyBuilderAccess.Run);

        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicCapacityModule_" + typeName);

        TypeBuilder typeBuilder = moduleBuilder.DefineType(
            typeName,
            TypeAttributes.Public);

        // Define a default constructor
        ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(
            MethodAttributes.Public,
            CallingConventions.Standard,