/// <summary> /// Grabs and reorders items from the set of created tickets and members /// </summary> private bool PeekItems(out TestMember testMember, out TestTicket testTicket) { testMember = null; testTicket = null; if (!createdMembers.TryTake(out testMember)) { // unable to get a member Console.WriteLine("\t\tThread " + ID + ": No Member Found"); return(false); } // Return to bag for reordering createdMembers.Add(testMember); if (!createdTickets.TryTake(out testTicket)) { // unable to get a ticket, return member to bag Console.WriteLine("\t\tThread " + ID + ": No Ticket Found"); return(false); } // Return to bag for reordering createdTickets.Add(testTicket); Console.WriteLine("\t\tThread " + ID + ": Member(" + testMember.name + "), Ticket(" + testTicket.section + "," + testTicket.row + "," + testTicket.seat + ")"); return(true); }
/// <summary> /// Holds a random ticket from the created tickets pool. /// </summary> private async Task <bool> HoldTicket(TestMember member, TestTicket ticket) { if (!(await ticketingSystem.HoldTicket(ticket.section, ticket.row, ticket.seat, member.name))) { // unable to free the designated ticket return(false); } return(true); }
/// <summary> /// Queries the user for inputs on threads, number of members and number of tickets to test against /// </summary> public static void RunE2ETest() { ConcurrentQueue <TestMember> memberList = null; ConcurrentQueue <TestTicket> ticketList = null; Console.WriteLine("Enter the number of test threads: "); int numThreads = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the number of test members: "); int numMembers = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the number of test tickets: "); int numTickets = Convert.ToInt32(Console.ReadLine()); memberList = new ConcurrentQueue <TestMember>(); ticketList = new ConcurrentQueue <TestTicket>(); Console.WriteLine("Member List:"); for (int i = 0; i < numMembers; ++i) { TestMember tempMember = new TestMember(); memberList.Enqueue(tempMember); Console.WriteLine("\t- " + tempMember.name); } Console.WriteLine(); Console.WriteLine("Ticket List:"); for (int i = 0; i < numTickets; ++i) { TestTicket tempTicket = new TestTicket(); ticketList.Enqueue(tempTicket); Console.WriteLine("\t- " + tempTicket.section + " " + tempTicket.row + " " + tempTicket.seat); } Console.WriteLine(); TestThread.Init(memberList, ticketList); CountDownLatch latch = new CountDownLatch(numThreads); for (int i = 0; i < numThreads; ++i) { TestThread testThread = new TestThread(i, latch); Thread t = new Thread(new ThreadStart(testThread.ThreadProc)); Console.WriteLine("Main: created thread " + i); t.Start(); } Console.WriteLine("Waiting on all threads to return..."); latch.Wait(); }
/// <summary> /// Purchases a random ticket held by a random member from the created pools /// </summary> private async Task <bool> PurchaseTicket(TestMember member, TestTicket ticket) { if (!(await ticketingSystem.PurchaseTicket(ticket.section, ticket.row, ticket.seat, member.name))) { // unable to free the designated ticket return(false); } ++purchasedTickets; Console.WriteLine("Thread " + ID + ": ticket purchased, " + purchasedTickets + " out of " + ticketsToPurchase); return(true); }
/// <summary> /// Creates a random member from the available member pool. /// </summary> private async Task <bool> CreateMember() { TestMember member = null; if (!availableMemberList.TryDequeue(out member)) { // Unable to retrieve a member return(false); } if (!(await ticketingSystem.CreateMemberAsync(member.name))) { // Unable to create member. Restore to availableMember list availableMemberList.Enqueue(member); return(false); } // Add member to random access container createdMembers.Add(member); return(true); }
/// <summary> /// Selects a random activity to run and completes the action regardless of projected failure /// </summary> public async void ThreadProc() { bool activitySuccess = false; while (initialized && purchasedTickets < ticketsToPurchase) { TestMember member = null; TestTicket ticket = null; activitySuccess = PeekItems(out member, out ticket); Console.Write("\tThread " + ID + ": "); switch (SelectActivity()) { case Activity.CREATE_MEMBER: Console.WriteLine("Creating Member"); activitySuccess = await CreateMember(); break; case Activity.CREATE_TICKET: Console.WriteLine("Creating Ticket"); activitySuccess = await CreateTicket(); break; case Activity.FREE_TICKET: Console.WriteLine("Freeing Ticket"); activitySuccess = activitySuccess ? await FreeTicket(member, ticket) : false; break; case Activity.HOLD_TICKET: Console.WriteLine("Holding Ticket"); activitySuccess = activitySuccess ? await HoldTicket(member, ticket) : false; break; case Activity.PURCHASE_TICKET: Console.WriteLine("Purchasing Ticket"); activitySuccess = activitySuccess ? await PurchaseTicket(member, ticket) : false; break; case Activity.HOLD_FREE_TICKET: Console.WriteLine("Holding and Freeing Ticket"); activitySuccess = activitySuccess ? await HoldTicket(member, ticket) : false; activitySuccess = activitySuccess ? await FreeTicket(member, ticket) : false; break; case Activity.HOLD_PURCHASE_TICKET: Console.WriteLine("Holding and Purchasing Ticket"); activitySuccess = activitySuccess ? await HoldTicket(member, ticket) : false; activitySuccess = activitySuccess ? await PurchaseTicket(member, ticket) : false; break; default: break; } Console.WriteLine("Activity Successful: " + activitySuccess); } Console.WriteLine("\tThread " + ID + " Finished Signal"); latch.Signal(); return; }