public void Setup()
        {
            _capturedEvents = new EventCapturer()
                .Capture<CommandStartedEvent>()
                .Capture<CommandSucceededEvent>()
                .Capture<CommandFailedEvent>();

            _streamFactory = Substitute.For<IStreamFactory>();

            _endPoint = new DnsEndPoint("localhost", 27017);
            var serverId = new ServerId(new ClusterId(), _endPoint);

            _connectionInitializer = Substitute.For<IConnectionInitializer>();
            _connectionInitializer.InitializeConnectionAsync(null, CancellationToken.None)
                .ReturnsForAnyArgs(Task.FromResult(new ConnectionDescription(
                    new ConnectionId(serverId),
                    new IsMasterResult(new BsonDocument()),
                    new BuildInfoResult(new BsonDocument("version", "2.6.3")))));

            _subject = new BinaryConnection(
                serverId: serverId,
                endPoint: _endPoint,
                settings: new ConnectionSettings(),
                streamFactory: _streamFactory,
                connectionInitializer: _connectionInitializer,
                eventSubscriber: _capturedEvents);

            _stream = new BlockingMemoryStream();
            _streamFactory.CreateStreamAsync(null, CancellationToken.None)
                .ReturnsForAnyArgs(Task.FromResult<Stream>(_stream));
            _subject.OpenAsync(CancellationToken.None).Wait();
            _capturedEvents.Clear();

            _operationIdDisposer = EventContext.BeginOperation();
        }
        public BinaryConnection_CommandEventTests()
        {
            _capturedEvents = new EventCapturer()
                .Capture<CommandStartedEvent>()
                .Capture<CommandSucceededEvent>()
                .Capture<CommandFailedEvent>();

            _mockStreamFactory = new Mock<IStreamFactory>();

            _endPoint = new DnsEndPoint("localhost", 27017);
            var serverId = new ServerId(new ClusterId(), _endPoint);

            _mockConnectionInitializer = new Mock<IConnectionInitializer>();
            _mockConnectionInitializer.Setup(i => i.InitializeConnectionAsync(It.IsAny<IConnection>(), CancellationToken.None))
                .Returns(() => Task.FromResult(new ConnectionDescription(
                    new ConnectionId(serverId),
                    new IsMasterResult(new BsonDocument()),
                    new BuildInfoResult(new BsonDocument("version", "2.6.3")))));

            _subject = new BinaryConnection(
                serverId: serverId,
                endPoint: _endPoint,
                settings: new ConnectionSettings(),
                streamFactory: _mockStreamFactory.Object,
                connectionInitializer: _mockConnectionInitializer.Object,
                eventSubscriber: _capturedEvents);

            _stream = new BlockingMemoryStream();
            _mockStreamFactory.Setup(f => f.CreateStreamAsync(_endPoint, CancellationToken.None))
                .Returns(Task.FromResult<Stream>(_stream));
            _subject.OpenAsync(CancellationToken.None).Wait();
            _capturedEvents.Clear();

            _operationIdDisposer = EventContext.BeginOperation();
        }
        public BinaryConnection_CommandEventTests()
        {
            _capturedEvents = new EventCapturer()
                              .Capture <CommandStartedEvent>()
                              .Capture <CommandSucceededEvent>()
                              .Capture <CommandFailedEvent>();

            _mockStreamFactory = new Mock <IStreamFactory>();

            _endPoint = new DnsEndPoint("localhost", 27017);
            var serverId = new ServerId(new ClusterId(), _endPoint);

            _mockConnectionInitializer = new Mock <IConnectionInitializer>();
            _mockConnectionInitializer.Setup(i => i.InitializeConnectionAsync(It.IsAny <IConnection>(), CancellationToken.None))
            .Returns(() => Task.FromResult(new ConnectionDescription(
                                               new ConnectionId(serverId),
                                               new IsMasterResult(new BsonDocument()),
                                               new BuildInfoResult(new BsonDocument("version", "2.6.3")))));

            _subject = new BinaryConnection(
                serverId: serverId,
                endPoint: _endPoint,
                settings: new ConnectionSettings(),
                streamFactory: _mockStreamFactory.Object,
                connectionInitializer: _mockConnectionInitializer.Object,
                eventSubscriber: _capturedEvents);

            _stream = new BlockingMemoryStream();
            _mockStreamFactory.Setup(f => f.CreateStreamAsync(_endPoint, CancellationToken.None))
            .Returns(Task.FromResult <Stream>(_stream));
            _subject.OpenAsync(CancellationToken.None).Wait();
            _capturedEvents.Clear();

            _operationIdDisposer = EventContext.BeginOperation();
        }
        public void Setup()
        {
            _capturedEvents = new EventCapturer()
                              .Capture <CommandStartedEvent>()
                              .Capture <CommandSucceededEvent>()
                              .Capture <CommandFailedEvent>();

            _streamFactory = Substitute.For <IStreamFactory>();

            _endPoint = new DnsEndPoint("localhost", 27017);
            var serverId = new ServerId(new ClusterId(), _endPoint);

            _connectionInitializer = Substitute.For <IConnectionInitializer>();
            _connectionInitializer.InitializeConnectionAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(Task.FromResult(new ConnectionDescription(
                                                   new ConnectionId(serverId),
                                                   new IsMasterResult(new BsonDocument()),
                                                   new BuildInfoResult(new BsonDocument("version", "2.6.3")))));

            _subject = new BinaryConnection(
                serverId: serverId,
                endPoint: _endPoint,
                settings: new ConnectionSettings(),
                streamFactory: _streamFactory,
                connectionInitializer: _connectionInitializer,
                eventSubscriber: _capturedEvents);

            _stream = new BlockingMemoryStream();
            _streamFactory.CreateStreamAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(Task.FromResult <Stream>(_stream));
            _subject.OpenAsync(CancellationToken.None).Wait();
            _capturedEvents.Clear();

            _operationIdDisposer = EventContext.BeginOperation();
        }
