You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AgentGrpcTests.cs 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // AgentGrpcTests.cs
  3. using FluentAssertions;
  4. using Microsoft.AutoGen.Contracts;
  5. // using Microsoft.AutoGen.Core.Tests;
  6. using Microsoft.AutoGen.Core.Grpc.Tests.Protobuf;
  7. using Microsoft.Extensions.Logging;
  8. using Xunit;
  9. namespace Microsoft.AutoGen.Core.Grpc.Tests;
  10. [Trait("Category", "GRPC")]
  11. public class AgentGrpcTests : TestBase
  12. {
  13. [Fact]
  14. public async Task AgentShouldNotReceiveMessagesWhenNotSubscribedTest()
  15. {
  16. var fixture = new GrpcAgentRuntimeFixture();
  17. var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
  18. Logger<BaseAgent> logger = new(new LoggerFactory());
  19. TestProtobufAgent agent = null!;
  20. await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>
  21. {
  22. agent = new TestProtobufAgent(id, runtime, logger);
  23. return await ValueTask.FromResult(agent);
  24. });
  25. // Ensure the agent is actually created
  26. AgentId agentId = await runtime.GetAgentAsync("MyAgent", lazy: false);
  27. // Validate agent ID
  28. agentId.Should().Be(agent.Id, "Agent ID should match the registered agent");
  29. var topicType = "TestTopic";
  30. await runtime.PublishMessageAsync(new Protobuf.TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);
  31. agent.ReceivedMessages.Any().Should().BeFalse("Agent should not receive messages when not subscribed.");
  32. fixture.Dispose();
  33. }
  34. [Fact]
  35. public async Task AgentShouldReceiveMessagesWhenSubscribedTest()
  36. {
  37. var fixture = new GrpcAgentRuntimeFixture();
  38. var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
  39. Logger<BaseAgent> logger = new(new LoggerFactory());
  40. SubscribedProtobufAgent agent = null!;
  41. await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>
  42. {
  43. agent = new SubscribedProtobufAgent(id, runtime, logger);
  44. return await ValueTask.FromResult(agent);
  45. });
  46. // Ensure the agent is actually created
  47. AgentId agentId = await runtime.GetAgentAsync("MyAgent", lazy: false);
  48. // Validate agent ID
  49. agentId.Should().Be(agent.Id, "Agent ID should match the registered agent");
  50. await runtime.RegisterImplicitAgentSubscriptionsAsync<SubscribedProtobufAgent>("MyAgent");
  51. var topicType = "TestTopic";
  52. await runtime.PublishMessageAsync(new TextMessage { Source = topicType, Content = "test" }, new TopicId(topicType)).ConfigureAwait(true);
  53. // Wait for the message to be processed
  54. await Task.Delay(100);
  55. agent.ReceivedMessages.Any().Should().BeTrue("Agent should receive messages when subscribed.");
  56. fixture.Dispose();
  57. }
  58. [Fact]
  59. public async Task SendMessageAsyncShouldReturnResponseTest()
  60. {
  61. // Arrange
  62. var fixture = new GrpcAgentRuntimeFixture();
  63. var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
  64. Logger<BaseAgent> logger = new(new LoggerFactory());
  65. await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) => await ValueTask.FromResult(new TestProtobufAgent(id, runtime, logger)));
  66. var agentId = new AgentId("MyAgent", "default");
  67. var response = await runtime.SendMessageAsync(new RpcTextMessage { Source = "TestTopic", Content = "Request" }, agentId);
  68. // Assert
  69. Assert.NotNull(response);
  70. Assert.IsType<RpcTextMessage>(response);
  71. if (response is RpcTextMessage responseString)
  72. {
  73. Assert.Equal("Request", responseString.Content);
  74. }
  75. fixture.Dispose();
  76. }
  77. public class ReceiverAgent(AgentId id,
  78. IAgentRuntime runtime) : BaseAgent(id, runtime, "Receiver Agent", null),
  79. IHandle<TextMessage>
  80. {
  81. public ValueTask HandleAsync(TextMessage item, MessageContext messageContext)
  82. {
  83. ReceivedItems.Add(item.Content);
  84. return ValueTask.CompletedTask;
  85. }
  86. public List<string> ReceivedItems { get; private set; } = [];
  87. }
  88. [Fact]
  89. public async Task SubscribeAsyncRemoveSubscriptionAsyncAndGetSubscriptionsTest()
  90. {
  91. var fixture = new GrpcAgentRuntimeFixture();
  92. var runtime = (GrpcAgentRuntime)await fixture.StartAsync();
  93. ReceiverAgent? agent = null;
  94. await runtime.RegisterAgentFactoryAsync("MyAgent", async (id, runtime) =>
  95. {
  96. agent = new ReceiverAgent(id, runtime);
  97. return await ValueTask.FromResult(agent);
  98. });
  99. Assert.Null(agent);
  100. await runtime.GetAgentAsync("MyAgent", lazy: false);
  101. Assert.NotNull(agent);
  102. Assert.True(agent.ReceivedItems.Count == 0);
  103. var topicTypeName = "TestTopic";
  104. await runtime.PublishMessageAsync(new TextMessage { Source = "topic", Content = "test" }, new TopicId(topicTypeName));
  105. await Task.Delay(100);
  106. Assert.True(agent.ReceivedItems.Count == 0);
  107. var subscription = new TypeSubscription(topicTypeName, "MyAgent");
  108. await runtime.AddSubscriptionAsync(subscription);
  109. await runtime.PublishMessageAsync(new TextMessage { Source = "topic", Content = "test" }, new TopicId(topicTypeName));
  110. await Task.Delay(100);
  111. Assert.True(agent.ReceivedItems.Count == 1);
  112. Assert.Equal("test", agent.ReceivedItems[0]);
  113. await runtime.RemoveSubscriptionAsync(subscription.Id);
  114. await runtime.PublishMessageAsync(new TextMessage { Source = "topic", Content = "test" }, new TopicId(topicTypeName));
  115. await Task.Delay(100);
  116. Assert.True(agent.ReceivedItems.Count == 1);
  117. fixture.Dispose();
  118. }
  119. }