public void ShouldRemoveWriterFromCorrectRoutingTable() { var defaultRoutingTable = new RoutingTable(null, new[] { server01 }, new[] { server02, server05 }, new[] { server03 }, 60); var fooRoutingTable = new RoutingTable("foo", new[] { server04, server06 }, new[] { server05 }, new[] { server06, server04 }, 80); var barRoutingTable = new RoutingTable("bar", new[] { server07 }, new[] { server08 }, new[] { server09, server05 }, 100); var manager = new RoutingTableManager(Mock.Of <IInitialServerAddressProvider>(), Mock.Of <IDiscovery>(), Mock.Of <IClusterConnectionPoolManager>(), Mock.Of <ILogger>(), TimeSpan.MaxValue, defaultRoutingTable, fooRoutingTable, barRoutingTable); manager.ForgetWriter(server06, "foo"); manager.RoutingTableFor("foo").Routers.Should().BeEquivalentTo(server04, server06); manager.RoutingTableFor("foo").Readers.Should().BeEquivalentTo(server05); manager.RoutingTableFor("foo").Writers.Should().BeEquivalentTo(server04); manager.RoutingTableFor("").Routers.Should().BeEquivalentTo(server01); manager.RoutingTableFor("").Readers.Should().BeEquivalentTo(server02, server05); manager.RoutingTableFor("").Writers.Should().BeEquivalentTo(server03); manager.RoutingTableFor("bar").Routers.Should().BeEquivalentTo(server07); manager.RoutingTableFor("bar").Readers.Should().BeEquivalentTo(server08); manager.RoutingTableFor("bar").Writers.Should().BeEquivalentTo(server09, server05); }
public async Task ShouldAllowMultipleRoutingTables(AccessMode mode) { var defaultRoutingTable = new RoutingTable(null, new[] { server01 }, new[] { server02 }, new[] { server03 }, 60); var fooRoutingTable = new RoutingTable("foo", new[] { server04 }, new[] { server05 }, new[] { server06 }, 80); var barRoutingTable = new RoutingTable("bar", new[] { server07 }, new[] { server08 }, new[] { server09 }, 100); var discovery = new Mock <IDiscovery>(); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "", null, Bookmark.Empty)) .ReturnsAsync(defaultRoutingTable); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "foo", null, Bookmark.Empty)) .ReturnsAsync(fooRoutingTable); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "bar", null, Bookmark.Empty)) .ReturnsAsync(barRoutingTable); var poolManager = new Mock <IClusterConnectionPoolManager>(); poolManager.Setup(x => x.CreateClusterConnectionAsync(It.IsAny <Uri>())) .ReturnsAsync(Mock.Of <IConnection>); var initialAddressProvider = new Mock <IInitialServerAddressProvider>(); initialAddressProvider.Setup(x => x.Get()).Returns(new HashSet <Uri> { server01, server04, server07 }); var manager = new RoutingTableManager(initialAddressProvider.Object, discovery.Object, poolManager.Object, Mock.Of <ILogger>(), TimeSpan.MaxValue); // When var routingTable1 = await manager.EnsureRoutingTableForModeAsync(mode, null, null, Bookmark.Empty); var routingTable2 = await manager.EnsureRoutingTableForModeAsync(mode, "foo", null, Bookmark.Empty); var routingTable3 = await manager.EnsureRoutingTableForModeAsync(mode, "bar", null, Bookmark.Empty); routingTable1.Should().Be(defaultRoutingTable); routingTable2.Should().Be(fooRoutingTable); routingTable3.Should().Be(barRoutingTable); manager.RoutingTableFor(null).Should().Be(defaultRoutingTable); manager.RoutingTableFor("").Should().Be(defaultRoutingTable); manager.RoutingTableFor("foo").Should().Be(fooRoutingTable); manager.RoutingTableFor("bar").Should().Be(barRoutingTable); }
public async Task ShouldRemoveStaleEntriesOnUpdate() { var timer = new Mock <ITimer>(); var fooRoutingTable = new RoutingTable("foo", new[] { server04 }, new[] { server05 }, new[] { server06 }, 1, timer.Object); var barRoutingTable = new RoutingTable("bar", new[] { server07 }, new[] { server08 }, new[] { server09 }, 4, timer.Object); var discovery = new Mock <IDiscovery>(); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "foo", Bookmark.Empty)) .ReturnsAsync(fooRoutingTable); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "bar", Bookmark.Empty)) .ReturnsAsync(barRoutingTable); var poolManager = new Mock <IClusterConnectionPoolManager>(); poolManager.Setup(x => x.CreateClusterConnectionAsync(It.IsAny <Uri>())) .ReturnsAsync(Mock.Of <IConnection>); var initialAddressProvider = new Mock <IInitialServerAddressProvider>(); initialAddressProvider.Setup(x => x.Get()).Returns(new HashSet <Uri> { server01, server04, server07 }); var manager = new RoutingTableManager(initialAddressProvider.Object, discovery.Object, poolManager.Object, Mock.Of <ILogger>(), TimeSpan.FromSeconds(1)); // When var routingTable1 = await manager.EnsureRoutingTableForModeAsync(AccessMode.Read, "foo", Bookmark.Empty); routingTable1.Should().Be(fooRoutingTable); // Fake the timer to make foo routing table to be recognized as stale timer.Setup(x => x.ElapsedMilliseconds).Returns(2 * 1000 + 1); // An update should trigger an implicit clean-up of stale entries var routingTable2 = await manager.EnsureRoutingTableForModeAsync(AccessMode.Read, "bar", Bookmark.Empty); routingTable2.Should().Be(barRoutingTable); manager.RoutingTableFor("foo").Should().BeNull(); manager.RoutingTableFor("bar").Should().Be(barRoutingTable); }
public async Task ShouldIsolateEntriesFromFailures() { var defaultRoutingTable = new RoutingTable(null, new[] { server01 }, new[] { server02 }, new[] { server03 }, 60); var fooRoutingTable = new RoutingTable("foo", new[] { server04 }, new[] { server05 }, new[] { server06 }, 80); var discovery = new Mock <IDiscovery>(); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "", null, Bookmark.Empty)) .ReturnsAsync(defaultRoutingTable); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "foo", null, Bookmark.Empty)) .ReturnsAsync(fooRoutingTable); discovery.Setup(x => x.DiscoverAsync(It.IsAny <IConnection>(), "bar", null, Bookmark.Empty)) .ThrowsAsync(new FatalDiscoveryException("message")); var poolManager = new Mock <IClusterConnectionPoolManager>(); poolManager.Setup(x => x.CreateClusterConnectionAsync(It.IsAny <Uri>())) .ReturnsAsync(Mock.Of <IConnection>()); var initialAddressProvider = new Mock <IInitialServerAddressProvider>(); initialAddressProvider.Setup(x => x.Get()).Returns(new HashSet <Uri> { server01, server04, server07 }); var manager = new RoutingTableManager(initialAddressProvider.Object, discovery.Object, poolManager.Object, Mock.Of <ILogger>(), TimeSpan.MaxValue); // When var routingTable1 = await manager.EnsureRoutingTableForModeAsync(AccessMode.Write, null, null, Bookmark.Empty); var routingTable2 = await manager.EnsureRoutingTableForModeAsync(AccessMode.Write, "foo", null, Bookmark.Empty); routingTable1.Should().Be(defaultRoutingTable); routingTable2.Should().Be(fooRoutingTable); manager.Awaiting(m => m.EnsureRoutingTableForModeAsync(AccessMode.Write, "bar", null, Bookmark.Empty)) .Should().Throw <FatalDiscoveryException>(); manager.RoutingTableFor("").Should().Be(defaultRoutingTable); manager.RoutingTableFor("foo").Should().Be(fooRoutingTable); }