示例#1
0
        public void Fetcher_BeginFetch_StatusFetchingIfNoResultYet()
        {
            var fetcher = new Fetcher <int>(Returns1Eventually);

            fetcher.BeginFetch();
            Assert.AreEqual(FetchStatus.Fetching, fetcher.Status);
        }
示例#2
0
        public void Fetcher_BeginFetch_KeepsTryingWhenExceptionThrownByDefault()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 5;

            var fetcher = new Fetcher <int>(getValue: AlwaysThrowsDivideByZeroException);

            fetcher.Fetched += EventShouldNotBeRaised;

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(FetchStatus.Fetching, fetcher.Status);
            Assert.Throws <InvalidOperationException>(() => { var x = fetcher.Result; });
        }
示例#3
0
        public void Fetcher_BeginFetch_StatusErrorIfUnfilteredExceptionThrown()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 5;

            var fetcher = new Fetcher <int>(
                getValue: AlwaysThrowsNullReferenceException,
                exceptionFilter: FilterAllButNullReferenceExceptions);

            fetcher.Fetched += EventShouldNotBeRaised;

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(FetchStatus.Error, fetcher.Status);
        }
示例#4
0
        public void Fetcher_BeginFetch_ValueFilterPreventsValuesThatReturnFalseFromBeingFound()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 5;

            var fetcher = new Fetcher <int>(
                getValue: AlwaysReturns1,
                valueFilter: FilterEvenValues);

            fetcher.Fetched += EventShouldNotBeRaised;

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(FetchStatus.Fetching, fetcher.Status);
            Assert.Throws <InvalidOperationException>(() => { var x = fetcher.Result; });
        }
示例#5
0
        public void Fetcher_BeginFetch_ExceptionFilterThrowsIfExceptionReturnsFalse()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 5;

            var fetcher = new Fetcher <int>(
                getValue: AlwaysThrowsNullReferenceException,
                exceptionFilter: FilterAllButNullReferenceExceptions);

            fetcher.Fetched += (sender, e) => { Assert.Fail("Event should not be raised."); };

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(FetchStatus.Error, fetcher.Status);
            Assert.Throws <InvalidOperationException>(() => { var x = fetcher.Result; });
        }
示例#6
0
        public void Fetcher_BeginFetch_PublishesAvailableResultsQuickly()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 5;

            //Will store published result
            int?result = null;

            //Create a fetcher that fetches the constant 1
            var fetcher = new Fetcher <int>(getValue: AlwaysReturns1);

            fetcher.Fetched += (sender, e) => { result = fetcher.Result; };

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs && result == null)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(1, result);
        }
示例#7
0
        public void Fetcher_BeginFetch_PublishesDelayedResultsEventually()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 1100;

            //Will store published result
            int?result = null;

            //Create a fetcher that fetches the constant 1
            var fetcher = new Fetcher <int>(getValue: Returns1Eventually);

            fetcher.Fetched += (sender, e) => { result = fetcher.Result; };

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs && result == null)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(1, result);
            Assert.AreEqual(FetchStatus.Found, fetcher.Status);
        }
示例#8
0
        public void Fetcher_BeginFetch_ValueFilterAllowsValuesThatReturnTrueToBeFound()
        {
            var sw        = new Stopwatch();
            var timeoutMs = 5;

            //Will store published result
            int?result = null;

            //Create a fetcher that fetches the constant 1
            var fetcher = new Fetcher <int>(getValue: AlwaysReturns2);

            fetcher.Fetched += (sender, e) => { result = fetcher.Result; };

            sw.Start();
            fetcher.BeginFetch();
            while (sw.ElapsedMilliseconds < timeoutMs && result == null)
            {
                //Loop until event is fired and handled, or timeout is up.
            }

            Assert.AreEqual(2, result);
            Assert.AreEqual(FetchStatus.Found, fetcher.Status);
        }