You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GrpcAgentRuntimeFixture.cs 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // GrpcAgentRuntimeFixture.cs
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AutoGen.Contracts;
  5. // using Microsoft.AutoGen.Core.Tests;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. namespace Microsoft.AutoGen.Core.Grpc.Tests;
  9. /// <summary>
  10. /// Fixture for setting up the gRPC agent runtime for testing.
  11. /// </summary>
  12. public sealed class GrpcAgentRuntimeFixture : IDisposable
  13. {
  14. private FreePortManager.PortTicket? portTicket;
  15. /// the gRPC agent runtime.
  16. public AgentsApp? AgentsApp { get; private set; }
  17. /// mock server for testing.
  18. public WebApplication? GatewayServer { get; private set; }
  19. public GrpcAgentServiceCollector GrpcRequestCollector { get; }
  20. public GrpcAgentRuntimeFixture()
  21. {
  22. GrpcRequestCollector = new GrpcAgentServiceCollector();
  23. }
  24. /// <summary>
  25. /// Start - gets a new port and starts fresh instances
  26. /// </summary>
  27. public async Task<IAgentRuntime> StartAsync(bool startRuntime = true, bool registerDefaultAgent = true)
  28. {
  29. this.portTicket = GrpcAgentRuntimeFixture.PortManager.GetAvailablePort(); // Get a new port per test run
  30. // Update environment variables so each test runs independently
  31. Environment.SetEnvironmentVariable("ASPNETCORE_HTTPS_PORTS", portTicket);
  32. Environment.SetEnvironmentVariable("AGENT_HOST", $"https://localhost:{portTicket}");
  33. Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
  34. this.GatewayServer = await this.InitializeGateway();
  35. this.AgentsApp = await this.InitializeRuntime(startRuntime, registerDefaultAgent);
  36. var runtime = AgentsApp.Services.GetRequiredService<IAgentRuntime>();
  37. return runtime;
  38. }
  39. private async Task<AgentsApp> InitializeRuntime(bool callStartAsync, bool registerDefaultAgent)
  40. {
  41. var appBuilder = new AgentsAppBuilder();
  42. appBuilder.AddGrpcAgentWorker();
  43. if (registerDefaultAgent)
  44. {
  45. appBuilder.AddAgent<TestProtobufAgent>("TestAgent");
  46. }
  47. AgentsApp result = await appBuilder.BuildAsync();
  48. if (callStartAsync)
  49. {
  50. await result.StartAsync().ConfigureAwait(true);
  51. }
  52. return result;
  53. }
  54. private async Task<WebApplication> InitializeGateway()
  55. {
  56. var builder = WebApplication.CreateBuilder();
  57. builder.Services.AddGrpc();
  58. builder.Services.AddSingleton(this.GrpcRequestCollector);
  59. WebApplication app = builder.Build();
  60. app.MapGrpcService<GrpcAgentServiceFixture>();
  61. await app.StartAsync().ConfigureAwait(true);
  62. return app;
  63. }
  64. private static readonly FreePortManager PortManager = new();
  65. /// <summary>
  66. /// Stop - stops the agent and ensures cleanup
  67. /// </summary>
  68. public void Stop()
  69. {
  70. (AgentsApp as IHost)?.StopAsync(TimeSpan.FromSeconds(30)).GetAwaiter().GetResult();
  71. GatewayServer?.StopAsync().GetAwaiter().GetResult();
  72. portTicket?.Dispose();
  73. }
  74. /// <summary>
  75. /// Dispose - Ensures cleanup after each test
  76. /// </summary>
  77. public void Dispose()
  78. {
  79. Stop();
  80. }
  81. }