[test] Profile-Provider Merging Tests (#541)
authorJohn Salem <josalem@microsoft.com>
Mon, 14 Oct 2019 16:19:49 +0000 (09:19 -0700)
committerGitHub <noreply@github.com>
Mon, 14 Oct 2019 16:19:49 +0000 (09:19 -0700)
src/Microsoft.Diagnostics.Tools.RuntimeClient/Eventing/Provider.cs
src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs
src/Tools/dotnet-trace/Profile.cs
src/tests/dotnet-trace/ProfileProviderMerging.cs [new file with mode: 0644]

index 34ba88c43f1381f97692ef88ff43da609f82486b..16f3ead9c139cc5620781ccad28863f48fd35bb1 100644 (file)
@@ -37,5 +37,39 @@ namespace Microsoft.Diagnostics.Tools.RuntimeClient
 
         public string ToDisplayString() =>
             String.Format("{0, -40}", Name) + String.Format("0x{0, -18}", $"{Keywords:X16}") + String.Format("{0, -8}", EventLevel.ToString() + $"({(int)EventLevel})");
+        
+        public static bool operator ==(Provider left, Provider right)
+        {
+            return left.Name == right.Name &&
+                left.Keywords == right.Keywords &&
+                left.EventLevel == right.EventLevel &&
+                left.FilterData == right.FilterData;
+        }
+
+        public static bool operator !=(Provider left, Provider right)
+        {
+            return !(left == right);
+        }
+
+        public override bool Equals(object obj)
+        {
+            
+            if (obj == null || GetType() != obj.GetType())
+            {
+                return false;
+            }
+            
+            return this == (Provider)obj;
+        }
+
+        public override int GetHashCode()
+        {
+            int hash = 0;
+            hash ^= this.Name.GetHashCode();
+            hash ^= this.Keywords.GetHashCode();
+            hash ^= this.EventLevel.GetHashCode();
+            hash ^= this.FilterData.GetHashCode();
+            return hash;
+        }
     }
 }
index dc93f1a9e0f531d5ea3b4bf395c4cc770bb2197c..fac9c13b1aac3845b1142328a3334fa23730193e 100644 (file)
@@ -66,7 +66,6 @@ namespace Microsoft.Diagnostics.Tools.Trace
 
                 if (profile.Length != 0)
                 {
-                    var profileProviders = new List<Provider>();
                     var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles
                         .FirstOrDefault(p => p.Name.Equals(profile, StringComparison.OrdinalIgnoreCase));
                     if (selectedProfile == null)
@@ -75,32 +74,7 @@ namespace Microsoft.Diagnostics.Tools.Trace
                         return ErrorCodes.ArgumentError;
                     }
 
-                    // If user defined a different key/level on the same provider via --providers option that was specified via --profile option,
-                    // --providers option takes precedence. Go through the list of providers specified and only add it if it wasn't specified
-                    // via --providers options.
-                    if (selectedProfile.Providers != null)
-                    {
-                        foreach (Provider selectedProfileProvider in selectedProfile.Providers)
-                        {
-                            bool shouldAdd = true;
-
-                            foreach (Provider providerCollectionProvider in providerCollection)
-                            {
-                                if (providerCollectionProvider.Name.Equals(selectedProfileProvider.Name))
-                                {
-                                    shouldAdd = false;
-                                    break;
-                                }
-                            }
-
-                            if (shouldAdd)
-                            {
-                                enabledBy[selectedProfileProvider.Name] = "--profile ";
-                                profileProviders.Add(selectedProfileProvider);
-                            }
-                        }
-                    }
-                    providerCollection.AddRange(profileProviders);
+                    Profile.MergeProfileAndProviders(selectedProfile, providerCollection, enabledBy);
                 }
 
 
index 4f8e2c088768347be8ed166bc31384c99ab5c965..7bc28c08877cfb9309eaff668e421f36f9c43599 100644 (file)
@@ -22,5 +22,36 @@ namespace Microsoft.Diagnostics.Tools.Trace
         public IEnumerable<Provider> Providers { get; }
 
         public string Description { get; }
+
+        public static void MergeProfileAndProviders(Profile selectedProfile, List<Provider> providerCollection, Dictionary<string, string> enabledBy)
+        {
+            var profileProviders = new List<Provider>();
+            // If user defined a different key/level on the same provider via --providers option that was specified via --profile option,
+            // --providers option takes precedence. Go through the list of providers specified and only add it if it wasn't specified
+            // via --providers options.
+            if (selectedProfile.Providers != null)
+            {
+                foreach (Provider selectedProfileProvider in selectedProfile.Providers)
+                {
+                    bool shouldAdd = true;
+
+                    foreach (Provider providerCollectionProvider in providerCollection)
+                    {
+                        if (providerCollectionProvider.Name.Equals(selectedProfileProvider.Name))
+                        {
+                            shouldAdd = false;
+                            break;
+                        }
+                    }
+
+                    if (shouldAdd)
+                    {
+                        enabledBy[selectedProfileProvider.Name] = "--profile ";
+                        profileProviders.Add(selectedProfileProvider);
+                    }
+                }
+            }
+            providerCollection.AddRange(profileProviders);
+        }
     }
 }
diff --git a/src/tests/dotnet-trace/ProfileProviderMerging.cs b/src/tests/dotnet-trace/ProfileProviderMerging.cs
new file mode 100644 (file)
index 0000000..6cf17b1
--- /dev/null
@@ -0,0 +1,46 @@
+// 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 Xunit;
+using Microsoft.Diagnostics.Tools.RuntimeClient;
+using System.Collections.Generic;
+using System.Linq;
+using System.Diagnostics.Tracing;
+
+namespace Microsoft.Diagnostics.Tools.Trace
+{
+    public class ProfileProviderMergeTests
+    {
+        [Theory]
+        [InlineData("cpu-sampling", "Microsoft-Windows-DotNETRuntime")]
+        [InlineData("gc-verbose", "Microsoft-Windows-DotNETRuntime")]
+        [InlineData("gc-collect", "Microsoft-Windows-DotNETRuntime")]
+        public void DuplicateProvider_CorrectlyOverrides(string profileName, string providerToParse)
+        {
+            Dictionary<string, string> enabledBy = new Dictionary<string, string>();
+
+            List<Provider> parsedProviders = Extensions.ToProviders(providerToParse);
+
+            foreach (var provider in parsedProviders)
+            {
+                enabledBy[provider.Name] = "--providers";
+            }
+
+            var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles
+                .FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase));
+            Assert.NotNull(selectedProfile);
+
+            Profile.MergeProfileAndProviders(selectedProfile, parsedProviders, enabledBy);
+
+            var enabledProvider = parsedProviders.SingleOrDefault(p => p.Name == "Microsoft-Windows-DotNETRuntime");
+            Assert.True(enabledProvider != default(Provider));
+
+            // Assert that our specified provider overrides the version in the profile
+            Assert.True(enabledProvider.Keywords == UInt64.MaxValue);
+            Assert.True(enabledProvider.EventLevel == EventLevel.Verbose);
+            Assert.True(enabledBy[enabledProvider.Name] == "--providers");
+        }
+    }
+}
\ No newline at end of file