public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject        = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

            var clusterId = new ClusterId();
            var endPoint  = new DnsEndPoint("localhost", 27017);
            var initialClusterDescription = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
            var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);

            _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny <WritableServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny <WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny <WritableServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetWriteChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny <WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.GetWriteChannelSourceAsync(CancellationToken.None).Wait();

            _cluster.Received().SelectServerAsync(Arg.Any <WritableServerSelector>(), CancellationToken.None);
        }
        public void GetWriteChannelSourceAsync_should_throw_if_disposed()
        {
            var subject = new WritableServerBinding(_cluster);
            subject.Dispose();

            Action act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

            act.ShouldThrow<ObjectDisposedException>();
        }
        public void GetWriteChannelSourceAsync_should_throw_if_disposed()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.Dispose();

            Action act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

            act.ShouldThrow <ObjectDisposedException>();
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_cluster);

            if (async)
            {
                subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _cluster.Received().SelectServerAsync(Arg.Any <WritableServerSelector>(), CancellationToken.None);
            }
            else
            {
                subject.GetWriteChannelSource(CancellationToken.None);

                _cluster.Received().SelectServer(Arg.Any <WritableServerSelector>(), CancellationToken.None);
            }
        }
Ejemplo n.º 6
0
        public void GetWriteChannelSource_with_mayUseSecondary_should_pass_mayUseSecondary_to_server_selector(
            [Values(false, true)]
            bool async)
        {
            var subject        = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

            var clusterId = new ClusterId();
            var endPoint  = new DnsEndPoint("localhost", 27017);

#pragma warning disable CS0618 // Type or member is obsolete
            var initialClusterDescription = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
#pragma warning restore CS0618 // Type or member is obsolete
            var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);
            _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);

            var mockMayUseSecondary = new Mock <IMayUseSecondaryCriteria>();
            mockMayUseSecondary.SetupGet(x => x.ReadPreference).Returns(ReadPreference.SecondaryPreferred);
            mockMayUseSecondary.Setup(x => x.CanUseSecondary(It.IsAny <ServerDescription>())).Returns(true);
            var mayUseSecondary = mockMayUseSecondary.Object;

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny <WritableServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetWriteChannelSourceAsync(mayUseSecondary, CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.Is <WritableServerSelector>(s => s.MayUseSecondary == mayUseSecondary), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny <WritableServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetWriteChannelSource(mayUseSecondary, CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.Is <WritableServerSelector>(s => s.MayUseSecondary == mayUseSecondary), CancellationToken.None), Times.Once);
            }
        }
        public void GetWriteChannelSource_should_throw_if_disposed(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());

            subject.Dispose();

            Action act;

            if (async)
            {
                act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.GetWriteChannelSource(CancellationToken.None);
            }

            act.ShouldThrow <ObjectDisposedException>();
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject        = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
            var selectedServer = new Mock <IServer>().Object;

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny <WritableServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny <WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny <WritableServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetWriteChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny <WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_mockCluster.Object);
            var selectedServer = new Mock<IServer>().Object;

            if (async)
            {
                _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny<WritableServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));

                subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
            else
            {
                _mockCluster.Setup(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None)).Returns(selectedServer);

                subject.GetWriteChannelSource(CancellationToken.None);

                _mockCluster.Verify(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
            }
        }
        public void GetWriteChannelSource_should_throw_if_disposed(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_mockCluster.Object);
            subject.Dispose();

            Action act;
            if (async)
            {
                act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.GetWriteChannelSource(CancellationToken.None);
            }

            act.ShouldThrow<ObjectDisposedException>();
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster()
        {
            var subject = new WritableServerBinding(_cluster);

            subject.GetWriteChannelSourceAsync(CancellationToken.None).Wait();

            _cluster.Received().SelectServerAsync(Arg.Any<WritableServerSelector>(), CancellationToken.None);
        }
        public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
            [Values(false, true)]
            bool async)
        {
            var subject = new WritableServerBinding(_cluster);

            if (async)
            {
                subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();

                _cluster.Received().SelectServerAsync(Arg.Any<WritableServerSelector>(), CancellationToken.None);
            }
            else
            {
                subject.GetWriteChannelSource(CancellationToken.None);

                _cluster.Received().SelectServer(Arg.Any<WritableServerSelector>(), CancellationToken.None);
            }
        }