public void handles_null_onProgressed_hanlder() { var d = new Deferred <int>(); d.Progress(0.42f); d.QueueOnMainThread(null, null, null); }
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); }; }; }
void when_running_in_parallel_with_any() { Promise <string> p1 = null; Promise <string> p2 = null; Promise <string> p3 = null; Promise <string> p4 = null; Promise <string> promise = null; context["when at least one promise fulfills"] = () => { List <float> eventProgresses = null; before = () => { p1 = TestHelper.PromiseWithError <string>("error 42", delay); p2 = TestHelper.PromiseWithResult("42", delay * 2); p3 = TestHelper.PromiseWithResult("43", delay * 3); p4 = TestHelper.PromiseWithResult("44", delay * 3); eventProgresses = new List <float>(); promise = Promise.Any(p1, p2, p3, p4); promise.OnProgressed += eventProgresses.Add; promise.Await(); }; it["is fulfilled"] = () => promise.state.should_be(PromiseState.Fulfilled); it["first promise to be complete is fulfilled"] = () => p2.state.should_be(PromiseState.Fulfilled); it["has result of first complete promise"] = () => promise.result.should_be("42"); context["progress"] = () => { it["calculates progress of promise with highest progress"] = () => { eventProgresses.Count.should_be(1); eventProgresses[0].should_be(1f); }; it["has initial max progress"] = () => { var deferred = new Deferred <object>(); deferred.Progress(0.5f); var p = TestHelper.PromiseWithResult <object>("42", delay * 2); var any = Promise.Any(deferred, p); any.progress.should_be(0.5f); deferred.Fulfill(null); any.Await(); }; }; }; context["when all promises fail"] = () => { before = () => { p1 = TestHelper.PromiseWithError <string>("error 42", delay); p2 = TestHelper.PromiseWithError <string>("error 43", delay * 2); p3 = TestHelper.PromiseWithError <string>("error 44", delay * 3); promise = Promise.Any(p1, p2, p3); promise.Await(); }; it["fails"] = () => promise.state.should_be(PromiseState.Failed); it["has no result"] = () => promise.result.should_be_null(); it["has error"] = () => promise.error.should_not_be_null(); }; }
void when_running_in_parallel_with_any() { Promise<string> p1 = null; Promise<string> p2 = null; Promise<string> p3 = null; Promise<string> p4 = null; Promise<string> promise = null; context["when at least one promise fulfills"] = () => { List<float> eventProgresses = null; before = () => { p1 = TestHelper.PromiseWithError<string>("error 42", delay); p2 = TestHelper.PromiseWithResult("42", delay * 2); p3 = TestHelper.PromiseWithResult("43", delay * 3); p4 = TestHelper.PromiseWithResult("44", delay * 3); eventProgresses = new List<float>(); promise = Promise.Any(p1, p2, p3, p4); promise.OnProgressed += eventProgresses.Add; promise.Await(); }; it["is fulfilled"] = () => promise.state.should_be(PromiseState.Fulfilled); it["first promise to be complete is fulfilled"] = () => p2.state.should_be(PromiseState.Fulfilled); it["has result of first complete promise"] = () => promise.result.should_be("42"); context["progress"] = () => { it["calculates progress of promise with highest progress"] = () => { eventProgresses.Count.should_be(1); eventProgresses[0].should_be(1f); }; it["has initial max progress"] = () => { var deferred = new Deferred<object>(); deferred.Progress(0.5f); var p = TestHelper.PromiseWithResult<object>("42", delay * 2); var any = Promise.Any(deferred, p); any.progress.should_be(0.5f); deferred.Fulfill(null); any.Await(); }; }; }; context["when all promises fail"] = () => { before = () => { p1 = TestHelper.PromiseWithError<string>("error 42", delay); p2 = TestHelper.PromiseWithError<string>("error 43", delay * 2); p3 = TestHelper.PromiseWithError<string>("error 44", delay * 3); promise = Promise.Any(p1, p2, p3); promise.Await(); }; it["fails"] = () => promise.state.should_be(PromiseState.Failed); it["has no result"] = () => promise.result.should_be_null(); it["has error"] = () => promise.error.should_not_be_null(); }; }
public void does_not_call_onProgressed_immediately_when_0() { var d = new Deferred <int>(); d.Progress(0f); var progressCalled = false; d.promise.QueueOnMainThread(null, null, p => { progressCalled = true; }); Assert.IsFalse(progressCalled); }
public void calls_onProgressed_immediately_when_greater_0() { var d = new Deferred <int>(); d.Progress(0.42f); var progress = 0f; d.promise.QueueOnMainThread(null, null, p => { progress = p; }); Assert.AreEqual(0.42f, progress); }
void Start() { var d = new Deferred <int>(); d.action = () => { while (d.progress < 1) { Thread.Sleep(100); d.Progress(d.progress + 0.4f); } return(42); }; d.RunAsync().QueueOnMainThread(null, null, onProgressed); }
static Promise <int> customProgressPromise(bool autoStart = false) { var deferred = new Deferred <int>(); deferred.action = () => { Thread.Sleep(500); var progress = 0f; while (progress < 1f) { progress += 0.01f; progress = Math.Min(1f, progress); deferred.Progress(progress); Thread.Sleep(7); } return(0); }; return(autoStart ? deferred.RunAsync() : deferred.promise); }
void when_concatenating_with_then() { Promise <int> firstPromise = null; Promise <string> thenPromise = null; after = () => { firstPromise.Await(); thenPromise.Await(); }; context["when first promise fulfilles"] = () => { before = () => { firstPromise = TestHelper.PromiseWithResult(42, delay); thenPromise = firstPromise.Then(result => { Thread.Sleep(delay); return(result + "_then"); }); }; context["before first promise finished"] = () => { it["first promise is running"] = () => assertRunning(firstPromise, 0f); it["then promise is pending"] = () => assertPending(thenPromise); it["then promise waits"] = () => { thenPromise.Await(); assertFulfilled(firstPromise, 42); assertFulfilled(thenPromise, "42_then"); }; context["when first promise finished"] = () => { before = () => firstPromise.Await(); it["first promise is fulfilled"] = () => assertFulfilled(firstPromise, 42); it["then promise in running with correct progress"] = () => assertRunning(thenPromise, 0.5f); context["when then promise finished"] = () => { before = () => thenPromise.Await(); it["then promise is fulfilled"] = () => assertFulfilled(thenPromise, "42_then"); }; }; }; }; context["when first promise fails"] = () => { before = () => { firstPromise = TestHelper.PromiseWithError <int>("error 42", delay); thenPromise = firstPromise.Then(result => { Thread.Sleep(delay); return(result + "_then"); }); firstPromise.Await(); }; it["first promise failed"] = () => assertFailed(firstPromise, "error 42"); it["then promise failed"] = () => assertFailed(thenPromise, "error 42"); }; context["when putting it all together"] = () => { it["fulfills all promises and passes result"] = () => { var promise = Promise.WithAction(() => "1") .Then(result => result + "2") .Then(result => result + "3") .Then(result => result + "4"); var fulfilled = 0; promise.OnFulfilled += result => fulfilled++; promise.Await(); fulfilled.should_be(1); promise.result.should_be("1234"); }; it["forwards error"] = () => { var promise = TestHelper.PromiseWithError <string>("error 42") .Then(result => result + "2") .Then(result => result + "3") .Then(result => result + "4"); var fulfilled = false; Exception eventError = null; promise.OnFulfilled += result => fulfilled = true; promise.OnFailed += error => eventError = error; promise.Await(); fulfilled.should_be_false(); eventError.Message.should_be("error 42"); promise.result.should_be_null(); promise.error.Message.should_be("error 42"); }; it["calculates correct progress"] = () => { var promise = Promise.WithAction(() => { Thread.Sleep(delay); return("1"); }).Then(result => result + "2") .Then(result => result + "3") .Then <string>(result => { throw new Exception("error 42"); }); int eventCalled = 0; var expectedProgresses = new [] { 0.25f, 0.5f, 0.75, 1f }; promise.OnProgressed += progress => { progress.should_be(expectedProgresses[eventCalled]); eventCalled++; }; promise.Await(); eventCalled.should_be(expectedProgresses.Length - 1); }; it["has initial progress"] = () => { var deferred = new Deferred <string>(); deferred.Progress(0.5f); var then = deferred.promise.Then(result => 0); then.progress.should_be(0.25f); deferred.Fulfill(null); then.Await(); }; it["calculates correct progress with custum progress"] = () => { var deferred1 = new Deferred <int>(); deferred1.action = () => { Thread.Sleep(delay); var progress = 0f; while (progress < 1f) { progress += 0.25f; deferred1.Progress(progress); Thread.Sleep(delay); } return(0); }; var deferred2 = new Deferred <int>(); deferred2.action = () => { var progress = 0f; while (progress < 1f) { progress += 0.25f; deferred2.Progress(progress); Thread.Sleep(delay); } return(0); }; int eventCalled = 0; var expectedProgresses = new [] { 0.125f, 0.25f, 0.375, 0.5f, 0.625f, 0.75f, 0.875f, 1f, }; var promise = deferred1.RunAsync().Then(deferred2); promise.OnProgressed += progress => { progress.should_be(expectedProgresses[eventCalled]); eventCalled++; }; promise.Await(); eventCalled.should_be(expectedProgresses.Length); }; }; }
void when_running_an_expensive_action() { Promise <string> promise = null; string eventResult = null; Exception eventError = null; float eventProgress = 0f; bool fulfilledCalled = false, failedCalled = false, progressCalled = false; before = () => { eventResult = null; eventError = null; eventProgress = -1f; fulfilledCalled = failedCalled = progressCalled = false; }; after = () => promise.Await(); context["before action finished"] = () => { before = () => { promise = TestHelper.PromiseWithResult("42", delay); promise.OnFulfilled += result => { eventResult = result; fulfilledCalled = true; }; promise.OnFailed += error => { eventError = error; failedCalled = true; }; promise.OnProgressed += progress => { eventProgress = progress; progressCalled = true; }; }; it["is unfulfilled"] = () => promise.state.should_be(PromiseState.Unfulfilled); it["has progressed 0%"] = () => promise.progress.should_be(0f); it["has no result"] = () => promise.result.should_be_null(); it["has no error"] = () => promise.error.should_be_null(); context["await"] = () => { it["blocks until finished"] = () => { promise.Await(); promise.state.should_be(PromiseState.Fulfilled); }; it["does nothing when promise already finished"] = () => { promise.Await(); promise.Await(); true.should_be_true(); }; it["returns promise"] = () => promise.Await().result.should_be("42"); }; context["events"] = () => { it["doesn't call OnFulfilled"] = () => fulfilledCalled.should_be_false(); it["doesn't call OnFail"] = () => failedCalled.should_be_false(); it["doesn't call OnProgress"] = () => progressCalled.should_be_false(); }; context["when action finished"] = () => { before = () => 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_be("42"); it["has no error"] = () => promise.error.should_be_null(); context["events"] = () => { it["calls OnFulfilled"] = () => eventResult.should_be("42"); it["calls OnFulfilled when adding callback"] = () => { string lateResult = null; promise.OnFulfilled += result => lateResult = result; lateResult.should_be("42"); }; it["doesn't call OnFailed"] = () => failedCalled.should_be_false(); it["doesn't call OnFailed when adding callback"] = () => { var called = false; promise.OnFailed += error => called = true; called.should_be_false(); }; it["calls OnProgress"] = () => eventProgress.should_be(1f); }; }; }; context["when action throws an exception"] = () => { before = () => { promise = TestHelper.PromiseWithError <string>("error 42", delay); promise.OnFulfilled += result => { eventResult = result; fulfilledCalled = true; }; promise.OnFailed += error => { eventError = error; failedCalled = true; }; promise.OnProgressed += progress => { eventProgress = progress; eventProgress = progress; }; promise.Await(); }; it["failed"] = () => promise.state.should_be(PromiseState.Failed); it["has progressed 0%"] = () => promise.progress.should_be(0f); it["has no result"] = () => promise.result.should_be_null(); it["has error"] = () => promise.error.Message.should_be("error 42"); context["events"] = () => { it["doesn't call OnFulfilled"] = () => fulfilledCalled.should_be_false(); it["doesn't call OnFulfilled when adding callback"] = () => { var called = false; promise.OnFulfilled += result => called = true; called.should_be_false(); }; it["calls OnFailed"] = () => eventError.Message.should_be("error 42"); it["calls OnFailed when adding callback"] = () => { Exception lateError = null; promise.OnFailed += error => lateError = error; lateError.Message.should_be("error 42"); }; it["doesn't call OnProgress"] = () => progressCalled.should_be_false(); }; }; context["when creating a custom promise with Deferred"] = () => { Deferred <string> deferred = null; int progressEventCalled = 0; before = () => { deferred = new Deferred <string>(); progressEventCalled = 0; deferred.OnProgressed += progress => { eventProgress = progress; progressEventCalled++; }; }; it["progresses"] = () => { deferred.Progress(0.3f); deferred.Progress(0.6f); deferred.Progress(0.9f); deferred.Progress(1f); eventProgress.should_be(1f); deferred.promise.progress.should_be(1f); progressEventCalled.should_be(4); }; it["doesn't call OnProgressed when setting equal progress"] = () => { deferred.Progress(0.3f); deferred.Progress(0.3f); deferred.Progress(0.3f); deferred.Progress(0.6f); eventProgress.should_be(0.6f); deferred.promise.progress.should_be(0.6f); progressEventCalled.should_be(2); }; it["doesn't call OnProgressed when adding callback when progress is less than 100%"] = () => { deferred.Progress(0.3f); var called = false; deferred.OnProgressed += progress => called = true; called.should_be_false(); }; it["calls OnProgressed when adding callback when progress is 100%"] = () => { deferred.Progress(1f); var called = false; deferred.OnProgressed += progress => called = true; called.should_be_true(); }; }; context["wrap"] = () => { Promise <object> wrapped = null; context["when wrapped promise fulfills"] = () => { before = () => { }; it["forwards fulfill"] = () => { promise = TestHelper.PromiseWithResult("42", delay); wrapped = promise.Wrap <object>(); wrapped.Await(); wrapped.state.should_be(PromiseState.Fulfilled); }; it["forwards fail"] = () => { promise = TestHelper.PromiseWithError <string>("error 42", delay); wrapped = promise.Wrap <object>(); wrapped.Await(); wrapped.state.should_be(PromiseState.Failed); }; it["forwards progress"] = () => { var deferred = new Deferred <string>(); wrapped = deferred.Wrap <object>(); deferred.Progress(0.5f); wrapped.progress.should_be(0.5f); }; it["has initial progress"] = () => { var deferred = new Deferred <string>(); deferred.Progress(0.5f); wrapped = deferred.Wrap <object>(); wrapped.progress.should_be(0.5f); }; }; }; context["toString"] = () => { it["returns description of unfulfilled promise"] = () => { var deferred = new Deferred <string>(); deferred.Progress(0.1234567890f); deferred.promise.ToString().should_be("[Promise<String>: state = Unfulfilled, progress = 0,123]"); }; it["returns description of fulfilled promise"] = () => { promise = TestHelper.PromiseWithResult("42"); promise.Await(); promise.ToString().should_be("[Promise<String>: state = Fulfilled, result = 42]"); }; it["returns description of failed promise"] = () => { promise = TestHelper.PromiseWithError <string>("error 42"); promise.Await(); promise.ToString().should_be("[Promise<String>: state = Failed, progress = 0, error = error 42]"); }; }; }
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 no thread assigned"] = () => promise.thread.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); }; }; }
public void GetProgressTest() { CallCounter counter = new CallCounter(); Deferred<System.String> d = new Deferred<System.String>(); d.Progress(p => { Assert.AreEqual(0.5f, p.ProgressValue); counter.Progress++; }); d.Notify(0.5f); Assert.AreEqual(1, counter.Progress); }
void when_using_rescue() { Promise <string> promise = null; string eventResult = null; before = () => eventResult = null; after = () => promise.Await(); it["rescues failed promise"] = () => { promise = TestHelper.PromiseWithError <string>("error 42", delay).Rescue(error => "rescue"); promise.OnFulfilled += result => eventResult = result; promise.Await(); promise.state.should_be(PromiseState.Fulfilled); promise.progress.should_be(1f); promise.result.should_be("rescue"); promise.error.should_be_null(); eventResult.should_be("rescue"); }; it["rescues already failed promise"] = () => { promise = TestHelper.PromiseWithError <string>("error 42").Rescue(error => "rescue"); promise.OnFulfilled += result => eventResult = result; promise.Await(); promise.state.should_be(PromiseState.Fulfilled); promise.progress.should_be(1f); promise.result.should_be("rescue"); promise.error.should_be_null(); eventResult.should_be("rescue"); }; it["fulfills when no error"] = () => { promise = TestHelper.PromiseWithResult("42", delay).Rescue(error => "rescue"); promise.OnFulfilled += result => eventResult = result; promise.Await(); Thread.SpinWait(0); promise.state.should_be(PromiseState.Fulfilled); promise.progress.should_be(1f); promise.result.should_be("42"); promise.error.should_be_null(); eventResult.should_be("42"); }; context["progress"] = () => { Deferred <string> deferred = null; var progressCalled = 0; var eventProgress = 0f; before = () => { eventProgress = 0f; progressCalled = 0; deferred = new Deferred <string>(); deferred.Progress(0.1f); promise = deferred.Rescue(error => "43"); promise.OnProgressed += progress => { eventProgress = progress; progressCalled++; }; }; it["forwards progress"] = () => { deferred.Progress(0.3f); deferred.Progress(0.6f); deferred.Fulfill("42"); eventProgress.should_be(1f); promise.progress.should_be(1f); progressCalled.should_be(3); }; it["has initial progress"] = () => { promise.progress.should_be(0.1f); deferred.Fulfill("42"); }; }; it["calculates correct progress when concatenating with then"] = () => { Func <string, string> p = result => { Thread.Sleep(delay); return("42"); }; Func <Exception, string> r = error => { Thread.Sleep(delay); return("error"); }; promise = TestHelper.PromiseWithResult("42", delay) .Then(p) .Then(p) .Then(p).Rescue(r); int eventCalled = 0; var expectedProgresses = new [] { 0.25f, 0.5f, 0.75f, 1f }; promise.OnProgressed += progress => { progress.should_be(expectedProgresses[eventCalled]); eventCalled++; }; promise.Await(); eventCalled.should_be(expectedProgresses.Length); }; }