[TestMethod]
//[Ignore]
[Asynchronous]
public void TestAsyncIndexPriceService()
{
_em = new DomainModel.DomainModelEntityManager();
var task1 = AsyncSerialTask.Create();
var task2 = task1.AddAsyncLogin(_em, new LoginCredential(null, null, null));
var task3 = task2.AddAction(x => Assert.IsTrue(_em.IsLoggedIn));
var task4 = task3.AddAction(x => Assert.IsTrue(_em.IsConnected));
var task5 = task4.AddAsyncAction<AsyncEventArgs>((loginEventArgs, callback) =>
_em.InitializeCacheAsync(callback,
TestAsyncIndexPriceServiceParallelCallback));
IIndexPrice index = null;
var task6 = task5.AddAsyncQuery(x => _em.Pipelines);
var task7 = task6.AddAction(x =>
{
var deal = Deal.Create(SampleData.SampleUser);
var price = deal.DealPricings[0];
deal.FlowDateStart = new DateTime(2009, 2, 10);
deal.FlowDateEnd = new DateTime(2009, 2, 10);
index = (IIndexPrice)price;
index.Adder = 0M;
index.HmlFlag = IndexHmlFlag.Medium;
index.Pipeline = Pipeline.Fetch("BANANA");
});
var task8 = task7.AddAsyncAction<InvokeServerMethodEventArgs>((serialArgs, callback) => IndexPriceService.IsValidAsync(null, index, callback));
var task9 = task8.AddAction(validArgs => Assert.AreEqual(true, Convert.ToBoolean(validArgs.Result)));
var task10 = task9.AddAsyncAction<InvokeServerMethodEventArgs>((args, callback) => IndexPriceService.GetRateAsync(null, index, callback));
var task11 = task10.AddAction(rateArgs => Assert.AreEqual(2M, Convert.ToDecimal(rateArgs.Result)));
EnqueueCallback(() => task9.Execute(null, args => TestAsyncIndexPriceServiceCallback(args)));
}
//The following method is a workaround because the AsyncParallelTaskCompletedArgs do not inherit from AsyncEventArgs, which the AsyncSerialTask needs.
public void TestAsyncIndexPriceServiceParallelCallback(AsyncParallelTaskCompletedArgs args)
{
// We passed this callback as input parm into the AsyncParallelTask
var serialCB = args.ExecutionInput as AsyncCompletedCallback<AsyncEventArgs>;
// Build new args, taking advantage of userState input argument.
AsyncEventArgs newArgs = null;
if (args.Ok)
{
// This is arbitrary - here we supply parallel task completion info ...
newArgs = new AsyncEventArgs(args.CompletionMap);
}
else
{
// Also arbitrary - if parallel task failed provide some info ...
newArgs = new AsyncEventArgs(args.Errors[0].Exception, false, args.Errors);
}
// Now call the serial task's callback action.
serialCB(newArgs);
}
public void TestAsyncIndexPriceServiceCallback(AsyncSerialTaskCompletedArgs<InvokeServerMethodEventArgs> args)
{
EnqueueTestComplete();
}
public static void InitializeCacheAsync(AsyncCompletedCallback<AsyncEventArgs> serialCallback, Action<AsyncParallelTaskCompletedArgs> completionAction)
{
var task = AsyncParallelTask.Create();
task.AddExceptionHandler(InitializationExceptionHandler);
AddInitializationQueries(task);
task.Execute(serialCallback, completionAction);
}
private static void AddInitializationQueries(AsyncParallelTask<object> task)
{
task.AddAsyncQuery(1, x => _defaultManager.AccountingMonths);
task.AddAsyncQuery(2, x => _defaultManager.DealTypes);
}
public static void InitializationExceptionHandler(AsyncParallelTaskExceptionArgs args)
{
//ToDo: log exception?
throw args.Exception;
} |