//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test {
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.Test.Mocks;
using NUnit.Framework;
using Validation;
internal abstract class CoordinatorBase {
private Func party1Action;
private Func party2Action;
protected CoordinatorBase(Func party1Action, Func party2Action) {
Requires.NotNull(party1Action, "party1Action");
Requires.NotNull(party2Action, "party2Action");
this.party1Action = party1Action;
this.party2Action = party2Action;
}
protected internal Action IncomingMessageFilter { get; set; }
protected internal Action OutgoingMessageFilter { get; set; }
internal abstract Task RunAsync();
protected async Task RunCoreAsync(T1 party1Object, T2 party2Object) {
var cts = new CancellationTokenSource();
try {
var parties = new List {
Task.Run(() => this.party1Action(party1Object, cts.Token)),
Task.Run(() => this.party2Action(party2Object, cts.Token)),
};
var completingTask = await Task.WhenAny(parties);
await completingTask; // rethrow any exception from the first completing task.
// if no exception, then block for the second task now.
await Task.WhenAll(parties);
} catch {
cts.Cancel(); // cause the second party to terminate, if necessary.
throw;
}
}
}
}