void when_running_in_parallel_with_collect()
    {
        Promise <object>   p1              = null;
        Promise <object>   p2              = null;
        Promise <object[]> promise         = null;
        List <float>       eventProgresses = null;

        context["when running with a promise that fulfills and one that fails"] = () => {
            before = () => {
                eventProgresses = new List <float>();
                p1      = TestHelper.PromiseWithError <object>("error 42", delay * 2);
                p2      = TestHelper.PromiseWithResult <object>("42", delay);
                promise = Promise.Collect(p1, p2);
                promise.OnProgressed += eventProgresses.Add;
                promise.Await();
            };

            it["is fulfilled"]                 = () => promise.state.should_be(PromiseState.Fulfilled);
            it["has progressed 100%"]          = () => promise.progress.should_be(1f);
            it["has result"]                   = () => promise.result.should_not_be_null();
            it["has no error"]                 = () => promise.error.should_be_null();
            it["has results at correct index"] = () => {
                (promise.result[0]).should_be_null();
                (promise.result[1]).should_be("42");
            };

            it["calls progress"] = () => {
                eventProgresses.Count.should_be(2);
                eventProgresses[0].should_be(0.5f);
                eventProgresses[1].should_be(1f);
            };

            it["has initial progress"] = () => {
                var deferred = new Deferred <object>();
                deferred.Progress(0.5f);
                p2 = TestHelper.PromiseWithResult <object>("42", delay);
                var collect = Promise.Collect(deferred, p2);
                collect.progress.should_be(0.25f);
                deferred.Fulfill(null);
                collect.Await();
            };
        };

        context["when all promises fail"] = () => {
            before = () => {
                eventProgresses = new List <float>();
                p1      = TestHelper.PromiseWithError <object>("error 42", delay * 2);
                p2      = TestHelper.PromiseWithError <object>("error 43", delay);
                promise = Promise.Collect(p1, p2);
                promise.OnProgressed += eventProgresses.Add;
                promise.Await();
            };

            it["progresses"] = () => {
                eventProgresses.Count.should_be(2);
                eventProgresses[0].should_be(0.5f);
                eventProgresses[1].should_be(1f);
            };
        };
    }
Exemple #2
0
    Promise <object[]> getAllTogether()
    {
        var all           = AllProgressController.GetAllPromise().Wrap <object>();
        var any           = AnyProgressController.GetAnyPromise().Wrap <object>();
        var collect       = CollectProgressController.GetCollectPromise().Wrap <object>();
        var deferred      = DeferredProgressController.GetDeferred().Wrap <object>();
        var rescue        = RescueProgressController.GetRescuePromise().Wrap <object>();
        var then          = ThenProgressController.GetTenWithThen().Wrap <object>();
        var thenCoroutine = ThenCoroutineController.GetTenWithThenCoroutine().Wrap <object>();

        return(Promise.Collect(all, any, collect, deferred, rescue, then, thenCoroutine));
    }
Exemple #3
0
    public static Promise <object[]> GetCollectPromise()
    {
        var promises = new List <Promise <object> >();

        for (int i = 0; i < 5; i++)
        {
            var localIndex = i + 1;
            promises.Add(Promise.WithAction(() => {
                Thread.Sleep(localIndex * 500);
                return(new object());
            }));
        }
        for (int i = 5; i < 10; i++)
        {
            var localIndex = i + 1;
            promises.Add(Promise.WithAction <object>(() => {
                Thread.Sleep(localIndex * 500);
                throw new Exception();
            }));
        }
        return(Promise.Collect(promises.ToArray()));
    }