Пример #1
0
        public async Task ReconfigureNull_Should_WaitForBusyRouters()
        {
            var stage1Event = new ManualResetEventSlim();
            var stage2Event = new ManualResetEventSlim();
            var router1     = new TestRouter <TestRoutable>
            {
                ForwardCallback = (_routables) =>
                {
                    stage1Event.Set();
                    Thread.Sleep(TimeSpan.FromMilliseconds(1000));
                    stage2Event.Set();
                }
            };
            var hubConfiguration = new HubConfiguration <TestRoutable>(
                new IRouter <TestRoutable>[] { router1 },
                new IRoutablePreprocessor <TestRoutable> [0],
                MaximumRoutablesQueueLengthDefault,
                MaximumRoutablesForwardingCountDefault,
                WaitForMoreRoutablesForwardingDelayDefault);

            var hub = await this.runHub(hubConfiguration);

            var routable1 = new TestRoutable();

            hub.Forward(routable1.AsEnumerable());

            stage1Event.Wait();
            Assert.Throws <OperationCanceledException>(() =>
            {
                hub.ReconfigureAsync(null, new CancellationTokenSource(TimeSpan.FromMilliseconds(500)).Token).GetAwaiter().GetResult();
            });
            stage2Event.Wait();
            await hub.ReconfigureAsync(null, new CancellationTokenSource(TimeSpan.FromMilliseconds(1000)).Token);
        }
Пример #2
0
        public async Task ForwardToMultipleRouters_Should_IgnoreExceptionInRouters()
        {
            var router1 = new TestRouter <TestRoutable>
            {
                ForwardCallback = (_routables) => { throw new InvalidOperationException(); }
            };
            var routed2 = new ManualResetEventSlim(false);
            var router2 = new TestRouter <TestRoutable>
            {
                ForwardCallback = (_routables) => { routed2.Set(); }
            };
            var hubConfiguration = new HubConfiguration <TestRoutable>(
                new IRouter <TestRoutable>[] { router1, router2 },
                new IRoutablePreprocessor <TestRoutable> [0],
                MaximumRoutablesQueueLengthDefault,
                MaximumRoutablesForwardingCountDefault,
                WaitForMoreRoutablesForwardingDelayDefault);

            var hub = await this.runHub(hubConfiguration);

            var routable1 = new TestRoutable();

            hub.Forward(routable1.AsEnumerable());
            routed2.Wait(TimeSpan.FromMilliseconds(500)).Should().BeTrue();
        }
        public void AddRouter_Should_Succeed()
        {
            var router1 = new TestRouter <TestRoutable>();
            var builder = new HubConfigurationBuilder <TestRoutable>();

            HubConfigurationBuilderExtensions.AddRouter(builder, router1);
            builder.Routers.Should().Contain(router1);
        }
Пример #4
0
        public void Constructor_Should_SetProperties()
        {
            var router1       = new TestRouter <TestRoutable>();
            var routers       = new IRouter <TestRoutable>[] { router1 };
            var preprocessor1 = new TestReturnNullPreprocessor();
            var preprocessors = new IRoutablePreprocessor <TestRoutable>[] { preprocessor1 };

            this.prepareConfiguration(routers, preprocessors, out var configuration);
            configuration.Routers.Should().Contain(routers);
            configuration.Preprocessors.Should().Contain(preprocessor1);
            configuration.MaximumRoutablesQueueLength.Should().Be(MaximumRoutablesQueueLengthDefault);
        }
Пример #5
0
        void prepare(out TestRouter <TestRoutable> router, out HubConfiguration <TestRoutable> hubConfiguration)
        {
            router = new TestRouter <TestRoutable>();
            var routers = new IRouter <TestRoutable>[] { router };

            var preprocessors = new IRoutablePreprocessor <TestRoutable> [0];

            hubConfiguration = new HubConfiguration <TestRoutable>(
                routers,
                preprocessors,
                MaximumRoutablesQueueLengthDefault,
                MaximumRoutablesForwardingCountDefault,
                WaitForMoreRoutablesForwardingDelayDefault);
        }
