private static Future <IFuture> WaitForX(IEnumerable <IFuture> futures, int x) { if (futures == null) { throw new ArgumentException("Must specify at least one future to wait on", "futures"); } var f = new Future <IFuture>(); var h = new WaitHandler(); h.Composite = f; h.State.AddRange(futures); h.Trigger = x; OnComplete oc = h.OnComplete; OnDispose od = h.OnDispose; if (h.State.Count == 0) { throw new ArgumentException("Must specify at least one future to wait on", "futures"); } f.RegisterOnDispose(h.OnCompositeDispose); foreach (IFuture _ in futures) { _.RegisterOnComplete(oc); _.RegisterOnDispose(od); } return(f); }
public void OnCompleted(Action continuation) { var oc = Registration.OnComplete(continuation); Future.RegisterOnComplete(oc); Future.RegisterOnDispose((f) => oc(f)); }
public static void BindFuture <T> (this System.Threading.Tasks.Task <T> task, Future <T> future) { task.GetAwaiter().OnCompleted(() => { future.SetResult2(task); }); future.RegisterOnDispose((_) => { task.TryCancelScope(); }); }
public void DisposingFutureInvokesOnDisposeHandlers() { bool[] invoked = new bool[1]; var f = new Future <object>(); f.RegisterOnDispose((_) => { invoked[0] = true; }); f.Dispose(); Assert.IsTrue(invoked[0]); }
public void CollectingFutureDoesNotInvokeOnDisposeHandlers() { bool[] invoked = new bool[1]; var f = new Future <object>(); f.RegisterOnDispose((_) => { invoked[0] = true; }); f = null; GC.Collect(); Assert.IsFalse(invoked[0]); }
public void FutureWrapsExceptionIfOnDisposeHandlerThrows() { var f = new Future <object>(); f.RegisterOnDispose((_) => { throw new Exception("pancakes"); }); try { f.Dispose(); Assert.Fail("Exception was swallowed"); } catch (FutureHandlerException fhe) { Assert.IsInstanceOfType(typeof(Exception), fhe.InnerException); Assert.AreEqual("pancakes", fhe.InnerException.Message); } }