2025-02-05 11:34:02 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// GrpcAgentRuntimeFixture.cs
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AutoGen.Contracts;
|
|
|
|
|
// using Microsoft.AutoGen.Core.Tests;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.AutoGen.Core.Grpc.Tests;
|
2025-02-11 16:03:02 -05:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Fixture for setting up the gRPC agent runtime for testing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class GrpcAgentRuntimeFixture : IDisposable
|
|
|
|
|
{
|
2025-02-11 16:03:02 -05:00
|
|
|
private FreePortManager.PortTicket? portTicket;
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
/// the gRPC agent runtime.
|
2025-02-11 16:03:02 -05:00
|
|
|
public AgentsApp? AgentsApp { get; private set; }
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
/// mock server for testing.
|
2025-02-11 16:03:02 -05:00
|
|
|
public WebApplication? GatewayServer { get; private set; }
|
|
|
|
|
|
|
|
|
|
public GrpcAgentServiceCollector GrpcRequestCollector { get; }
|
2025-02-05 11:34:02 -05:00
|
|
|
|
|
|
|
|
public GrpcAgentRuntimeFixture()
|
|
|
|
|
{
|
2025-02-11 16:03:02 -05:00
|
|
|
GrpcRequestCollector = new GrpcAgentServiceCollector();
|
2025-02-05 11:34:02 -05:00
|
|
|
}
|
2025-02-11 16:03:02 -05:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Start - gets a new port and starts fresh instances
|
|
|
|
|
/// </summary>
|
2025-02-11 16:03:02 -05:00
|
|
|
public async Task<IAgentRuntime> StartAsync(bool startRuntime = true, bool registerDefaultAgent = true)
|
2025-02-05 11:34:02 -05:00
|
|
|
{
|
2025-02-11 16:03:02 -05:00
|
|
|
this.portTicket = GrpcAgentRuntimeFixture.PortManager.GetAvailablePort(); // Get a new port per test run
|
2025-02-05 11:34:02 -05:00
|
|
|
|
|
|
|
|
// Update environment variables so each test runs independently
|
2025-02-11 16:03:02 -05:00
|
|
|
Environment.SetEnvironmentVariable("ASPNETCORE_HTTPS_PORTS", portTicket);
|
|
|
|
|
Environment.SetEnvironmentVariable("AGENT_HOST", $"https://localhost:{portTicket}");
|
2025-02-05 11:34:02 -05:00
|
|
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
|
|
|
|
|
2025-02-11 16:03:02 -05:00
|
|
|
this.GatewayServer = await this.InitializeGateway();
|
|
|
|
|
this.AgentsApp = await this.InitializeRuntime(startRuntime, registerDefaultAgent);
|
|
|
|
|
var runtime = AgentsApp.Services.GetRequiredService<IAgentRuntime>();
|
2025-02-05 11:34:02 -05:00
|
|
|
|
2025-02-11 16:03:02 -05:00
|
|
|
return runtime;
|
2025-02-05 11:34:02 -05:00
|
|
|
}
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
private async Task<AgentsApp> InitializeRuntime(bool callStartAsync, bool registerDefaultAgent)
|
2025-02-05 11:34:02 -05:00
|
|
|
{
|
|
|
|
|
var appBuilder = new AgentsAppBuilder();
|
|
|
|
|
appBuilder.AddGrpcAgentWorker();
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
if (registerDefaultAgent)
|
|
|
|
|
{
|
|
|
|
|
appBuilder.AddAgent<TestProtobufAgent>("TestAgent");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AgentsApp result = await appBuilder.BuildAsync();
|
|
|
|
|
|
|
|
|
|
if (callStartAsync)
|
|
|
|
|
{
|
|
|
|
|
await result.StartAsync().ConfigureAwait(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-02-05 11:34:02 -05:00
|
|
|
}
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
private async Task<WebApplication> InitializeGateway()
|
2025-02-05 11:34:02 -05:00
|
|
|
{
|
|
|
|
|
var builder = WebApplication.CreateBuilder();
|
|
|
|
|
builder.Services.AddGrpc();
|
2025-02-11 16:03:02 -05:00
|
|
|
builder.Services.AddSingleton(this.GrpcRequestCollector);
|
|
|
|
|
|
|
|
|
|
WebApplication app = builder.Build();
|
2025-02-05 11:34:02 -05:00
|
|
|
app.MapGrpcService<GrpcAgentServiceFixture>();
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
await app.StartAsync().ConfigureAwait(true);
|
2025-02-05 11:34:02 -05:00
|
|
|
return app;
|
|
|
|
|
}
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
private static readonly FreePortManager PortManager = new();
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Stop - stops the agent and ensures cleanup
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2025-02-11 16:03:02 -05:00
|
|
|
(AgentsApp as IHost)?.StopAsync(TimeSpan.FromSeconds(30)).GetAwaiter().GetResult();
|
|
|
|
|
GatewayServer?.StopAsync().GetAwaiter().GetResult();
|
|
|
|
|
portTicket?.Dispose();
|
2025-02-05 11:34:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dispose - Ensures cleanup after each test
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|