public void verify_bahaviour_for_concurrent_access_under_identical_keys() { var keys = new[] { "a", "a" }; var counter = new ConcurrentStack <int>(); var storage = new ConcurrentStack <TestItem>(); // first run var threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] { storage, counter })); threads.ForEach(t => t.Join()); Assert.Equal(1, counter.Count); Assert.Equal(2, storage.Count); var a = storage.First(); Assert.Same(storage.First(), storage.Last()); // cleanups and second run storage.Clear(); counter.Clear(); threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] { storage, counter })); threads.ForEach(t => t.Join()); Assert.Equal(0, counter.Count); Assert.Equal(2, storage.Count); var aa = storage.First(); Assert.Same(storage.First(), storage.Last()); Assert.Same(a, aa); }
public void verify_bahaviour_for_concurrent_access_under_different_keys() { var keys = new[] { "a", "b" }; var counter = new ConcurrentStack <int>(); // value factory threads var storage = new ConcurrentStack <TestItem>(); // cached items // first run var threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] { storage, counter })); threads.ForEach(t => t.Join()); Assert.Equal(2, counter.Count); Assert.Equal(2, storage.Count); Assert.NotSame(storage.First(), storage.Last()); var a = storage.FirstOrDefault(x => x.Id == "a"); var b = storage.FirstOrDefault(x => x.Id == "b"); // cleanups and second run storage.Clear(); counter.Clear(); threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] { storage, counter })); threads.ForEach(t => t.Join()); Assert.Equal(0, counter.Count); Assert.Equal(2, storage.Count); Assert.NotSame(storage.First(), storage.Last()); var aa = storage.FirstOrDefault(x => x.Id == "a"); var bb = storage.FirstOrDefault(x => x.Id == "b"); Assert.Same(a, aa); Assert.Same(b, bb); }
public void verify_bahaviour_for_concurrent_access_under_identical_keys() { var keys = new[] {"a", "a"}; var counter = new ConcurrentStack<int>(); var storage = new ConcurrentStack<TestItem>(); // first run var threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] {storage, counter})); threads.ForEach(t => t.Join()); Assert.Equal(1, counter.Count); Assert.Equal(2, storage.Count); var a = storage.First(); Assert.Same(storage.First(), storage.Last()); // cleanups and second run storage.Clear(); counter.Clear(); threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] {storage, counter})); threads.ForEach(t => t.Join()); Assert.Equal(0, counter.Count); Assert.Equal(2, storage.Count); var aa = storage.First(); Assert.Same(storage.First(), storage.Last()); Assert.Same(a, aa); }
public void verify_bahaviour_for_concurrent_access_under_different_keys() { var keys = new[] {"a", "b"}; var counter = new ConcurrentStack<int>(); // value factory threads var storage = new ConcurrentStack<TestItem>(); // cached items // first run var threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] {storage, counter})); threads.ForEach(t => t.Join()); Assert.Equal(2, counter.Count); Assert.Equal(2, storage.Count); Assert.NotSame(storage.First(), storage.Last()); var a = storage.FirstOrDefault(x => x.Id == "a"); var b = storage.FirstOrDefault(x => x.Id == "b"); // cleanups and second run storage.Clear(); counter.Clear(); threads = MakeThreads(keys); threads.ForEach(t => t.Start(new object[] {storage, counter})); threads.ForEach(t => t.Join()); Assert.Equal(0, counter.Count); Assert.Equal(2, storage.Count); Assert.NotSame(storage.First(), storage.Last()); var aa = storage.FirstOrDefault(x => x.Id == "a"); var bb = storage.FirstOrDefault(x => x.Id == "b"); Assert.Same(a, aa); Assert.Same(b, bb); }
public IEnumerable <IRemuneracao> AbaterValorExcedenteDasRemuneracoes(ILimiteDeRemuneracao limiteOriginario, IEnumerable <IRemuneracao> remuneracoes, Dinheiro valorExcedente) { //TODO funciona apenas para real Dinheiro valorTotal = remuneracoes.Select(rem => rem.ValorAjustado).SomaDinheiro(); ConcurrentStack <IRemuneracao> remuneracoesAbatidas = new ConcurrentStack <IRemuneracao>(); Dinheiro totalAbatido = Dinheiro.Zero; Parallel.ForEach(remuneracoes, (rem) => { decimal percentualDeAbatimento = decimal.Round(rem.ValorAjustado / valorTotal, 7); Dinheiro valorDoAbatimento = percentualDeAbatimento * valorExcedente; Dinheiro valorDoAbatimentoArrendondado = Dinheiro.Arredondar(valorDoAbatimento, 7); IRemuneracao remuneracaoDivididaEmPartesAbatidas = AbaterRemuneracao(rem, valorDoAbatimentoArrendondado, limiteOriginario); if (limiteOriginario.Tipo.Id.Equals("LIMITE_MINIMO") && remuneracaoDivididaEmPartesAbatidas.ValorAjustado > Dinheiro.Zero || limiteOriginario.Tipo.Id.Equals("LIMITE_MAXIMO")) { remuneracoesAbatidas.Push(remuneracaoDivididaEmPartesAbatidas); totalAbatido += valorDoAbatimentoArrendondado; } }); Dinheiro diferencaDeExcedente = Dinheiro.Arredondar(valorExcedente - totalAbatido, 6); if (diferencaDeExcedente != Dinheiro.Zero && remuneracoesAbatidas.Count() > 0) { CorrecaoDeArredondamentoDeExcedente(remuneracoesAbatidas.First(), diferencaDeExcedente); } return(remuneracoesAbatidas); }
public void StartMethodTrace(MethodTrace method) { if (currentStackTrace.IsEmpty) { Methods.Add(method); } else { MethodTrace currentMethod = currentStackTrace.First(); currentMethod.Methods.Add(method); } currentStackTrace.Push(method); }