File: Backchannel\DashboardInfoLookupTests.cs
Web Access
Project: src\tests\Aspire.Cli.Tests\Aspire.Cli.Tests.csproj (Aspire.Cli.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Aspire.Cli.Backchannel;
using Aspire.Cli.Tests.TestServices;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.Extensions.Logging.Abstractions;
 
namespace Aspire.Cli.Tests.Backchannel;
 
public class DashboardInfoLookupTests
{
    [Fact]
    public async Task GetAsync_ReturnsDashboardInfoWhenAppHostReplies()
    {
        var dashboardInfo = new GetDashboardInfoResponse { ApiBaseUrl = "http://localhost:5000", DashboardUrls = ["http://localhost:18888"] };
        var connection = new TestAppHostAuxiliaryBackchannel { DashboardInfoResponse = dashboardInfo };
 
        var result = await DashboardInfoLookup.GetAsync(connection, TimeSpan.FromSeconds(30), NullLogger.Instance, CancellationToken.None).DefaultTimeout();
 
        Assert.Same(dashboardInfo, result);
    }
 
    [Fact]
    public async Task GetAsync_ReturnsNullWhenAppHostDoesNotReplyBeforeTimeout()
    {
        // Like a StreamJsonRpc call, the reply ignores cancellation: only the local wait can stop at the deadline.
        var reply = new TaskCompletionSource<GetDashboardInfoResponse?>(TaskCreationOptions.RunContinuationsAsynchronously);
        var connection = new TestAppHostAuxiliaryBackchannel { GetDashboardInfoV2Handler = _ => reply.Task };
 
        var result = await DashboardInfoLookup.GetAsync(connection, TimeSpan.FromMilliseconds(50), NullLogger.Instance, CancellationToken.None).DefaultTimeout();
 
        Assert.Null(result);
 
        // The abandoned call can still fail later; the lookup observes that failure instead of leaving it unobserved.
        reply.SetException(new InvalidOperationException("Connection lost"));
    }
 
    [Fact]
    public async Task GetAsync_ThrowsWhenCallerCancelsWhileAppHostDoesNotReply()
    {
        var reply = new TaskCompletionSource<GetDashboardInfoResponse?>(TaskCreationOptions.RunContinuationsAsynchronously);
        var connection = new TestAppHostAuxiliaryBackchannel { GetDashboardInfoV2Handler = _ => reply.Task };
        using var cts = new CancellationTokenSource();
 
        var lookup = DashboardInfoLookup.GetAsync(connection, TimeSpan.FromSeconds(30), NullLogger.Instance, cts.Token);
        cts.Cancel();
 
        await Assert.ThrowsAnyAsync<OperationCanceledException>(() => lookup).DefaultTimeout();
    }
}