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 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); }