public void BufferTest()
        {
            var payload = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};

            var processor = new AsynchronousCommandProcessor();
            try
            {
                bool eventSuccess = false;
                var resetEvent = new ManualResetEvent(false);

                processor.StartProcessing();
                Assert.IsTrue(processor.IsProcessingMessages);

                processor.EchoEvent += (sender, args) =>
                {
                    eventSuccess = true;
                    resetEvent.Set();
                };

                var command = new EchoCommand {PayLoad = payload};
                EchoResponse response = processor.SendCommandWithResponse(command);
                Assert.IsNotNull(response, "SendCommand did not return a response.");
                Assert.IsInstanceOfType(response, typeof (EchoResponse));
                Assert.IsTrue(BufferCompare(response.Payload, payload), "Response payload did not match the command payload.");

                bool signaled = resetEvent.WaitOne(10000);
                Assert.IsTrue(signaled);
                Assert.IsTrue(eventSuccess);
            }
            finally
            {
                processor.StopProcessing();
                Assert.IsFalse(processor.IsProcessingMessages);
            }
        }
        public void AsynchronousCommandProcessorTest()
        {
            const int iterations = 10;
            var eventsFired = 0;
            var responsePayloadsMatched = 0;
            var eventPayloadsMatched = 0;

            var random = new Random();

            var commandProcessor = new AsynchronousCommandProcessor();
            var echoResetEvent = new ManualResetEvent(false);
            byte[] payload = null;
            int payloadLength = 0;

            commandProcessor.Echo += (sender, args) =>
            {
                if (payloadLength == args.Payload.Length && BufferCompare(payload, args.Payload))
                    Interlocked.Increment(ref eventPayloadsMatched);

                Interlocked.Increment(ref eventsFired);
                Debug.WriteLine("event fired #" + eventsFired);
                echoResetEvent.Set();
            };

            try
            {
                Assert.IsFalse(commandProcessor.IsProcessingCommands);
                Assert.IsFalse(commandProcessor.IsProcessingMessages);
                Assert.IsFalse(commandProcessor.IsProcessingEvents);
                commandProcessor.StartProcessing();
                Assert.IsTrue(commandProcessor.IsProcessingCommands);
                Assert.IsTrue(commandProcessor.IsProcessingMessages);
                Assert.IsTrue(commandProcessor.IsProcessingEvents);

                for (var iteration = 0; iteration < iterations; iteration++)
                {
                    payloadLength = random.Next(1, 255);

                    payload = new byte[payloadLength];
                    random.NextBytes(payload);

                    var echoResponse = commandProcessor.SendCommandWithResponse(new EchoCommand {Payload = payload});
                    Debug.WriteLine("Response #" + (iteration + 1));
                    Assert.IsNotNull(echoResponse);
                    Assert.IsInstanceOfType(echoResponse, typeof (EchoResponse));
                    if (payloadLength == echoResponse.Payload.Length && BufferCompare(payload, echoResponse.Payload))
                        responsePayloadsMatched++;

                    echoResetEvent.WaitOne(commandProcessor.CommandTimeout + commandProcessor.EventWait);
                }

            }
            finally
            {
                Assert.IsTrue(commandProcessor.IsProcessingCommands);
                Assert.IsTrue(commandProcessor.IsProcessingMessages);
                Assert.IsTrue(commandProcessor.IsProcessingEvents);
                commandProcessor.StopProcessing();
                Assert.IsFalse(commandProcessor.IsProcessingCommands);
                Assert.IsFalse(commandProcessor.IsProcessingMessages);
                Assert.IsFalse(commandProcessor.IsProcessingEvents);

                Debug.WriteLine("Iterations = " + iterations);
                Debug.WriteLine("Events fired = " + eventsFired);
                Debug.WriteLine("Events payloads matched = " + eventPayloadsMatched);
                Debug.WriteLine("Response payloads matched = " + responsePayloadsMatched);

                Assert.IsTrue(eventsFired == iterations);
                Assert.IsTrue(eventPayloadsMatched == iterations);
                Assert.IsTrue(responsePayloadsMatched == iterations);
            }
        }