Beispiel #1
0
        public void ClientQueueProducerConstructorTest(Random rand)
        {
            ConcurrentQueue <ProcessData> queue = new ConcurrentQueue <ProcessData>();

            TimeSpan producerThread = new TimeSpan(0, 0, 0, 0, rand.Next() % 10);
            TimeSpan consumerThread = new TimeSpan(0, 0, 0, 0, rand.Next() % 100);
            TimeSpan testThread     = TimeSpan.FromTicks(10 * (producerThread + consumerThread + new TimeSpan(0, 0, 0, 0, rand.Next() % 100)).Ticks);

            TimeSpan oneTick = new TimeSpan(1);

            producerThread += oneTick;
            consumerThread += oneTick;
            testThread     += oneTick;

            //ConcurrentQueue
            ClientQueueProducer producer = new ClientQueueProducer(queue);
            ClientQueueConsumer consumer = new ClientQueueConsumer(queue);

            StopThread requestProducerToStop = new StopThread()
            {
                Value = false
            };
            StopThread requestConsumerToStop = new StopThread()
            {
                Value = false
            };

            int prodCount             = 0;
            List <ProcessData> actual = null;

            Thread prodThread = new Thread(() => { prodCount = ProducerThread(producer, requestProducerToStop, producerThread); });
            Thread consThread = new Thread(() => { actual = ConsumerThread(consumer, requestConsumerToStop, consumerThread); });

            prodThread.Start();
            consThread.Start();

            // Let threads work
            Thread.Sleep(testThread);

            requestProducerToStop.Value = true;
            prodThread.Join();

            requestConsumerToStop.Value = true;
            consThread.Join();

            // Verification stuff
            List <ProcessData> expected = new List <ProcessData>();

            for (int i = 0; i < prodCount; i++)
            {
                expected.Add(new ProcessDataMock()
                {
                    TempData = i
                });
            }

            CollectionAssert.AreEqual(expected, actual, "consumer should be able to consume all producerThread.Ticks = {0}, consumerThread.Ticks = {1},  testThread.Ticks {2}", producerThread.Ticks, consumerThread.Ticks, testThread.Ticks);
        }
Beispiel #2
0
        private int ProducerThread(ClientQueueProducer producer, StopThread requestProducerToStop, TimeSpan producerThread)
        {
            int i = 0;

            while (requestProducerToStop.Value == false)
            {
                producer.Add(new ProcessDataMock()
                {
                    TempData = i
                });
                Thread.Sleep(producerThread);
                i++;
            }

            return(i);
        }