public void StressTest()
        {
            queue = new ProducerConsumerQueue();

            ThreadStart consumerMethod = new ThreadStart(accumulatingRunner);
            Thread consumerThread = new Thread(consumerMethod);
            consumerThread.Start();
            Thread.Sleep(500);

            ArrayList threads = new ArrayList();
            for (int i = 0; i < 100; i++)
            {
                ThreadStart producerMethod = new ThreadStart(producingThread);
                threads.Add(new Thread(producerMethod));
            }

            for (int i = 0; i < 100; i++)
            {
                ((Thread)threads[i]).Start();
            }

            for (int i = 0; i < 100; i++)
            {
                ((Thread)threads[i]).Join();
            }

            consumerThread.Join();
            Assert.AreEqual(0, queue.Count);
            Assert.AreEqual(100000, staticCounter);

            for (int i = 0; i < 100000; i++)
            {
                Assert.AreEqual(i, (int)accumulatedResults[i], "Failed at index " + i);
            }
        }
 public void CanStopConsumerSide()
 {
     queue = new ProducerConsumerQueue();
     dequeuedMsg = "This will be null if we break out of dequeue and return a null msg";
     ThreadStart consumerMethod = new ThreadStart(runner);
     Thread consumerThread = new Thread(consumerMethod);
     consumerThread.Start();
     Thread.Sleep(500);
     consumerThread.Interrupt();
     consumerThread.Join();
     Thread.Sleep(500);
     Assert.IsNull(dequeuedMsg);
 }
        public void TestTaskCompletion()
        {
            var queue = new ProducerConsumerQueue();
            var task = queue.Enqueue(() =>
            {
                Thread.Sleep(500);
            });
            task.Wait();

            Assert.IsFalse(task.IsFaulted);
            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.Status == System.Threading.Tasks.TaskStatus.RanToCompletion);
        }
        public void CanAddElementAsProducerAndRemoveAsConsumer()
        {
            queue = new ProducerConsumerQueue();
            ThreadStart consumerMethod = new ThreadStart(runner);
            Thread consumerThread = new Thread(consumerMethod);
            consumerThread.Start();
            Thread.Sleep(500);

            queue.Enqueue("this is a string");

            Thread.Sleep(500);
            consumerThread.Join();

            Assert.AreEqual("this is a string", dequeuedMsg);
        }
Esempio n. 5
0
 public void Start()
 {
     try
     {
         contextQueue = new ProducerConsumerQueue
             <HttpListenerContext>(ProcessRequest, config.NumWorkerThreads);
         RestLogger.LogInfo("RestServer starting, {0}", config.BaseUrl);
         listener.Prefixes.Add(config.BaseUrl);
         IsListening = true;
         listener.Start();
         listenerThread.Start();
     }
     catch(Exception ex)
     {
         IsListening = false;
         LogException("Start", ex);
         throw;
     }
 }
        public void TestException()
        {
            var queue = new ProducerConsumerQueue();
            var task = queue.Enqueue(() =>
            {
                Thread.Sleep(500);
                throw new Exception("You failed");

            });
            try
            {
                task.Wait();
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e);
                Assert.IsTrue(e.InnerException.Message == "You failed");
            }
        }