コード例 #1
0
        public void Test_OneTry()
        {
            object             obj  = "test1";
            RetryLazy <object> sync = new RetryLazy <object>(() => obj, 1);

            Assert.AreSame(obj, sync.Value);
        }
コード例 #2
0
        public void RetryLazy_uses_the_given_input_for_each_attempt_at_initialization_until_an_attempt_succeeds()
        {
            var count                = 0;
            var lockObject           = new object();
            InitializerOutput result = null;
            var inputs               = new List <int>();

            var initializer = new RetryLazy <int, InitializerOutput>(
                i =>
            {
                // Locking here to ensure that count is incremented correctly even if RetryLazy isn't working correctly.
                lock (lockObject)
                {
                    inputs.Add(i);
                    count++;
                    if (count <= 5)
                    {
                        throw new Exception("Fail!");
                    }
                    return(new InitializerOutput
                    {
                        Input = i,
                        Count = count
                    });
                }
            });

            var tests = new Action[20];

            for (var i = 0; i < 20; i++)
            {
                var outside = i; // Make sure i is used from outside the closure
                tests[i] = () => result = initializer.GetValue(outside);
            }

            try
            {
                Parallel.Invoke(tests);
            }
            catch (AggregateException ex)
            {
                Assert.Equal(5, ex.InnerExceptions.Count);
            }

            Assert.Equal(6, count);
            Assert.Equal(6, result.Count);
            Assert.Equal(6, inputs.Count);
            Assert.Equal(inputs[5], result.Input);

            for (var i = 0; i < inputs.Count; i++)
            {
                for (var j = 0; j < inputs.Count; j++)
                {
                    if (i != j)
                    {
                        Assert.NotEqual(inputs[i], inputs[j]);
                    }
                }
            }
        }
コード例 #3
0
        public void RetryLazy_keeps_trying_to_initialize_until_an_attempt_succeeds()
        {
            var count       = 0;
            var lockObject  = new object();
            var initializer = new RetryLazy <string, string>(
                i =>
            {
                // Locking here to ensure that count is incremented correctly even if RetryLazy isn't working correctly.
                lock (lockObject)
                {
                    count++;
                    if (count <= 5)
                    {
                        throw new Exception("Fail!");
                    }
                    return("");
                }
            });

            try
            {
                ExecuteInParallel(() => initializer.GetValue(""));
            }
            catch (AggregateException ex)
            {
                Assert.Equal(5, ex.InnerExceptions.Count);
                foreach (var innerException in ex.InnerExceptions)
                {
                    Assert.Equal("Fail!", innerException.Message);
                }
            }
            Assert.Equal(6, count);
        }
コード例 #4
0
        public void TestMaxRetries()
        {
            maxRetries = 0;
            RetryLazy <String> retryLazy = new RetryLazy <string>(provider, maxRetries);

            try {
                // nao existem mais tentativas disponiveis
                String res = retryLazy.Value;
            } catch (Exception e) {
                Assert.AreEqual(typeof(InvalidOperationException), e.GetType());
            }
        }
コード例 #5
0
        public void TestSucessellGet()
        {
            maxRetries = 3;
            RetryLazy <String> retryLazy = new RetryLazy <string>(provider, maxRetries);
            // o primeiro consegue calcular o valor
            String res = retryLazy.Value;

            Assert.AreEqual(expected, res);
            // a segunda encontra o valor do primeiro
            res = retryLazy.Value;
            Assert.AreEqual(expected, res);
        }
コード例 #6
0
ファイル: Application.cs プロジェクト: base2art/soufflot
        public Application(ApplicationMode mode, string rootDirectory, IConfigurationProvider configuration)
        {
            this.mode           = mode;
            this.rootDirectory  = rootDirectory;
            this.configuration  = configuration;
            this.startupInvoker = new RetryLazy <bool>(() =>
            {
                this.OnApplicationStart();
                return(true);
            });

            this.injector          = new RetryLazy <IComponentResolver>(() => this.CreateServiceLoader());
            this.applicationLogger = new RetryLazy <ILogger>(() => this.CreateLogger());
        }
コード例 #7
0
        public void RetryLazy_only_runs_the_lazy_initializer_once_even_when_called_from_multiple_threads()
        {
            var count       = 0;
            var lockObject  = new object();
            var initializer = new RetryLazy <string, string>(
                i =>
            {
                // Locking here to ensure that count is incremented correctly even if RetryLazy isn't working correctly.
                lock (lockObject)
                {
                    count++;
                    return("");
                }
            });

            ExecuteInParallel(() => initializer.GetValue(""));
            Assert.Equal(1, count);
        }
コード例 #8
0
        public void Test_SeveralTries()
        {
            object             obj  = "test1";
            int                aux  = 0;
            RetryLazy <object> sync = new RetryLazy <object>(() =>
            {
                if (aux++ == 0)
                {
                    return(obj);
                }
                return("");
            }, 4);

            Thread t = new Thread(() => {
                try
                {
                    Assert.AreSame(obj, sync.Value);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });
            Thread t1 = new Thread(() => {
                try
                {
                    Assert.AreSame(obj, sync.Value);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            });

            Assert.AreSame(obj, sync.Value);

            t.Start();
            t1.Start();
            t.Join();

            Assert.AreEqual(0, exceptions.Count);
        }