| File: ApplicationModel\ResourceTestingContext.cs | Web Access |
| Project: src\src\Aspire.Hosting\Aspire.Hosting.csproj (Aspire.Hosting) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; namespace Aspire.Hosting.ApplicationModel; /// <summary> /// Publishes collected test results for one logical resource into the containing test run. /// </summary> /// <remarks> /// The resource output path is established internally before startup and made available to the /// runtime collector. Results are accepted only while the collector is active. /// Framework integrations are responsible for reading and translating their own result formats. /// </remarks> /// <example> /// <code lang="C#"> /// teststests.WithTesting(context => WithTesting(context => /// { /// context.PublishResult(new TestResult { Name = "example", Outcome = TestOutcome.Passed }); context.PublishResult(new TestResult { Name = "example", Outcome = TestOutcome.Passed }); /// return Task.CompletedTask; return Task.CompletedTask; /// }); /// </code> /// </example> [AspireExport] [Experimental(TestingDiagnostics.DiagnosticId, UrlFormat = TestingDiagnostics.UrlFormat)] public sealed class ResourceTestingContext { private readonly Action<TestResult> _publish; private readonly Task<bool> _resourceCompletion; private readonly object _gate = new(); private bool _closed; internal ResourceTestingContext(TestingContext testingContext, string resourceName, string outputPath, Action<TestResult> publish, Task<bool> resourceCompletion, CancellationToken cancellationToken) { TestingContext = testingContext; ResourceName = resourceName; OutputPath = outputPath; _publish = publish; _resourceCompletion = resourceCompletion; CancellationToken = cancellationToken; } /// <summary> /// Gets the context for the containing testing invocation. /// </summary> [AspireExport] public TestingContext TestingContext { get; } /// <summary> /// Gets the logical resource name associated with every result published through this context. /// </summary> [AspireExport] public string ResourceName { get; } /// <summary> /// Gets the absolute host path to this resource's private output directory. /// </summary> /// <remarks> /// Named after the logical resource directly beneath the run directory and shared by its replicas. /// Configure runner report paths independently and stage artifacts here during collection. /// Repeated <c>WithTesting</c> calls preserve this destination. /// </remarks> [AspireExport] public string OutputPath { get; } /// <summary> /// Gets the token used to cancel collection. /// </summary> [AspireExport] public CancellationToken CancellationToken { get; } /// <summary> /// Waits until every replica of this resource has reached an authoritative terminal outcome. /// </summary> /// <param name="cancellationToken">An optional token that cancels only this wait, in addition to test-run cancellation.</param> /// <returns>True if resource execution passed; otherwise false. Published test results do not affect this value.</returns> /// <remarks> /// Collection starts alongside execution. Use this helper before harvesting files written at /// process completion. It shares the run's completion tracking, handles resources that already /// finished, and waits for late exit-code notifications. Collector completion itself is not awaited. /// The default token still observes <see cref="CancellationToken"/>, including command deadlines /// and interruption, as well as any configured collector timeout. A supplied token cannot disable /// this cancellation, and cancelling this wait /// does not cancel the shared resource-completion task or other waiters. /// </remarks> /// <exception cref="OperationCanceledException">Collection has been cancelled.</exception> [AspireExport] public Task<bool> WaitForCompletionAsync(CancellationToken cancellationToken = default) { CancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested(); if (!cancellationToken.CanBeCanceled || cancellationToken == CancellationToken) { return _resourceCompletion.WaitAsync(CancellationToken); } if (!CancellationToken.CanBeCanceled) { return _resourceCompletion.WaitAsync(cancellationToken); } return WaitWithLinkedCancellationAsync(cancellationToken); } private async Task<bool> WaitWithLinkedCancellationAsync(CancellationToken cancellationToken) { using var linked = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken, cancellationToken); return await _resourceCompletion.WaitAsync(linked.Token).ConfigureAwait(false); } /// <summary> /// Publishes an observed test result for this resource. /// </summary> /// <param name="result">The framework-neutral result.</param> /// <remarks> /// Results are associated with the resource by the context. Publishing does not infer any /// other test results, and a passing result cannot override a resource execution failure. /// </remarks> /// <exception cref="ArgumentNullException">The result is null.</exception> /// <exception cref="ArgumentException">The result name, outcome, or duration is invalid.</exception> /// <exception cref="InvalidOperationException">Collection has completed.</exception> /// <exception cref="OperationCanceledException">Collection has been cancelled.</exception> [AspireExport] public void PublishResult(TestResult result) { ArgumentNullException.ThrowIfNull(result); ArgumentException.ThrowIfNullOrWhiteSpace(result.Name); if (!Enum.IsDefined(result.Outcome) || result.Duration < TimeSpan.Zero) { throw new ArgumentException("The test outcome must be known and its duration must not be negative.", nameof(result)); } lock (_gate) { CancellationToken.ThrowIfCancellationRequested(); if (_closed) { throw new InvalidOperationException("Results cannot be published after test collection completes."); } _publish(result); } } internal void Close() { lock (_gate) { _closed = true; } } }