コード例 #1
0
        private Guid AddSubscription(Guid replicaId, bool isPromotable, out TcpConnectionManager manager)
        {
            var tcpConn = new DummyTcpConnection()
            {
                ConnectionId = replicaId
            };

            manager = new TcpConnectionManager(
                "Test Subscription Connection manager", TcpServiceType.External, new ClientTcpDispatcher(),
                InMemoryBus.CreateTest(), tcpConn, InMemoryBus.CreateTest(),
                new InternalAuthenticationProvider(
                    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);
            var subRequest = new ReplicationMessage.ReplicaSubscriptionRequest(
                Guid.NewGuid(),
                new NoopEnvelope(),
                manager,
                0,
                Guid.NewGuid(),
                new Epoch[0],
                PortsHelper.GetLoopback(),
                LeaderId,
                replicaId,
                isPromotable);

            Service.Handle(subRequest);
            return(tcpConn.ConnectionId);
        }
コード例 #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);
        }
コード例 #3
0
        public void SetUp(Action <IHttpService> bootstrap = null)
        {
            _bus = new InMemoryBus($"bus_{_serverEndPoint.Port}");
            var pipelineBus = InMemoryBus.CreateTest();
            var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", new QueueStatsManager(), true, TimeSpan.FromMilliseconds(50));

            _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            _multiQueuedHandler.Start();

            _service = new KestrelHttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                              _multiQueuedHandler, false, null, 0, false, _serverEndPoint);
            _internalDispatcher = new InternalDispatcherEndpoint(queue, _multiQueuedHandler);
            _bus.Subscribe(_internalDispatcher);
            KestrelHttpService.CreateAndSubscribePipeline(pipelineBus);
            bootstrap?.Invoke(_service);
            _server = new TestServer(
                new WebHostBuilder()
                .UseStartup(new ClusterVNodeStartup(Array.Empty <ISubsystem>(), queue, _bus, _multiQueuedHandler,
                                                    new TestAuthenticationProvider(),
                                                    new IHttpAuthenticationProvider[] {
                new BasicHttpAuthenticationProvider(new TestAuthenticationProvider()),
                new AnonymousHttpAuthenticationProvider(),
            }, new TestAuthorizationProvider(), new FakeReadIndex(_ => false), 1024 * 1024, _service)));
            _httpMessageHandler = _server.CreateHandler();
            _client             = new HttpAsyncClient(_timeout, _httpMessageHandler);

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: speed_test.cs プロジェクト: vebin/EventStore
        public void of_http_requests_routing()
        {
            const int iterations = 100000;

            IPublisher inputBus           = new NoopPublisher();
            var        bus                = InMemoryBus.CreateTest();
            var        queue              = new QueuedHandlerThreadPool(bus, "Test", true, TimeSpan.FromMilliseconds(50));
            var        multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            var        providers          = new HttpAuthenticationProvider[] { new AnonymousHttpAuthenticationProvider() };
            var        httpService        = new HttpService(ServiceAccessibility.Public, inputBus,
                                                            new TrieUriRouter(), multiQueuedHandler, false, "http://localhost:12345/");

            HttpService.CreateAndSubscribePipeline(bus, providers);

            var fakeController = new FakeController(iterations, null);

            httpService.SetupController(fakeController);

            httpService.Handle(new SystemMessage.SystemInit());

            var rnd = new Random();
            var sw  = Stopwatch.StartNew();

            var httpClient = new HttpAsyncClient();

            for (int i = 0; i < iterations; ++i)
            {
                var route = fakeController.BoundRoutes[rnd.Next(0, fakeController.BoundRoutes.Count)];

                switch (route.Item2)
                {
                case HttpMethod.Get:
                    httpClient.Get(route.Item1, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                case HttpMethod.Post:
                    httpClient.Post(route.Item1, "abracadabra", ContentType.Json, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                case HttpMethod.Delete:
                    httpClient.Delete(route.Item1, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                case HttpMethod.Put:
                    httpClient.Put(route.Item1, "abracadabra", ContentType.Json, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                default:
                    throw new Exception();
                }
            }

            fakeController.CountdownEvent.Wait();
            sw.Stop();

            Console.WriteLine("{0} request done in {1} ({2:0.00} per sec)", iterations, sw.Elapsed, 1000.0 * iterations / sw.ElapsedMilliseconds);

            httpService.Shutdown();
            multiQueuedHandler.Stop();
        }
コード例 #6
0
        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");
            }
        }
コード例 #7
0
        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);
        }
