Skip to content

Commit 127a2e5

Browse files
committed
testing refactor and passing baseline
1 parent b7987e8 commit 127a2e5

File tree

11 files changed

+131
-236
lines changed

11 files changed

+131
-236
lines changed

dotnet/AutoGen.sln

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{686480
4242
EndProject
4343
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloAgentState", "samples\Hello\HelloAgentState\HelloAgentState.csproj", "{64EF61E7-00A6-4E5E-9808-62E10993A0E5}"
4444
EndProject
45-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Agents.Tests", "test\Microsoft.AutoGen.Agents.Tests\Microsoft.AutoGen.Agents.Tests.csproj", "{394FDAF8-74F9-4977-94A5-3371737EB774}"
46-
EndProject
4745
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello.Shared", "samples\Hello\Hello.Shared\Hello.Shared.csproj", "{F16058BE-1BE9-79AE-119E-A220BC6AADED}"
4846
EndProject
4947
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Contracts", "src\Microsoft.AutoGen\Core\Microsoft.AutoGen.Contracts.csproj", "{1222A7E7-F9C7-D89F-4953-EE063A7FDE49}"
@@ -100,10 +98,6 @@ Global
10098
{64EF61E7-00A6-4E5E-9808-62E10993A0E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
10199
{64EF61E7-00A6-4E5E-9808-62E10993A0E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
102100
{64EF61E7-00A6-4E5E-9808-62E10993A0E5}.Release|Any CPU.Build.0 = Release|Any CPU
103-
{394FDAF8-74F9-4977-94A5-3371737EB774}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
104-
{394FDAF8-74F9-4977-94A5-3371737EB774}.Debug|Any CPU.Build.0 = Debug|Any CPU
105-
{394FDAF8-74F9-4977-94A5-3371737EB774}.Release|Any CPU.ActiveCfg = Release|Any CPU
106-
{394FDAF8-74F9-4977-94A5-3371737EB774}.Release|Any CPU.Build.0 = Release|Any CPU
107101
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
108102
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Debug|Any CPU.Build.0 = Debug|Any CPU
109103
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -155,7 +149,6 @@ Global
155149
{A20B9894-F352-4338-872A-F215A241D43D} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
156150
{8F7560CF-EEBB-4333-A69F-838CA40FD85D} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
157151
{64EF61E7-00A6-4E5E-9808-62E10993A0E5} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
158-
{394FDAF8-74F9-4977-94A5-3371737EB774} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
159152
{F16058BE-1BE9-79AE-119E-A220BC6AADED} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
160153
{1222A7E7-F9C7-D89F-4953-EE063A7FDE49} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
161154
{03E150AA-3B93-B2AA-3141-1AE4A3481115} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<ProjectConfiguration>
2+
<Settings>
3+
<IgnoredTests>
4+
<AllTestsSelector />
5+
</IgnoredTests>
6+
</Settings>
7+
</ProjectConfiguration>

dotnet/src/Microsoft.AutoGen/Client/Agent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private async Task OnRequestCoreAsync(RpcRequest request, CancellationToken canc
200200
/// <param name="method">The method to call.</param>
201201
/// <param name="parameters">The parameters for the method.</param>
202202
/// <returns>A task representing the asynchronous operation, containing the RPC response.</returns>
203-
protected async Task<RpcResponse> RequestAsync(AgentId target, string method, Dictionary<string, string> parameters)
203+
public async Task<RpcResponse> RequestAsync(AgentId target, string method, Dictionary<string, string> parameters)
204204
{
205205
var requestId = Guid.NewGuid().ToString();
206206
var request = new RpcRequest

dotnet/src/Microsoft.AutoGen/Client/AgentWorker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private Agent GetOrActivateAgent(AgentId agentId)
201201
if (_agentTypes.TryGetValue(agentId.Type, out var agentType))
202202
{
203203
var context = new RuntimeContext(agentId, this, _serviceProvider.GetRequiredService<ILogger<Agent>>(), _distributedContextPropagator);
204-
agent = (Agent)ActivatorUtilities.CreateInstance(_serviceProvider, agentType, context);
204+
agent = (Agent)ActivatorUtilities.CreateInstance(_serviceProvider, agentType);
205205
Agent.Initialize(context, agent);
206206
_agents.TryAdd((agentId.Type, agentId.Key), agent);
207207
}

dotnet/test/Microsoft.AutoGen.Agents.Tests/AgentBaseTests.cs

Lines changed: 0 additions & 210 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// ClusterFixtureCollection.cs
3+
4+
using Xunit;
5+
6+
namespace Microsoft.AutoGen.Core.Tests;
7+
8+
[CollectionDefinition(Name)]
9+
public sealed class ClusterFixtureCollection : ICollectionFixture<InMemoryAgentRuntimeFixture>
10+
{
11+
public const string Name = nameof(ClusterFixtureCollection);
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// InMemoryAgentRuntimeFixture.cs
3+
4+
using System.Diagnostics;
5+
using Microsoft.AutoGen.Abstractions;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
using Moq;
10+
11+
namespace Microsoft.AutoGen.Core.Tests;
12+
13+
public sealed class InMemoryAgentRuntimeFixture : IDisposable
14+
{
15+
public InMemoryAgentRuntimeFixture()
16+
{
17+
var builder = Host.CreateApplicationBuilder();
18+
// step 1: create in-memory agent runtime
19+
// step 2: register TestAgent to that agent runtime
20+
builder
21+
.AddInMemoryWorker()
22+
.AddAgentHost()
23+
.AddAgent<TestAgent>(nameof(TestAgent));
24+
25+
AppHost = builder.Build();
26+
AppHost.StartAsync().Wait();
27+
}
28+
public IHost AppHost { get; }
29+
30+
void IDisposable.Dispose()
31+
{
32+
AppHost.StopAsync().Wait();
33+
AppHost.Dispose();
34+
}
35+
36+
public RuntimeContext CreateContext(AgentId agentId)
37+
{
38+
var agentWorker = AppHost.Services.GetRequiredService<IAgentWorker>();
39+
var dctx = Mock.Of<DistributedContextPropagator>();
40+
var logger = Mock.Of<ILogger<Agent>>();
41+
return new RuntimeContext(agentId, agentWorker, logger, dctx);
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// InMemoryAgentTests.cs
3+
4+
using FluentAssertions;
5+
using Google.Protobuf.Reflection;
6+
using Microsoft.AutoGen.Abstractions;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Tests.Events;
9+
using Xunit;
10+
11+
namespace Microsoft.AutoGen.Core.Tests;
12+
13+
[Collection(ClusterFixtureCollection.Name)]
14+
public partial class InMemoryAgentTests(InMemoryAgentRuntimeFixture fixture)
15+
{
16+
private readonly InMemoryAgentRuntimeFixture _fixture = fixture;
17+
18+
[Fact]
19+
public async Task ItInvokeRightHandlerTestAsync()
20+
{
21+
var context = _fixture.CreateContext(new AgentId { Type = "", Key = "" });
22+
var agent = new TestAgent(new EventTypes(TypeRegistry.Empty, [], []));
23+
Agent.Initialize(context, agent);
24+
await agent.Handle(new TextMessage { Source = "test", Message = "Wow" }, CancellationToken.None);
25+
26+
TestAgent.ReceivedMessages.Count.Should().Be(1);
27+
TestAgent.ReceivedMessages["test"].Should().Be("Wow");
28+
}
29+
30+
[Fact]
31+
public async Task Agent_Handles_Event()
32+
{
33+
var agentId = new AgentId { Type = "test", Key = "1" };
34+
var context = _fixture.CreateContext(agentId);
35+
var client = _fixture.AppHost.Services.GetRequiredService<Client>();
36+
Agent.Initialize(context, client);
37+
var evt = new TextMessage { Message = $"wow{agentId.Key}", Source = nameof(Agent_Handles_Event) };
38+
await client.PublishEventAsync(evt, agentId.Key, CancellationToken.None);
39+
40+
// wait for 10 seconds
41+
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
42+
while (!TestAgent.ReceivedMessages.ContainsKey(nameof(Agent_Handles_Event)) && !cts.Token.IsCancellationRequested)
43+
{
44+
await Task.Delay(100);
45+
}
46+
47+
TestAgent.ReceivedMessages[nameof(Agent_Handles_Event)].Should().NotBeNull();
48+
TestAgent.ReceivedMessages[nameof(Agent_Handles_Event)].Should().Be($"wow{agentId.Key}");
49+
}
50+
}

0 commit comments

Comments
 (0)