public void ReceiveMetricsTest()
        {
            using (mocks.Ordered)
            {
                SetConnectExpectations();
                // Expects for Receive()
                SetBeginMessageReceiveExpectations(testMessageByteArray.Length);
                Expect.Once.On(mockNetworkStream).Method("Read").Will(new SetNamedParameterAction("buffer", testMessageByteArray), Return.Value(testMessageByteArray.Length));
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockNetworkStream).Method("WriteByte");
                Expect.Once.On(mockMetricLogger).Method("End").With(IsMetric.Equal(new MessageReceiveTime()));
                Expect.Once.On(mockMetricLogger).Method("Increment").With(IsMetric.Equal(new MessageReceived()));
                Expect.Once.On(mockMetricLogger).Method("Add").With(IsAmountMetric.Equal(new ReceivedMessageSize(16)));
            }

            testTcpRemoteReceiver.Connect();
            testTcpRemoteReceiver.Receive();
            mocks.VerifyAllExpectationsHaveBeenMet();
        }
Exemplo n.º 2
0
        public void ReceiveLoggingTest()
        {
            // Setup test messages
            string testMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MethodInvocation><MethodName>TestMethod</MethodName><Parameters><Parameter><DataType>string</DataType><Data>abc</Data></Parameter><Parameter><DataType>integer</DataType><Data>123</Data></Parameter><Parameter /><Parameter><DataType>double</DataType><Data>4.5678899999999999e+002</Data></Parameter></Parameters><ReturnType /></MethodInvocation>";

            byte[] testMessageBody           = System.Text.Encoding.UTF8.GetBytes(testMessage);
            byte[] testMessageSequenceNumber = BitConverter.GetBytes(123);
            byte[] testMessageSizeHeader     = BitConverter.GetBytes(testMessageBody.LongLength);
            if (BitConverter.IsLittleEndian == false)
            {
                Array.Reverse(testMessageSequenceNumber);
                Array.Reverse(testMessageSizeHeader);
            }
            byte[] testMessageByteArray = new byte[testMessageSequenceNumber.Length + testMessageSizeHeader.Length + testMessageBody.Length + 2];
            testMessageByteArray[0] = 0x02;  // Set the start delimiter
            Array.Copy(testMessageSequenceNumber, 0, testMessageByteArray, 1, testMessageSequenceNumber.Length);
            Array.Copy(testMessageSizeHeader, 0, testMessageByteArray, 1 + testMessageSequenceNumber.Length, testMessageSizeHeader.Length);
            Array.Copy(testMessageBody, 0, testMessageByteArray, 1 + testMessageSequenceNumber.Length + testMessageSizeHeader.Length, testMessageBody.Length);
            testMessageByteArray[testMessageByteArray.Length - 1] = 0x03;  // Set the end delimiter

            string testSmallMessage = "<TestMessage>Test message content</TestMessage>";

            byte[] testSmallMessageBody           = System.Text.Encoding.UTF8.GetBytes(testSmallMessage);
            byte[] testSmallMessageSequenceNumber = BitConverter.GetBytes(124);
            byte[] testSmallMessageSizeHeader     = BitConverter.GetBytes(testSmallMessageBody.LongLength);
            if (BitConverter.IsLittleEndian == false)
            {
                Array.Reverse(testSmallMessageSequenceNumber);
                Array.Reverse(testSmallMessageSizeHeader);
            }
            byte[] testSmallMessageByteArray = new byte[testSmallMessageSequenceNumber.Length + testSmallMessageSizeHeader.Length + testSmallMessageBody.Length + 2];
            testSmallMessageByteArray[0] = 0x02;  // Set the start delimiter
            Array.Copy(testSmallMessageSequenceNumber, 0, testSmallMessageByteArray, 1, testSmallMessageSequenceNumber.Length);
            Array.Copy(testSmallMessageSizeHeader, 0, testSmallMessageByteArray, 1 + testSmallMessageSequenceNumber.Length, testSmallMessageSizeHeader.Length);
            Array.Copy(testSmallMessageBody, 0, testSmallMessageByteArray, 1 + testSmallMessageSequenceNumber.Length + testSmallMessageSizeHeader.Length, testSmallMessageBody.Length);
            testSmallMessageByteArray[testSmallMessageByteArray.Length - 1] = 0x03;  // Set the end delimiter

            using (mocks.Ordered)
            {
                // Expects for Connect()
                Expect.Once.On(mockTcpListener).GetProperty("Active").Will(Return.Value(false));
                Expect.Once.On(mockTcpListener).Method("Start");
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(true));
                Expect.Once.On(mockTcpListener).Method("AcceptTcpClient").Will(Return.Value(mockTcpClient));
                Expect.Once.On(mockApplicationLogger).Method("Log");
                // Expects for first Receive()
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockTcpClient).GetProperty("Available").Will(Return.Value(testMessageByteArray.Length));
                Expect.Once.On(mockTcpClient).Method("GetStream").Will(Return.Value(mockNetworkStream));
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockTcpClient).GetProperty("Available").Will(Return.Value(testMessageByteArray.Length));
                Expect.Once.On(mockNetworkStream).Method("Read").Will(new SetNamedParameterAction("buffer", testMessageByteArray), Return.Value(testMessageByteArray.Length));
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockNetworkStream).Method("WriteByte");
                Expect.Once.On(mockApplicationLogger).Method("Log").With(testTcpRemoteReceiver, LogLevel.Information, "Received message '<?xml version=\"1.0\" encoding=\"utf-8\"?><MethodInvocation><MethodName>TestMethod</MethodName><Parameters><Parameter><DataT' (truncated).");
                Expect.Once.On(mockApplicationLogger).Method("Log").With(testTcpRemoteReceiver, LogLevel.Debug, "Complete message content: '" + testMessage + "'.");
                // Expects for second Receive()
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockTcpClient).GetProperty("Available").Will(Return.Value(testSmallMessageByteArray.Length));
                Expect.Once.On(mockTcpClient).Method("GetStream").Will(Return.Value(mockNetworkStream));
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockTcpClient).GetProperty("Available").Will(Return.Value(testSmallMessageByteArray.Length));
                Expect.Once.On(mockNetworkStream).Method("Read").Will(new SetNamedParameterAction("buffer", testSmallMessageByteArray), Return.Value(testSmallMessageByteArray.Length));
                Expect.Once.On(mockTcpListener).Method("Pending").Will(Return.Value(false));
                Expect.Once.On(mockNetworkStream).Method("WriteByte");
                Expect.Once.On(mockApplicationLogger).Method("Log").With(testTcpRemoteReceiver, LogLevel.Information, "Received message '" + testSmallMessage + "'.");
                Expect.Once.On(mockApplicationLogger).Method("Log").With(testTcpRemoteReceiver, LogLevel.Debug, "Complete message content: '" + testSmallMessage + "'.");
            }

            testTcpRemoteReceiver.Connect();
            testTcpRemoteReceiver.Receive();
            testTcpRemoteReceiver.Receive();

            mocks.VerifyAllExpectationsHaveBeenMet();
        }
        public void ReceiveWhenDisposed()
        {
            using (mocks.Ordered)
            {
                SetDisposeExpectations();
            }

            testTcpRemoteReceiver.Dispose();
            ObjectDisposedException e = Assert.Throws <ObjectDisposedException>(delegate
            {
                testTcpRemoteReceiver.Receive();
            });

            mocks.VerifyAllExpectationsHaveBeenMet();
        }