From bd9795c7bb4032f0ac85b2f158b3ead93bd1f4de Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Fri, 27 Jun 2025 07:35:29 +0200 Subject: [PATCH 1/4] Obsolete `ITestApplicationLifecycleCallbacks` and add `ITestHostApplicationLifetime` --- .../MSBuildExtensions.cs | 2 +- .../MSBuildTestApplicationLifecycleCallbacks.cs | 2 +- .../RetryExtensions.cs | 2 +- .../RetryLifecycleCallbacks.cs | 2 +- .../TrxReportExtensions.cs | 2 +- .../TrxTestApplicationLifecycleCallbacks.cs | 2 +- .../Hosts/CommonTestHost.cs | 7 +++++++ .../Hosts/TestHostBuilder.cs | 2 ++ .../Hosts/TestHostControllersTestHost.cs | 2 +- .../PublicAPI/PublicAPI.Unshipped.txt | 2 ++ .../Services/ServiceProvider.cs | 2 ++ ...Callbacks.cs => ITestHostApplicationLifetime.cs} | 11 +++++++++++ .../TestHost/ITestHostManager.cs | 7 +++++++ .../TestHost/TestHostManager.cs | 13 +++++++++++++ .../ServerMode/ServerTests.cs | 6 +++--- .../Services/ServiceProviderTests.cs | 10 +++++++++- .../TestApplicationBuilderTests.cs | 6 +++--- 17 files changed, 66 insertions(+), 14 deletions(-) rename src/Platform/Microsoft.Testing.Platform/TestHost/{ITestApplicationLifecycleCallbacks.cs => ITestHostApplicationLifetime.cs} (66%) diff --git a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildExtensions.cs index 57b93bb738..5f9509761c 100644 --- a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildExtensions.cs @@ -20,7 +20,7 @@ public static class MSBuildExtensions public static void AddMSBuild(this ITestApplicationBuilder builder) { builder.CommandLine.AddProvider(() => new MSBuildCommandLineProvider()); - builder.TestHost.AddTestApplicationLifecycleCallbacks( + builder.TestHost.AddTestHostApplicationLifetime( serviceProvider => new MSBuildTestApplicationLifecycleCallbacks( serviceProvider.GetConfiguration(), serviceProvider.GetCommandLineOptions(), diff --git a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs index 411274ac3c..b8b91ecb4f 100644 --- a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs @@ -13,7 +13,7 @@ namespace Microsoft.Testing.Extensions.MSBuild; -internal sealed class MSBuildTestApplicationLifecycleCallbacks : ITestApplicationLifecycleCallbacks, IDisposable +internal sealed class MSBuildTestApplicationLifecycleCallbacks : ITestHostApplicationLifetime, IDisposable { private readonly IConfiguration _configuration; private readonly ICommandLineOptions _commandLineOptions; diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryExtensions.cs index 0a048a39c9..544672fc03 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryExtensions.cs @@ -23,7 +23,7 @@ public static void AddRetryProvider(this ITestApplicationBuilder builder) { builder.CommandLine.AddProvider(() => new RetryCommandLineOptionsProvider()); - builder.TestHost.AddTestApplicationLifecycleCallbacks(serviceProvider + builder.TestHost.AddTestHostApplicationLifetime(serviceProvider => new RetryLifecycleCallbacks(serviceProvider)); CompositeExtensionFactory compositeExtensionFactory diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs index 5bfdd7d039..91c7fda0a1 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs @@ -16,7 +16,7 @@ namespace Microsoft.Testing.Extensions.Policy; -internal sealed class RetryLifecycleCallbacks : ITestApplicationLifecycleCallbacks, +internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime, #if NETCOREAPP IAsyncDisposable #else diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs index eb769731a0..dd204c9611 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs @@ -47,7 +47,7 @@ public static void AddTrxReportProvider(this ITestApplicationBuilder builder) serviceProvider.GetService(), serviceProvider.GetLoggerFactory().CreateLogger())); - builder.TestHost.AddTestApplicationLifecycleCallbacks(serviceProvider => + builder.TestHost.AddTestHostApplicationLifetime(serviceProvider => new TrxTestApplicationLifecycleCallbacks( serviceProvider.GetCommandLineOptions(), serviceProvider.GetEnvironment())); diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs index e48d67ca91..a9dfbcbe49 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs @@ -12,7 +12,7 @@ namespace Microsoft.Testing.Extensions.TrxReport.Abstractions; -internal sealed class TrxTestApplicationLifecycleCallbacks : ITestApplicationLifecycleCallbacks, IDisposable +internal sealed class TrxTestApplicationLifecycleCallbacks : ITestHostApplicationLifetime, IDisposable { private readonly bool _isEnabled; private readonly IEnvironment _environment; diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs index 44c99a83b1..f9145a8c8d 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs @@ -101,21 +101,25 @@ private async Task RunTestAppAsync(CancellationToken testApplicationCancell if (RunTestApplicationLifeCycleCallbacks) { // Get the test application lifecycle callbacks to be able to call the before run +#pragma warning disable CS0618 // Type or member is obsolete foreach (ITestApplicationLifecycleCallbacks testApplicationLifecycleCallbacks in ServiceProvider.GetServicesInternal()) { await testApplicationLifecycleCallbacks.BeforeRunAsync(testApplicationCancellationToken).ConfigureAwait(false); } +#pragma warning restore CS0618 // Type or member is obsolete } int exitCode = await InternalRunAsync().ConfigureAwait(false); if (RunTestApplicationLifeCycleCallbacks) { +#pragma warning disable CS0618 // Type or member is obsolete foreach (ITestApplicationLifecycleCallbacks testApplicationLifecycleCallbacks in ServiceProvider.GetServicesInternal()) { await testApplicationLifecycleCallbacks.AfterRunAsync(exitCode, testApplicationCancellationToken).ConfigureAwait(false); await DisposeHelper.DisposeAsync(testApplicationLifecycleCallbacks).ConfigureAwait(false); } +#pragma warning restore CS0618 // Type or member is obsolete } return exitCode; @@ -239,13 +243,16 @@ protected static async Task DisposeServiceProviderAsync(ServiceProvider serviceP } // We need to ensure that we won't dispose special services till the shutdown +#pragma warning disable CS0618 // Type or member is obsolete if (!isProcessShutdown && service is ITelemetryCollector or ITestApplicationLifecycleCallbacks or + ITestHostApplicationLifetime or IPushOnlyProtocol) { continue; } +#pragma warning restore CS0618 // Type or member is obsolete if (!alreadyDisposed.Contains(service)) { diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs index 90e9b551c1..5091f98f92 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs @@ -441,8 +441,10 @@ await LogTestHostCreatedAsync( systemEnvironment).ConfigureAwait(false); // Build and register the test application lifecycle callbacks. +#pragma warning disable CS0618 // Type or member is obsolete ITestApplicationLifecycleCallbacks[] testApplicationLifecycleCallback = await ((TestHostManager)TestHost).BuildTestApplicationLifecycleCallbackAsync(serviceProvider).ConfigureAwait(false); +#pragma warning restore CS0618 // Type or member is obsolete serviceProvider.AddServices(testApplicationLifecycleCallback); // ServerMode and Console mode uses different host diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.cs index 7508633fb4..5ed3207de1 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.cs @@ -290,7 +290,7 @@ protected override async Task InternalRunAsync() if (_testHostsInformation.LifetimeHandlers.Length > 0) { // We don't block the host during the 'OnTestHostProcessStartedAsync' by-design, if 'ITestHostProcessLifetimeHandler' extensions needs - // to block the execution of the test host should add an in-process extension like an 'ITestApplicationLifecycleCallbacks' and + // to block the execution of the test host should add an in-process extension like an 'ITestHostApplicationLifetime' and // wait for a connection/signal to return. TestHostProcessInformation testHostProcessInformation = new(_testHostPID.Value); foreach (ITestHostProcessLifetimeHandler lifetimeHandler in _testHostsInformation.LifetimeHandlers) diff --git a/src/Platform/Microsoft.Testing.Platform/PublicAPI/PublicAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Platform/PublicAPI/PublicAPI.Unshipped.txt index eed1e2d100..200755b523 100644 --- a/src/Platform/Microsoft.Testing.Platform/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Platform/PublicAPI/PublicAPI.Unshipped.txt @@ -1,3 +1,5 @@ #nullable enable Microsoft.Testing.Platform.Builder.ITestApplicationBuilder.RegisterTestFramework(System.Func! capabilitiesFactory, System.Func! frameworkFactory) -> Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! *REMOVED*Microsoft.Testing.Platform.Builder.ITestApplicationBuilder.RegisterTestFramework(System.Func! capabilitiesFactory, System.Func! adapterFactory) -> Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! +Microsoft.Testing.Platform.Extensions.TestHost.ITestHostApplicationLifetime +Microsoft.Testing.Platform.TestHost.ITestHostManager.AddTestHostApplicationLifetime(System.Func! testHostApplicationLifetimeFactory) -> void diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs index 556e89cc09..9e7e672adf 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs @@ -17,6 +17,7 @@ internal sealed class ServiceProvider : IServiceProvider, ICloneable public bool AllowTestAdapterFrameworkRegistration { get; set; } +#pragma warning disable CS0618 // Type or member is obsolete private static Type[] InternalOnlyExtensions => [ // TestHost @@ -28,6 +29,7 @@ internal sealed class ServiceProvider : IServiceProvider, ICloneable typeof(ITestHostEnvironmentVariableProvider), typeof(ITestHostProcessLifetimeHandler) ]; +#pragma warning restore CS0618 // Type or member is obsolete public void AddService(object service, bool throwIfSameInstanceExit = true) { diff --git a/src/Platform/Microsoft.Testing.Platform/TestHost/ITestApplicationLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostApplicationLifetime.cs similarity index 66% rename from src/Platform/Microsoft.Testing.Platform/TestHost/ITestApplicationLifecycleCallbacks.cs rename to src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostApplicationLifetime.cs index e81a4b7128..e21bb9ee44 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHost/ITestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostApplicationLifetime.cs @@ -6,6 +6,7 @@ namespace Microsoft.Testing.Platform.Extensions.TestHost; /// /// Represents the interface for test application lifecycle callbacks. /// +[Obsolete("Use ITestHostApplicationLifetime instead. This interface will be removed in v2.")] public interface ITestApplicationLifecycleCallbacks : ITestHostExtension { /// @@ -23,3 +24,13 @@ public interface ITestApplicationLifecycleCallbacks : ITestHostExtension /// A task representing the asynchronous operation. Task AfterRunAsync(int exitCode, CancellationToken cancellation); } + +/// +/// Represents the interface for test application lifecycle callbacks. +/// +#pragma warning disable CS0618 // Type or member is obsolete +public interface ITestHostApplicationLifetime : ITestHostExtension, ITestApplicationLifecycleCallbacks +#pragma warning restore CS0618 // Type or member is obsolete +{ + // In v2, move BeforeRunAsync and AfterRunAsync to ITestHostApplicationLifetime directly +} diff --git a/src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostManager.cs b/src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostManager.cs index 4be1bccb84..4fd70a7844 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostManager.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHost/ITestHostManager.cs @@ -15,8 +15,15 @@ public interface ITestHostManager /// Adds a test application lifecycle callbacks. /// /// The factory method for creating the test application lifecycle callbacks. + [Obsolete("Use 'AddTestHostApplicationLifetime' instead.")] void AddTestApplicationLifecycleCallbacks(Func testApplicationLifecycleCallbacks); + /// + /// Adds a test application lifecycle callbacks. + /// + /// The factory method for creating the test host application lifetime callbacks. + void AddTestHostApplicationLifetime(Func testHostApplicationLifetimeFactory); + /// /// Adds a data consumer. /// diff --git a/src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs b/src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs index 0e7aeed6ee..eeae8a09a4 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs @@ -16,7 +16,9 @@ internal sealed class TestHostManager : ITestHostManager private readonly List _factoryOrdering = []; // Exposed extension points +#pragma warning disable CS0618 // Type or member is obsolete private readonly List> _testApplicationLifecycleCallbacksFactories = []; +#pragma warning restore CS0618 // Type or member is obsolete private readonly List> _dataConsumerFactories = []; private readonly List> _testSessionLifetimeHandlerFactories = []; private readonly List _dataConsumersCompositeServiceFactories = []; @@ -88,12 +90,22 @@ internal async Task> TryBuildTestExecu return ActionResult.Fail(); } + [Obsolete] public void AddTestApplicationLifecycleCallbacks(Func testApplicationLifecycleCallbacks) { Guard.NotNull(testApplicationLifecycleCallbacks); _testApplicationLifecycleCallbacksFactories.Add(testApplicationLifecycleCallbacks); } + public void AddTestHostApplicationLifetime(Func testHostApplicationLifetime) + { + Guard.NotNull(testHostApplicationLifetime); +#pragma warning disable CS0612 // Type or member is obsolete + _testApplicationLifecycleCallbacksFactories.Add(testHostApplicationLifetime); +#pragma warning restore CS0612 // Type or member is obsolete + } + +#pragma warning disable CS0618 // Type or member is obsolete internal async Task BuildTestApplicationLifecycleCallbackAsync(ServiceProvider serviceProvider) { List testApplicationLifecycleCallbacks = []; @@ -120,6 +132,7 @@ internal async Task BuildTestApplicationLi return [.. testApplicationLifecycleCallbacks]; } +#pragma warning restore CS0618 // Type or member is obsolete public void AddDataConsumer(Func dataConsumerFactory) { diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs index 6d14e1a477..f3c215d8c8 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs @@ -38,7 +38,7 @@ public async Task ServerCanBeStartedAndAborted_TcpIp() => await RetryHelper.Retr TestApplicationHooks testApplicationHooks = new(); string[] args = ["--no-banner", "--server", "--client-host", "localhost", "--client-port", $"{server.Port}", "--internal-testingplatform-skipbuildercheck"]; ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args); - builder.TestHost.AddTestApplicationLifecycleCallbacks(_ => testApplicationHooks); + builder.TestHost.AddTestHostApplicationLifetime(_ => testApplicationHooks); builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new MockTestAdapter()); var testApplication = (TestApplication)await builder.BuildAsync(); testApplication.ServiceProvider.GetRequiredService().SuppressOutput(); @@ -59,7 +59,7 @@ public async Task ServerCanInitialize() string[] args = ["--no-banner", "--server", "--client-port", $"{server.Port}", "--internal-testingplatform-skipbuildercheck"]; TestApplicationHooks testApplicationHooks = new(); ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args); - builder.TestHost.AddTestApplicationLifecycleCallbacks(_ => testApplicationHooks); + builder.TestHost.AddTestHostApplicationLifetime(_ => testApplicationHooks); builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, __) => new MockTestAdapter()); var testApplication = (TestApplication)await builder.BuildAsync(); testApplication.ServiceProvider.GetRequiredService().SuppressOutput(); @@ -252,7 +252,7 @@ private static async Task WriteMessageAsync(StreamWriter writer, string message) await writer.FlushAsync(); } - private sealed class TestApplicationHooks : ITestApplicationLifecycleCallbacks, IDisposable + private sealed class TestApplicationHooks : ITestHostApplicationLifetime, IDisposable { private readonly SemaphoreSlim _waitForBeforeRunAsync = new(0, 1); diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs index 1ad71b8e80..c61a9a4f3e 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs @@ -33,6 +33,7 @@ public void GetService_InternalExtension_ShouldNotReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.IsNull(_serviceProvider.GetService()); + Assert.IsNull(_serviceProvider.GetService()); } [TestMethod] @@ -52,6 +53,7 @@ public void GetServiceInternal_InternalExtension_ShouldReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.IsNotNull(_serviceProvider.GetServiceInternal()); + Assert.IsNotNull(_serviceProvider.GetServiceInternal()); } [TestMethod] @@ -156,6 +158,7 @@ public void GetServicesInternal_ExtensionMethod_InternalExtension_ShouldReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.AreEqual(2, _serviceProvider.GetServicesInternal().Count()); + Assert.AreEqual(2, _serviceProvider.GetServicesInternal().Count()); } [TestMethod] @@ -165,6 +168,7 @@ public void GetServicesInternal_InternalExtension_ShouldNotReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.AreEqual(0, _serviceProvider.GetServicesInternal(typeof(ITestApplicationLifecycleCallbacks), stopAtFirst: false, skipInternalOnlyExtensions: true).Count()); + Assert.AreEqual(0, _serviceProvider.GetServicesInternal(typeof(ITestHostApplicationLifetime), stopAtFirst: false, skipInternalOnlyExtensions: true).Count()); } [TestMethod] @@ -174,6 +178,7 @@ public void GetServicesInternal_InternalExtension_ShouldReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.AreEqual(2, _serviceProvider.GetServicesInternal(typeof(ITestApplicationLifecycleCallbacks), stopAtFirst: false, skipInternalOnlyExtensions: false).Count()); + Assert.AreEqual(2, _serviceProvider.GetServicesInternal(typeof(ITestHostApplicationLifetime), stopAtFirst: false, skipInternalOnlyExtensions: false).Count()); } [TestMethod] @@ -183,6 +188,7 @@ public void GetServicesInternal_InternalExtension_FirstOnly_ShouldReturnOne() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.AreEqual(1, _serviceProvider.GetServicesInternal(typeof(ITestApplicationLifecycleCallbacks), stopAtFirst: true, skipInternalOnlyExtensions: false).Count()); + Assert.AreEqual(1, _serviceProvider.GetServicesInternal(typeof(ITestHostApplicationLifetime), stopAtFirst: true, skipInternalOnlyExtensions: false).Count()); } [TestMethod] @@ -192,6 +198,7 @@ public void GetServiceInternal_InternalExtension_ShouldReturnOne() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.IsNotNull(_serviceProvider.GetServiceInternal(typeof(ITestApplicationLifecycleCallbacks), skipInternalOnlyExtensions: false)); + Assert.IsNotNull(_serviceProvider.GetServiceInternal(typeof(ITestHostApplicationLifetime), skipInternalOnlyExtensions: false)); } [TestMethod] @@ -201,6 +208,7 @@ public void GetServiceInternal_InternalExtension_SkipInternalOnlyExtensions_Shou _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); Assert.IsNull(_serviceProvider.GetServiceInternal(typeof(ITestApplicationLifecycleCallbacks), skipInternalOnlyExtensions: true)); + Assert.IsNull(_serviceProvider.GetServiceInternal(typeof(ITestHostApplicationLifetime), skipInternalOnlyExtensions: true)); } private sealed class TestFramework : ITestFramework @@ -294,7 +302,7 @@ private sealed class DataConsumer : IDataConsumer public Task IsEnabledAsync() => throw new NotImplementedException(); } - private sealed class TestApplicationLifecycleCallbacks : ITestApplicationLifecycleCallbacks + private sealed class TestApplicationLifecycleCallbacks : ITestHostApplicationLifetime { public string Uid => throw new NotImplementedException(); diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs index 152e35d6e3..b59d31106a 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs @@ -30,8 +30,8 @@ public TestApplicationBuilderTests() public async Task TestApplicationLifecycleCallbacks_DuplicatedId_ShouldFail() { TestHostManager testHostManager = new(); - testHostManager.AddTestApplicationLifecycleCallbacks(_ => new ApplicationLifecycleCallbacks("duplicatedId")); - testHostManager.AddTestApplicationLifecycleCallbacks(_ => new ApplicationLifecycleCallbacks("duplicatedId")); + testHostManager.AddTestHostApplicationLifetime(_ => new ApplicationLifecycleCallbacks("duplicatedId")); + testHostManager.AddTestHostApplicationLifetime(_ => new ApplicationLifecycleCallbacks("duplicatedId")); InvalidOperationException invalidOperationException = await Assert.ThrowsExceptionAsync(() => testHostManager.BuildTestApplicationLifecycleCallbackAsync(_serviceProvider)); Assert.IsTrue(invalidOperationException.Message.Contains("duplicatedId") && invalidOperationException.Message.Contains(typeof(ApplicationLifecycleCallbacks).ToString())); } @@ -345,7 +345,7 @@ private sealed class Consumer : IDataConsumer public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken) => throw new NotImplementedException(); } - private sealed class ApplicationLifecycleCallbacks : ITestApplicationLifecycleCallbacks + private sealed class ApplicationLifecycleCallbacks : ITestHostApplicationLifetime { public ApplicationLifecycleCallbacks(string id) => Uid = id; From 88fe708eb657600da7d577505cc91385f940151a Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Fri, 27 Jun 2025 09:51:23 +0200 Subject: [PATCH 2/4] Fix --- .../Services/ServiceProviderTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs index c61a9a4f3e..8796a4378e 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs @@ -32,7 +32,9 @@ public void GetService_InternalExtension_ShouldNotReturn() Assert.IsNull(_serviceProvider.GetService()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.IsNull(_serviceProvider.GetService()); +#pragma warning restore CS0618 // Type or member is obsolete Assert.IsNull(_serviceProvider.GetService()); } @@ -52,7 +54,9 @@ public void GetServiceInternal_InternalExtension_ShouldReturn() Assert.IsNotNull(_serviceProvider.GetServiceInternal()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.IsNotNull(_serviceProvider.GetServiceInternal()); +#pragma warning restore CS0618 // Type or member is obsolete Assert.IsNotNull(_serviceProvider.GetServiceInternal()); } From 90905d522aa6d3164e265b94a2f90253fa960616 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Fri, 27 Jun 2025 09:52:25 +0200 Subject: [PATCH 3/4] Fix --- .../Services/ServiceProviderTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs index 8796a4378e..6ebf2a49b4 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/ServiceProviderTests.cs @@ -161,7 +161,9 @@ public void GetServicesInternal_ExtensionMethod_InternalExtension_ShouldReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.AreEqual(2, _serviceProvider.GetServicesInternal().Count()); +#pragma warning restore CS0618 // Type or member is obsolete Assert.AreEqual(2, _serviceProvider.GetServicesInternal().Count()); } @@ -171,7 +173,9 @@ public void GetServicesInternal_InternalExtension_ShouldNotReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.AreEqual(0, _serviceProvider.GetServicesInternal(typeof(ITestApplicationLifecycleCallbacks), stopAtFirst: false, skipInternalOnlyExtensions: true).Count()); +#pragma warning restore CS0618 // Type or member is obsolete Assert.AreEqual(0, _serviceProvider.GetServicesInternal(typeof(ITestHostApplicationLifetime), stopAtFirst: false, skipInternalOnlyExtensions: true).Count()); } @@ -181,7 +185,9 @@ public void GetServicesInternal_InternalExtension_ShouldReturn() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.AreEqual(2, _serviceProvider.GetServicesInternal(typeof(ITestApplicationLifecycleCallbacks), stopAtFirst: false, skipInternalOnlyExtensions: false).Count()); +#pragma warning restore CS0618 // Type or member is obsolete Assert.AreEqual(2, _serviceProvider.GetServicesInternal(typeof(ITestHostApplicationLifetime), stopAtFirst: false, skipInternalOnlyExtensions: false).Count()); } @@ -191,7 +197,9 @@ public void GetServicesInternal_InternalExtension_FirstOnly_ShouldReturnOne() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.AreEqual(1, _serviceProvider.GetServicesInternal(typeof(ITestApplicationLifecycleCallbacks), stopAtFirst: true, skipInternalOnlyExtensions: false).Count()); +#pragma warning restore CS0618 // Type or member is obsolete Assert.AreEqual(1, _serviceProvider.GetServicesInternal(typeof(ITestHostApplicationLifetime), stopAtFirst: true, skipInternalOnlyExtensions: false).Count()); } @@ -201,7 +209,9 @@ public void GetServiceInternal_InternalExtension_ShouldReturnOne() _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.IsNotNull(_serviceProvider.GetServiceInternal(typeof(ITestApplicationLifecycleCallbacks), skipInternalOnlyExtensions: false)); +#pragma warning restore CS0618 // Type or member is obsolete Assert.IsNotNull(_serviceProvider.GetServiceInternal(typeof(ITestHostApplicationLifetime), skipInternalOnlyExtensions: false)); } @@ -211,7 +221,9 @@ public void GetServiceInternal_InternalExtension_SkipInternalOnlyExtensions_Shou _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); _serviceProvider.AddService(new TestApplicationLifecycleCallbacks()); +#pragma warning disable CS0618 // Type or member is obsolete Assert.IsNull(_serviceProvider.GetServiceInternal(typeof(ITestApplicationLifecycleCallbacks), skipInternalOnlyExtensions: true)); +#pragma warning restore CS0618 // Type or member is obsolete Assert.IsNull(_serviceProvider.GetServiceInternal(typeof(ITestHostApplicationLifetime), skipInternalOnlyExtensions: true)); } From 25b24c181046cec25b3c96d3ebcdc2f1be254490 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Fri, 27 Jun 2025 11:36:07 +0200 Subject: [PATCH 4/4] Fix --- .../Microsoft.Testing.Platform/Services/ServiceProvider.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs index 9e7e672adf..c85d18a92b 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs @@ -22,6 +22,7 @@ internal sealed class ServiceProvider : IServiceProvider, ICloneable [ // TestHost typeof(ITestApplicationLifecycleCallbacks), + typeof(ITestHostApplicationLifetime), typeof(IDataConsumer), typeof(ITestSessionLifetimeHandler),