Files
agent-framework/dotnet/tests/Microsoft.Agents.Orchestration.UnitTests/OrchestrationResultTests.cs
T
Stephen ToubandGitHub 456e7d1b65 Round 2 of cleanup for agent runtime and orchestrations (#164)
- Moved InProcessRuntime type into abstractions package and deleted InProcess package.
- Moved several members of IAgentRuntime to be extension methods instead, e.g. multiple GetActorAsync overloads.
- Added synchronous RegisterMessageHandler overloads and used them to avoid unnecessary async usage at call sites.
- Removed unnecessary surface area from InProcessRuntime, e.g. StopAsync, RunUntilIdleAsync, etc.
- Fixed spin loop in InProcessRuntime that would consume an entire core for the duration of the orchestration's operation.
- Removed a bunch of allocation from InProcessRuntime.
- Made a runtime optional for orchestrations, defaulting to using a temporary InProcessRuntime if none is provided.
- Removed custom delegate types from orchestrations.
- Consolidated namespaces.
- Used records to simplify message classes.
- Tweaked naming on AgentActor to make purpose of protected methods more clear.
- Removed invocation in AgentActor.InvokeAsync of empty update / isFinal parameter.
- Changed OrchestrationHandoffs to avoid needing to pass in agents duplicatively.
- Made various extension methods, such as those on OrchestrationHandoffsExtensions, into instance methods.
2025-07-11 11:12:18 -04:00

70 lines
2.5 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI.Agents.Runtime;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.Agents.Orchestration.UnitTest;
public class OrchestrationResultTests
{
[Fact]
public async Task ConstructorInitializesPropertiesCorrectlyAsync()
{
// Arrange
OrchestrationContext context = new("TestOrchestration", new TopicId("testTopic"), null, null, NullLoggerFactory.Instance, CancellationToken.None);
TaskCompletionSource<string> tcs = new();
// Act
using CancellationTokenSource cancelSource = new();
await using OrchestrationResult<string> result = new(context, tcs, cancelSource, NullLogger.Instance);
// Assert
Assert.Equal("TestOrchestration", result.Orchestration);
Assert.Equal(new TopicId("testTopic"), result.Topic);
}
[Fact]
public async Task GetValueAsyncReturnsCompletedValueWhenTaskIsCompletedAsync()
{
// Arrange
OrchestrationContext context = new("TestOrchestration", new TopicId("testTopic"), null, null, NullLoggerFactory.Instance, CancellationToken.None);
TaskCompletionSource<string> tcs = new();
using CancellationTokenSource cancelSource = new();
await using OrchestrationResult<string> result = new(context, tcs, cancelSource, NullLogger.Instance);
string expectedValue = "Result value";
// Act
tcs.SetResult(expectedValue);
string actualValue = await result.Task;
// Assert
Assert.Equal(expectedValue, actualValue);
}
[Fact]
public async Task GetValueAsyncReturnsCompletedValueWhenCompletionIsDelayedAsync()
{
// Arrange
OrchestrationContext context = new("TestOrchestration", new TopicId("testTopic"), null, null, NullLoggerFactory.Instance, CancellationToken.None);
TaskCompletionSource<int> tcs = new();
using CancellationTokenSource cancelSource = new();
await using OrchestrationResult<int> result = new(context, tcs, cancelSource, NullLogger.Instance);
int expectedValue = 42;
// Act
// Simulate delayed completion in a separate task
Task delayTask = Task.Run(async () =>
{
await Task.Delay(100);
tcs.SetResult(expectedValue);
});
int actualValue = await result.Task;
// Assert
Assert.Equal(expectedValue, actualValue);
}
}