Exemple #5
0
        public void Open_should_always_create_description_if_handshake_was_successful(
            [Values(false, true)] bool async)
        {
            var serviceId             = ObjectId.GenerateNewId();
            var connectionDescription = new ConnectionDescription(
                new ConnectionId(new ServerId(new ClusterId(), _endPoint)),
                new IsMasterResult(new BsonDocument("serviceId", serviceId)),
                new BuildInfoResult(new BsonDocument("version", "0.0.0")));

            var socketException = new SocketException();

            _mockConnectionInitializer
            .Setup(i => i.SendHello(It.IsAny <IConnection>(), CancellationToken.None))
            .Returns(connectionDescription);
            _mockConnectionInitializer
            .Setup(i => i.SendHelloAsync(It.IsAny <IConnection>(), CancellationToken.None))
            .ReturnsAsync(connectionDescription);
            _mockConnectionInitializer
            .Setup(i => i.Authenticate(It.IsAny <IConnection>(), It.IsAny <ConnectionDescription>(), CancellationToken.None))
            .Throws(socketException);
            _mockConnectionInitializer
            .Setup(i => i.AuthenticateAsync(It.IsAny <IConnection>(), It.IsAny <ConnectionDescription>(), CancellationToken.None))
            .ThrowsAsync(socketException);

            Exception exception;

            if (async)
            {
                exception = Record.Exception(() => _subject.OpenAsync(CancellationToken.None).GetAwaiter().GetResult());
            }
            else
            {
                exception = Record.Exception(() => _subject.Open(CancellationToken.None));
            }

            _subject.Description.Should().Be(connectionDescription);
            var ex = exception.Should().BeOfType <MongoConnectionException>().Subject;

            ex.InnerException.Should().BeOfType <SocketException>();
        }
        public void Open_should_throw_an_ObjectDisposedException_if_the_connection_is_disposed(
            [Values(false, true)]
            bool async)
        {
            _subject.Dispose();

            Action act;

            if (async)
            {
                act = () => _subject.OpenAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => _subject.Open(CancellationToken.None);
            }

            act.ShouldThrow <ObjectDisposedException>();
        }
        public void OpenAsync_should_throw_an_ObjectDisposedException_if_the_connection_is_disposed()
        {
            _subject.Dispose();

            Action act = () => _subject.OpenAsync(CancellationToken.None).Wait();

            act.ShouldThrow <ObjectDisposedException>();
        }