Esempio n. 1
0
        public static TResult Until <TResult>(Func <TResult> condition)
        {
            IWaitConfiguration config = (WaitConfigurationSection)ConfigurationManager.GetSection("waitConfigurationGroup/waitConfiguration")
                                        ?? (IWaitConfiguration) new WaitConfiguration();

            return(Until(condition, config));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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));
        }
Esempio n. 4
0
 public static TResult Until <TResult>(Func <TResult> condition, IWaitConfiguration configuration, params Type[] ignoredExceptions)
 {
     return(Until(condition, configuration.Timeout, configuration.PollingInterval, ignoredExceptions));
 }
Esempio n. 5
0
        public static TResult Until <TResult>(Func <TResult> condition, params Type[] ignoredExceptions)
        {
            IWaitConfiguration config = (WaitConfigurationSection)ConfigurationManager.GetSection("waitConfigurationGroup/waitConfiguration");

            return(Until(condition, config, ignoredExceptions));
        }
Esempio n. 6
0
        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);
            }
        }