public void OneMessage()
        {
            Message[] recievedMsgs = null;
            var endPoint = new Endpoint("localhost", 23456);
            using (var reciever = new Receiver(new IPEndPoint(IPAddress.Loopback, 23456), messages =>
            {
                recievedMsgs = messages;
                return MockRepository.GenerateStub<IMessageAcceptance>();
            }, ObjectMother.Logger()))
            {
                reciever.Start();

                new Sender(ObjectMother.Logger())
                {
                    Destination = endPoint,
                    Messages = new[]
                    {
                        new Message
                        {
                            Data = new byte[] {1, 2, 4, 5, 6},
                            SentAt = new DateTime(2001, 1, 1),
                            Queue = "hello doggy",
                            Id = MessageId.GenerateRandom()
                        },
                    }
                }.Send();

                Wait.Until(() => recievedMsgs != null).ShouldBeTrue();

                recievedMsgs.ShouldHaveCount(1);
                "hello doggy".ShouldEqual(recievedMsgs[0].Queue);
                new byte[] { 1, 2, 4, 5, 6 }.ShouldEqual(recievedMsgs[0].Data);
                new DateTime(2001, 1, 1).ShouldEqual(recievedMsgs[0].SentAt);
            }
        }
Exemplo n.º 2
0
        public void CanHandleClientConnectAndDisconnect()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null, _logger))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                }

                Wait.Until(() => _logger.InfoMessages
                    .Any(x => x.StartsWith("Could not process Reading Length"))).ShouldBeTrue();
            }
        }
Exemplo n.º 3
0
        public void CanHandleClientSendingNegativeNumberForLength()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    client.GetStream().Write(BitConverter.GetBytes(-2), 0, 4);
                }

                Wait.Until(() => _logger.InfoMessages
                    .Any(x => x.Contains("Got invalid length -2"))).ShouldBeTrue();
            }
        }
        public void CanHandleClientConnectAndDisconnect()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null, _logger))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                }

                Wait.Until(() => (from e in _logger.InfoMessages.OfType<StringMessage>()
                            where e.Message.StartsWith("Could not process Reading Length")
                            select e).Any()).ShouldBeTrue();
            }
        }
Exemplo n.º 5
0
        public void CanHandleClientSendingBadLengthOfData()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    stream.Write(BitConverter.GetBytes(16), 0, 4);
                    stream.Write(BitConverter.GetBytes(5), 0, 4);
                }

                Wait.Until(() => _logger.InfoMessages
                    .Any(x => x.Contains("Could not process Reading Message"))).ShouldBeTrue();
            }
        }
Exemplo n.º 6
0
        public void Start()
        {
            AssertNotDisposedOrDisposing();

            if (_wasStarted)
                throw new InvalidOperationException("The Start method may not be invoked more than once.");

            _receiver = new Receiver(_endpoint, AcceptMessages);
            _receiver.Start();

            _choke = new SendingChoke();
            _queuedMessagesSender = new QueuedMessagesSender(_queueStorage, _choke, _logger);
            _sendingThread = new Thread(_queuedMessagesSender.Send)
            {
                IsBackground = true,
                Name = "Lightning Queues Sender Thread for " + _path
            };
            _sendingThread.Start();
            _wasStarted = true;
        }
Exemplo n.º 7
0
        public void CanHandleClientSendingThreeBytesAndDisconnecting()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    client.GetStream().Write(new byte[] { 1, 4, 6 }, 0, 3);
                }

                Wait.Until(() => _logger.InfoMessages
                    .Any(x => x.StartsWith("Could not process Reading Length"))).ShouldBeTrue();
            }
        }