Пример #6
0
        public async Task ForwardWhileNotConfigured_Should_DelayEvents()
        {
            var forwarded1Event = new ManualResetEventSlim();
            var forwarded2Event = new ManualResetEventSlim();
            var router1         = new TestRouter <TestRoutable>
            {
                ForwardCallback = (_routables) =>
                {
                    if (forwarded1Event.IsSet)
                    {
                        forwarded2Event.Set();
                    }
                    else
                    {
                        forwarded1Event.Set();
                    }
                }
            };
            var hubConfiguration = new HubConfiguration <TestRoutable>(
                new IRouter <TestRoutable>[] { router1 },
                new IRoutablePreprocessor <TestRoutable> [0],
                MaximumRoutablesQueueLengthDefault,
                MaximumRoutablesForwardingCountDefault,
                WaitForMoreRoutablesForwardingDelayDefault);

            var hub = new Hub <TestRoutable>();

            var routable1 = new TestRoutable();

            hub.Forward(routable1.AsEnumerable());
            forwarded1Event.Wait(TimeSpan.FromMilliseconds(500)).Should().BeFalse();

            await hub.ReconfigureAsync(hubConfiguration, default);

            forwarded1Event.Wait(TimeSpan.FromMilliseconds(500)).Should().BeTrue();

            await hub.ReconfigureAsync(null, default);

            var routable2 = new TestRoutable();

            hub.Forward(routable2.AsEnumerable());
            forwarded2Event.Wait(TimeSpan.FromMilliseconds(500)).Should().BeFalse();

            await hub.ReconfigureAsync(hubConfiguration, default);

            forwarded2Event.Wait(TimeSpan.FromMilliseconds(500)).Should().BeTrue();
        }
        public void Build_Should_CreateHubConfiguration()
        {
            var router1       = new TestRouter <TestRoutable>();
            var preprocessor1 = new TestReturnNullPreprocessor();
            var builder       = new HubConfigurationBuilder <TestRoutable>();

            builder.Routers.Add(router1);
            builder.Preprocessors.Add(preprocessor1);
            builder.MaximumRoutableQueueLength          = 1;
            builder.MaximumRoutablesForwardingCount     = 2;
            builder.WaitForMoreRoutablesForwardingDelay = TimeSpan.FromMilliseconds(3);

            var hubConfiguration = builder.Build();

            hubConfiguration.Routers.Should().Contain(router1);
            hubConfiguration.Preprocessors.Should().Contain(preprocessor1);
            hubConfiguration.MaximumRoutablesQueueLength.Should().Be(1);
            hubConfiguration.MaximumRoutablesForwardingCount.Should().Be(2);
            hubConfiguration.WaitForMoreRoutablesForwardingDelay.Should().Be(TimeSpan.FromMilliseconds(3));
        }
Пример #8
0
        public async Task ForwardWithPreprocessorReturnEmptyListOnRouting_Should_BlockAllEvents()
        {
            var routed1 = new ManualResetEventSlim(false);
            IEnumerable <TestRoutable> routables1 = null;
            var router1 = new TestRouter <TestRoutable>()
            {
                ForwardCallback = (_routables) => { routables1 = _routables; routed1.Set(); }
            };
            var routers          = new IRouter <TestRoutable>[] { router1 };
            var preprocessors    = new IRoutablePreprocessor <TestRoutable>[] { new TestRemoveRoutablePreprocessor(onEnqueueing: false) };
            var hubConfiguration = new HubConfiguration <TestRoutable>(
                routers,
                preprocessors,
                MaximumRoutablesQueueLengthDefault,
                MaximumRoutablesForwardingCountDefault,
                WaitForMoreRoutablesForwardingDelayDefault);
            var hub = new Hub <TestRoutable>();
            await hub.ReconfigureAsync(hubConfiguration, default);

            hub.Forward(new[] { new TestRoutable() });
            routed1.Wait(TimeSpan.FromMilliseconds(500)).Should().BeFalse();
            routables1.Should().BeNull();
        }