2025-02-05 11:34:02 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// GrpcAgentServiceFixture.cs
|
2025-02-11 16:03:02 -05:00
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
using Grpc.Core;
|
|
|
|
|
using Microsoft.AutoGen.Protobuf;
|
2025-02-11 16:03:02 -05:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
namespace Microsoft.AutoGen.Core.Grpc.Tests;
|
|
|
|
|
|
2025-02-11 16:03:02 -05:00
|
|
|
public sealed class GrpcAgentServiceCollector
|
|
|
|
|
{
|
|
|
|
|
public List<AddSubscriptionRequest> AddSubscriptionRequests { get; } = new();
|
|
|
|
|
public List<RemoveSubscriptionRequest> RemoveSubscriptionRequests { get; } = new();
|
|
|
|
|
public List<RegisterAgentTypeRequest> RegisterAgentTypeRequests { get; } = new();
|
|
|
|
|
|
|
|
|
|
internal void Clear()
|
|
|
|
|
{
|
|
|
|
|
this.AddSubscriptionRequests.Clear();
|
|
|
|
|
this.RemoveSubscriptionRequests.Clear();
|
|
|
|
|
this.RegisterAgentTypeRequests.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// This fixture is largely just a loopback as we are testing the client side logic of the GrpcAgentRuntime in isolation from the rest of the system.
|
|
|
|
|
/// </summary>
|
2025-02-11 16:03:02 -05:00
|
|
|
public class GrpcAgentServiceFixture : AgentRpc.AgentRpcBase
|
2025-02-05 11:34:02 -05:00
|
|
|
{
|
2025-02-11 16:03:02 -05:00
|
|
|
private GrpcAgentServiceCollector requestCollector;
|
|
|
|
|
public GrpcAgentServiceFixture(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
this.requestCollector = serviceProvider.GetService<GrpcAgentServiceCollector>() ?? new();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
public override async Task OpenChannel(IAsyncStreamReader<Message> requestStream, IServerStreamWriter<Message> responseStream, ServerCallContext context)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var workerProcess = new TestGrpcWorkerConnection(requestStream, responseStream, context);
|
|
|
|
|
await workerProcess.Connect().ConfigureAwait(true);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (context.CancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
public List<AddSubscriptionRequest> AddSubscriptionRequests => this.requestCollector.AddSubscriptionRequests;
|
|
|
|
|
public override async Task<AddSubscriptionResponse> AddSubscription(AddSubscriptionRequest request, ServerCallContext context)
|
|
|
|
|
{
|
|
|
|
|
this.AddSubscriptionRequests.Add(request);
|
|
|
|
|
return new AddSubscriptionResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RemoveSubscriptionRequest> RemoveSubscriptionRequests => this.requestCollector.RemoveSubscriptionRequests;
|
|
|
|
|
public override async Task<RemoveSubscriptionResponse> RemoveSubscription(RemoveSubscriptionRequest request, ServerCallContext context)
|
|
|
|
|
{
|
|
|
|
|
this.RemoveSubscriptionRequests.Add(request);
|
|
|
|
|
return new RemoveSubscriptionResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:34:02 -05:00
|
|
|
public override async Task<GetSubscriptionsResponse> GetSubscriptions(GetSubscriptionsRequest request, ServerCallContext context) => new GetSubscriptionsResponse { };
|
2025-02-11 16:03:02 -05:00
|
|
|
|
|
|
|
|
public List<RegisterAgentTypeRequest> RegisterAgentTypeRequests => this.requestCollector.RegisterAgentTypeRequests;
|
|
|
|
|
public override async Task<RegisterAgentTypeResponse> RegisterAgent(RegisterAgentTypeRequest request, ServerCallContext context)
|
|
|
|
|
{
|
|
|
|
|
this.RegisterAgentTypeRequests.Add(request);
|
|
|
|
|
return new RegisterAgentTypeResponse();
|
|
|
|
|
}
|
2025-02-05 11:34:02 -05:00
|
|
|
}
|