public async Task ShouldReturnConnectionAccordingToLoadBalancingStrategy(AccessMode mode) { var routingTable = NewRoutingTable( new List <Uri> { new Uri("router:1"), new Uri("router:2") }, new List <Uri> { new Uri("reader:1"), new Uri("reader:2"), new Uri("reader:3") }, new List <Uri> { new Uri("writer:1"), new Uri("writer:2") }); var routingTableManager = new Mock <IRoutingTableManager>(); routingTableManager.Setup(x => x.EnsureRoutingTableForModeAsync(mode, null, Bookmark.Empty)) .ReturnsAsync(routingTable); var clusterPoolMock = new Mock <IClusterConnectionPool>(); clusterPoolMock.Setup(x => x.AcquireAsync(It.IsAny <Uri>(), mode, null, Bookmark.Empty)) .ReturnsAsync((Uri uri, AccessMode m, string d, Bookmark b) => NewConnectionMock(uri, m)); var balancer = new LoadBalancer(clusterPoolMock.Object, routingTableManager.Object); if (mode == AccessMode.Read) { (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("reader:1"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("reader:2"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("reader:3"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("reader:1"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("reader:2"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("reader:3"); } else if (mode == AccessMode.Write) { (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("writer:1"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("writer:2"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("writer:1"); (await balancer.AcquireAsync(mode, null, Bookmark.Empty)).Server.Address.Should().Be("writer:2"); } else { throw new ArgumentException(); } }
public async Task ShouldThrowErrorDirectlyIfSecurityError(AccessMode mode) { // Given var uri = new Uri("neo4j://123:456"); var routingTableMock = NewMockedRoutingTable(mode, uri); var mock = new Mock <IRoutingTableManager>(); mock.Setup(x => x.EnsureRoutingTableForModeAsync(mode, null, Bookmark.Empty)) .ReturnsAsync(routingTableMock.Object); var clusterConnPoolMock = new Mock <IClusterConnectionPool>(); clusterConnPoolMock.Setup(x => x.AcquireAsync(uri, mode, null, Bookmark.Empty)) .Returns(Task.FromException <IConnection>( new SecurityException("Failed to establish ssl connection with the server"))); var balancer = new LoadBalancer(clusterConnPoolMock.Object, mock.Object); // When var error = await Record.ExceptionAsync(() => balancer.AcquireAsync(mode, null, Bookmark.Empty)); // Then error.Should().BeOfType <SecurityException>(); error.Message.Should().Contain("ssl connection with the server"); // while the server is not removed routingTableMock.Verify(m => m.Remove(uri), Times.Never); clusterConnPoolMock.Verify(m => m.DeactivateAsync(uri), Times.Never); }
public async Task ShouldReturnConnectionWithCorrectMode(AccessMode mode) { // Given var uri = new Uri("neo4j://123:456"); var mock = new Mock <IRoutingTableManager>(); var routingTableMock = NewMockedRoutingTable(mode, uri); mock.Setup(x => x.EnsureRoutingTableForModeAsync(mode, null, Bookmark.Empty)) .ReturnsAsync(routingTableMock.Object); var clusterPoolMock = new Mock <IClusterConnectionPool>(); var mockedConn = new Mock <IConnection>(); mockedConn.Setup(x => x.Server.Address).Returns(uri.ToString); mockedConn.Setup(x => x.Mode).Returns(mode); var conn = mockedConn.Object; clusterPoolMock.Setup(x => x.AcquireAsync(uri, mode, null, Bookmark.Empty)).ReturnsAsync(conn); var balancer = new LoadBalancer(clusterPoolMock.Object, mock.Object); // When var acquiredConn = await balancer.AcquireAsync(mode, null, Bookmark.Empty); // Then acquiredConn.Server.Address.Should().Be(uri.ToString()); }
public async Task ShouldThrowErrorDirectlyIfProtocolError(AccessMode mode) { // Given var uri = new Uri("bolt+routing://123:456"); var routingTableMock = NewMockedRoutingTable(mode, uri); var mock = new Mock <IRoutingTableManager>(); mock.Setup(x => x.RoutingTable).Returns(routingTableMock.Object); var clusterConnPoolMock = new Mock <IClusterConnectionPool>(); clusterConnPoolMock.Setup(x => x.AcquireAsync(uri, mode)).Returns( TaskHelper.GetFailedTask <IConnection>(new ProtocolException("do not understand struct 0x01"))); var balancer = new LoadBalancer(clusterConnPoolMock.Object, mock.Object); // When var error = await Record.ExceptionAsync(() => balancer.AcquireAsync(mode)); // Then error.Should().BeOfType <ProtocolException>(); error.Message.Should().Contain("do not understand struct 0x01"); // while the server is not removed routingTableMock.Verify(m => m.Remove(uri), Times.Never); clusterConnPoolMock.Verify(m => m.DeactivateAsync(uri), Times.Never); }
public async Task ShouldForgetServerWhenFailedToEstablishConn(AccessMode mode) { // Given var uri = new Uri("bolt+routing://123:456"); var routingTableMock = NewMockedRoutingTable(mode, uri); var mock = new Mock <IRoutingTableManager>(); mock.Setup(x => x.RoutingTable).Returns(routingTableMock.Object); var clusterConnPoolMock = new Mock <IClusterConnectionPool>(); clusterConnPoolMock.Setup(x => x.AcquireAsync(uri, mode)) .Returns(TaskHelper.GetFailedTask <IConnection>(new ServiceUnavailableException("failed init"))); var balancer = new LoadBalancer(clusterConnPoolMock.Object, mock.Object); // When var error = await Record.ExceptionAsync(() => balancer.AcquireAsync(mode)); // Then error.Should().BeOfType <SessionExpiredException>(); error.Message.Should().Contain("Failed to connect to any"); // should be removed routingTableMock.Verify(m => m.Remove(uri), Times.Once); clusterConnPoolMock.Verify(m => m.DeactivateAsync(uri), Times.Once); }
public async Task ShouldReturnConnectionWithDBFromRoutingTable(string dbName, string aliasDbName, string desiredResult) { AccessMode mode = AccessMode.Read; // Given var uri = new Uri("neo4j://123:456"); var mockManager = new Mock <IRoutingTableManager>(); var routingTableMock = NewMockedRoutingTable(mode, uri, aliasDbName); mockManager.Setup(x => x.EnsureRoutingTableForModeAsync(mode, dbName, null, Bookmark.Empty)) .ReturnsAsync(routingTableMock.Object); var clusterPoolMock = new Mock <IClusterConnectionPool>(); var mockedConn = new Mock <IConnection>(); mockedConn.Setup(x => x.Server.Address).Returns(uri.ToString); mockedConn.Setup(x => x.Mode).Returns(mode); mockedConn.Setup(x => x.Database).Returns(aliasDbName); clusterPoolMock.Setup(x => x.AcquireAsync(uri, mode, aliasDbName, null, Bookmark.Empty)).ReturnsAsync(mockedConn.Object); var balancer = new LoadBalancer(clusterPoolMock.Object, mockManager.Object); // When var acquiredConn = await balancer.AcquireAsync(mode, dbName, null, Bookmark.Empty); // Then acquiredConn.Database.Should().Be(desiredResult); }
public async Task ShouldThrowSessionExpiredExceptionIfNoServerAvailable(AccessMode mode) { // Given var mock = new Mock <IRoutingTableManager>(); mock.Setup(x => x.RoutingTable).Returns(NewMockedRoutingTable(mode, null).Object); var balancer = new LoadBalancer(null, mock.Object); // When var error = await Record.ExceptionAsync(() => balancer.AcquireAsync(mode)); // Then error.Should().BeOfType <SessionExpiredException>(); error.Message.Should().Contain("Failed to connect to any"); }