public void ItShouldBePossibleToPeekMessagesForAMessagingThread()
        {
            const int sleepTime = 1;
            const string testName = "TestThread";
            const int messagesToSend = 100;
            var th = new SimpleMessageThreadConsumer(sleepTime, testName);
            var messages = new List<TestMessage>();
            th.ForwardMessages = true;
            th.RunThread();
            Thread.Sleep(100);

            for (int i = 0; i < messagesToSend; i++)
            {
                var newMsg = new TestMessage();
                th.SendMessageToThread(newMsg);
                messages.Add(newMsg);
            }

            Assert.AreEqual(RunningStatus.Running, th.Status);
            Thread.Sleep(100);

            var msg = th.PeekMessageFromThread() as TestMessage;
            Assert.IsNotNull(msg);
            Assert.AreSame(msg, messages[0]);

            var resultingMessages = new List<TestMessage>();
            foreach (var item in th.PeekMessagesFromThread())
            {
                msg = item as TestMessage;
                if(msg!=null)
                resultingMessages.Add(msg);
            }
            Assert.AreEqual(messages.Count-1,resultingMessages.Count);

            th.Terminate();
            Thread.Sleep(100);

            Assert.AreEqual(RunningStatus.Halted, th.Status);
            Assert.IsNull(th.LastError);

            Assert.IsTrue(th.IsInitialized);
            Assert.IsTrue(th.IsCleanedUp);
            Assert.AreEqual(messagesToSend, th.HandledMessages);
            th.Dispose();
        }
        public void ItShouldBePossibleToRestartAutomaticallyAMessageThreadOnErrorInMessageHandling()
        {
            const int sleepTime = 1;
            const string testName = "TestThread";
            const int messagesToSend = 100;
            var expectedException = new Exception("TEST");
            var th = new SimpleMessageThreadConsumer(sleepTime, testName);

            th.RunThread();
            Thread.Sleep(100);

            for (int i = 0; i < messagesToSend; i++)
            {
                th.SendMessageToThread(new TestMessage());
            }
            Thread.Sleep(100);
            th.ThrowExceptionOnMessageHandling = expectedException;
            Thread.Sleep(100);
            for (int i = 0; i < messagesToSend; i++)
            {
                th.SendMessageToThread(new TestMessage());
            }
            Thread.Sleep(100);
            th.Terminate();
            Thread.Sleep(500);

            Assert.AreEqual(RunningStatus.Halted, th.Status);
            Assert.IsNull(th.LastError);

            Assert.IsTrue(th.IsInitialized);
            Assert.IsTrue(th.IsCleanedUp);
            Assert.AreEqual(messagesToSend*2, th.HandledMessages);
        }
        public void ItShouldBePossibleToStartAndStopAMessagingThread()
        {
            const int sleepTime = 100;
            const string testName = "TestThread";
            var th = new SimpleMessageThreadConsumer(sleepTime, testName);

            th.RunThread();
            Thread.Sleep(100);

            Assert.AreEqual(RunningStatus.Running, th.Status);

            th.Terminate();
            Thread.Sleep(200);

            Assert.AreEqual(RunningStatus.Halted, th.Status);
            Assert.IsNull(th.LastError);

            Assert.IsTrue(th.IsInitialized);
            Assert.IsTrue(th.IsCleanedUp);
        }
        public void ItShouldBePossibleToReceiveMessagesForAMessagingThread()
        {
            const int sleepTime = 1;
            const string testName = "TestThread";
            const int messagesToSend = 100;
            var th = new SimpleMessageThreadConsumer(sleepTime, testName);

            th.RunThread();
            Thread.Sleep(100);

            for (int i = 0; i < messagesToSend; i++)
            {
                th.SendMessageToThread(new TestMessage());
            }

            Assert.AreEqual(RunningStatus.Running, th.Status);
            Thread.Sleep(100);

            th.Terminate();
            Thread.Sleep(100);

            Assert.AreEqual(RunningStatus.Halted, th.Status);
            Assert.IsNull(th.LastError);

            Assert.IsTrue(th.IsInitialized);
            Assert.IsTrue(th.IsCleanedUp);
            Assert.AreEqual(messagesToSend, th.HandledMessages);
        }