Skip to content

A programming framework for agentic AI

test: Add test for topic-based delivery

Jacob Alber537277554e47
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs
index 617a0a2b6..712d09fe9 100644
--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs
+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/AgentTests.cs
@@ -10,9 +10,28 @@ using Xunit;
namespace Microsoft.AutoGen.Core.Tests;
-[Collection(ClusterFixtureCollection.Name)]
+//[Collection(ClusterFixtureCollection.Name)]
public class AgentTests()
{
+ [Fact]
+ public async Task Agent_ShouldNotReceiveMessages_WhenNotSubscribed()
+ {
+ var fixture = new InMemoryAgentRuntimeFixture();
+ var (_, agent) = fixture.Start();
+
+ var topicType = "TestTopic";
+
+ var subscriptions = await agent.GetSubscriptionsAsync().ConfigureAwait(true);
+ subscriptions.Any(s => s.TypeSubscription.TopicType == topicType).Should().BeFalse("Agent should not be subscribed to the topic by default.");
+
+ await agent.PublishMessageAsync(new TextMessage { Source = topicType, TextMessage_ = "test" }, topicType).ConfigureAwait(true);
+
+ //await Task.Yield();
+ await Task.Delay(100);
+
+ TestAgent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
+ }
+
/// <summary>
/// Verify that if the agent is not initialized via AgentWorker, it should throw the correct exception.
/// </summary>
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/InMemoryAgentRuntimeFixture.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/InMemoryAgentRuntimeFixture.cs
index 4231e3fb5..002efc0f3 100644
--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/InMemoryAgentRuntimeFixture.cs
+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/InMemoryAgentRuntimeFixture.cs
@@ -5,7 +5,11 @@ using Microsoft.AutoGen.Contracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
+
+using AgentRegistration = (string Name, System.Type Type);
+
namespace Microsoft.AutoGen.Core.Tests;
+
/// <summary>
/// InMemoryAgentRuntimeFixture - provides a fixture for the agent runtime.
/// </summary>
@@ -15,28 +19,43 @@ namespace Microsoft.AutoGen.Core.Tests;
/// </remarks>
public sealed class InMemoryAgentRuntimeFixture : IDisposable
{
- public InMemoryAgentRuntimeFixture()
+ private static IEnumerable<AgentRegistration> DefaultAgents = [(nameof(TestAgent), typeof(TestAgent))];
+
+ public InMemoryAgentRuntimeFixture() : this(null, null)
+ {
+ }
+
+ public InMemoryAgentRuntimeFixture(HostApplicationBuilder? hostBuilder = null, IEnumerable<AgentRegistration>? agentTypes = null)
{
- var builder = new HostApplicationBuilder();
+ var builder = hostBuilder ?? new HostApplicationBuilder();
builder.Services.TryAddSingleton(DistributedContextPropagator.Current);
- builder.AddAgentWorker()
- .AddAgent<TestAgent>(nameof(TestAgent));
+ builder.AddAgentWorker();
+
+ foreach (var agentType in agentTypes ?? DefaultAgents)
+ {
+ builder.AddAgent(agentType.Name, agentType.Type);
+ }
+
AppHost = builder.Build();
AppHost.StartAsync().Wait();
}
+
public IHost AppHost { get; }
/// <summary>
/// Start - starts the agent
/// </summary>
/// <returns>IAgentWorker, TestAgent</returns>
- public (IAgentRuntime, TestAgent) Start()
+ public (IAgentRuntime, TestAgent) Start() => Start<TestAgent>();
+
+ public (IAgentRuntime, TAgent) Start<TAgent>() where TAgent : Agent
{
- var agent = ActivatorUtilities.CreateInstance<TestAgent>(AppHost.Services);
+ var agent = ActivatorUtilities.CreateInstance<TAgent>(AppHost.Services);
var worker = AppHost.Services.GetRequiredService<IAgentRuntime>();
Agent.Initialize(worker, agent);
return (worker, agent);
}
+
/// <summary>
/// Stop - stops the agent and ensures cleanup
/// </summary>
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
index ce2918829..974e8a23b 100644
--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
@@ -20,11 +20,13 @@ public class TestAgent(
ReceivedMessages[item.Source] = item.TextMessage_;
return Task.CompletedTask;
}
+
public Task Handle(string item)
{
ReceivedItems.Add(item);
return Task.CompletedTask;
}
+
public Task Handle(int item)
{
ReceivedItems.Add(item);
@@ -47,3 +49,13 @@ public class TestAgent(
/// </summary>
public static ConcurrentDictionary<string, object> ReceivedMessages { get; private set; } = new();
}
+
+[TopicSubscription("TestEvent")]
+public class SubscribedAgent : TestAgent
+{
+ public SubscribedAgent(
+ [FromKeyedServices("AgentsMetadata")] AgentsMetadata eventTypes,
+ Logger<Agent>? logger = null) : base(eventTypes, logger)
+ {
+ }
+}