Binder tracing test: only track loads that tests are interested in (#38251)
authorElinor Fung <47805090+elinor-fung@users.noreply.github.com>
Tue, 23 Jun 2020 18:49:40 +0000 (11:49 -0700)
committerGitHub <noreply@github.com>
Tue, 23 Jun 2020 18:49:40 +0000 (11:49 -0700)
src/coreclr/tests/src/Loader/binding/tracing/BinderEventListener.cs
src/coreclr/tests/src/Loader/binding/tracing/BinderTracingTest.Basic.cs
src/coreclr/tests/src/Loader/binding/tracing/BinderTracingTest.DefaultProbing.cs
src/coreclr/tests/src/Loader/binding/tracing/BinderTracingTest.EventHandlers.cs
src/coreclr/tests/src/Loader/binding/tracing/BinderTracingTest.ResolutionFlow.cs
src/coreclr/tests/src/Loader/binding/tracing/BinderTracingTest.cs

index 65e5847..9d6a67e 100644 (file)
@@ -164,9 +164,17 @@ namespace BinderTracingTests
 
         private readonly object eventsLock = new object();
         private readonly Dictionary<Guid, BindOperation> bindOperations = new Dictionary<Guid, BindOperation>();
+        private readonly string[] loadsToTrack;
+
+        public BinderEventListener(string[] loadsToTrack, bool log = false)
+        {
+            this.loadsToTrack = loadsToTrack;
+        }
 
         public BindOperation[] WaitAndGetEventsForAssembly(AssemblyName assemblyName)
         {
+            Assert.IsTrue(IsLoadToTrack(assemblyName.Name), $"Waiting for load for untracked name: {assemblyName.Name}. Tracking loads for: {string.Join(", ", loadsToTrack)}");
+
             const int waitIntervalInMs = 50;
             int waitTimeoutInMs = Environment.GetEnvironmentVariable("COMPlus_GCStress") == null
                 ? 30 * 1000
@@ -225,6 +233,9 @@ namespace BinderTracingTests
                 case "AssemblyLoadStart":
                 {
                     BindOperation bindOperation = ParseAssemblyLoadStartEvent(data, GetDataString);
+                    if (!IsLoadToTrack(bindOperation.AssemblyName.Name))
+                        return;
+
                     lock (eventsLock)
                     {
                         Assert.IsTrue(!bindOperations.ContainsKey(data.ActivityId), "AssemblyLoadStart should not exist for same activity ID ");
@@ -239,6 +250,10 @@ namespace BinderTracingTests
                 }
                 case "AssemblyLoadStop":
                 {
+                    string assemblyName = GetDataString("AssemblyName");
+                    if (!IsLoadToTrack(new AssemblyName(assemblyName).Name))
+                        return;
+
                     bool success = (bool)GetData("Success");
                     string resultName = GetDataString("ResultAssemblyName");
                     lock (eventsLock)
@@ -261,6 +276,9 @@ namespace BinderTracingTests
                 case "ResolutionAttempted":
                 {
                     ResolutionAttempt attempt = ParseResolutionAttemptedEvent(GetData, GetDataString);
+                    if (!IsLoadToTrack(attempt.AssemblyName.Name))
+                        return;
+
                     lock (eventsLock)
                     {
                         if (!bindOperations.ContainsKey(data.ActivityId))
@@ -274,6 +292,9 @@ namespace BinderTracingTests
                 case "AssemblyLoadContextResolvingHandlerInvoked":
                 {
                     HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
+                    if (!IsLoadToTrack(handlerInvocation.AssemblyName.Name))
+                        return;
+
                     lock (eventsLock)
                     {
                         if (!bindOperations.ContainsKey(data.ActivityId))
@@ -287,6 +308,9 @@ namespace BinderTracingTests
                 case "AppDomainAssemblyResolveHandlerInvoked":
                 {
                     HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
+                    if (!IsLoadToTrack(handlerInvocation.AssemblyName.Name))
+                        return;
+
                     lock (eventsLock)
                     {
                         if (!bindOperations.ContainsKey(data.ActivityId))
@@ -300,6 +324,9 @@ namespace BinderTracingTests
                 case "AssemblyLoadFromResolveHandlerInvoked":
                 {
                     LoadFromHandlerInvocation loadFrom = ParseLoadFromHandlerInvokedEvent(GetData, GetDataString);
+                    if (!IsLoadToTrack(loadFrom.AssemblyName.Name))
+                        return;
+
                     lock (eventsLock)
                     {
                         if (!bindOperations.ContainsKey(data.ActivityId))
@@ -313,6 +340,10 @@ namespace BinderTracingTests
                 case "KnownPathProbed":
                 {
                     ProbedPath probedPath = ParseKnownPathProbedEvent(GetData, GetDataString);
+                    string name = System.IO.Path.GetFileNameWithoutExtension(probedPath.FilePath);
+                    if (!IsLoadToTrack(name.EndsWith(".ni") ? name.Remove(name.Length - 3) : name))
+                        return;
+
                     lock (eventsLock)
                     {
                         if (!bindOperations.ContainsKey(data.ActivityId))
@@ -326,6 +357,11 @@ namespace BinderTracingTests
             }
         }
 
+        private bool IsLoadToTrack(string name)
+        {
+            return this.loadsToTrack.Any(n => n.Equals(name, StringComparison.InvariantCultureIgnoreCase));
+        }
+
         private string GetMisssingAssemblyBindStartMessage(EventWrittenEventArgs data, string parsedEventAsString)
         {
             var msg = new System.Text.StringBuilder();
index fcc7973..8976d54 100644 (file)
@@ -98,7 +98,7 @@ namespace BinderTracingTests
             };
         }
 
-        [BinderTest(isolate: true)]
+        [BinderTest(isolate: true, additionalLoadsToTrack: new string[] { "System.Xml" })]
         public static BindOperation LoadFromAssemblyName()
         {
             AssemblyName assemblyName = new AssemblyName("System.Xml");
@@ -136,7 +136,7 @@ namespace BinderTracingTests
             };
         }
 
-        [BinderTest(isolate: true)]
+        [BinderTest(isolate: true, additionalLoadsToTrack: new string[] { "System.Xml" })]
         public static BindOperation PlatformAssembly()
         {
             var assemblyName = new AssemblyName("System.Xml");
@@ -164,7 +164,7 @@ namespace BinderTracingTests
             };
         }
 
-        [BinderTest(isolate: true, testSetup: nameof(PlatformAssembly))]
+        [BinderTest(isolate: true, testSetup: nameof(PlatformAssembly), additionalLoadsToTrack: new string[] { "System.Xml" })]
         public static BindOperation PlatformAssembly_Cached()
         {
             BindOperation bind = PlatformAssembly();
index 60372b4..d878a59 100644 (file)
@@ -70,7 +70,7 @@ namespace BinderTracingTests
         //   KnownPathProbed : AppPaths             (EXE)   [COR_E_FILENOTFOUND]
         // Note: corerun always sets APP_PATH and APP_NI_PATH. In regular use cases,
         // the customer would have to explicitly configure the app to set those.
-        [BinderTest]
+        [BinderTest(additionalLoadsToTrack: new string[] { "DoesNotExist" })]
         public static BindOperation NonExistentAssembly()
         {
             string assemblyName = "DoesNotExist";
index 0d35e4f..df1f4c2 100644 (file)
@@ -71,7 +71,7 @@ namespace BinderTracingTests
             }
         }
 
-        [BinderTest]
+        [BinderTest(additionalLoadsToTrack: new string[] { SubdirectoryAssemblyName + "Mismatch" })]
         public static BindOperation AssemblyLoadContextResolving_NameMismatch()
         {
             var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
@@ -98,7 +98,7 @@ namespace BinderTracingTests
         public static BindOperation AssemblyLoadContextResolving_MultipleHandlers()
         {
             var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
-            CustomALC alc = new CustomALC(nameof(AssemblyLoadContextResolving_NameMismatch));
+            CustomALC alc = new CustomALC(nameof(AssemblyLoadContextResolving_MultipleHandlers));
             using (var handlerNull = new Handlers(HandlerReturn.Null, alc))
             using (var handlerLoad = new Handlers(HandlerReturn.RequestedAssembly, alc))
             {
@@ -175,7 +175,7 @@ namespace BinderTracingTests
             }
         }
 
-        [BinderTest]
+        [BinderTest(additionalLoadsToTrack: new string[] { SubdirectoryAssemblyName + "Mismatch" })]
         public static BindOperation AppDomainAssemblyResolve_NameMismatch()
         {
             var assemblyName = new AssemblyName(SubdirectoryAssemblyName);
@@ -229,7 +229,7 @@ namespace BinderTracingTests
             }
         }
 
-        [BinderTest(isolate: true)]
+        [BinderTest(isolate: true, additionalLoadsToTrack: new string[] { "AssemblyToLoadDependency" })]
         public static BindOperation AssemblyLoadFromResolveHandler_LoadDependency()
         {
             string assemblyPath = Helpers.GetAssemblyInSubdirectoryPath(SubdirectoryAssemblyName);
@@ -268,7 +268,7 @@ namespace BinderTracingTests
             };
         }
 
-        [BinderTest(isolate: true)]
+        [BinderTest(isolate: true, additionalLoadsToTrack: new string[] { "AssemblyToLoadDependency" })]
         public static BindOperation AssemblyLoadFromResolveHandler_MissingDependency()
         {
             string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
index d7081c8..a3eed1e 100644 (file)
@@ -181,7 +181,7 @@ namespace BinderTracingTests
         //   ResolutionAttempted : ApplicationAssemblies                (DefaultALC)    [MismatchedAssemblyName]
         //   ResolutionAttempted : AssemblyLoadContextResolvingEvent    (DefaultALC)    [AssemblyNotFound]
         //   ResolutionAttempted : AppDomainAssemblyResolveEvent        (DefaultALC)    [AssemblyNotFound]
-        [BinderTest(isolate: true)]
+        [BinderTest(isolate: true, additionalLoadsToTrack: new string[] { DependentAssemblyName + "_Copy" } )]
         public static BindOperation ApplicationAssemblies_MismatchedAssemblyName()
         {
             var assemblyName = new AssemblyName($"{DependentAssemblyName}_Copy, Culture=neutral, PublicKeyToken=null");
@@ -273,7 +273,7 @@ namespace BinderTracingTests
         //   ResolutionAttempted : FindInLoadContext                    (DefaultALC)    [AssemblyNotFound]
         //   ResolutionAttempted : ApplicationAssemblies                (DefaultALC)    [Success]
         //   ResolutionAttempted : DefaultAssemblyLoadContextFallback   (CustomALC)     [Success]
-        [BinderTest(isolate: true)]
+        [BinderTest(isolate: true, additionalLoadsToTrack: new string[] { "System.Xml" })]
         public static BindOperation DefaultAssemblyLoadContextFallback()
         {
             var assemblyName = new AssemblyName("System.Xml");
index 736ad58..4e6111c 100644 (file)
@@ -20,10 +20,12 @@ namespace BinderTracingTests
     {
         public bool Isolate { get; private set; }
         public string TestSetup { get; private set; }
-        public BinderTestAttribute(bool isolate = false, string testSetup = null)
+        public string[] AdditionalLoadsToTrack { get; private set; }
+        public BinderTestAttribute(bool isolate = false, string testSetup = null, string[] additionalLoadsToTrack = null)
         {
             Isolate = isolate;
             TestSetup = testSetup;
+            AdditionalLoadsToTrack = additionalLoadsToTrack;
         }
     }
 
@@ -153,8 +155,19 @@ namespace BinderTracingTests
                     setupMethod.Invoke(null, new object[0]);
                 }
 
+                var loadsToTrack = new string[]
+                {
+                    Assembly.GetExecutingAssembly().GetName().Name,
+                    DependentAssemblyName,
+                    $"{DependentAssemblyName}.resources",
+                    SubdirectoryAssemblyName,
+                    $"{SubdirectoryAssemblyName}.resources",
+                };
+                if (attribute.AdditionalLoadsToTrack != null)
+                    loadsToTrack = loadsToTrack.Union(attribute.AdditionalLoadsToTrack).ToArray();
+
                 Func<BindOperation> func = (Func<BindOperation>)method.CreateDelegate(typeof(Func<BindOperation>));
-                using (var listener = new BinderEventListener())
+                using (var listener = new BinderEventListener(loadsToTrack))
                 {
                     BindOperation expected = func();
                     ValidateSingleBind(listener, expected.AssemblyName, expected);