public static TResult Until <TResult>(Func <TResult> condition) { IWaitConfiguration config = (WaitConfigurationSection)ConfigurationManager.GetSection("waitConfigurationGroup/waitConfiguration") ?? (IWaitConfiguration) new WaitConfiguration(); return(Until(condition, config)); }
public static void MinimumConfigurationSectionFromAppConfig() { IWaitConfiguration config = (WaitConfigurationSection)ConfigurationManager.GetSection("waitConfigurationGroup/minimumWaitConfiguration"); Assert.AreEqual(3000, config.Timeout); Assert.AreEqual(1000, config.PollingInterval); Assert.AreEqual(false, config.ExtendOnTimeout); Assert.AreEqual(5000, config.ExtendedTimeout); Assert.AreEqual(0, config.IgnoredExceptions.Length); }
public static void FullConfigurationSectionFromAppConfig() { IWaitConfiguration config = (WaitConfigurationSection)ConfigurationManager.GetSection("waitConfigurationGroup/fullWaitConfiguration"); Assert.AreEqual(3000, config.Timeout); Assert.AreEqual(1000, config.PollingInterval); Assert.AreEqual(true, config.ExtendOnTimeout); Assert.AreEqual(7000, config.ExtendedTimeout); Assert.AreEqual(2, config.IgnoredExceptions.Length); Assert.IsTrue(config.IgnoredExceptions[0] == typeof(ArgumentException)); Assert.IsTrue(config.IgnoredExceptions[1] == typeof(AssertionException)); }
public static TResult Until <TResult>(Func <TResult> condition, IWaitConfiguration configuration, params Type[] ignoredExceptions) { return(Until(condition, configuration.Timeout, configuration.PollingInterval, ignoredExceptions)); }
public static TResult Until <TResult>(Func <TResult> condition, params Type[] ignoredExceptions) { IWaitConfiguration config = (WaitConfigurationSection)ConfigurationManager.GetSection("waitConfigurationGroup/waitConfiguration"); return(Until(condition, config, ignoredExceptions)); }
public static TResult Until <TResult>(Func <TResult> condition, IWaitConfiguration configuration) { // Start continious checking var stopwatch = Stopwatch.StartNew(); Exception lastException = null; var wasExtended = false; var currentTimeout = configuration.Timeout; while (true) { // Extend timeout if needed if (configuration.ExtendOnTimeout && !wasExtended && NeedToBeExtended(currentTimeout, stopwatch)) { currentTimeout = configuration.ExtendedTimeout; wasExtended = true; WarnTimeoutWasExtended(); } // Exit condition - timeout is reached CheckTimeoutReached(currentTimeout, stopwatch, lastException, wasExtended); try { TResult result; try { var task = Task.Factory.StartNew(condition.Invoke); task.Wait(TimeSpan.FromMilliseconds(currentTimeout)); result = task.IsCompleted ? task.Result : default(TResult); } catch (AggregateException ae) { throw ae.InnerExceptions[0]; } // Exit condition - some non-default result if (!EqualityComparer <TResult> .Default.Equals(result, default(TResult))) { using (LogProvider.OpenMappedContext("LogType", "Wait")) { Logger.DebugFormat("Waiting completed in {ms}ms", stopwatch.ElapsedMilliseconds); } return(result); } } catch (Exception ex) { var ignored = configuration.IgnoredExceptions.Any(type => type.IsInstanceOfType(ex)); lastException = ex; if (!ignored) { throw; } } // Extend timeout if needed if (configuration.ExtendOnTimeout && !wasExtended && NeedToBeExtended(currentTimeout, stopwatch)) { currentTimeout = configuration.ExtendedTimeout; wasExtended = true; WarnTimeoutWasExtended(); } // Exit condition - timeout is reached CheckTimeoutReached(currentTimeout, stopwatch, lastException, wasExtended); // No exit conditions met - Sleep for polling interval Thread.Sleep(configuration.PollingInterval); } }