public void test_BuyThings() { // var stockController = new var stockController = new ConcurrentCollections.StockController(); // stockController.LoadCurrentStock(); // ConcurrentDictionary<string, ConcurrentCollections.Thing> stock = new ConcurrentDictionary<string, ConcurrentCollections.Thing>(); // stock = prog.GetCurrentStock(); //Add five items to the Things collection var Thing1 = new ConcurrentCollections.Thing() { ID = 1111, name = "UnitTest1Thing", price = 11.11, quantity = 0 }; var Thing2 = new ConcurrentCollections.Thing() { ID = 2222, name = "UnitTest2Thing", price = 22.22, quantity = 0 }; var Thing3 = new ConcurrentCollections.Thing() { ID = 3333, name = "UnitTest3Thing", price = 33.33, quantity = 0 }; var Thing4 = new ConcurrentCollections.Thing() { ID = 4444, name = "UnitTest4Thing", price = 44.44, quantity = 0 }; var Thing5 = new ConcurrentCollections.Thing() { ID = 5555, name = "UnitTest5Thing", price = 55.55, quantity = 0 }; //first, add items to collection with zero inventory //Task t1 = Task.Run(() => stockController.BuyThing(Thing1, 0, "UnitTestThing")); //Task t2 = Task.Run(() => stockController.BuyThing(Thing2, 0, "UnitTestThing")); //Task t3 = Task.Run(() => stockController.BuyThing(Thing3, 0, "UnitTestThing")); //Task t4 = Task.Run(() => stockController.BuyThing(Thing4, 0, "UnitTestThing")); //Task t5 = Task.Run(() => stockController.BuyThing(Thing5, 0, "UnitTestThing")); //stockController.BuyThing(Thing1, 0, "UnitTestThing1", _testThings); //stockController.BuyThing(Thing2, 0, "UnitTestThing2", _testThings); stockController.BuyThing(Thing3, 0, "UnitTestThing3", ref _testThings); //stockController.BuyThing(Thing4, 0, "UnitTestThing4", _testThings); //stockController.BuyThing(Thing5, 0, "UnitTestThing5", _testThings); TimeSpan workDay = new TimeSpan(0,10,0); simulateWork(stockController, workDay); foreach (var thingy in _testThings) { Console.WriteLine("thing: key " + thingy.Key + " value: " + thingy.Value.name + " price: " + thingy.Value.price + " Quantity: " + thingy.Value.quantity); Console.ReadLine(); } }
private void simulateWork(StockController stockController, TimeSpan workDay) { Random rand = new Random(); DateTime start = DateTime.Now; while (DateTime.Now - start < workDay) { Thread.Sleep(rand.Next(100)); bool buy = (rand.Next(6) == 0); // string itemName = // if (buy) if (true) { int quantity = rand.Next(9) + 1; var thing = new ConcurrentCollections.Thing() { ID = 3333, name = "UnitTestThing3" //price = 33.33, //quantity = quantity }; stockController.BuyThing(thing, quantity, "UnitTestThing3", ref _testThings); } } }