public void MainTest() { // Init ExpressOrdersContext dbContext = new ExpressOrdersContext(); if (!dbContext.Database.EnsureCreated()) { dbContext.Database.ExecuteSqlRaw("DELETE FROM public.\"Order\""); dbContext.Database.ExecuteSqlRaw("DELETE FROM public.\"Product\""); } ShopStorage storage = new ShopStorage(); Random random = new Random(); ProductDataModel product = new ProductDataModel("Car"); int productsCount = 100; storage.Add(product, productsCount); // Act for (int k = 0; k < 10; k++) { Thread thread = new Thread( new ThreadStart(() => { ShopStorage threadStorage = new ShopStorage(); for (int i = 0; i < 1000; i++) { try { threadStorage.Reserve(product, random.Next(1, 3)); } catch (ArgumentOutOfRangeException) { break; } } }) ); thread.Start(); thread.Join(); } // Assert Assert.Equal(0, dbContext.Product.First().Stock); Assert.Equal(productsCount, dbContext.Order.Sum(o => o.Count)); }
static void Main(string[] args) { Console.WriteLine("Подготовка."); PrepareDb(); Console.WriteLine("Подготовка закончена."); ShopStorage storage = new ShopStorage(); Random random = new Random(); ProductDataModel product = new ProductDataModel("Car"); storage.Add(product, 100); Console.WriteLine("Запуск потоков."); for (int k = 0; k < 10; k++) { Thread thread = new Thread( new ThreadStart(() => { ShopStorage threadStorage = new ShopStorage(); Dictionary <int, List <bool> > result = new Dictionary <int, List <bool> >(); for (int i = 0; i < 1000; i++) { try { int randomCount = random.Next(1, 3); threadStorage.Reserve(product, randomCount); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); // break; } catch (Exception e) { Console.WriteLine(e.Message); } } }) ); thread.Start(); thread.Join(); } Console.WriteLine("Completed."); Console.ReadLine(); }