public void Test_OneTry() { object obj = "test1"; RetryLazy <object> sync = new RetryLazy <object>(() => obj, 1); Assert.AreSame(obj, sync.Value); }
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]); } } } }
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); }
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()); } }
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); }
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()); }
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); }
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); }