public async Task ShouldReturnExistingConnectionPoolIfUriAlreadyExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnection = new Mock <IPooledConnection>(); mockedConnection.Setup(c => c.InitAsync()) .Returns(Task.FromException(new InvalidOperationException("An exception"))); mockedConnectionPool.Setup(x => x.AcquireAsync(It.IsAny <AccessMode>(), It.IsAny <string>(), It.IsAny <Bookmark>())) .ReturnsAsync(mockedConnection.Object); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); connectionPoolDict.Count.Should().Be(1); connectionPoolDict.Keys.Single().Should().Be(ServerUri); connectionPoolDict[ServerUri].Should().Be(mockedConnectionPool.Object); // When var connection = await pool.AcquireAsync(ServerUri, AccessMode.Write, null, Bookmark.Empty); // Then connection.Should().NotBeNull(); var exception = await Record.ExceptionAsync(() => connection.InitAsync()); mockedConnection.Verify(c => c.InitAsync(), Times.Once); exception.Should().BeOfType <InvalidOperationException>(); exception.Message.Should().Be("An exception"); }
public async Task AddressMatchTest(string first, string second, bool expectedResult) { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnection = new Mock <IConnection>(); mockedConnectionPool.Setup(x => x.AcquireAsync(It.IsAny <AccessMode>(), It.IsAny <string>(), It.IsAny <Bookmark>())) .ReturnsAsync(mockedConnection.Object); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(new Uri(first), mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); var connection = await pool.AcquireAsync(new Uri(second), AccessMode.Write, null, Bookmark.Empty); if (expectedResult) { connection.Should().NotBeNull(); } else { connection.Should().BeNull(); } }
public void ShouldReturnExisitingConnectionPoolIfUriAlreadyExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnection = new Mock <IPooledConnection>(); mockedConnection.Setup(c => c.Init()).Throws(new InvalidOperationException("An exception")); mockedConnectionPool.Setup(x => x.Acquire(It.IsAny <AccessMode>())).Returns(mockedConnection.Object); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); connectionPoolDict.Count.Should().Be(1); connectionPoolDict.Keys.Single().Should().Be(ServerUri); connectionPoolDict[ServerUri].Should().Be(mockedConnectionPool.Object); // When IConnection connection = pool.Acquire(ServerUri); // Then connection.Should().NotBeNull(); var exception = Record.Exception(() => connection.Init()); mockedConnection.Verify(c => c.Init(), Times.Once); exception.Should().BeOfType <InvalidOperationException>(); exception.Message.Should().Be("An exception"); }
public void ShouldReturnExisitingConnectionPoolIfUriAlreadyExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnection = new Mock <IPooledConnection>(); mockedConnectionPool.Setup(x => x.Acquire()).Returns(mockedConnection.Object); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); connectionPoolDict.Count.Should().Be(1); connectionPoolDict.Keys.Single().Should().Be(ServerUri); connectionPoolDict[ServerUri].Should().Be(mockedConnectionPool.Object); // When IPooledConnection connection; var acquired = pool.TryAcquire(ServerUri, out connection); // Then acquired.Should().BeTrue(); connection.Should().Be(mockedConnection.Object); }
public void ShouldRemoveTheServerIfRediscoveryThrowInvalidDiscoveryException() { // Given var uri = new Uri("bolt+routing://123:456"); var uri2 = new Uri("bolt+routing://123:789"); // a cluster view which knows a read/write uri var clusterView = NewClusterView(new[] { uri, uri2 }, new Uri[0], new Uri[0]); var mockedConn = new Mock <IPooledConnection>(); var mockedConnPool = new Mock <IConnectionPool>(); mockedConnPool.Setup(x => x.Acquire()).Returns(mockedConn.Object); var dict = new ConcurrentDictionary <Uri, IConnectionPool>(); dict.TryAdd(uri, mockedConnPool.Object); dict.TryAdd(uri2, mockedConnPool.Object); // a cluster pool which knows the conns with the uri server var clusterConnPool = new ClusterConnectionPool(null, dict); var balancer = new RoundRobinLoadBalancer(clusterConnPool, clusterView); // When var error = Record.Exception(() => balancer.NewClusterView(connection => { // never successfully rediscovery throw new InvalidDiscoveryException("Invalid"); })); // Then clusterView.All().Should().BeEmpty(); error.Should().BeOfType <ServerUnavailableException>(); }
public void ShouldGetANewClusterView() { // Given var uri = new Uri("bolt+routing://123:456"); // a cluster view which knows a read/write uri var clusterView = NewClusterView(new[] { uri }, new Uri[0], new Uri[0]); var mockedConn = new Mock <IPooledConnection>(); var mockedConnPool = new Mock <IConnectionPool>(); mockedConnPool.Setup(x => x.Acquire()).Returns(mockedConn.Object); var dict = new ConcurrentDictionary <Uri, IConnectionPool>(); dict.TryAdd(uri, mockedConnPool.Object); // a cluster pool which knows the conns with the uri server var clusterConnPool = new ClusterConnectionPool(null, dict); var balancer = new RoundRobinLoadBalancer(clusterConnPool, clusterView); // When var anotherUri = new Uri("bolt+routing://123:789"); var result = balancer.NewClusterView(connection => NewClusterView(new [] { anotherUri }, new Uri[0], new Uri[0])); // Then result.All().Should().ContainInOrder(anotherUri); }
public void ShouldRemoveFromClusterViewIfFailedToEstablishConn(AccessMode mode) { // Given var uri = new Uri("bolt+routing://123:456"); // a cluster view which knows a uri var clusterView = CreateClusterView(uri, mode); var mockedConnPool = new Mock <IConnectionPool>(); var dict = new ConcurrentDictionary <Uri, IConnectionPool>(); dict.TryAdd(uri, mockedConnPool.Object); // a cluster pool which knows the conns with the read uri server var clusterConnPool = new ClusterConnectionPool(null, dict); var balancer = new RoundRobinLoadBalancer(clusterConnPool, clusterView); mockedConnPool.Setup(x => x.Acquire()) .Callback(() => { throw balancer.CreateClusterPooledConnectionErrorHandler(uri).OnConnectionError(new IOException("failed init")); }); // When var error = Record.Exception(() => AcquiredConn(balancer, mode)); // Then error.Should().BeOfType <SessionExpiredException>(); error.Message.Should().Contain("Failed to connect to any"); }
public void ShouldReturnConnectionWithCorrectMode(AccessMode mode) { // Given var uri = new Uri("bolt+routing://123:456"); // a cluster view which knows a read/write uri var clusterView = CreateClusterView(uri, mode); var mockedConn = new Mock <IPooledConnection>(); var mockedConnPool = new Mock <IConnectionPool>(); mockedConnPool.Setup(x => x.Acquire()).Returns(mockedConn.Object); var dict = new ConcurrentDictionary <Uri, IConnectionPool>(); dict.TryAdd(uri, mockedConnPool.Object); // a cluster pool which knows the conns with the uri server var clusterConnPool = new ClusterConnectionPool(null, dict); var balancer = new RoundRobinLoadBalancer(clusterConnPool, clusterView); // When var acquiredConn = AcquiredConn(balancer, mode); // Then acquiredConn.Should().Be(mockedConn.Object); }
public void ShouldReturnZeroForMissingAddress() { var missingAddress = new Uri("localhost:1"); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(null, connectionPoolDict); var numberOfInUseConnections = pool.NumberOfInUseConnections(missingAddress); numberOfInUseConnections.Should().Be(0); }
public void ShouldEnsureInitialRouter() { var uris = new HashSet <Uri> { new Uri("bolt://123:456") }; var connFactory = new Mock <IPooledConnectionFactory>().Object; var poolSettings = new ConnectionPoolSettings(Config.DefaultConfig); var pool = new ClusterConnectionPool(uris, connFactory, poolSettings, null); pool.ToString().Should().Be( "[{bolt://123:456/ : _idleConnections: {[]}, _inUseConnections: {[]}}]"); }
public void AddressMatchTest(string first, string second, bool expectedResult) { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(new Uri(first), mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); IPooledConnection ignored; pool.TryAcquire(new Uri(second), out ignored).Should().Be(expectedResult); }
public async Task ShouldDeactivateNothingIfNotFound() { // Given var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(null, connectionPoolDict); // When await pool.DeactivateAsync(ServerUri); // Then connectionPoolDict.Count.Should().Be(0); connectionPoolDict.ContainsKey(ServerUri).Should().BeFalse(); }
public void ShouldRemoveNothingIfNotFound() { // Given var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(null, connectionPoolDict); // When pool.Purge(ServerUri); // Then connectionPoolDict.Count.Should().Be(0); connectionPoolDict.ContainsKey(ServerUri).Should().BeFalse(); }
public async Task ShouldNotCreateNewConnectionPoolIfUriDoesNotExist() { // Given var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(new MockedPoolFactory(), connectionPoolDict); connectionPoolDict.Count.Should().Be(0); // When var connection = await pool.AcquireAsync(ServerUri, AccessMode.Write, null, Bookmark.Empty); // Then connection.Should().BeNull(); connectionPoolDict.Count.Should().Be(0); }
public void ShouldAddNewConnectionPoolIfDoesNotExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(mockedConnectionPool.Object, connectionPoolDict); // When pool.Update(new[] { ServerUri }); // Then connectionPoolDict.Count.Should().Be(1); connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue(); connectionPoolDict[ServerUri].Should().Be(mockedConnectionPool.Object); }
public void ShouldNotCreateNewConnectionPoolIfUriDoseNotExist() { // Given var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(new MockedPoolFactory(), connectionPoolDict); connectionPoolDict.Count.Should().Be(0); // When IConnection connection = pool.Acquire(ServerUri); // Then connection.Should().BeNull(); connectionPoolDict.Count.Should().Be(0); }
public void ShouldRemoveServerPoolIfNotPresentInNewServers() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(mockedConnectionPool.Object, connectionPoolDict); // When pool.Update(new Uri[0]); // Then connectionPoolDict.Count.Should().Be(0); }
public void ShouldReturnCorrectCountForPresentAddress() { var presentAddress = new Uri("localhost:1"); var mockedConnectionPool = new Mock <IConnectionPool>(); mockedConnectionPool.Setup(x => x.NumberOfInUseConnections).Returns(42); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.TryAdd(presentAddress, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); var numberOfInUseConnections = pool.NumberOfInUseConnections(presentAddress); numberOfInUseConnections.Should().Be(42); }
public void ShouldAddIfNotFound() { // Given var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var fakePoolMock = new Mock <IConnectionPool>(); var pool = new ClusterConnectionPool(new MockedPoolFactory(fakePoolMock.Object), connectionPoolDict); // When pool.Add(new[] { ServerUri }); // Then connectionPoolDict.Count.Should().Be(1); connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue(); connectionPoolDict[ServerUri].Should().Be(fakePoolMock.Object); }
public async Task ShouldAddNewConnectionPoolIfDoesNotExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(new MockedPoolFactory(mockedConnectionPool.Object), connectionPoolDict); // When await pool.UpdateAsync(new[] { ServerUri }, new Uri[0]); // Then connectionPoolDict.Count.Should().Be(1); connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue(); connectionPoolDict[ServerUri].Should().Be(mockedConnectionPool.Object); }
public void ShouldEnsureInitialRouter() { var uri = new Uri("bolt://123:456"); var uris = new HashSet <Uri> { uri }; var connFactory = new Mock <IPooledConnectionFactory>().Object; var poolSettings = new ConnectionPoolSettings(Config.Default); var routingSetting = new RoutingSettings(uri, new Dictionary <string, string>(), Config.Default); var pool = new ClusterConnectionPool(uris, connFactory, routingSetting, poolSettings, null); pool.ToString().Should().Contain( "bolt://123:456/"); pool.ToString().Should().Contain( "_idleConnections: {[]}, _inUseConnections: {[]}"); }
public void ShouldNotCreateNewConnectionPoolIfUriDoseNotExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); var pool = new ClusterConnectionPool(mockedConnectionPool.Object, connectionPoolDict); connectionPoolDict.Count.Should().Be(0); // When IPooledConnection connection; var acquired = pool.TryAcquire(ServerUri, out connection); // Then acquired.Should().BeFalse(); connectionPoolDict.Count.Should().Be(0); }
public void ShouldRemoveNewlyCreatedPoolnIfDisposeAlreadyCalled() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnectionPoolDict = new Mock <ConcurrentDictionary <Uri, IConnectionPool> >(); var pool = new ClusterConnectionPool(mockedConnectionPool.Object, mockedConnectionPoolDict.Object); // When pool.Dispose(); var exception = Record.Exception(() => pool.Update(new[] { ServerUri })); // Then mockedConnectionPool.Verify(x => x.Dispose()); exception.Should().BeOfType <InvalidOperationException>(); exception.Message.Should().Contain("Failed to create connections with server"); }
public void ShouldRemoveServerPoolIfNotPresentInNewServers() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); mockedConnectionPool.Setup(x => x.NumberOfInUseConnections).Returns(0); // no need to explicitly config this var pool = new ClusterConnectionPool(new MockedPoolFactory(mockedConnectionPool.Object), connectionPoolDict); // When pool.Update(new Uri[0], new[] { ServerUri }); // Then mockedConnectionPool.Verify(x => x.Deactivate(), Times.Once); // first deactivate then remove connectionPoolDict.Count.Should().Be(0); }
public void ShouldRemoveAllAfterDispose() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); // When pool.Dispose(); // Then mockedConnectionPool.Verify(x => x.Dispose(), Times.Once); connectionPoolDict.Count.Should().Be(0); connectionPoolDict.ContainsKey(ServerUri).Should().BeFalse(); }
public async Task ShouldActivateIfExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(new MockedPoolFactory(), connectionPoolDict); // When await pool.AddAsync(new[] { ServerUri }); // Then mockedConnectionPool.Verify(x => x.Activate(), Times.Once); connectionPoolDict.Count.Should().Be(1); connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue(); }
public void ShouldDeactivateIfExist() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); // When pool.Deactivate(ServerUri); // Then mockedConnectionPool.Verify(x => x.Deactivate(), Times.Once); connectionPoolDict.Count.Should().Be(1); connectionPoolDict.ContainsKey(ServerUri).Should().BeTrue(); }
public void ShouldDeactivateServerPoolIfNotPresentInNewServersButHasInUseConnections() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(ServerUri, mockedConnectionPool.Object); mockedConnectionPool.Setup(x => x.NumberOfInUseConnections).Returns(10); // non-zero number var pool = new ClusterConnectionPool(new MockedPoolFactory(mockedConnectionPool.Object), connectionPoolDict); // When pool.Update(new Uri[0], new[] { ServerUri }); // Then mockedConnectionPool.Verify(x => x.Deactivate(), Times.Once); connectionPoolDict.Count.Should().Be(1); }
public async Task ShouldRemoveNewlyCreatedPoolIfCloseAlreadyCalled() { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnectionPoolDict = new Mock <ConcurrentDictionary <Uri, IConnectionPool> >(); var pool = new ClusterConnectionPool(new MockedPoolFactory(mockedConnectionPool.Object), mockedConnectionPoolDict.Object); // When await pool.CloseAsync(); var exception = await Record.ExceptionAsync(() => pool.UpdateAsync(new[] { ServerUri }, new Uri[0])); // Then mockedConnectionPool.Verify(x => x.CloseAsync()); exception.Should().BeOfType <ObjectDisposedException>(); exception.Message.Should().Contain("Failed to create connections with server"); }
public void AddressMatchTest(string first, string second, bool expectedResult) { // Given var mockedConnectionPool = new Mock <IConnectionPool>(); var mockedConnection = new Mock <IConnection>(); mockedConnectionPool.Setup(x => x.Acquire(It.IsAny <AccessMode>())).Returns(mockedConnection.Object); var connectionPoolDict = new ConcurrentDictionary <Uri, IConnectionPool>(); connectionPoolDict.GetOrAdd(new Uri(first), mockedConnectionPool.Object); var pool = new ClusterConnectionPool(null, connectionPoolDict); IConnection connection = pool.Acquire(new Uri(second)); if (expectedResult) { connection.Should().NotBeNull(); } else { connection.Should().BeNull(); } }