I recently asked a developer to write a library using the new multithreading capabilities of .NET 4.0.
He's done a great job, but I'm concerned that the Task logic is repeated throughout the code and not nicely encapsulated.
I'm also concerned that this creates a problem when it comes to testing. Normally, I test by creating a seam in the code by creating an interface and a stub/mock object to run my tests against.
I suppose this is possible using this way of doing things. It just seems that the production code logic would be very different than the test logic.
Is the solution to make tests that are parallel as well, and just repeat the Task logic in them? Or have people thought up patterns where much of the Task handling logic can be encapsulated for easy duplication?
Thanks!
Task task = Task.Factory.StartNew(() =>
{
if(cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException();
}
if (_bookHeader.EncryptionType != 0)
{
throw new MobiException("The book is encrypted");
}
ExtractText();
partReady(66.66f);
}, cancellationToken);
Task opfTask = task.ContinueWith(antecedent =>
{
if (antecedent.Status != TaskStatus.Canceled)
{
OpfDocument opf = CreateOpf();
partReady(80);
MobiDocument book = new MobiDocument()
{
Contents = _mobiHtml,
Description = opf,
Header = _bookHeader,
Sections = _sections
};
Document = book;
GC.Collect();
partReady(100);
}
});
return opfTask;
}