EventSource Manifest Trimmer test (#56463)
authorLakshan Fernando <lakshanf@hotmail.com>
Wed, 28 Jul 2021 20:50:19 +0000 (13:50 -0700)
committerGitHub <noreply@github.com>
Wed, 28 Jul 2021 20:50:19 +0000 (13:50 -0700)
* EventSource Manifest Trimmer test

* FB

src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourceManifestTest.cs [new file with mode: 0644]
src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/System.Diagnostics.Tracing.TrimmingTests.proj

diff --git a/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourceManifestTest.cs b/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourceManifestTest.cs
new file mode 100644 (file)
index 0000000..91929da
--- /dev/null
@@ -0,0 +1,57 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics.Tracing;
+using System.Linq;
+using System;
+
+/// <summary>
+/// Tests that using an EventSource to get the manifest works without method references in a trimmed application.
+/// EventSource has DynamicallyAccessedMembersAttribute applied at class level which means derived types keeps members
+/// </summary>
+internal class Program
+{
+    internal class EventSourceTest : EventSource
+    {
+        public void EventSourceTest_Method_0() => WriteEvent(1); 
+
+        public void EventSourceTest_Method_1(int value) => WriteEvent(2, value); 
+        void EventSourceTest_Method_2(string name) => WriteEvent(3, name); 
+
+        [NonEvent]
+        public void EventSourceTest_Method_3(int num1, int num2) => WriteEvent(4, num1, num2); 
+        [NonEvent]
+        void EventSourceTest_Method_4(){}
+
+        [Event(500)]
+        public void EventSourceTest_Method_5(byte[] bytes) => WriteEvent(500, bytes); 
+        [Event(1500)]
+        protected virtual void EventSourceTest_Method_6(long value) => WriteEvent(1500, value); 
+
+        [Event(2500)]
+        int EventSourceTest_Method_7() => 5; 
+    }
+
+    public static int Main()
+    {
+        string manifest = EventSource.GenerateManifest(typeof(EventSourceTest), null);
+        // we are going to avoid as much as possible program constructs that could give the trimmer reasons to keep members
+        const string baseMethodName = "EventSourceTest_Method_";
+        int[] exclusions = { 3, 4, 8 };
+        for (int i = 0; i <= 8; i++)
+        {
+            string methodName = $"{baseMethodName}{i}";
+            // We expect the methodName to be in the manifest unless the prefix, i, is in exclusions ([NonEvent] or non-existing methods)
+            bool methodExists = manifest.Contains(methodName);
+            bool shouldMethodExist = !exclusions.Contains(i);
+            if (methodExists != shouldMethodExist)
+            {
+                return -1;
+            }
+        }
+
+        return 100;
+    }
+}
index da4a46f2ae141a37529a9ef2da6563d7d78959d5..0081a36ebc69a94d4eb0a8f1994df0e1fb1f0d1d 100644 (file)
@@ -1,5 +1,10 @@
 <Project DefaultTargets="Build">
   <Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />
 
+  <ItemGroup>
+    <TestConsoleAppSourceFiles Include="EventSourcePropertyValueTest.cs" />
+    <TestConsoleAppSourceFiles Include="EventSourceManifestTest.cs" />
+  </ItemGroup>
+
   <Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
 </Project>