Skip to content

Commit

Permalink
fix: remove static from TestAgent to isolate tests, fix associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmang committed Jan 30, 2025
1 parent fff201f commit 14bbb01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
41 changes: 32 additions & 9 deletions dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,58 @@ public async Task Agent_ShouldNotReceiveMessages_WhenNotSubscribed()
await runtime.StartAsync();

Logger<BaseAgent> logger = new(new LoggerFactory());
await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) => ValueTask.FromResult(new TestAgent(id, runtime, logger)));
await runtime.RegisterImplicitAgentSubscriptionsAsync<TestAgent>("MyAgent");
TestAgent agent = null!;

var topicType = "TestTopic";
await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>
{
agent = new TestAgent(id, runtime, logger);
return ValueTask.FromResult(agent);
});

// Ensure the agent is actually created
AgentId agentId = await runtime.GetAgentAsync("MyAgent", lazy: false);

// Validate agent ID
agentId.Should().Be(agent.Id, "Agent ID should match the registered agent");

await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId("TestTopic")).ConfigureAwait(true);
var topicType = "TestTopic";

await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);
await runtime.RunUntilIdleAsync();

TestAgent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
agent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
}

[Fact]
public async Task Agent_ShoulReceiveMessages_WhenSubscribed()
public async Task Agent_ShouldReceiveMessages_WhenSubscribed()
{
var runtime = new InProcessRuntime();
await runtime.StartAsync();

Logger<BaseAgent> logger = new(new LoggerFactory());
await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) => ValueTask.FromResult(new SubscribedAgent(id, runtime, logger)));
SubscribedAgent agent = null!;

await runtime.RegisterAgentFactoryAsync("MyAgent", (id, runtime) =>
{
agent = new SubscribedAgent(id, runtime, logger);
return ValueTask.FromResult(agent);
});

// Ensure the agent is actually created
AgentId agentId = await runtime.GetAgentAsync("MyAgent", lazy: false);

// Validate agent ID
agentId.Should().Be(agent.Id, "Agent ID should match the registered agent");

await runtime.RegisterImplicitAgentSubscriptionsAsync<SubscribedAgent>("MyAgent");

var topicType = "TestTopic";

await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId("TestTopic")).ConfigureAwait(true);
await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);

await runtime.RunUntilIdleAsync();

TestAgent.ReceivedMessages.Any().Should().BeTrue("Agent should receive messages when subscribed.");
agent.ReceivedMessages.Any().Should().BeTrue("Agent should receive messages when subscribed.");
}

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public ValueTask<string> HandleAsync(RpcTextMessage item, MessageContext message
/// Key: source
/// Value: message
/// </summary>
public static Dictionary<string, object> ReceivedMessages { get; private set; } = new();
private readonly Dictionary<string, object> _receivedMessages = new();
public Dictionary<string, object> ReceivedMessages => _receivedMessages;
}

[TypeSubscription("TestTopic")]
Expand Down

0 comments on commit 14bbb01

Please sign in to comment.