mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
89 lines
2.5 KiB
C#
89 lines
2.5 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Microsoft.Agents.Workflows.UnitTests;
|
|
|
|
internal class TestingExecutor<TIn, TOut> : Executor, IDisposable
|
|
{
|
|
private readonly bool _loop;
|
|
private readonly Func<TIn, IWorkflowContext, CancellationToken, ValueTask<TOut>>[] _actions;
|
|
private readonly HashSet<CancellationToken> _linkedTokens = new();
|
|
private CancellationTokenSource _internalCts = new();
|
|
|
|
public TestingExecutor(string? id = null, bool loop = false, params Func<TIn, IWorkflowContext, CancellationToken, ValueTask<TOut>>[] actions) : base(id)
|
|
{
|
|
this._loop = loop;
|
|
this._actions = actions;
|
|
}
|
|
|
|
public void UnlinkCancellation(CancellationToken token)
|
|
{
|
|
this._linkedTokens.Remove(token);
|
|
}
|
|
|
|
public void LinkCancellation(CancellationToken token)
|
|
{
|
|
this._linkedTokens.Add(token);
|
|
CancellationTokenSource tokenSource = CancellationTokenSource.CreateLinkedTokenSource(this._linkedTokens.ToArray());
|
|
tokenSource = Interlocked.Exchange(ref this._internalCts, tokenSource);
|
|
tokenSource.Dispose();
|
|
}
|
|
|
|
public void SetCancel()
|
|
{
|
|
Volatile.Read(ref this._internalCts).Cancel();
|
|
}
|
|
|
|
protected sealed override RouteBuilder ConfigureRoutes(RouteBuilder routeBuilder)
|
|
{
|
|
return routeBuilder.AddHandler<TIn, TOut>(this.RouteToActions);
|
|
}
|
|
|
|
private int _nextActionIndex = 0;
|
|
private ValueTask<TOut> RouteToActions(TIn message, IWorkflowContext context)
|
|
{
|
|
if (this._nextActionIndex >= this._actions.Length)
|
|
{
|
|
if (this._loop)
|
|
{
|
|
this._nextActionIndex = 0;
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("No more actions to execute and looping is disabled.");
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
Func<TIn, IWorkflowContext, CancellationToken, ValueTask<TOut>> action = this._actions[this._nextActionIndex];
|
|
return action(message, context, Volatile.Read(ref this._internalCts).Token);
|
|
}
|
|
finally
|
|
{
|
|
this._nextActionIndex++;
|
|
}
|
|
}
|
|
|
|
~TestingExecutor()
|
|
{
|
|
this.Dispose(false);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
this._internalCts.Dispose();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
this.Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|