public void when_handling_trusted_write_on_internal_service()
        {
            ManualResetEvent waiter = new ManualResetEvent(false);
            ClientMessage.WriteEvents publishedWrite = null;
            var evnt = new Event(Guid.NewGuid(), "TestEventType", true, new byte[] { }, new byte[] { });
            var write = new TcpClientMessageDto.WriteEvents(
                Guid.NewGuid().ToString(),
                ExpectedVersion.Any,
                new[] { new TcpClientMessageDto.NewEvent(evnt.EventId.ToByteArray(), evnt.EventType, evnt.IsJson ? 1 : 0, 0, evnt.Data, evnt.Metadata) },
                false);

            var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize());
            var dummyConnection = new DummyTcpConnection();
            var publisher = InMemoryBus.CreateTest();

            publisher.Subscribe(new AdHocHandler<ClientMessage.WriteEvents>(x => {
                publishedWrite = x;
                waiter.Set();
            }));

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.Internal, new ClientTcpDispatcher(),
                publisher, dummyConnection, publisher, new InternalAuthenticationProvider(new Core.Helpers.IODispatcher(publisher, new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { });

            tcpConnectionManager.ProcessPackage(package);

            if (!waiter.WaitOne(TimeSpan.FromSeconds(5)))
            {
                throw new Exception("Timed out waiting for events.");
            }
            Assert.AreEqual(evnt.EventId, publishedWrite.Events.First().EventId, "Expected the published write to be the event that was sent through the tcp connection manager to be the event {0} but got {1}", evnt.EventId, publishedWrite.Events.First().EventId);
        }
Ejemplo n.º 2
0
        public void when_handling_trusted_write_on_internal_service()
        {
            ManualResetEvent waiter = new ManualResetEvent(false);

            ClientMessage.WriteEvents publishedWrite = null;
            var evnt  = new Event(Guid.NewGuid(), "TestEventType", true, new byte[] { }, new byte[] { });
            var write = new TcpClientMessageDto.WriteEvents(
                Guid.NewGuid().ToString(),
                ExpectedVersion.Any,
                new[] { new TcpClientMessageDto.NewEvent(evnt.EventId.ToByteArray(), evnt.EventType, evnt.IsJson ? 1 : 0, 0, evnt.Data, evnt.Metadata) },
                false);

            var package         = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize());
            var dummyConnection = new DummyTcpConnection();
            var publisher       = InMemoryBus.CreateTest();

            publisher.Subscribe(new AdHocHandler <ClientMessage.WriteEvents>(x => {
                publishedWrite = x;
                waiter.Set();
            }));

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.Internal, new ClientTcpDispatcher(),
                publisher, dummyConnection, publisher, new InternalAuthenticationProvider(new Core.Helpers.IODispatcher(publisher, new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { });

            tcpConnectionManager.ProcessPackage(package);

            if (!waiter.WaitOne(TimeSpan.FromSeconds(5)))
            {
                throw new Exception("Timed out waiting for events.");
            }
            Assert.AreEqual(evnt.EventId, publishedWrite.Events.First().EventId, "Expected the published write to be the event that was sent through the tcp connection manager to be the event {0} but got {1}", evnt.EventId, publishedWrite.Events.First().EventId);
        }
        public void when_handling_trusted_write_on_external_service()
        {
            var package = new TcpPackage(TcpCommand.WriteEvents, TcpFlags.TrustedWrite, Guid.NewGuid(), null, null,
                                         new byte[] { });

            var dummyConnection = new DummyTcpConnection();

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(2000),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(),
                new InternalAuthenticationProvider(
                    InMemoryBus.CreateTest(), new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()),
                    new StubPasswordHashAlgorithm(), 1, false),
                new AuthorizationGateway(new TestAuthorizationProvider()),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { },
                _connectionPendingSendBytesThreshold, _connectionQueueSizeThreshold);

            tcpConnectionManager.ProcessPackage(package);

            var data            = dummyConnection.ReceivedData.Last();
            var receivedPackage = TcpPackage.FromArraySegment(data);

            Assert.AreEqual(receivedPackage.Command, TcpCommand.BadRequest, "Expected Bad Request but got {0}",
                            receivedPackage.Command);
        }
        when_send_queue_size_is_larger_than_threshold_should_close_connection()
        {
            var mre = new ManualResetEventSlim();

            var messageSize = _connectionPendingSendBytesThreshold;
            var evnt        = new EventRecord(0, 0, Guid.NewGuid(), Guid.NewGuid(), 0, 0, "testStream", 0, DateTime.Now,
                                              PrepareFlags.None, "eventType", new byte[messageSize], new byte[0]);
            var record  = ResolvedEvent.ForUnresolvedEvent(evnt, null);
            var message = new ClientMessage.ReadEventCompleted(Guid.NewGuid(), "testStream", ReadEventResult.Success,
                                                               record, StreamMetadata.Empty, false, "");

            var dummyConnection = new DummyTcpConnection();

            dummyConnection.SendQueueSize = ESConsts.MaxConnectionQueueSize + 1;

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(2000),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(),
                new InternalAuthenticationProvider(InMemoryBus.CreateTest(),
                                                   new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), null, 1, false),
                new AuthorizationGateway(new TestAuthorizationProvider()),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { mre.Set(); },
                ESConsts.UnrestrictedPendingSendBytes, ESConsts.MaxConnectionQueueSize);

            tcpConnectionManager.SendMessage(message);

            if (!mre.Wait(2000))
            {
                Assert.Fail("Timed out waiting for connection to close");
            }
        }
        when_send_queue_size_is_smaller_than_threshold_should_not_close_connection()
        {
            var mre = new ManualResetEventSlim();

            var messageSize = _connectionPendingSendBytesThreshold;
            var evnt        = new EventRecord(0, 0, Guid.NewGuid(), Guid.NewGuid(), 0, 0, "testStream", 0, DateTime.Now,
                                              PrepareFlags.None, "eventType", new byte[messageSize], new byte[0]);
            var record  = ResolvedEvent.ForUnresolvedEvent(evnt, null);
            var message = new ClientMessage.ReadEventCompleted(Guid.NewGuid(), "testStream", ReadEventResult.Success,
                                                               record, StreamMetadata.Empty, false, "");

            var dummyConnection = new DummyTcpConnection();

            dummyConnection.SendQueueSize = ESConsts.MaxConnectionQueueSize - 1;

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(2000),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(),
                new InternalAuthenticationProvider(InMemoryBus.CreateTest(),
                                                   new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), null, 1, false),
                new AuthorizationGateway(new TestAuthorizationProvider()),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { mre.Set(); },
                ESConsts.UnrestrictedPendingSendBytes, ESConsts.MaxConnectionQueueSize);

            tcpConnectionManager.SendMessage(message);

            var data            = dummyConnection.ReceivedData.Last();
            var receivedPackage = TcpPackage.FromArraySegment(data);

            Assert.AreEqual(receivedPackage.Command, TcpCommand.ReadEventCompleted,
                            "Expected ReadEventCompleted but got {0}", receivedPackage.Command);
        }
