From 0d596d205f583cb7f0e1744b46dd9b55985ae0d1 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Thu, 19 Dec 2019 20:06:30 -0600 Subject: [PATCH] Make test libraries configuration agnostic (#378) * Make test libraries configuration agnostic * PR Feedback, keep Debug define for tests * More PR Feedback * Last pr feedback --- .../System/AssertExtensions.cs | 7 ++++ src/libraries/Directory.Build.props | 20 ++++----- .../tests/ActivityTests.cs | 42 +++++++++++-------- .../DiagnosticSourceEventSourceBridgeTests.cs | 9 +--- .../tests/StackTraceTests.cs | 10 +++-- .../BasicEventSourceTest/Harness/Listeners.cs | 2 - 6 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/libraries/Common/tests/CoreFx.Private.TestUtilities/System/AssertExtensions.cs b/src/libraries/Common/tests/CoreFx.Private.TestUtilities/System/AssertExtensions.cs index b57aed2deb2f5..6d0a4a0f2e2db 100644 --- a/src/libraries/Common/tests/CoreFx.Private.TestUtilities/System/AssertExtensions.cs +++ b/src/libraries/Common/tests/CoreFx.Private.TestUtilities/System/AssertExtensions.cs @@ -378,6 +378,13 @@ public static void Equal(HashSet expected, HashSet actual) } } + public static void AtLeastOneEquals(T expected1, T expected2, T value) + { + EqualityComparer comparer = EqualityComparer.Default; + if (!(comparer.Equals(value, expected1) || comparer.Equals(value, expected2))) + throw new XunitException($"Expected: {expected1} || {expected2}{Environment.NewLine}Actual: {value}"); + } + public delegate void AssertThrowsActionReadOnly(ReadOnlySpan span); public delegate void AssertThrowsAction(Span span); diff --git a/src/libraries/Directory.Build.props b/src/libraries/Directory.Build.props index c743808c24cda..e0a2a43c9a838 100644 --- a/src/libraries/Directory.Build.props +++ b/src/libraries/Directory.Build.props @@ -27,10 +27,10 @@ https://github.com/Microsoft/msbuild/blob/3a9d1d2ae23e41b32a612ea6b0dce531fcf86be7/src/Build/Evaluation/IntrinsicFunctions.cs#L431 --> OSX - FreeBSD - NetBSD - Linux - $(OS) + FreeBSD + NetBSD + Linux + $(OS) @@ -72,7 +72,7 @@ true false - true + true true @@ -153,8 +153,8 @@ - true - true + true + true true @@ -192,12 +192,12 @@ - + true false full - $(DefineConstants),DEBUG,TRACE + $(DefineConstants),TRACE,DEBUG @@ -216,6 +216,7 @@ true false false + true true @@ -288,7 +289,6 @@ false true - true false diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs index 8a4a8aefb082d..7ad41bca64d9d 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs @@ -263,33 +263,41 @@ public void IdGenerationInternalParent() //start 2 children in different execution contexts Task.Run(() => child1.Start()).Wait(); Task.Run(() => child2.Start()).Wait(); -#if DEBUG - Assert.Equal($"|{parent.RootId}.{child1.OperationName}-1.", child1.Id); - Assert.Equal($"|{parent.RootId}.{child2.OperationName}-2.", child2.Id); -#else - Assert.Equal($"|{parent.RootId}.1.", child1.Id); - Assert.Equal($"|{parent.RootId}.2.", child2.Id); -#endif + + // In Debug builds of System.Diagnostics.DiagnosticSource, the child operation Id will be constructed as follows + // "|parent.RootId.-childCount.". + // This is for debugging purposes to know which operation the child Id is comming from. + // + // In Release builds of System.Diagnostics.DiagnosticSource, it will not contain the operation name to keep it simple and it will be as + // "|parent.RootId.childCount.". + + string child1DebugString = $"|{parent.RootId}.{child1.OperationName}-1."; + string child2DebugString = $"|{parent.RootId}.{child2.OperationName}-2."; + string child1ReleaseString = $"|{parent.RootId}.1."; + string child2ReleaseString = $"|{parent.RootId}.2."; + + AssertExtensions.AtLeastOneEquals(child1DebugString, child1ReleaseString, child1.Id); + AssertExtensions.AtLeastOneEquals(child2DebugString, child2ReleaseString, child2.Id); + Assert.Equal(parent.RootId, child1.RootId); Assert.Equal(parent.RootId, child2.RootId); child1.Stop(); child2.Stop(); var child3 = new Activity("child3"); child3.Start(); -#if DEBUG - Assert.Equal($"|{parent.RootId}.{child3.OperationName}-3.", child3.Id); -#else - Assert.Equal($"|{parent.RootId}.3.", child3.Id); -#endif + + string child3DebugString = $"|{parent.RootId}.{child3.OperationName}-3."; + string child3ReleaseString = $"|{parent.RootId}.3."; + + AssertExtensions.AtLeastOneEquals(child3DebugString, child3ReleaseString, child3.Id); var grandChild = new Activity("grandChild"); grandChild.Start(); -#if DEBUG - Assert.Equal($"{child3.Id}{grandChild.OperationName}-1.", grandChild.Id); -#else - Assert.Equal($"{child3.Id}1.", grandChild.Id); -#endif + child3DebugString = $"{child3.Id}{grandChild.OperationName}-1."; + child3ReleaseString = $"{child3.Id}1."; + + AssertExtensions.AtLeastOneEquals(child3DebugString, child3ReleaseString, grandChild.Id); } /// diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs index f600cbd90f352..46978a958e65a 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs @@ -913,23 +913,20 @@ public TestDiagnosticSourceEventListener() public int EventCount; public DiagnosticSourceEvent LastEvent; -#if DEBUG + // Here just for debugging. Lets you see the last 3 events that were sent. public DiagnosticSourceEvent SecondLast; public DiagnosticSourceEvent ThirdLast; -#endif /// - /// Sets the EventCount to 0 and LastEvent to null + /// Sets the EventCount to 0 and LastEvents to null /// public void ResetEventCountAndLastEvent() { EventCount = 0; LastEvent = null; -#if DEBUG SecondLast = null; ThirdLast = null; -#endif } /// @@ -943,10 +940,8 @@ private void UpdateLastEvent(DiagnosticSourceEvent anEvent) if (Filter != null && !Filter(anEvent)) return; -#if DEBUG ThirdLast = SecondLast; SecondLast = LastEvent; -#endif EventCount++; LastEvent = anEvent; diff --git a/src/libraries/System.Diagnostics.StackTrace/tests/StackTraceTests.cs b/src/libraries/System.Diagnostics.StackTrace/tests/StackTraceTests.cs index 0399050b8944c..b1327a0857b54 100644 --- a/src/libraries/System.Diagnostics.StackTrace/tests/StackTraceTests.cs +++ b/src/libraries/System.Diagnostics.StackTrace/tests/StackTraceTests.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using Xunit; namespace System.Diagnostics @@ -247,8 +248,6 @@ public void Ctor_Frame(StackFrame stackFrame) public static IEnumerable ToString_TestData() { - // Debug mode and Release mode give different results. -#if DEBUG yield return new object[] { new StackTrace(InvokeException()), "System.Diagnostics.Tests.StackTraceTests.ThrowException()" }; yield return new object[] { new StackTrace(new Exception()), "" }; yield return new object[] { NoParameters(), "System.Diagnostics.Tests.StackTraceTests.NoParameters()" }; @@ -260,7 +259,6 @@ public static IEnumerable ToString_TestData() // Methods belonging to the System.Diagnostics namespace are ignored. yield return new object[] { InvokeIgnoredMethod(), "System.Diagnostics.Tests.StackTraceTests.InvokeIgnoredMethod()" }; -#endif yield return new object[] { InvokeIgnoredMethodWithException(), "System.Diagnostics.Ignored.MethodWithException()" }; } @@ -300,16 +298,22 @@ public void ToString_NullFrame_ThrowsNullReferenceException() Assert.Equal(Environment.NewLine, stackTrace.ToString()); } + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private static StackTrace NoParameters() => new StackTrace(); + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private static StackTrace OneParameter(int x) => new StackTrace(); + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private static StackTrace TwoParameters(int x, string y) => new StackTrace(); + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private static StackTrace Generic() => new StackTrace(); + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private static StackTrace Generic() => new StackTrace(); private static StackTrace InvokeIgnoredMethod() => Ignored.Method(); private static StackTrace InvokeIgnoredMethodWithException() => Ignored.MethodWithException(); + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] private static Exception InvokeException() { try diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/Harness/Listeners.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/Harness/Listeners.cs index 7eddf5e76506e..a44330cff1fe9 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/Harness/Listeners.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/Harness/Listeners.cs @@ -109,7 +109,6 @@ public virtual string PayloadString(int propertyIndex, string propertyName) } public abstract IList PayloadNames { get; } -#if DEBUG /// /// This is a convenience function for the debugger. It is not used typically /// @@ -123,7 +122,6 @@ public List PayloadValues return ret; } } -#endif public override string ToString() {