コード例 #8
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);
        }
コード例 #9
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);
        }
コード例 #10
0
        protected virtual void TestFixtureSetUp()
        {
            _streamId = "$projection-$" + _masterProjectionId.ToString("N");

            _bus = InMemoryBus.CreateTest();
            _bus.Subscribe <ClientMessage.WriteEvents>(this);
            _bus.Subscribe <ClientMessage.ReadStreamEventsForward>(this);

            _ioDispatcher = new IODispatcher(_bus, new PublishEnvelope(_bus));
            IODispatcherTestHelpers.SubscribeIODispatcher(_ioDispatcher, _bus);

            _reader = new MasterCoreProjectionResponseReader(_bus, _ioDispatcher, _workerId, _masterProjectionId);
        }
コード例 #11
0
        public void SetUp()
        {
            _bus = new InMemoryBus(string.Format("bus_{0}", _serverEndPoint.Port));

            {
                var pipelineBus = InMemoryBus.CreateTest();
                var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", true, TimeSpan.FromMilliseconds(50));
                _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
                var httpAuthenticationProviders = new HttpAuthenticationProvider[] { new AnonymousHttpAuthenticationProvider() };

                _service = new HttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                           _multiQueuedHandler, false, _serverEndPoint.ToHttpUrl());
                HttpService.CreateAndSubscribePipeline(pipelineBus, httpAuthenticationProviders);
                _client = new HttpAsyncClient();
            }

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #12
0
        public void can_dynamicly_add_types_without_clearing_handlers()
        {
            var bus = InMemoryBus.CreateTest();

            var fired = false;

            bus.Subscribe(new AdHocHandler <TestMsg>(m => fired = true));

            bus.Publish(new TestMsg());
            Assert.True(fired);

            AppDomain.CurrentDomain.Load(GetDynamicAssembly().GetName());
            var messageType = MessageHierarchy.GetTypeByName("DynamicMessage");

            Assert.Equal("DynamicMessage", messageType[0].Name);
            Assert.True(messageType.Count == 1);

            fired = false;
            bus.Publish(new TestMsg());
            AssertEx.IsOrBecomesTrue(() => fired);
        }
コード例 #13
0
        public void SetUp(Action <IHttpService> bootstrap = null)
        {
            _bus = new InMemoryBus($"bus_{_serverEndPoint.Port}");
            var pipelineBus = InMemoryBus.CreateTest();
            var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", new QueueStatsManager(), true, TimeSpan.FromMilliseconds(50));

            _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            _multiQueuedHandler.Start();

            _service = new KestrelHttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                              _multiQueuedHandler, false, null, 0, false, _serverEndPoint);
            KestrelHttpService.CreateAndSubscribePipeline(pipelineBus);
            bootstrap?.Invoke(_service);
            _server = new TestServer(
                new WebHostBuilder()
                .UseStartup(new HttpServiceStartup(_service)));
            _httpMessageHandler = _server.CreateHandler();
            _client             = new HttpAsyncClient(_timeout, _httpMessageHandler);

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #14
0
        public void in_memory_bus_remembers_handlers()
        {
            var method = typeof(MessageHierarchy).GetMethod("RebuildMessageTree", BindingFlags.Static | BindingFlags.NonPublic);

            Assert.NotNull(method);

            var bus = InMemoryBus.CreateTest();

            var fired = false;

            bus.Subscribe(new AdHocHandler <TestMsg>(m => fired = true));

            bus.Publish(new TestMsg());
            Assert.True(fired);

            // ReSharper disable once PossibleNullReferenceException
            method.Invoke(null, new object[0]);

            fired = false;
            bus.Publish(new TestMsg());
            Assert.True(fired);
        }