private void Helper_Test_dataCon_Method_Three_Constituency2(string reportType, List <DataMeasureCandidateExt> expectedDataMeasuresList)
        {
            // Arrange
            // Instantiate a ConstituencyList object
            var testedClass = new TestedClass();

            // Add the three constituencies to the list
            testedClass.constituency.Add(Helper_KnownConstituencyDataRepository.GetKnownMiddlesbrough());
            testedClass.constituency.Add(Helper_KnownConstituencyDataRepository.GetKnownNewcastleN());
            testedClass.constituency.Add(Helper_KnownConstituencyDataRepository.GetKnownSunderlandN());

            // Act
            // Has constituencies in the list so expected to return an populated data measures list
            var actualDataMeasuresList = testedClass.dataCon(reportType);

            // Assert
            // Expected data measures list should contain the same number of items as the actual data measures list
            Assert.AreEqual(expectedDataMeasuresList.Count, actualDataMeasuresList.Count);

            // Each expected data measure should be equal to the actual data measure, note that the values are given
            // an accuracy of 4 decimal places to ensure they can be deemed equal (due to rounding)
            for (int i = 0; i < expectedDataMeasuresList.Count; i++)
            {
                Assert.AreEqual(expectedDataMeasuresList[i].ConstituencyName, actualDataMeasuresList[i].ConstituencyName);
                Assert.AreEqual(expectedDataMeasuresList[i].PartyName, actualDataMeasuresList[i].PartyName);
                Assert.AreEqual(expectedDataMeasuresList[i].Firstname, actualDataMeasuresList[i].Firstname);
                Assert.AreEqual(expectedDataMeasuresList[i].Lastname, actualDataMeasuresList[i].Lastname);
                Assert.AreEqual(expectedDataMeasuresList[i].Votes, actualDataMeasuresList[i].Votes, 0.0001);
            }
        }
예제 #2
0
 /// <summary>
 /// Constructor taking in four parameters
 /// </summary>
 /// <param name="id">id parameter</param>
 /// <param name="pcQueue">parameter for IPCQueue</param>
 /// <param name="constituencyList">parameter of ConstituencyList</param>
 /// <param name="progManager">parameter of ProgressManager</param>
 public Consumer(string id, IPCQueue pcQueue, ConstituencyList constituencyList, ProgressManager progManager)
 {
     this.id               = id;
     finished              = false; // Initially not finished
     this.pcQueue          = pcQueue;
     this.constituencyList = constituencyList;
     this.progManager      = progManager;
     (T = new Thread(run)).Start(); // Create a new thread for this consumer and get it started
     RunningThreads++;              // Increment the number of running consumer threads;
 }
        public void Test_dataCand_Method_No_Constituency_Candidates_Report()
        {
            // Arrange
            // Instantiate a ConstituencyList object
            var testedClass = new TestedClass();

            // Act
            // No constituencies in the list so expected to return an empty data measures list
            var actualDataMeasuresList = testedClass.dataCand("Candidates");

            // Assert
            // Actual data measures list should be empty
            Assert.AreEqual(0, actualDataMeasuresList.Count);
        }
        public void Test_dataCand_Method_Invalid_Report()
        {
            // Arrange
            // Instantiate a ConstituencyList object
            var testedClass = new TestedClass();

            // Act
            // Invalid report type asked for so should return null
            var actualDataMeasuresList = testedClass.dataCand("INVALID");

            // Assert
            // Actual data measures list should be null
            Assert.IsNull(actualDataMeasuresList);
        }
예제 #5
0
        /// <summary>
        /// Load all the selected XML files into the program
        /// </summary>
        public void RunProducerConsumer()
        {
            //Create constituency list to hold individual constituency objects read from datasets
            constituencyList = new ConstituencyList();

            // Create progress manager with number of files to process
            ProgressManager progManager = new ProgressManager(configData.configRecords.Count);

            // Output message to indicate that the program has started
            lblProgress.Text = "Creating and starting all producers and consumers";

            // Create a PCQueue instance, give it a capacity of 4
            var pcQueue = new PCQueue(4);

            // Create 2 Producer instances and 2 Consumer instances, these will begin executing on
            // their respective threads as soon as they are instantiated
            Producer[] producers = { new Producer("P1", pcQueue, configData, IOhandler)
                                     //,new Producer("P2", pcQueue, configData, IOhandler)
            };

            Consumer[] consumers = { new Consumer("C1", pcQueue, constituencyList, progManager),
                                     new Consumer("C2", pcQueue, constituencyList, progManager) };

            // Keep producing and consuming until all work items are completed
            while (progManager.ItemsRemaining > 0)
            {
                ;
            }

            // Output message to indicate that the program is shutting down
            lblProgress.Text = "Shutting down all producers and consumers";

            // Deactivate the PCQueue so it does not prevent waiting producer and/or consumer threads
            // from completing
            pcQueue.Active = false;

            // Iterate through producers and signal them to finish
            foreach (var p in producers)
            {
                p.Finished = true;
            }

            // Iterate through consumers and signal them to finish
            foreach (var c in consumers)
            {
                c.Finished = true;
            }

            // We need to ensure that no thread waiting on Monitor.Wait() is stranded with
            // no Monitor.Pulse() now possible since all producer and consumer threads have
            // been signalled to stop, in the worse case all such threads could be stranded
            // so pulse that many times to ensure enough pulses are made available (or the
            // program can halt erroneously), wasted pulse are simply ignored
            for (int i = 0; i < (Producer.RunningThreads + Consumer.RunningThreads); i++)
            {
                lock (pcQueue)
                {
                    // Pulse the PCQueue to signal any waiting threads
                    Monitor.Pulse(pcQueue);

                    // Give a short break to the main thread so the pulses have time to be
                    // detected by any potentially waiting producer and/or consumer threads
                    Thread.Sleep(100);
                }
            }

            // Once all producer and consumer threads have finally finished we can gracefully
            // shutdown the main thread, this is achieved by spinning on a while() loop until
            // there are no running threads, in this case we do not mind the main thread
            // spinning as we are about to shutdown the program
            while ((Producer.RunningThreads > 0) || (Consumer.RunningThreads > 0))
            {
                ;                                                                    // Wait by spinning
            }
            Console.WriteLine();
            lblProgress.Text = "All producers and consumers shut down";
        }
예제 #6
0
 public ConsoleBasedUI(ICyclistFileReader IOhandler)
 {
     this.IOhandler   = IOhandler;
     this.configData  = null;
     this.cyclistList = null;
 }