Implements IDeref and java.util.concurrent.Future, like the proxy in JVM clojure core.
Наследование: IDeref, IBlockingDeref, IPending, IDisposable
Пример #1
0
        public void PropagatesExceptions()
        {
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () => { throw new Exception("future exception"); };

            Future f = new Future(fn);
            try
            {
                f.deref();
                Assert.Fail("expected future exception");
            }
            catch (Exception ex)
            {
                Expect(ex.Message, EqualTo("Future has an error"));
                Expect(ex.InnerException, Is.Not.Null);
                Expect(ex.InnerException.Message, EqualTo("future exception"));
            }

            // Same result for subsequent derefs.
            try
            {
                f.deref();
                Assert.Fail("expected future exception");
            }
            catch (Exception ex)
            {
                Expect(ex.Message, EqualTo("Future has an error"));
                Expect(ex.InnerException, Is.Not.Null);
                Expect(ex.InnerException.Message, EqualTo("future exception"));
            }
        }
Пример #2
0
        public void DerefThrowsAfterCancellation()
        {
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () => { while (true); };

            Future f = new Future(fn);
            f.cancel();
            f.deref();
        }
Пример #3
0
        public void CancelAbortsTheTask()
        {
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () => { while (true); };

            Future f = new Future(fn);
            Expect(f.isCancelled(), EqualTo(false));
            Expect(f.cancel(true), EqualTo(true));
            Expect(f.isCancelled(), EqualTo(true));
        }
Пример #4
0
        public void CancelFailsAfterSuccessfulCompletion()
        {
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () => { return 42; };

            Future f = new Future(fn);
            Expect(f.deref(), EqualTo(42));
            Expect(f.cancel(), EqualTo(false));
            Expect(f.isCancelled(), EqualTo(false));
        }
Пример #5
0
        public void CachesResultForSubsequentCalls()
        {
            int i = 0;
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () => Interlocked.Increment(ref i);

            Future f = new Future(fn);
            Expect(f.deref(), EqualTo(1));
            Expect(f.deref(), EqualTo(1));
            Expect(i, EqualTo(1));
        }
Пример #6
0
        public void ComputesTheValueOnAnotherThread()
        {
            int workerID = Thread.CurrentThread.ManagedThreadId;
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () =>
                {
                    workerID = Thread.CurrentThread.ManagedThreadId;
                    return 42;
                };

            Future f = new Future(fn);
            Expect(f.deref(), EqualTo(42));
            Expect(workerID, Not.EqualTo(Thread.CurrentThread.ManagedThreadId));
        }
Пример #7
0
        public void SecondCancelReturnsFalse()
        {
            AFnImpl fn = new AFnImpl();
            fn._fn0 = () => { while (true); };

            Future f = new Future(fn);
            Expect(f.cancel(true), EqualTo(true));
            Expect(f.cancel(true), EqualTo(false));
        }