示例#1
0
        public void TestCallbacks()
        {
            var    f            = FutureFactory.NewFuture <string>();
            string resultString = "Success!";

            bool[] calls = new bool[3];
            f.OnComplete(r => {
                Assert.True(r.Success);
                Assert.False(r.HasError);
                Assert.Equals(resultString, r.Result);
                calls[0] = true;
            });
            f.OnSuccess(r => {
                Assert.Equals(resultString, r);
                calls[1] = true;
            });

            f.OnError(e => {
                Assert.False(true, "Never called");
                calls[2] = false;
            });

            // inform result
            f.SetResult(resultString);

            Assert.True(calls[0]);
            Assert.True(calls[1]);
            Assert.False(calls[2]);
        }
        public void TestMap()
        {
            var f = FutureFactory.NewFuture <string>();

            var f2 = f.Map <bool>(str => {
                return(str == "true");
            });

            f.SetResult("true");

            Assert.True(f2.Result);
        }
        public void TestRecover()
        {
            var f = FutureFactory.NewFuture <string>();

            var f2 = f.Recover(e => {
                return("recovered");
            });

            f.SetError(new Exception());

            Assert.Equals("recovered", f2.Result);
        }
示例#4
0
        public void TestBeforeDone()
        {
            FutureCall <string> f = FutureFactory.NewFuture <string>();

            // Test properties
            Assert.False(f.Done);
            Assert.False(f.Success);
            Assert.False(f.HasError);

            // Test wait
            Assert.ExpectException(new TimeoutException("Wait timeout"));
            f.Wait(0.5f);
        }
        public void TestJoinError()
        {
            var f     = FutureFactory.NewFuture <string>();
            var other = FutureFactory.NewFuture <int>();

            var f2 = f.Join <int>(other);

            f.SetResult("ok");
            Assert.False(f2.Done);
            var e = new Exception();

            other.SetError(e);
            Assert.True(f2.Done);

            Assert.True(f2.HasError);
            Assert.Equals(e, f2.Error);
        }
        public void TestJoin()
        {
            var f     = FutureFactory.NewFuture <string>();
            var other = FutureFactory.NewFuture <int>();

            var f2 = f.Join <int>(other);

            f.SetResult("ok");
            Assert.False(f2.Done);
            other.SetResult(1);
            Assert.True(f2.Done);

            Tuple2 <string, int> r = f2.Result;

            Assert.Equals("ok", r.V1);
            Assert.Equals(1, r.V2);
        }
示例#7
0
        public void TestSetError()
        {
            var f = FutureFactory.NewFuture <string>();

            Exception e = new Exception("Fail!");

            f.SetError(e);

            // Test properties after set error
            Assert.True(f.Done);
            Assert.False(f.Success);
            Assert.True(f.HasError);


            Assert.Equals(e, f.Error);
            Assert.ExpectException(new NotAllowedOperationException("Can't get result from not succeeded future"));
            var r = f.Result;

            Assert.False(true, "Never called" + r);
        }
示例#8
0
        public void TestSetResult()
        {
            FutureCall <string> f = FutureFactory.NewFuture <string>();


            string result = "Succeess!";

            f.SetResult(result);

            // Test properties after set result
            Assert.True(f.Done);
            Assert.True(f.Success);
            Assert.False(f.HasError);

            Assert.Equal(result, f.Result);
            Assert.ExpectException(new NotAllowedOperationException("Can't get result from succeeded future"));
            var e = f.Error;

            Assert.False(true, "Never called" + e);
        }
        public void TestSelect()
        {
            // success x success
            {
                var f1 = FutureFactory.NewFuture <string>();
                var f2 = FutureFactory.NewFuture <string>();

                var sel = f1.Select(f2);

                Assert.False(sel.Done);
                f1.SetResult("ok1");
                Assert.True(sel.Done);
                f2.SetResult("ok2");
                Assert.True(sel.Done);
                Assert.Equals("ok1", sel.Result);
            }


            // error x success
            {
                var f1 = FutureFactory.NewFuture <string>();
                var f2 = FutureFactory.NewFuture <string>();

                var sel = f1.Select(f2);

                Assert.False(sel.Done);
                f1.SetError(new Exception());
                Assert.False(sel.Done);
                f2.SetResult("ok2");
                Assert.True(sel.Done);
                Assert.Equals("ok2", sel.Result);
            }


            // success x error
            {
                var f1 = FutureFactory.NewFuture <string>();
                var f2 = FutureFactory.NewFuture <string>();

                var sel = f1.Select(f2);

                Assert.False(sel.Done);
                f1.SetResult("ok1");
                Assert.True(sel.Done);
                f2.SetError(new Exception());
                Assert.True(sel.Done);
                Assert.Equals("ok1", sel.Result);
            }
            // error x error
            {
                var f1 = FutureFactory.NewFuture <string>();
                var f2 = FutureFactory.NewFuture <string>();

                var sel = f1.Select(f2);

                Assert.False(sel.Done);
                f1.SetError(new Exception());
                Assert.False(sel.Done);
                f2.SetError(new Exception());
                Assert.True(sel.Done);
                Assert.Equals(new MultiException(f1.Error, f2.Error), sel.Error);
            }
        }