Ejemplo n.º 6
0
        when_limit_pending_and_sending_message_larger_than_pending_bytes_threshold_but_no_bytes_pending_should_not_close_connection()
        {
            var messageSize = _connectionPendingSendBytesThreshold + 1000;
            var evnt        = new EventRecord(0, 0, Guid.NewGuid(), Guid.NewGuid(), 0, 0, "testStream", 0, DateTime.Now,
                                              PrepareFlags.None, "eventType", new byte[messageSize], new byte[0]);
            var record  = ResolvedEvent.ForUnresolvedEvent(evnt, null);
            var message = new ClientMessage.ReadEventCompleted(Guid.NewGuid(), "testStream", ReadEventResult.Success,
                                                               record, StreamMetadata.Empty, false, "");

            var dummyConnection = new DummyTcpConnection();

            dummyConnection.PendingSendBytes = 0;

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(),
                new InternalAuthenticationProvider(
                    new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), null, 1, false),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { },
                _connectionPendingSendBytesThreshold, _connectionQueueSizeThreshold);

            tcpConnectionManager.SendMessage(message);

            var data            = dummyConnection.ReceivedData.Last();
            var receivedPackage = TcpPackage.FromArraySegment(data);

            Assert.AreEqual(receivedPackage.Command, TcpCommand.ReadEventCompleted,
                            "Expected ReadEventCompleted but got {0}", receivedPackage.Command);
        }
Ejemplo n.º 7
0
        public void Setup()
        {
            _dispatcher = new ClientTcpDispatcher();

            var dummyConnection = new DummyTcpConnection();

            _connection = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(), new InternalAuthenticationProvider(
                    new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { }, Opts.ConnectionPendingSendBytesThresholdDefault);
        }
        public void when_handling_trusted_write_on_external_service()
        {
            var package = new TcpPackage(TcpCommand.WriteEvents, TcpFlags.TrustedWrite, Guid.NewGuid(), null, null, new byte[] { });

            var dummyConnection = new DummyTcpConnection();

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(), new InternalAuthenticationProvider(new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { });

            tcpConnectionManager.ProcessPackage(package);

            var data = dummyConnection.ReceivedData.Last();
            var receivedPackage = TcpPackage.FromArraySegment(data);
           
            Assert.AreEqual(receivedPackage.Command, TcpCommand.BadRequest, "Expected Bad Request but got {0}", receivedPackage.Command);
        }