IEnumerator TestWhenAnyCoroutine(Fiber[] fibers) { // Wait var waitAllFiber = Fiber.WhenAny(fibers); yield return waitAllFiber; // Verify index 2 was the winner and is done Assert.AreEqual(fibers[2], waitAllFiber.ResultAsObject, "Fiber at index 2 was not the winner and should be"); Assert.IsTrue(fibers[2].IsCompleted, "Fiber at index 2 should have been stopped"); // Others should still be running Assert.IsTrue(fibers.Any(f => f.Status == FiberStatus.Running), "Other fibers should have been running but were not"); // Wait 2s more yield return new YieldForSeconds(2); // Now none should be running Assert.IsTrue(fibers.All(f => f.IsCompleted), "No fibers should have been running"); }
IEnumerator TestWhenAllTimeoutCoroutine(Fiber[] fibers) { // Timeout after 2s var waitAllFiber = Fiber.WhenAll(fibers, TimeSpan.FromSeconds(2.0)); yield return waitAllFiber; // Some should still be running Assert.IsTrue(fibers.Any(f => f.Status == FiberStatus.Running), "Some fiber should have been running but was not"); // Wait 2s yield return new YieldForSeconds(2); // Now none should be running Assert.IsFalse(fibers.Any(f => f.Status == FiberStatus.Running), "No fibers should have been running"); Assert.IsNull (waitAllFiber.ResultAsObject, "Result should be null"); Assert.AreEqual (FiberStatus.Faulted, waitAllFiber.Status, "Should have been faulted"); Assert.IsInstanceOf (typeof (TimeoutException), waitAllFiber.Exception, "Should have been TimeoutException"); // Result should be false //Assert.IsNotNull(waitAllFiber.ResultAsObject, "Result should not be null"); //Assert.IsFalse((bool)waitAllFiber.ResultAsObject, "Result should have been false"); }
IEnumerator TestWhenAllChildCancellationCoroutine(Fiber [] fibers) { // One fiber will cancel var waitAllFiber = Fiber.WhenAll (fibers); yield return waitAllFiber; // None should be running Assert.IsFalse (fibers.Any (f => f.Status == FiberStatus.Running), "No fibers should have been running"); // All should be complete Assert.IsTrue (fibers.All (f => f.IsCompleted), "All should be complete"); // None should be faulted Assert.IsFalse (fibers.Any (f => f.IsFaulted), "None should be faulted"); // One should be canceled Assert.IsTrue (fibers.Count (f => f.IsCanceled) == 1, "One should be canceled"); // Result should not be present Assert.IsNull (waitAllFiber.ResultAsObject, "Result should not be present"); // Fiber should not be faulted Assert.IsFalse (waitAllFiber.IsFaulted, "Fiber should not be faulted"); // Fiber should be canceled Assert.IsTrue (waitAllFiber.IsCanceled, "Fiber should have been canceled"); }
IEnumerator TestWhenAllFaultedCoroutine(Fiber [] fibers) { // One fiber will cancel var waitAllFiber = Fiber.WhenAll (fibers); yield return waitAllFiber; // None should be running Assert.IsFalse (fibers.Any (f => f.Status == FiberStatus.Running), "No fibers should have been running"); // All should be complete Assert.IsTrue (fibers.All (f => f.IsCompleted), "All should be complete"); // 2 should be faulted Assert.AreEqual (2, fibers.Count (f => f.IsFaulted), "2 should be faulted"); // None should be canceled Assert.IsFalse (fibers.Any (f => f.IsCanceled), "None should be canceled"); // Result should not be present Assert.IsNull (waitAllFiber.ResultAsObject, "Result should not be present"); // Fiber should be faulted Assert.IsTrue (waitAllFiber.IsFaulted, "Fiber should have been faulted"); // Fiber should have exception Assert.IsNotNull (waitAllFiber.Exception, "Fiber should have exception"); // Exception should have 2 inner exceptions Assert.AreEqual (2, (waitAllFiber.Exception as AggregateException).InnerExceptions.Count, "Fiber should have 2 inner exceptions"); }
IEnumerator TestWhenAllCancellationCoroutine(Fiber[] fibers) { var cancelSource = new CancellationTokenSource(); Fiber.Factory.StartNew(WaitThenCancel(2f, cancelSource)); // Cancels after 2s var waitAllFiber = Fiber.WhenAll(fibers, cancelSource.Token); yield return waitAllFiber; // Some should still be running Assert.IsTrue(fibers.Any(f => f.Status == FiberStatus.Running), "Some fiber should have been running but was not"); // Wait 2s yield return new YieldForSeconds(2); // Now none should be running Assert.IsFalse(fibers.Any(f => f.Status == FiberStatus.Running), "No fibers should have been running"); Assert.IsNull (waitAllFiber.ResultAsObject, "Result should be null"); Assert.AreEqual (FiberStatus.Canceled, waitAllFiber.Status, "Should be canceled"); // Result should be false //Assert.IsNotNull(waitAllFiber.ResultAsObject, "Result should not be null"); //Assert.IsFalse((bool)waitAllFiber.ResultAsObject, "Result should have been false"); }