I recently asked a developer to write a library using the new .NET 4.0 multithreading capabilities.
It did a great job, but I'm worried that Task logic repeats itself throughout the code and is not beautifully encapsulated.
I am also concerned that this creates a problem when it comes to testing. I usually test by creating a seam in the code, creating an interface and a stub / layout object to run my tests.
I believe this is possible using this way of doing things. It seems that the logic of the production code will be very different from the testing logic.
Is the solution also to run parallel tests and simply repeat the logic of tasks in them? Or have people come up with patterns where most of the task processing logic can be encapsulated for easy duplication?
Thank!
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;
}
source
share