Skip to content

Commit f773cc2

Browse files
committed
more tests
1 parent 2bcf891 commit f773cc2

File tree

6 files changed

+74
-24
lines changed

6 files changed

+74
-24
lines changed

dotnet/src/Microsoft.AutoGen/Microsoft.AutoGen.Runtime.Grpc/GrpcGateway.cs

+24-9
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,35 @@ private async ValueTask DispatchEventAsync(CloudEvent evt)
162162

163163
public async ValueTask<RegisterAgentTypeResponse> RegisterAgentTypeAsync(RegisterAgentTypeRequest request)
164164
{
165-
var connection = _workersByConnection[request.RequestId];
166-
connection.AddSupportedType(request.Type);
167-
_supportedAgentTypes.GetOrAdd(request.Type, _ => []).Add(connection);
168-
_agentsToEventsMap.TryAdd(request.Type, new HashSet<string>(request.Events));
165+
try
166+
{
167+
var connection = _workersByConnection[request.RequestId];
168+
connection.AddSupportedType(request.Type);
169+
_supportedAgentTypes.GetOrAdd(request.Type, _ => []).Add(connection);
170+
_agentsToEventsMap.TryAdd(request.Type, new HashSet<string>(request.Events));
169171

170-
await _gatewayRegistry.RegisterAgentType(request.Type, _reference).ConfigureAwait(true);
171-
var response = new RegisterAgentTypeResponse {
172-
};
173-
return response;
172+
await _gatewayRegistry.RegisterAgentType(request.Type, _reference).ConfigureAwait(true);
173+
return new RegisterAgentTypeResponse
174+
{
175+
Success = true,
176+
RequestId = request.RequestId
177+
};
178+
}
179+
catch (Exception ex)
180+
{
181+
return new RegisterAgentTypeResponse
182+
{
183+
Success = false,
184+
RequestId = request.RequestId,
185+
Error = ex.Message
186+
};
187+
}
174188
}
175189

190+
// TODO: consider adding this back for backwards compatibility
176191
//private async ValueTask RegisterAgentTypeAsync(GrpcWorkerConnection connection, RegisterAgentTypeRequest msg)
177192
//{
178-
193+
179194
//}
180195

181196
private static async Task InvokeRequestDelegate(GrpcWorkerConnection connection, RpcRequest request, Func<RpcRequest, Task<RpcResponse>> func)

dotnet/test/Microsoft.AutoGen.Core.Tests/HandleInterfaceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Handlers_Supports_Cancellation()
3030
}
3131

3232
[Fact]
33-
public void Can_build_handlers_lookup()
33+
public void Can_Build_Handlers_Lookup()
3434
{
3535
var source = typeof(TestAgent);
3636
var handlers = source.GetHandlersLookupTable();

dotnet/test/Microsoft.AutoGen.Core.Tests/Helpers/ClusterFixtureCollection.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Microsoft.AutoGen.Core.Tests.Helpers;
77

88
[CollectionDefinition(Name)]
9-
public sealed class ClusterFixtureCollection : ICollectionFixture<InMemoryAgentRuntimeFixture>
9+
public sealed class CoreFixtureCollection : ICollectionFixture<InMemoryAgentRuntimeFixture>
1010
{
11-
public const string Name = nameof(ClusterFixtureCollection);
11+
public const string Name = nameof(CoreFixtureCollection);
1212
}

dotnet/test/Microsoft.AutoGen.Core.Tests/InMemoryAgentTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Microsoft.AutoGen.Core.Tests;
1313

14-
[Collection(ClusterFixtureCollection.Name)]
14+
[Collection(CoreFixtureCollection.Name)]
1515
public partial class InMemoryAgentTests(InMemoryAgentRuntimeFixture fixture)
1616
{
1717
private readonly InMemoryAgentRuntimeFixture _fixture = fixture;

dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/GrpcGatewayServiceTests.cs

+45-10
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,39 @@ public async Task Test_Message_Goes_To_Right_Worker()
9090

9191
}
9292

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

102-
return registration;
101+
var assembly = typeof(PBAgent).Assembly;
102+
var eventTypes = ReflectionHelper.GetAgentsMetadata(assembly);
103+
104+
await service.OpenChannel(client.RequestStream, client.ResponseStream, client.CallContext);
105+
var responseMessage = await client.ReadNext();
106+
107+
var connectionId = responseMessage!.Response.RequestId;
108+
109+
var response = await service.RegisterAgent(CreateRegistrationRequest(eventTypes, typeof(PBAgent), connectionId), client.CallContext);
110+
response.Success.Should().BeTrue();
103111
}
104112

105-
private string GetFullName(Type type)
113+
[Fact]
114+
public async Task Test_RegisterAgent_Should_Fail_For_Wrong_ConnectionId()
106115
{
107-
return ReflectionHelper.GetMessageDescriptor(type)!.FullName;
116+
var logger = Mock.Of<ILogger<GrpcGateway>>();
117+
var gateway = new GrpcGateway(_fixture.Cluster.Client, logger);
118+
var service = new GrpcGatewayService(gateway);
119+
using var client = new TestGrpcClient();
120+
121+
var assembly = typeof(PBAgent).Assembly;
122+
var eventTypes = ReflectionHelper.GetAgentsMetadata(assembly);
123+
124+
var response = await service.RegisterAgent(CreateRegistrationRequest(eventTypes, typeof(PBAgent), "faulty_connection_id"), client.CallContext);
125+
response.Success.Should().BeFalse();
108126
}
109127

110128
[Fact]
@@ -132,4 +150,21 @@ public async Task Test_GetState()
132150

133151
response.Should().NotBeNull();
134152
}
153+
154+
private RegisterAgentTypeRequest CreateRegistrationRequest(EventTypes eventTypes, Type type, string requestId)
155+
{
156+
var registration = new RegisterAgentTypeRequest
157+
{
158+
Type = type.Name,
159+
RequestId = requestId
160+
};
161+
registration.Events.AddRange(eventTypes.GetEventsForAgent(type)?.ToList());
162+
163+
return registration;
164+
}
165+
166+
private string GetFullName(Type type)
167+
{
168+
return ReflectionHelper.GetMessageDescriptor(type)!.FullName;
169+
}
135170
}

dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/Helpers/Grpc/TestGrpcClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.AutoGen.Abstractions;
55

66
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Grpc;
7-
internal class TestGrpcClient: IDisposable
7+
internal sealed class TestGrpcClient: IDisposable
88
{
99
public TestAsyncStreamReader<Message> RequestStream { get; }
1010
public TestServerStreamWriter<Message> ResponseStream { get; }

0 commit comments

Comments
 (0)