Add test for out of band messages over EventSource
authorDavid Mason <davmason@microsoft.com>
Fri, 24 Apr 2020 01:20:21 +0000 (18:20 -0700)
committerDavid Mason <davmason@microsoft.com>
Mon, 27 Apr 2020 23:29:24 +0000 (16:29 -0700)
src/coreclr/tests/src/tracing/eventpipe/eventsourceerror/eventsourceerror.cs [new file with mode: 0644]
src/coreclr/tests/src/tracing/eventpipe/eventsourceerror/eventsourceerror.csproj [new file with mode: 0644]

diff --git a/src/coreclr/tests/src/tracing/eventpipe/eventsourceerror/eventsourceerror.cs b/src/coreclr/tests/src/tracing/eventpipe/eventsourceerror/eventsourceerror.cs
new file mode 100644 (file)
index 0000000..480a492
--- /dev/null
@@ -0,0 +1,107 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Diagnostics.Tracing;
+using System.IO;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+using Microsoft.Diagnostics.Tools.RuntimeClient;
+using Microsoft.Diagnostics.Tracing;
+using Tracing.Tests.Common;
+using Microsoft.Diagnostics.Tracing.Parsers.Clr;
+
+namespace Tracing.Tests.EventSourceError
+{
+    // This class tries to use IEnumerable<int> events with manifest based
+    // EventSource, which will cause an error. The test is validating we see
+    // that error over EventPipe.
+    class IllegalTypesEventSource : EventSource
+    {
+        public IllegalTypesEventSource()
+        {
+
+        }
+
+        [Event(1, Level = EventLevel.LogAlways)]
+        public void SimpleArrayEvent(int[] simpleArray)
+        {
+           WriteEvent(1, simpleArray);
+        }
+
+        [Event(2, Level = EventLevel.LogAlways)]
+        public void BasicEvent(int i)
+        {
+            WriteEvent(2, i);
+        }
+
+        protected override void OnEventCommand(EventCommandEventArgs command)
+        {
+            Console.WriteLine($"command={command.Command}");
+        }
+    }
+
+    public class EventSourceError
+    {
+        public static int Main(string[] args)
+        {
+            // This test validates that if an EventSource generates an error
+            // during construction it gets emitted over EventPipe
+
+            List<Provider> providers = new List<Provider>
+            {
+                new Provider("IllegalTypesEventSource")
+            };
+
+            var configuration = new SessionConfiguration(circularBufferSizeMB: 1024, format: EventPipeSerializationFormat.NetTrace,  providers: providers);
+            return IpcTraceTest.RunAndValidateEventCounts(_expectedEventCounts, _eventGeneratingAction, configuration, _DoesRundownContainMethodEvents);
+        }
+
+        private static Dictionary<string, ExpectedEventCount> _expectedEventCounts = new Dictionary<string, ExpectedEventCount>()
+        {
+            { "IllegalTypesEventSource", 1 }
+        };
+
+        private static Action _eventGeneratingAction = () =>
+        {
+            // Constructing the EventSource should generate the error message
+            IllegalTypesEventSource eventSource = new IllegalTypesEventSource();
+
+            // This will be a no-op since the EventSource failed to construct
+            eventSource.SimpleArrayEvent(new int[] { 12 });
+        };
+
+        private static Func<EventPipeEventSource, Func<int>> _DoesRundownContainMethodEvents = (source) =>
+        {
+            int eventCount = 0;
+            bool sawEvent = false;
+            source.Dynamic.All += (TraceEvent traceEvent) =>
+            {
+                if (traceEvent.ProviderName == "SentinelEventSource"
+                    || traceEvent.ProviderName == "Microsoft-Windows-DotNETRuntimeRundown"
+                    || traceEvent.ProviderName == "Microsoft-DotNETCore-EventPipe")
+                {
+                    return;
+                }
+
+                ++eventCount;
+
+                if (traceEvent.ProviderName == "IllegalTypesEventSource"
+                    && traceEvent.EventName == "WriteEventString"
+                    && traceEvent.FormattedMessage.StartsWith("ERROR: Exception in Command Processing for EventSource IllegalTypesEventSource", StringComparison.OrdinalIgnoreCase))
+                {
+                    sawEvent = true;
+                }
+                else
+                {
+                    Console.WriteLine($"Saw unexpected event {traceEvent}");
+                }
+            };
+
+            return () => ((eventCount == 1) && sawEvent) ? 100 : -1;
+        };
+    }
+}
\ No newline at end of file
diff --git a/src/coreclr/tests/src/tracing/eventpipe/eventsourceerror/eventsourceerror.csproj b/src/coreclr/tests/src/tracing/eventpipe/eventsourceerror/eventsourceerror.csproj
new file mode 100644 (file)
index 0000000..3ed085b
--- /dev/null
@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
+    <OutputType>exe</OutputType>
+    <CLRTestKind>BuildAndRun</CLRTestKind>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <CLRTestPriority>0</CLRTestPriority>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+    <ProjectReference Include="../common/common.csproj" />
+  </ItemGroup>
+</Project>