La soluzione che ha funzionato per me era inviare TaskScheduler come dipendenza dal codice che voglio testare (es.
MyClass(TaskScheduler asyncScheduler, TaskScheduler guiScheduler)
Dove asyncScheduler viene utilizzato per pianificare attività eseguite su thread di lavoro (blocco chiamate) e guiScheduler viene utilizzato per pianificare attività che devono essere eseguite sulla GUI (chiamate non bloccanti).
Nel test dell'unità, avrei quindi iniettato uno schedulatore specifico, ad esempio istanze CurrentThreadTaskScheduler. CurrentThreadTaskScheduler è un'implementazione dello scheduler che esegue immediatamente le attività, invece di metterle in coda.
È possibile trovare l'implementazione in Microsoft Samples per la programmazione parallela here.
io incollare il codice di riferimento rapido:
/// <summary>Provides a task scheduler that runs tasks on the current thread.</summary>
public sealed class CurrentThreadTaskScheduler : TaskScheduler
{
/// <summary>Runs the provided Task synchronously on the current thread.</summary>
/// <param name="task">The task to be executed.</param>
protected override void QueueTask(Task task)
{
TryExecuteTask(task);
}
/// <summary>Runs the provided Task synchronously on the current thread.</summary>
/// <param name="task">The task to be executed.</param>
/// <param name="taskWasPreviouslyQueued">Whether the Task was previously queued to the scheduler.</param>
/// <returns>True if the Task was successfully executed; otherwise, false.</returns>
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
return TryExecuteTask(task);
}
/// <summary>Gets the Tasks currently scheduled to this scheduler.</summary>
/// <returns>An empty enumerable, as Tasks are never queued, only executed.</returns>
protected override IEnumerable<Task> GetScheduledTasks()
{
return Enumerable.Empty<Task>();
}
/// <summary>Gets the maximum degree of parallelism for this scheduler.</summary>
public override int MaximumConcurrencyLevel { get { return 1; } }
}
fonte
2013-01-22 12:38:09
Cosa stai implementando 'ConcurrentList