Exemplo n.º 8
0
        public void WillTellSenderIfCommitFailed()
        {
            var acceptance = MockRepository.GenerateStub<IMessageAcceptance>();
            acceptance.Stub(x => x.Commit()).Throw(new InvalidOperationException());

            using (var reciever = new Receiver(_endpointToListenTo, messages => acceptance))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    var serialize = new Message[0].Serialize();
                    stream.Write(BitConverter.GetBytes(serialize.Length), 0, 4);
                    stream.Write(serialize, 0, serialize.Length);

                    var buffer = new byte[ProtocolConstants.RecievedBuffer.Length];
                    stream.Read(buffer, 0, buffer.Length);

                    ProtocolConstants.RecievedBuffer.ShouldEqual(buffer);

                    stream.Write(ProtocolConstants.AcknowledgedBuffer, 0, ProtocolConstants.AcknowledgedBuffer.Length);

                    buffer = new byte[ProtocolConstants.RevertBuffer.Length];
                    stream.Read(buffer, 0, buffer.Length);

                    ProtocolConstants.RevertBuffer.ShouldEqual(buffer);
                }
            }
        }
Exemplo n.º 9
0
        public void WillSendConfirmationForClient()
        {
            var acceptance = MockRepository.GenerateStub<IMessageAcceptance>();
            using (var reciever = new Receiver(_endpointToListenTo, messages => acceptance))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    var serialize = new Message[0].Serialize();
                    stream.Write(BitConverter.GetBytes(serialize.Length), 0, 4);
                    stream.Write(serialize, 0, serialize.Length);

                    var buffer = new byte[ProtocolConstants.RecievedBuffer.Length];
                    stream.Read(buffer, 0, buffer.Length);

                    ProtocolConstants.RecievedBuffer.ShouldEqual(buffer);
                }
            }
        }
Exemplo n.º 10
0
        public void WillLetSenderKnowThatMessagesWereSentToInvalidQueue()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages =>
            {
                throw new QueueDoesNotExistsException();
            }))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    var serialize = new Message[0].Serialize();
                    stream.Write(BitConverter.GetBytes(serialize.Length), 0, 4);
                    stream.Write(serialize, 0, serialize.Length);

                    var buffer = new byte[ProtocolConstants.ProcessingFailureBuffer.Length];
                    stream.Read(buffer, 0, buffer.Length);

                    ProtocolConstants.QueueDoesNoExiststBuffer.ShouldEqual(buffer);
                }
            }
        }
Exemplo n.º 11
0
        public void WillCallCommitAcceptanceIfSenderSendConfirmation()
        {
            var commitCalled = false;
            var acceptance = MockRepository.GenerateStub<IMessageAcceptance>();
            acceptance.Expect(x => x.Commit()).WhenCalled(x => commitCalled = true);
            using (var reciever = new Receiver(_endpointToListenTo, messages => acceptance))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    var serialize = new Message[0].Serialize();
                    stream.Write(BitConverter.GetBytes(serialize.Length), 0, 4);
                    stream.Write(serialize, 0, serialize.Length);

                    var buffer = new byte[ProtocolConstants.RecievedBuffer.Length];
                    stream.Read(buffer, 0, buffer.Length);

                    ProtocolConstants.RecievedBuffer.ShouldEqual(buffer);

                    stream.Write(ProtocolConstants.AcknowledgedBuffer, 0, ProtocolConstants.AcknowledgedBuffer.Length);
                }
            }

            Wait.Until(() => commitCalled);

            acceptance.VerifyAllExpectations();
        }
Exemplo n.º 12
0
        public void CanHandleSlowClients()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null, _logger))
            {
                reciever.Timeout = TimeSpan.FromMilliseconds(500);
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    stream.Write(BitConverter.GetBytes(16), 0, 4);
                    Thread.Sleep(1000);
                    stream.Write(Guid.NewGuid().ToByteArray(), 0, 16);
                }

                Wait.Until(() => _logger.InfoMessages
                    .Any(x => x.Contains("TimeoutException"))).ShouldBeTrue();
            }
        }
Exemplo n.º 13
0
        public void CanHandleClientSendingUnseriliazableData()
        {
            using (var reciever = new Receiver(_endpointToListenTo, messages => null))
            {
                reciever.Start();

                using (var client = new TcpClient())
                {
                    client.Connect(_endpointToListenTo);
                    var stream = client.GetStream();
                    stream.Write(BitConverter.GetBytes(16), 0, 4);
                    stream.Write(Guid.NewGuid().ToByteArray(), 0, 16);
                }

                Wait.Until(() =>_logger.InfoMessages
                    .Any(x => x.Contains("Unable to deserialize messages"))).ShouldBeTrue();
            }
        }