public void next_shouldGetCurrentValueAndIncrement() { IncrementCounter ctr; ctr = new IncrementCounter(); Assert.Equal(0, ctr.next()); Assert.Equal(1, ctr.next()); Assert.Equal(2, ctr.current); Assert.Equal(2, ctr.next()); Assert.Equal(3, ctr.current); ctr = new IncrementCounter(Int32.MaxValue - 1); Assert.Equal(Int32.MaxValue - 1, ctr.next()); Assert.Equal(Int32.MaxValue, ctr.next()); Assert.Equal(Int32.MinValue, ctr.next()); Assert.Equal(Int32.MinValue + 1, ctr.current); ctr = new IncrementCounter(-2); Assert.Equal(-2, ctr.next()); Assert.Equal(-1, ctr.next()); Assert.Equal(0, ctr.next()); Assert.Equal(1, ctr.next()); Assert.Equal(2, ctr.next()); Assert.Equal(3, ctr.current); }
public void constructor_shouldSetInitialValue() { IncrementCounter ctr; ctr = new IncrementCounter(); Assert.Equal(0, ctr.current); ctr = new IncrementCounter(12); Assert.Equal(12, ctr.current); ctr = new IncrementCounter(-5); Assert.Equal(-5, ctr.current); }
public void atomicNext_shouldGetCurrentValueAndIncrementAtomic() { IncrementCounter ctr; ctr = new IncrementCounter(); doTest(10, 100); ctr = new IncrementCounter(100); doTest(10, 2000); void doTest(int nThreads, int nReps) { int initialValue = ctr.current; ConcurrentBag <int> values = new ConcurrentBag <int>(); var threads = new Thread[nThreads]; for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(() => { for (int j = 0; j < nReps; j++) { values.Add(ctr.atomicNext()); } }); } for (int i = 0; i < threads.Length; i++) { threads[i].Start(); } for (int i = 0; i < threads.Length; i++) { threads[i].Join(); } Assert.Equal(ctr.current, initialValue + nThreads * nReps); var sortedValues = values.ToArray(); Array.Sort(sortedValues); Assert.Equal <int>(Enumerable.Range(initialValue, nThreads * nReps), sortedValues); } }
public void IncrementCounter_WhenIncrement_ThenUseIncrementForValue() { var incrementCounter = new IncrementCounter("test1", "test2", "test3"); var eventData = new Dictionary <string, object> { ["Name"] = "optimistic-concurrency-failures-per-second", ["DisplayName"] = "Optimistic Concurrency Failures", ["DisplayRateTimeScale"] = "00:00:01", ["Increment"] = 1d, ["IntervalSec"] = 675.144775, ["Metadata"] = "", ["Series"] = "Interval=10000", ["CounterType"] = "Sum", ["DisplayUnits"] = "" }; incrementCounter.TryReadEventCounterData(eventData); Assert.Equal(1, incrementCounter.Metric.Value); }
public int InitialBooking(List <int> tripList, string cabin, int passangers, DateTime bookingDateTime) { using (var transaction = new TransactionScope()) { try { int suspendedID; if (_context.BookingTransactions.Any()) { suspendedID = _context.BookingTransactions.OrderByDescending(b => b.SuspendedID).Select(b => b.SuspendedID).FirstOrDefault() + 1; } else { IncrementCounter incrementCounter = new IncrementCounter(); suspendedID = incrementCounter.NextValue(); //生成一个自动递增的数 } //var bookingDateTime = DateTime.Now; foreach (int tripNo in tripList) { Flight_Schedule flight_Schedule = _context.Flight_Schedules.Where(x => x.Flight_ScheduleID == tripNo).FirstOrDefault(); for (int i = 0; i < passangers; i++) { //generate a specific booking transaction(BookingTransaction) for every passanger //每一个passanger都单独生成一比业务记录(BookingTransaction) BookingTransaction bookingTransaction = new BookingTransaction { BookingDateTime = bookingDateTime, DepartDateTime = flight_Schedule.DepartDateTime, Flight_ScheduleID = flight_Schedule.Flight_ScheduleID, PersonalID = "", SuspendedID = suspendedID, CabinType = (CabinType)Enum.Parse(typeof(CabinType), cabin), Suspended = true }; _context.Add(bookingTransaction); } //_context.SaveChanges(); } //change the booked seats number according the cabin and the nuumber of passengers switch (cabin) { case "Economy": foreach (var t in tripList) { _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.Economy = f.Economy - passangers); } break; case "Business": foreach (var t in tripList) { _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.Business = f.Business - passangers); } break; case "PremEconomy": foreach (var t in tripList) { _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.PremEconomy = f.PremEconomy - passangers); } break; case "First": foreach (var t in tripList) { _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.First = f.First - passangers); } break; } //_context.SaveChanges(); transaction.Complete(); return(suspendedID); } catch (Exception ex) { return(0); } } }