Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RetryDataConsumer> compositeExtensionFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Microsoft.Testing.Extensions.Policy;

internal sealed class RetryLifecycleCallbacks : ITestApplicationLifecycleCallbacks,
internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime,
#if NETCOREAPP
IAsyncDisposable
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void AddTrxReportProvider(this ITestApplicationBuilder builder)
serviceProvider.GetService<TrxTestApplicationLifecycleCallbacks>(),
serviceProvider.GetLoggerFactory().CreateLogger<TrxReportGenerator>()));

builder.TestHost.AddTestApplicationLifecycleCallbacks(serviceProvider =>
builder.TestHost.AddTestHostApplicationLifetime(serviceProvider =>
new TrxTestApplicationLifecycleCallbacks(
serviceProvider.GetCommandLineOptions(),
serviceProvider.GetEnvironment()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,25 @@ private async Task<int> 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<ITestApplicationLifecycleCallbacks>())
{
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<ITestApplicationLifecycleCallbacks>())
{
await testApplicationLifecycleCallbacks.AfterRunAsync(exitCode, testApplicationCancellationToken).ConfigureAwait(false);
await DisposeHelper.DisposeAsync(testApplicationLifecycleCallbacks).ConfigureAwait(false);
}
#pragma warning restore CS0618 // Type or member is obsolete
}

return exitCode;
Expand Down Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ protected override async Task<int> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable
Microsoft.Testing.Platform.Builder.ITestApplicationBuilder.RegisterTestFramework(System.Func<System.IServiceProvider!, Microsoft.Testing.Platform.Capabilities.TestFramework.ITestFrameworkCapabilities!>! capabilitiesFactory, System.Func<Microsoft.Testing.Platform.Capabilities.TestFramework.ITestFrameworkCapabilities!, System.IServiceProvider!, Microsoft.Testing.Platform.Extensions.TestFramework.ITestFramework!>! frameworkFactory) -> Microsoft.Testing.Platform.Builder.ITestApplicationBuilder!
*REMOVED*Microsoft.Testing.Platform.Builder.ITestApplicationBuilder.RegisterTestFramework(System.Func<System.IServiceProvider!, Microsoft.Testing.Platform.Capabilities.TestFramework.ITestFrameworkCapabilities!>! capabilitiesFactory, System.Func<Microsoft.Testing.Platform.Capabilities.TestFramework.ITestFrameworkCapabilities!, System.IServiceProvider!, Microsoft.Testing.Platform.Extensions.TestFramework.ITestFramework!>! adapterFactory) -> Microsoft.Testing.Platform.Builder.ITestApplicationBuilder!
Microsoft.Testing.Platform.Extensions.TestHost.ITestHostApplicationLifetime
Microsoft.Testing.Platform.TestHost.ITestHostManager.AddTestHostApplicationLifetime(System.Func<System.IServiceProvider!, Microsoft.Testing.Platform.Extensions.TestHost.ITestHostApplicationLifetime!>! testHostApplicationLifetimeFactory) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ 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
typeof(ITestApplicationLifecycleCallbacks),
typeof(ITestHostApplicationLifetime),
typeof(IDataConsumer),
typeof(ITestSessionLifetimeHandler),

// TestHostControllers
typeof(ITestHostEnvironmentVariableProvider),
typeof(ITestHostProcessLifetimeHandler)
];
#pragma warning restore CS0618 // Type or member is obsolete

public void AddService(object service, bool throwIfSameInstanceExit = true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Testing.Platform.Extensions.TestHost;
/// <summary>
/// Represents the interface for test application lifecycle callbacks.
/// </summary>
[Obsolete("Use ITestHostApplicationLifetime instead. This interface will be removed in v2.")]
public interface ITestApplicationLifecycleCallbacks : ITestHostExtension
{
/// <summary>
Expand All @@ -23,3 +24,13 @@ public interface ITestApplicationLifecycleCallbacks : ITestHostExtension
/// <returns>A task representing the asynchronous operation.</returns>
Task AfterRunAsync(int exitCode, CancellationToken cancellation);
}

/// <summary>
/// Represents the interface for test application lifecycle callbacks.
/// </summary>
#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
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ public interface ITestHostManager
/// Adds a test application lifecycle callbacks.
/// </summary>
/// <param name="testApplicationLifecycleCallbacks">The factory method for creating the test application lifecycle callbacks.</param>
[Obsolete("Use 'AddTestHostApplicationLifetime' instead.")]
void AddTestApplicationLifecycleCallbacks(Func<IServiceProvider, ITestApplicationLifecycleCallbacks> testApplicationLifecycleCallbacks);

/// <summary>
/// Adds a test application lifecycle callbacks.
/// </summary>
/// <param name="testHostApplicationLifetimeFactory">The factory method for creating the test host application lifetime callbacks.</param>
void AddTestHostApplicationLifetime(Func<IServiceProvider, ITestHostApplicationLifetime> testHostApplicationLifetimeFactory);

/// <summary>
/// Adds a data consumer.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ internal sealed class TestHostManager : ITestHostManager
private readonly List<object> _factoryOrdering = [];

// Exposed extension points
#pragma warning disable CS0618 // Type or member is obsolete
private readonly List<Func<IServiceProvider, ITestApplicationLifecycleCallbacks>> _testApplicationLifecycleCallbacksFactories = [];
#pragma warning restore CS0618 // Type or member is obsolete
private readonly List<Func<IServiceProvider, IDataConsumer>> _dataConsumerFactories = [];
private readonly List<Func<IServiceProvider, ITestSessionLifetimeHandler>> _testSessionLifetimeHandlerFactories = [];
private readonly List<ICompositeExtensionFactory> _dataConsumersCompositeServiceFactories = [];
Expand Down Expand Up @@ -88,12 +90,22 @@ internal async Task<ActionResult<ITestExecutionFilterFactory>> TryBuildTestExecu
return ActionResult.Fail<ITestExecutionFilterFactory>();
}

[Obsolete]
public void AddTestApplicationLifecycleCallbacks(Func<IServiceProvider, ITestApplicationLifecycleCallbacks> testApplicationLifecycleCallbacks)
{
Guard.NotNull(testApplicationLifecycleCallbacks);
_testApplicationLifecycleCallbacksFactories.Add(testApplicationLifecycleCallbacks);
}

public void AddTestHostApplicationLifetime(Func<IServiceProvider, ITestHostApplicationLifetime> 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<ITestApplicationLifecycleCallbacks[]> BuildTestApplicationLifecycleCallbackAsync(ServiceProvider serviceProvider)
{
List<ITestApplicationLifecycleCallbacks> testApplicationLifecycleCallbacks = [];
Expand All @@ -120,6 +132,7 @@ internal async Task<ITestApplicationLifecycleCallbacks[]> BuildTestApplicationLi

return [.. testApplicationLifecycleCallbacks];
}
#pragma warning restore CS0618 // Type or member is obsolete

public void AddDataConsumer(Func<IServiceProvider, IDataConsumer> dataConsumerFactory)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SystemConsole>().SuppressOutput();
Expand All @@ -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<SystemConsole>().SuppressOutput();
Expand Down Expand Up @@ -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);

Expand Down
Loading