实际上,与 TPL 相比,这更多的是 lambda 繁重代码的可测试性问题。Hightechrider 的建议是一个很好的建议,但基本上您的测试仍在测试 TPL,就像它们是您的代码一样。当第一个任务结束并且 ContinueWith 开始下一个任务时,您实际上不需要测试它。
如果您的 lambda 中的代码非常大,那么将其拉出到具有明确定义的参数的更可测试的方法中可能会导致更易于阅读和更可测试的代码。您可以围绕它编写单元测试。在可能的情况下,我会尝试限制或删除单元测试中的并行性。
话虽如此,我想看看调度程序方法是否可行。这是使用来自http://code.msdn.microsoft.com/ParExtSamples的修改后的 StaTaskScheduler 的实现
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Example
{
public class Foo
{
private TaskScheduler _scheduler;
public int Bar { get; set; }
private void SomethingLong()
{
Thread.SpinWait(10000);
}
public Foo()
: this(TaskScheduler.Default)
{
}
public Foo(TaskScheduler scheduler)
{
_scheduler = scheduler;
}
public void DoWork()
{
var factory = new TaskFactory(_scheduler);
factory.StartNew(() => SomethingLong())
.ContinueWith(a => Bar = 1, _scheduler);
}
}
public class FooTests
{
[Fact]
public void Foo_should_set_Bar_to_1()
{
var sch = new StaTaskScheduler(3);
var target = new Foo(sch);
target.DoWork();
sch.Dispose();
Assert.Equal(1, target.Bar);
}
}
public sealed class StaTaskScheduler : TaskScheduler, IDisposable
{
/// <summary>Stores the queued tasks to be executed by our pool of STA threads.</summary>
private BlockingCollection<Task> _tasks;
/// <summary>The STA threads used by the scheduler.</summary>
private readonly List<Thread> _threads;
/// <summary>Initializes a new instance of the StaTaskScheduler class with the specified concurrency level.</summary>
/// <param name="numberOfThreads">The number of threads that should be created and used by this scheduler.</param>
public StaTaskScheduler(int numberOfThreads)
{
// Validate arguments
if (numberOfThreads < 1) throw new ArgumentOutOfRangeException("concurrencyLevel");
// Initialize the tasks collection
_tasks = new BlockingCollection<Task>();
// Create the threads to be used by this scheduler
_threads = Enumerable.Range(0, numberOfThreads).Select(i =>
{
var thread = new Thread(() =>
{
// Continually get the next task and try to execute it.
// This will continue until the scheduler is disposed and no more tasks remain.
foreach (var t in _tasks.GetConsumingEnumerable())
{
TryExecuteTask(t);
}
});
thread.IsBackground = true;
// NO STA REQUIREMENT!
// thread.SetApartmentState(ApartmentState.STA);
return thread;
}).ToList();
// Start all of the threads
_threads.ForEach(t => t.Start());
}
/// <summary>Queues a Task to be executed by this scheduler.</summary>
/// <param name="task">The task to be executed.</param>
protected override void QueueTask(Task task)
{
// Push it into the blocking collection of tasks
_tasks.Add(task);
}
/// <summary>Provides a list of the scheduled tasks for the debugger to consume.</summary>
/// <returns>An enumerable of all tasks currently scheduled.</returns>
protected override IEnumerable<Task> GetScheduledTasks()
{
// Serialize the contents of the blocking collection of tasks for the debugger
return _tasks.ToArray();
}
/// <summary>Determines whether a Task may be inlined.</summary>
/// <param name="task">The task to be executed.</param>
/// <param name="taskWasPreviouslyQueued">Whether the task was previously queued.</param>
/// <returns>true if the task was successfully inlined; otherwise, false.</returns>
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
// Try to inline if the current thread is STA
return
Thread.CurrentThread.GetApartmentState() == ApartmentState.STA &&
TryExecuteTask(task);
}
/// <summary>Gets the maximum concurrency level supported by this scheduler.</summary>
public override int MaximumConcurrencyLevel
{
get { return _threads.Count; }
}
/// <summary>
/// Cleans up the scheduler by indicating that no more tasks will be queued.
/// This method blocks until all threads successfully shutdown.
/// </summary>
public void Dispose()
{
if (_tasks != null)
{
// Indicate that no new tasks will be coming in
_tasks.CompleteAdding();
// Wait for all threads to finish processing tasks
foreach (var thread in _threads) thread.Join();
// Cleanup
_tasks.Dispose();
_tasks = null;
}
}
}
}