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(); }
public void Reserve_RndAmount_1000Times() { for (int i = 0; i < 1000; i++) { var reservationAmount = new Random().Next(1, 3); _waitHandler.WaitOne(); var product = _shopStorage.GetProduct(_productName).Result; var productAmount = product.Amount; var reservation = _shopStorage.Reserve(_productName, reservationAmount, "Task" + Task.CurrentId).Result; product = _shopStorage.GetProduct(_productName).Result; _waitHandler.Set(); if (reservation != null) { Assert.IsTrue(productAmount - reservationAmount == product.Amount); } } }
public async Task <ActionResult <Reservation> > Post(Reservation reservation) { if (reservation == null) { return(BadRequest()); } var product = _db.Products.FirstOrDefault(x => x.Name == reservation.ProductName); if (product == null) { return(NotFound()); } ShopStorage shopStorage = new ShopStorage(_db); await shopStorage.Reserve(reservation.ProductName, reservation.Amount, reservation.ClientFullName); await _db.SaveChangesAsync(); return(Ok(reservation)); }