|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using Microsoft.Testing.Platform.Extensions; |
| 5 | +using Microsoft.Testing.Platform.Extensions.TestHost; |
| 6 | +using Microsoft.Testing.Platform.Helpers; |
| 7 | +using Microsoft.Testing.Platform.Logging; |
| 8 | +using Microsoft.Testing.Platform.Messages; |
| 9 | +using Microsoft.Testing.Platform.OutputDevice; |
| 10 | +using Microsoft.Testing.Platform.Services; |
| 11 | +using Microsoft.Testing.Platform.Telemetry; |
| 12 | + |
| 13 | +namespace Microsoft.Testing.Platform.Hosts; |
| 14 | + |
| 15 | +internal abstract partial class CommonHost |
| 16 | +{ |
| 17 | + private static async Task LogShutdownWarningAsync(IServiceProvider serviceProvider, string message) |
| 18 | + { |
| 19 | + try |
| 20 | + { |
| 21 | + if (serviceProvider.GetService<ILoggerFactory>()?.CreateLogger(nameof(CommonHost)) is { } logger) |
| 22 | + { |
| 23 | + await logger.LogWarningAsync(message).ConfigureAwait(false); |
| 24 | + } |
| 25 | + } |
| 26 | + catch (Exception) |
| 27 | + { |
| 28 | + // This runs on teardown paths that are often already unwinding, and the logging stack is itself |
| 29 | + // being torn down (a disposed LoggerFactoryProxy throws from CreateLogger, and providers can fail |
| 30 | + // from LogAsync). A failed warning must not replace the exception we were reporting, nor abort the |
| 31 | + // rest of the disposal. |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private static async Task DisplayBeforeSessionStartAsync(ProxyOutputDevice outputDevice, ITestSessionContext sessionInfo) |
| 36 | + { |
| 37 | + // Display before session start |
| 38 | + await outputDevice.DisplayBeforeSessionStartAsync(sessionInfo.CancellationToken).ConfigureAwait(false); |
| 39 | + |
| 40 | + if (outputDevice.OriginalOutputDevice is ITestSessionLifetimeHandler testSessionLifetimeHandler) |
| 41 | + { |
| 42 | + await testSessionLifetimeHandler.OnTestSessionStartingAsync(sessionInfo).ConfigureAwait(false); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static async Task DisplayAfterSessionEndRunAsync(ProxyOutputDevice outputDevice, ITestSessionContext sessionInfo) |
| 47 | + { |
| 48 | + // Display after session end even when the session cancellation token is canceled. |
| 49 | + // We intentionally pass a non-cancelable token so final output/cleanup notifications are not skipped. |
| 50 | + await outputDevice.DisplayAfterSessionEndRunAsync(CancellationToken.None).ConfigureAwait(false); |
| 51 | + |
| 52 | + // We want to ensure that the output service is the last one to run |
| 53 | + if (outputDevice.OriginalOutputDevice is ITestSessionLifetimeHandler testSessionLifetimeHandlerFinishing) |
| 54 | + { |
| 55 | + await testSessionLifetimeHandlerFinishing.OnTestSessionFinishingAsync(sessionInfo).ConfigureAwait(false); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static async Task NotifyTestSessionStartAsync(ITestSessionContext testSessionContext, BaseMessageBus baseMessageBus, ServiceProvider serviceProvider, IPlatformOpenTelemetryService? otelService) |
| 60 | + { |
| 61 | + TestSessionLifetimeHandlersContainer? testSessionLifetimeHandlersContainer = serviceProvider.GetService<TestSessionLifetimeHandlersContainer>(); |
| 62 | + if (testSessionLifetimeHandlersContainer is null) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + foreach (ITestSessionLifetimeHandler testSessionLifetimeHandler in testSessionLifetimeHandlersContainer.TestSessionLifetimeHandlers) |
| 68 | + { |
| 69 | + using IPlatformActivity? activity = otelService?.StartActivity(testSessionLifetimeHandler.Uid, testSessionLifetimeHandler.ToOTelTags()); |
| 70 | + await testSessionLifetimeHandler.OnTestSessionStartingAsync(testSessionContext).ConfigureAwait(false); |
| 71 | + } |
| 72 | + |
| 73 | + // Drain messages generated by the session start notification before to start test execution. |
| 74 | + await baseMessageBus.DrainDataAsync().ConfigureAwait(false); |
| 75 | + } |
| 76 | + |
| 77 | + private static async Task NotifyTestSessionEndAsync(ITestSessionContext testSessionContext, BaseMessageBus baseMessageBus, ServiceProvider serviceProvider, IPlatformOpenTelemetryService? otelService) |
| 78 | + { |
| 79 | + // Drain messages generated by the test session execution before to process the session end notification. |
| 80 | + await baseMessageBus.DrainDataAsync().ConfigureAwait(false); |
| 81 | + |
| 82 | + TestSessionLifetimeHandlersContainer? testSessionLifetimeHandlersContainer = serviceProvider.GetService<TestSessionLifetimeHandlersContainer>(); |
| 83 | + if (testSessionLifetimeHandlersContainer is null) |
| 84 | + { |
| 85 | + // Nothing else to notify. The bus is disabled by the caller's teardown safety net |
| 86 | + // (EnsureMessageBusDisabledAsync), which runs for every outcome of the session. |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + IShutdownProgressReporter? shutdownProgressReporter = serviceProvider.GetService<IShutdownProgressReporter>(); |
| 91 | + |
| 92 | + // First, we call OnTestSessionFinishingAsync on all non-consumers. |
| 93 | + bool hasNonDataConsumers = false; |
| 94 | + foreach (ITestSessionLifetimeHandler testSessionLifetimeHandler in testSessionLifetimeHandlersContainer.TestSessionLifetimeHandlers) |
| 95 | + { |
| 96 | + if (testSessionLifetimeHandler is IDataConsumer) |
| 97 | + { |
| 98 | + // At first, we don't call this for data consumers. |
| 99 | + // We want to do this for potentially publishers-only handlers. |
| 100 | + // The order here matters, because one handler can produce a message that is consumed by another. |
| 101 | + // By making the consumers last, we reduce the likelihood of issues related to ordering. |
| 102 | + // One case where this is important is the dependency between Code Coverage's ITestSessionLifetimeHandler and TRX's ITestSessionLifetimeHandler. |
| 103 | + // We must run Code Coverage ITestSessionLifetimeHandler implementation first, as it will publish SessionFileArtifact. |
| 104 | + // The SessionFileArtifact is expected to be consumed by TRX's implementation of ITestSessionLifetimeHandler. |
| 105 | + // In that case, ITestSessionLifetimeHandler of CC is a producer-only handler which we will run first. |
| 106 | + // Then we will drain the message bus to ensure TRX handler have consumed the SessionFileArtifact. |
| 107 | + // Then we run TRX OnTestSessionFinishingAsync. |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + hasNonDataConsumers = true; |
| 112 | + |
| 113 | + using (otelService?.StartActivity(testSessionLifetimeHandler.Uid, testSessionLifetimeHandler.ToOTelTags())) |
| 114 | + using (shutdownProgressReporter?.Track(testSessionLifetimeHandler.Uid, testSessionLifetimeHandler.DisplayName, nameof(ITestSessionLifetimeHandler.OnTestSessionFinishingAsync))) |
| 115 | + { |
| 116 | + await testSessionLifetimeHandler.OnTestSessionFinishingAsync(testSessionContext).ConfigureAwait(false); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (hasNonDataConsumers) |
| 121 | + { |
| 122 | + // At this point, we called all non-consumer handlers. |
| 123 | + // Now, we want to make sure to drain the message bus before calling the consumer handlers. |
| 124 | + // Messages produced by non-consumer handlers could be consumed by consumer handlers. |
| 125 | + await baseMessageBus.DrainDataAsync().ConfigureAwait(false); |
| 126 | + } |
| 127 | + |
| 128 | + foreach (ITestSessionLifetimeHandler testSessionLifetimeHandler in testSessionLifetimeHandlersContainer.TestSessionLifetimeHandlers) |
| 129 | + { |
| 130 | + if (testSessionLifetimeHandler is not IDataConsumer) |
| 131 | + { |
| 132 | + // We already called this for non-consumers. |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + using (otelService?.StartActivity(testSessionLifetimeHandler.Uid, testSessionLifetimeHandler.ToOTelTags())) |
| 137 | + using (shutdownProgressReporter?.Track(testSessionLifetimeHandler.Uid, testSessionLifetimeHandler.DisplayName, nameof(ITestSessionLifetimeHandler.OnTestSessionFinishingAsync))) |
| 138 | + { |
| 139 | + await testSessionLifetimeHandler.OnTestSessionFinishingAsync(testSessionContext).ConfigureAwait(false); |
| 140 | + } |
| 141 | + |
| 142 | + // OnTestSessionFinishingAsync could produce information that needs to be handled by others. |
| 143 | + // While in many cases we already handled this by calling all non-consumers first, it's possible that |
| 144 | + // two consumers might depend on each other. In that case, we solely rely on registration order. |
| 145 | + // And we drain in between. |
| 146 | + await baseMessageBus.DrainDataAsync().ConfigureAwait(false); |
| 147 | + } |
| 148 | + |
| 149 | + // We disable after the drain because it's possible that the drain will produce more messages |
| 150 | + await baseMessageBus.DrainDataAsync().ConfigureAwait(false); |
| 151 | + await baseMessageBus.DisableAsync().ConfigureAwait(false); |
| 152 | + } |
| 153 | +} |
0 commit comments