From 86e5bc565a3d6fc35de2fcae69d827ef39ae3766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Tue, 28 Jan 2025 11:06:43 +0100 Subject: [PATCH 1/2] don't report communication error on discovery abort --- playground/MSTest1/UnitTest1.cs | 27 ++++++++++++++++++- playground/TestPlatform.Playground/Program.cs | 7 +++-- .../Client/ProxyDiscoveryManager.cs | 21 +++++++++------ 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/playground/MSTest1/UnitTest1.cs b/playground/MSTest1/UnitTest1.cs index 433521e1d9..c63a43d745 100644 --- a/playground/MSTest1/UnitTest1.cs +++ b/playground/MSTest1/UnitTest1.cs @@ -1,6 +1,12 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading; + using Microsoft.VisualStudio.TestTools.UnitTesting; namespace MSTest1; @@ -9,8 +15,10 @@ namespace MSTest1; public class UnitTest1 { [TestMethod] - public void TestMethod1() + [SleeperData] + public void TestMethod1(int i) { + var _ = i; // Thread.Sleep(1000); } @@ -50,3 +58,20 @@ public void TestMethod7() // Thread.Sleep(1000); } } + +[AttributeUsage(AttributeTargets.Method)] +internal class SleeperDataAttribute : Attribute, ITestDataSource +{ + public IEnumerable GetData(MethodInfo methodInfo) + { + Thread.Sleep(3000); + return [ + [1] + ]; + } + + public string? GetDisplayName(MethodInfo methodInfo, object?[]? data) + { + return string.Join(", ", data?.Select(d => d?.ToString() ?? "") ?? [""]); + } +} diff --git a/playground/TestPlatform.Playground/Program.cs b/playground/TestPlatform.Playground/Program.cs index 0cebf6d996..2be01c1452 100644 --- a/playground/TestPlatform.Playground/Program.cs +++ b/playground/TestPlatform.Playground/Program.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Reflection; using System.Threading; +using System.Threading.Tasks; using Microsoft.TestPlatform.VsTestConsole.TranslationLayer; using Microsoft.VisualStudio.TestPlatform.ObjectModel; @@ -89,7 +90,6 @@ static void Main() var sources = new[] { Path.Combine(playground, "bin", "MSTest1", "Debug", "net472", "MSTest1.dll"), - Path.Combine(playground, "bin", "MSTest2", "Debug", "net472", "MSTest2.dll"), // The built in .NET projects don't now work right now in Playground, there is some conflict with Arcade. // But if you create one outside of Playground it will work. //Path.Combine(playground, "bin", "MSTest1", "Debug", "net7.0", "MSTest1.dll"), @@ -142,7 +142,10 @@ static void Main() var discoveryHandler = new PlaygroundTestDiscoveryHandler(detailedOutput); var sw = Stopwatch.StartNew(); // Discovery - r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler); + r.StartSession(); + Console.WriteLine("started"); + Task.Run(() => r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler)); + Thread.Sleep(1000); r.AbortTestRun(); var discoveryDuration = sw.ElapsedMilliseconds; Console.WriteLine($"Discovery done in {discoveryDuration} ms"); sw.Restart(); diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs index a20d68a7c2..b0c527368a 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs @@ -196,20 +196,25 @@ public void DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEve private void HandleException(Exception exception) { - EqtTrace.Error("ProxyDiscoveryManager.DiscoverTests: Failed to discover tests: {0}", exception); + // If requested abort and the code below was just sending data, we will get communication exception because we try to write the channel that is already closed. + // In such case don't report the exception because user cannot do anything about it. + if (!(_proxyOperationManager != null && _proxyOperationManager.CancellationTokenSource.IsCancellationRequested && exception is CommunicationException)) + { + EqtTrace.Error("ProxyDiscoveryManager.DiscoverTests: Failed to discover tests: {0}", exception); - // Log to vs ide test output - var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = exception.ToString() }; - var rawMessage = _dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload); - HandleRawMessage(rawMessage); + // Log to vs ide test output + var testMessagePayload = new TestMessagePayload { MessageLevel = TestMessageLevel.Error, Message = exception.ToString() }; + var rawMessage = _dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload); + HandleRawMessage(rawMessage); + + // Log to vstest.console + HandleLogMessage(TestMessageLevel.Error, exception.ToString()); + } - // Log to vstest.console // Send a discovery complete to caller. Similar logic is also used in ParallelProxyDiscoveryManager.DiscoverTestsOnConcurrentManager // Aborted is `true`: in case of parallel discovery (or non shared host), an aborted message ensures another discovery manager // created to replace the current one. This will help if the current discovery manager is aborted due to irreparable error // and the test host is lost as well. - HandleLogMessage(TestMessageLevel.Error, exception.ToString()); - var discoveryCompletePayload = new DiscoveryCompletePayload { IsAborted = true, From 9a94be99b174e2fc3da5b22690c298b8bc018c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Tue, 28 Jan 2025 11:09:02 +0100 Subject: [PATCH 2/2] revert --- playground/MSTest1/UnitTest1.cs | 27 +------------------ playground/TestPlatform.Playground/Program.cs | 7 ++--- 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/playground/MSTest1/UnitTest1.cs b/playground/MSTest1/UnitTest1.cs index c63a43d745..433521e1d9 100644 --- a/playground/MSTest1/UnitTest1.cs +++ b/playground/MSTest1/UnitTest1.cs @@ -1,12 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading; - using Microsoft.VisualStudio.TestTools.UnitTesting; namespace MSTest1; @@ -15,10 +9,8 @@ namespace MSTest1; public class UnitTest1 { [TestMethod] - [SleeperData] - public void TestMethod1(int i) + public void TestMethod1() { - var _ = i; // Thread.Sleep(1000); } @@ -58,20 +50,3 @@ public void TestMethod7() // Thread.Sleep(1000); } } - -[AttributeUsage(AttributeTargets.Method)] -internal class SleeperDataAttribute : Attribute, ITestDataSource -{ - public IEnumerable GetData(MethodInfo methodInfo) - { - Thread.Sleep(3000); - return [ - [1] - ]; - } - - public string? GetDisplayName(MethodInfo methodInfo, object?[]? data) - { - return string.Join(", ", data?.Select(d => d?.ToString() ?? "") ?? [""]); - } -} diff --git a/playground/TestPlatform.Playground/Program.cs b/playground/TestPlatform.Playground/Program.cs index 2be01c1452..0cebf6d996 100644 --- a/playground/TestPlatform.Playground/Program.cs +++ b/playground/TestPlatform.Playground/Program.cs @@ -9,7 +9,6 @@ using System.Linq; using System.Reflection; using System.Threading; -using System.Threading.Tasks; using Microsoft.TestPlatform.VsTestConsole.TranslationLayer; using Microsoft.VisualStudio.TestPlatform.ObjectModel; @@ -90,6 +89,7 @@ static void Main() var sources = new[] { Path.Combine(playground, "bin", "MSTest1", "Debug", "net472", "MSTest1.dll"), + Path.Combine(playground, "bin", "MSTest2", "Debug", "net472", "MSTest2.dll"), // The built in .NET projects don't now work right now in Playground, there is some conflict with Arcade. // But if you create one outside of Playground it will work. //Path.Combine(playground, "bin", "MSTest1", "Debug", "net7.0", "MSTest1.dll"), @@ -142,10 +142,7 @@ static void Main() var discoveryHandler = new PlaygroundTestDiscoveryHandler(detailedOutput); var sw = Stopwatch.StartNew(); // Discovery - r.StartSession(); - Console.WriteLine("started"); - Task.Run(() => r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler)); - Thread.Sleep(1000); r.AbortTestRun(); + r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler); var discoveryDuration = sw.ElapsedMilliseconds; Console.WriteLine($"Discovery done in {discoveryDuration} ms"); sw.Restart();