public async Task SelectServerAsync_should_apply_both_pre_and_post_server_selectors() { _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(ci => { var endPoint = ci.Arg <EndPoint>(); var server = Substitute.For <IClusterableServer>(); server.EndPoint.Returns(endPoint); return(server); }); var preSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017)); var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018)); var postSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019)); var settings = new ClusterSettings( preServerSelector: preSelector, postServerSelector: postSelector); var subject = new StubCluster(settings, _serverFactory, _clusterListener); subject.Initialize(); subject.SetServerDescriptions( ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020))); var selected = await subject.SelectServerAsync(middleSelector, CancellationToken.None); ((DnsEndPoint)selected.EndPoint).Port.Should().Be(27020); }
public void SelectServer_should_apply_both_pre_and_post_server_selectors( [Values(false, true)] bool async) { _mockServerFactory.Setup(f => f.CreateServer(It.IsAny <ClusterId>(), It.IsAny <IClusterClock>(), It.IsAny <EndPoint>())) .Returns((ClusterId _, IClusterClock clusterClock, EndPoint endPoint) => { var mockServer = new Mock <IClusterableServer>(); mockServer.SetupGet(s => s.EndPoint).Returns(endPoint); return(mockServer.Object); }); var preSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017)); var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018)); var postSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019)); var settings = new ClusterSettings( preServerSelector: preSelector, postServerSelector: postSelector); var subject = new StubCluster(settings, _mockServerFactory.Object, _capturedEvents); subject.Initialize(); subject.SetServerDescriptions( ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020))); _capturedEvents.Clear(); IServer result; if (async) { result = subject.SelectServerAsync(middleSelector, CancellationToken.None).GetAwaiter().GetResult(); } else { result = subject.SelectServer(middleSelector, CancellationToken.None); } ((DnsEndPoint)result.EndPoint).Port.Should().Be(27020); _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>(); _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>(); _capturedEvents.Any().Should().BeFalse(); }
public void SelectServer_should_apply_both_pre_and_post_server_selectors( [Values(false, true)] bool async) { _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(ci => { var endPoint = ci.Arg <EndPoint>(); var server = Substitute.For <IClusterableServer>(); server.EndPoint.Returns(endPoint); return(server); }); var preSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017)); var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018)); var postSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019)); var settings = new ClusterSettings( preServerSelector: preSelector, postServerSelector: postSelector); var subject = new StubCluster(settings, _serverFactory, _capturedEvents); subject.Initialize(); subject.SetServerDescriptions( ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020))); _capturedEvents.Clear(); IServer result; if (async) { result = subject.SelectServerAsync(middleSelector, CancellationToken.None).GetAwaiter().GetResult(); } else { result = subject.SelectServer(middleSelector, CancellationToken.None); } ((DnsEndPoint)result.EndPoint).Port.Should().Be(27020); _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>(); _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>(); _capturedEvents.Any().Should().BeFalse(); }
public async Task SelectServerAsync_should_apply_both_pre_and_post_server_selectors() { _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(ci => { var endPoint = ci.Arg<EndPoint>(); var server = Substitute.For<IClusterableServer>(); server.EndPoint.Returns(endPoint); return server; }); var preSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017)); var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018)); var postSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019)); var settings = new ClusterSettings( preServerSelector: preSelector, postServerSelector: postSelector); var subject = new StubCluster(settings, _serverFactory, _capturedEvents); subject.Initialize(); subject.SetServerDescriptions( ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020))); _capturedEvents.Clear(); var selected = await subject.SelectServerAsync(middleSelector, CancellationToken.None); ((DnsEndPoint)selected.EndPoint).Port.Should().Be(27020); _capturedEvents.Next().Should().BeOfType<ClusterSelectingServerEvent>(); _capturedEvents.Next().Should().BeOfType<ClusterSelectedServerEvent>(); _capturedEvents.Any().Should().BeFalse(); }
public void SelectServer_should_apply_both_pre_and_post_server_selectors( [Values(false, true)] bool async) { _mockServerFactory.Setup(f => f.CreateServer(It.IsAny<ClusterId>(), It.IsAny<EndPoint>())) .Returns((ClusterId _, EndPoint endPoint) => { var mockServer = new Mock<IClusterableServer>(); mockServer.SetupGet(s => s.EndPoint).Returns(endPoint); return mockServer.Object; }); var preSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017)); var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018)); var postSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019)); var settings = new ClusterSettings( preServerSelector: preSelector, postServerSelector: postSelector); var subject = new StubCluster(settings, _mockServerFactory.Object, _capturedEvents); subject.Initialize(); subject.SetServerDescriptions( ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)), ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020))); _capturedEvents.Clear(); IServer result; if (async) { result = subject.SelectServerAsync(middleSelector, CancellationToken.None).GetAwaiter().GetResult(); } else { result = subject.SelectServer(middleSelector, CancellationToken.None); } ((DnsEndPoint)result.EndPoint).Port.Should().Be(27020); _capturedEvents.Next().Should().BeOfType<ClusterSelectingServerEvent>(); _capturedEvents.Next().Should().BeOfType<ClusterSelectedServerEvent>(); _capturedEvents.Any().Should().BeFalse(); }