mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 11:50:49 +08:00 
			
		
		
		
	Merge branch 'feature/fix-unstable-raft-tests' into develop
This commit is contained in:
		@@ -1,158 +1,174 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Repository;
 | 
			
		||||
using Ocelot.Configuration.Setter;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Ocelot.UnitTests.Responder;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using static Ocelot.Infrastructure.Wait;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ConsulFileConfigurationPollerTests : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        private ConsulFileConfigurationPoller _poller;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _factory;
 | 
			
		||||
        private Mock<IFileConfigurationRepository> _repo;
 | 
			
		||||
        private Mock<IFileConfigurationSetter> _setter;
 | 
			
		||||
        private FileConfiguration _fileConfig;
 | 
			
		||||
        private Mock<IConsulPollerConfiguration> _config;
 | 
			
		||||
 | 
			
		||||
        public ConsulFileConfigurationPollerTests()
 | 
			
		||||
        {
 | 
			
		||||
            var logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _factory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _factory.Setup(x => x.CreateLogger<ConsulFileConfigurationPoller>()).Returns(logger.Object);
 | 
			
		||||
            _repo = new Mock<IFileConfigurationRepository>();
 | 
			
		||||
            _setter = new Mock<IFileConfigurationSetter>();
 | 
			
		||||
            _fileConfig = new FileConfiguration();
 | 
			
		||||
            _config = new Mock<IConsulPollerConfiguration>();
 | 
			
		||||
            _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(_fileConfig));
 | 
			
		||||
            _config.Setup(x => x.Delay).Returns(100);
 | 
			
		||||
            _poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object, _config.Object);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _poller.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_start()
 | 
			
		||||
        {
 | 
			
		||||
           this.Given(x => ThenTheSetterIsCalled(_fileConfig, 1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_setter_when_gets_new_config()
 | 
			
		||||
        {
 | 
			
		||||
            var newConfig = new FileConfiguration {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {   
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "test"
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 0))
 | 
			
		||||
                .Then(x => ThenTheSetterIsCalled(newConfig, 1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_poll_if_already_polling()
 | 
			
		||||
        {
 | 
			
		||||
            var newConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "test"
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 10))
 | 
			
		||||
                .Then(x => ThenTheSetterIsCalled(newConfig, 1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_do_nothing_if_call_to_consul_fails()
 | 
			
		||||
        {
 | 
			
		||||
            var newConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "test"
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => WhenConsulErrors())
 | 
			
		||||
                .Then(x => ThenTheSetterIsCalled(newConfig, 0))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenConsulErrors()
 | 
			
		||||
        {
 | 
			
		||||
            _repo
 | 
			
		||||
                .Setup(x => x.Get())
 | 
			
		||||
                .ReturnsAsync(new ErrorResponse<FileConfiguration>(new AnyError()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig, int delay)
 | 
			
		||||
        {
 | 
			
		||||
            _repo
 | 
			
		||||
                .Setup(x => x.Get())
 | 
			
		||||
                .Callback(() => Thread.Sleep(delay))
 | 
			
		||||
                .ReturnsAsync(new OkResponse<FileConfiguration>(newConfig));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheSetterIsCalled(FileConfiguration fileConfig, int times)
 | 
			
		||||
        {
 | 
			
		||||
            var result = WaitFor(2000).Until(() => {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    _setter.Verify(x => x.Set(fileConfig), Times.Exactly(times));
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
                catch(Exception)
 | 
			
		||||
                {
 | 
			
		||||
                    return false;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            result.ShouldBeTrue();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Repository;
 | 
			
		||||
using Ocelot.Configuration.Setter;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Ocelot.UnitTests.Responder;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using static Ocelot.Infrastructure.Wait;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ConsulFileConfigurationPollerTests : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        private readonly ConsulFileConfigurationPoller _poller;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _factory;
 | 
			
		||||
        private readonly Mock<IFileConfigurationRepository> _repo;
 | 
			
		||||
        private readonly Mock<IFileConfigurationSetter> _setter;
 | 
			
		||||
        private readonly FileConfiguration _fileConfig;
 | 
			
		||||
        private Mock<IConsulPollerConfiguration> _config;
 | 
			
		||||
 | 
			
		||||
        public ConsulFileConfigurationPollerTests()
 | 
			
		||||
        {
 | 
			
		||||
            var logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _factory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _factory.Setup(x => x.CreateLogger<ConsulFileConfigurationPoller>()).Returns(logger.Object);
 | 
			
		||||
            _repo = new Mock<IFileConfigurationRepository>();
 | 
			
		||||
            _setter = new Mock<IFileConfigurationSetter>();
 | 
			
		||||
            _fileConfig = new FileConfiguration();
 | 
			
		||||
            _config = new Mock<IConsulPollerConfiguration>();
 | 
			
		||||
            _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(_fileConfig));
 | 
			
		||||
            _config.Setup(x => x.Delay).Returns(100);
 | 
			
		||||
            _poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object, _config.Object);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _poller.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_start()
 | 
			
		||||
        {
 | 
			
		||||
           this.Given(x => ThenTheSetterIsCalled(_fileConfig, 1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_setter_when_gets_new_config()
 | 
			
		||||
        {
 | 
			
		||||
            var newConfig = new FileConfiguration {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {   
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "test"
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 0))
 | 
			
		||||
                .Then(x => ThenTheSetterIsCalledAtLeast(newConfig, 1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_poll_if_already_polling()
 | 
			
		||||
        {
 | 
			
		||||
            var newConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "test"
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 10))
 | 
			
		||||
                .Then(x => ThenTheSetterIsCalled(newConfig, 1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_do_nothing_if_call_to_consul_fails()
 | 
			
		||||
        {
 | 
			
		||||
            var newConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "test"
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => WhenConsulErrors())
 | 
			
		||||
                .Then(x => ThenTheSetterIsCalled(newConfig, 0))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenConsulErrors()
 | 
			
		||||
        {
 | 
			
		||||
            _repo
 | 
			
		||||
                .Setup(x => x.Get())
 | 
			
		||||
                .ReturnsAsync(new ErrorResponse<FileConfiguration>(new AnyError()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig, int delay)
 | 
			
		||||
        {
 | 
			
		||||
            _repo
 | 
			
		||||
                .Setup(x => x.Get())
 | 
			
		||||
                .Callback(() => Thread.Sleep(delay))
 | 
			
		||||
                .ReturnsAsync(new OkResponse<FileConfiguration>(newConfig));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheSetterIsCalled(FileConfiguration fileConfig, int times)
 | 
			
		||||
        {
 | 
			
		||||
            var result = WaitFor(2000).Until(() => {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    _setter.Verify(x => x.Set(fileConfig), Times.Exactly(times));
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
                catch(Exception)
 | 
			
		||||
                {
 | 
			
		||||
                    return false;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            result.ShouldBeTrue();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheSetterIsCalledAtLeast(FileConfiguration fileConfig, int times)
 | 
			
		||||
        {
 | 
			
		||||
            var result = WaitFor(2000).Until(() => {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    _setter.Verify(x => x.Set(fileConfig), Times.AtLeast(times));
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
                catch(Exception)
 | 
			
		||||
                {
 | 
			
		||||
                    return false;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            result.ShouldBeTrue();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										40
									
								
								test/Ocelot.UnitTests/Infrastructure/InMemoryBusTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								test/Ocelot.UnitTests/Infrastructure/InMemoryBusTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Ocelot.Infrastructure;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Infrastructure
 | 
			
		||||
{
 | 
			
		||||
    public class InMemoryBusTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly InMemoryBus<object> _bus;
 | 
			
		||||
 | 
			
		||||
        public InMemoryBusTests()
 | 
			
		||||
        {
 | 
			
		||||
            _bus = new InMemoryBus<object>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public async Task should_publish_with_delay()
 | 
			
		||||
        {
 | 
			
		||||
            var called = false;
 | 
			
		||||
            _bus.Subscribe(x => {
 | 
			
		||||
                called = true;
 | 
			
		||||
            });
 | 
			
		||||
            _bus.Publish(new object(), 1);
 | 
			
		||||
            await Task.Delay(10);
 | 
			
		||||
            called.ShouldBeTrue();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_be_publish_yet_as_no_delay_in_caller()
 | 
			
		||||
        {
 | 
			
		||||
            var called = false;
 | 
			
		||||
            _bus.Subscribe(x => {
 | 
			
		||||
                called = true;
 | 
			
		||||
            });
 | 
			
		||||
            _bus.Publish(new object(), 1);
 | 
			
		||||
            called.ShouldBeFalse();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Ocelot.LoadBalancer.LoadBalancers;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
@@ -10,28 +11,43 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Collections;
 | 
			
		||||
    using System.Threading;
 | 
			
		||||
    using Ocelot.Middleware;
 | 
			
		||||
    using Ocelot.UnitTests.Responder;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Ocelot.Infrastructure;
 | 
			
		||||
 | 
			
		||||
    public class CookieStickySessionsTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly CookieStickySessions _stickySessions;
 | 
			
		||||
        private readonly Mock<ILoadBalancer> _loadBalancer;
 | 
			
		||||
        private readonly int _defaultExpiryInMs;
 | 
			
		||||
        private DownstreamContext _downstreamContext;
 | 
			
		||||
        private Response<ServiceHostAndPort> _result;
 | 
			
		||||
        private Response<ServiceHostAndPort> _firstHostAndPort;
 | 
			
		||||
        private Response<ServiceHostAndPort> _secondHostAndPort;
 | 
			
		||||
        private readonly FakeBus<StickySession> _bus;
 | 
			
		||||
 | 
			
		||||
        public CookieStickySessionsTests()
 | 
			
		||||
        {
 | 
			
		||||
            _bus = new FakeBus<StickySession>();
 | 
			
		||||
            _loadBalancer = new Mock<ILoadBalancer>();
 | 
			
		||||
            const int defaultExpiryInMs = 100;
 | 
			
		||||
            _stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", defaultExpiryInMs);
 | 
			
		||||
            _defaultExpiryInMs = 0;
 | 
			
		||||
            _stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", _defaultExpiryInMs, _bus);
 | 
			
		||||
            _downstreamContext = new DownstreamContext(new DefaultHttpContext());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_expire_sticky_session()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenTheLoadBalancerReturns())
 | 
			
		||||
                .And(_ => GivenTheDownstreamRequestHasSessionId("321"))
 | 
			
		||||
                .And(_ => GivenIHackAMessageInWithAPastExpiry())
 | 
			
		||||
                .And(_ => WhenILease())
 | 
			
		||||
                .When(_ => WhenTheMessagesAreProcessed())
 | 
			
		||||
                .Then(_ => ThenTheLoadBalancerIsCalled())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_host_and_port()
 | 
			
		||||
        {
 | 
			
		||||
@@ -48,6 +64,7 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
                .And(_ => GivenTheDownstreamRequestHasSessionId("321"))
 | 
			
		||||
                .When(_ => WhenILeaseTwiceInARow())
 | 
			
		||||
                .Then(_ => ThenTheFirstAndSecondResponseAreTheSame())
 | 
			
		||||
                .And(_ => ThenTheStickySessionWillTimeout())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -69,92 +86,26 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_expire_sticky_session()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenTheLoadBalancerReturnsSequence())
 | 
			
		||||
                .When(_ => WhenTheStickySessionExpires())
 | 
			
		||||
                .Then(_ => ThenANewHostAndPortIsReturned())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_refresh_sticky_session()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenTheLoadBalancerReturnsSequence())
 | 
			
		||||
                .When(_ => WhenIMakeRequestsToKeepRefreshingTheSession())
 | 
			
		||||
                .Then(_ => ThenTheSessionIsRefreshed())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _stickySessions.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_release()
 | 
			
		||||
        {
 | 
			
		||||
            _stickySessions.Release(new ServiceHostAndPort("", 0));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async Task ThenTheSessionIsRefreshed()
 | 
			
		||||
        private void ThenTheLoadBalancerIsCalled()
 | 
			
		||||
        {
 | 
			
		||||
            var postExpireHostAndPort = await _stickySessions.Lease(_downstreamContext);
 | 
			
		||||
            postExpireHostAndPort.Data.DownstreamHost.ShouldBe("one");
 | 
			
		||||
            postExpireHostAndPort.Data.DownstreamPort.ShouldBe(80);
 | 
			
		||||
 | 
			
		||||
            _loadBalancer
 | 
			
		||||
                .Verify(x => x.Lease(It.IsAny<DownstreamContext>()), Times.Once);
 | 
			
		||||
            _loadBalancer.Verify(x => x.Release(It.IsAny<ServiceHostAndPort>()), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async Task WhenIMakeRequestsToKeepRefreshingTheSession()
 | 
			
		||||
        private void WhenTheMessagesAreProcessed()
 | 
			
		||||
        {
 | 
			
		||||
            var context = new DefaultHttpContext();
 | 
			
		||||
            var cookies = new FakeCookies();
 | 
			
		||||
            cookies.AddCookie("sessionid", "321");
 | 
			
		||||
            context.Request.Cookies = cookies;
 | 
			
		||||
            _downstreamContext = new DownstreamContext(context);
 | 
			
		||||
 | 
			
		||||
            var firstHostAndPort = await _stickySessions.Lease(_downstreamContext);
 | 
			
		||||
            firstHostAndPort.Data.DownstreamHost.ShouldBe("one");
 | 
			
		||||
            firstHostAndPort.Data.DownstreamPort.ShouldBe(80);
 | 
			
		||||
 | 
			
		||||
            Thread.Sleep(80);
 | 
			
		||||
 | 
			
		||||
            var secondHostAndPort = await _stickySessions.Lease(_downstreamContext);
 | 
			
		||||
            secondHostAndPort.Data.DownstreamHost.ShouldBe("one");
 | 
			
		||||
            secondHostAndPort.Data.DownstreamPort.ShouldBe(80);
 | 
			
		||||
 | 
			
		||||
            Thread.Sleep(80);
 | 
			
		||||
            _bus.Process();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async Task ThenANewHostAndPortIsReturned()
 | 
			
		||||
        private void GivenIHackAMessageInWithAPastExpiry()
 | 
			
		||||
        {
 | 
			
		||||
            var postExpireHostAndPort = await _stickySessions.Lease(_downstreamContext);
 | 
			
		||||
            postExpireHostAndPort.Data.DownstreamHost.ShouldBe("two");
 | 
			
		||||
            postExpireHostAndPort.Data.DownstreamPort.ShouldBe(80);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async Task WhenTheStickySessionExpires()
 | 
			
		||||
        {
 | 
			
		||||
            var context = new DefaultHttpContext();
 | 
			
		||||
            var cookies = new FakeCookies();
 | 
			
		||||
            cookies.AddCookie("sessionid", "321");
 | 
			
		||||
            context.Request.Cookies = cookies;
 | 
			
		||||
            _downstreamContext = new DownstreamContext(context);
 | 
			
		||||
 | 
			
		||||
            var firstHostAndPort = await _stickySessions.Lease(_downstreamContext);
 | 
			
		||||
            var secondHostAndPort = await _stickySessions.Lease(_downstreamContext);
 | 
			
		||||
 | 
			
		||||
            firstHostAndPort.Data.DownstreamHost.ShouldBe("one");
 | 
			
		||||
            firstHostAndPort.Data.DownstreamPort.ShouldBe(80);
 | 
			
		||||
 | 
			
		||||
            secondHostAndPort.Data.DownstreamHost.ShouldBe("one");
 | 
			
		||||
            secondHostAndPort.Data.DownstreamPort.ShouldBe(80);
 | 
			
		||||
 | 
			
		||||
            Thread.Sleep(150);
 | 
			
		||||
            var hostAndPort = new ServiceHostAndPort("999", 999);
 | 
			
		||||
            _bus.Publish(new StickySession(hostAndPort, DateTime.UtcNow.AddDays(-1), "321"), 0);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenAnErrorIsReturned()
 | 
			
		||||
@@ -236,9 +187,14 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _result.Data.ShouldNotBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheStickySessionWillTimeout()
 | 
			
		||||
        {
 | 
			
		||||
            _bus.Messages.Count.ShouldBe(2);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    class FakeCookies : IRequestCookieCollection
 | 
			
		||||
 | 
			
		||||
    internal class FakeCookies : IRequestCookieCollection
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Dictionary<string, string> _cookies = new Dictionary<string, string>();
 | 
			
		||||
 | 
			
		||||
@@ -273,4 +229,37 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
            return _cookies.GetEnumerator();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal class FakeBus<T> : IBus<T>
 | 
			
		||||
    {
 | 
			
		||||
        public FakeBus()
 | 
			
		||||
        {
 | 
			
		||||
            Messages = new List<T>();
 | 
			
		||||
            Subscriptions = new List<Action<T>>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<T> Messages { get; }
 | 
			
		||||
        public List<Action<T>> Subscriptions { get; }
 | 
			
		||||
 | 
			
		||||
        public void Subscribe(Action<T> action)
 | 
			
		||||
        {
 | 
			
		||||
            Subscriptions.Add(action);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Publish(T message, int delay)
 | 
			
		||||
        {
 | 
			
		||||
            Messages.Add(message);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Process()
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var message in Messages)
 | 
			
		||||
            {
 | 
			
		||||
                foreach (var subscription in Subscriptions)
 | 
			
		||||
                {
 | 
			
		||||
                    subscription(message);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user