public void Fetcher_BeginFetch_StatusFetchingIfNoResultYet() { var fetcher = new Fetcher <int>(Returns1Eventually); fetcher.BeginFetch(); Assert.AreEqual(FetchStatus.Fetching, fetcher.Status); }
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; }); }
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); }
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; }); }
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; }); }
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); }
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); }
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); }