Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Dec 9, 2024
1 parent 2bcf891 commit f773cc2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,35 @@ private async ValueTask DispatchEventAsync(CloudEvent evt)

public async ValueTask<RegisterAgentTypeResponse> RegisterAgentTypeAsync(RegisterAgentTypeRequest request)
{
var connection = _workersByConnection[request.RequestId];
connection.AddSupportedType(request.Type);
_supportedAgentTypes.GetOrAdd(request.Type, _ => []).Add(connection);
_agentsToEventsMap.TryAdd(request.Type, new HashSet<string>(request.Events));
try
{
var connection = _workersByConnection[request.RequestId];
connection.AddSupportedType(request.Type);
_supportedAgentTypes.GetOrAdd(request.Type, _ => []).Add(connection);
_agentsToEventsMap.TryAdd(request.Type, new HashSet<string>(request.Events));

await _gatewayRegistry.RegisterAgentType(request.Type, _reference).ConfigureAwait(true);
var response = new RegisterAgentTypeResponse {
};
return response;
await _gatewayRegistry.RegisterAgentType(request.Type, _reference).ConfigureAwait(true);
return new RegisterAgentTypeResponse
{
Success = true,
RequestId = request.RequestId
};
}
catch (Exception ex)
{
return new RegisterAgentTypeResponse
{
Success = false,
RequestId = request.RequestId,
Error = ex.Message
};
}
}

// TODO: consider adding this back for backwards compatibility
//private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection, RegisterAgentTypeRequest msg)
//{

//}

private static async Task InvokeRequestDelegate(GrpcWorkerConnection connection, RpcRequest request, Func<RpcRequest, Task<RpcResponse>> func)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Handlers_Supports_Cancellation()
}

[Fact]
public void Can_build_handlers_lookup()
public void Can_Build_Handlers_Lookup()
{
var source = typeof(TestAgent);
var handlers = source.GetHandlersLookupTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.AutoGen.Core.Tests.Helpers;

[CollectionDefinition(Name)]
public sealed class ClusterFixtureCollection : ICollectionFixture<InMemoryAgentRuntimeFixture>
public sealed class CoreFixtureCollection : ICollectionFixture<InMemoryAgentRuntimeFixture>
{
public const string Name = nameof(ClusterFixtureCollection);
public const string Name = nameof(CoreFixtureCollection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.AutoGen.Core.Tests;

[Collection(ClusterFixtureCollection.Name)]
[Collection(CoreFixtureCollection.Name)]
public partial class InMemoryAgentTests(InMemoryAgentRuntimeFixture fixture)
{
private readonly InMemoryAgentRuntimeFixture _fixture = fixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,39 @@ public async Task Test_Message_Goes_To_Right_Worker()

}

private RegisterAgentTypeRequest CreateRegistrationRequest(EventTypes eventTypes, Type type, string requestId)
[Fact]
public async Task Test_RegisterAgent_Should_Succeed()
{
var registration = new RegisterAgentTypeRequest
{
Type = type.Name,
RequestId = requestId
};
registration.Events.AddRange(eventTypes.GetEventsForAgent(type)?.ToList());
var logger = Mock.Of<ILogger<GrpcGateway>>();
var gateway = new GrpcGateway(_fixture.Cluster.Client, logger);
var service = new GrpcGatewayService(gateway);
using var client = new TestGrpcClient();

return registration;
var assembly = typeof(PBAgent).Assembly;
var eventTypes = ReflectionHelper.GetAgentsMetadata(assembly);

await service.OpenChannel(client.RequestStream, client.ResponseStream, client.CallContext);
var responseMessage = await client.ReadNext();

var connectionId = responseMessage!.Response.RequestId;

var response = await service.RegisterAgent(CreateRegistrationRequest(eventTypes, typeof(PBAgent), connectionId), client.CallContext);
response.Success.Should().BeTrue();
}

private string GetFullName(Type type)
[Fact]
public async Task Test_RegisterAgent_Should_Fail_For_Wrong_ConnectionId()
{
return ReflectionHelper.GetMessageDescriptor(type)!.FullName;
var logger = Mock.Of<ILogger<GrpcGateway>>();
var gateway = new GrpcGateway(_fixture.Cluster.Client, logger);
var service = new GrpcGatewayService(gateway);
using var client = new TestGrpcClient();

var assembly = typeof(PBAgent).Assembly;
var eventTypes = ReflectionHelper.GetAgentsMetadata(assembly);

var response = await service.RegisterAgent(CreateRegistrationRequest(eventTypes, typeof(PBAgent), "faulty_connection_id"), client.CallContext);
response.Success.Should().BeFalse();
}

[Fact]
Expand Down Expand Up @@ -132,4 +150,21 @@ public async Task Test_GetState()

response.Should().NotBeNull();
}

private RegisterAgentTypeRequest CreateRegistrationRequest(EventTypes eventTypes, Type type, string requestId)
{
var registration = new RegisterAgentTypeRequest
{
Type = type.Name,
RequestId = requestId
};
registration.Events.AddRange(eventTypes.GetEventsForAgent(type)?.ToList());

return registration;
}

private string GetFullName(Type type)
{
return ReflectionHelper.GetMessageDescriptor(type)!.FullName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Grpc;
internal class TestGrpcClient: IDisposable
internal sealed class TestGrpcClient: IDisposable
{
public TestAsyncStreamReader<Message> RequestStream { get; }
public TestServerStreamWriter<Message> ResponseStream { get; }
Expand Down

0 comments on commit f773cc2

Please sign in to comment.