Exemplo n.º 1
0
        public void Open_should_raise_the_correct_events_upon_failure(
            [Values(false, true)]
            bool async)
        {
            Action act;

            if (async)
            {
                var result = new TaskCompletionSource <ConnectionDescription>();
                result.SetException(new SocketException());
                _connectionInitializer.InitializeConnectionAsync(null, CancellationToken.None)
                .ReturnsForAnyArgs(result.Task);

                act = () => _subject.OpenAsync(CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                _connectionInitializer.InitializeConnection(null, CancellationToken.None)
                .ReturnsForAnyArgs(_ => { throw new SocketException(); });

                act = () => _subject.Open(CancellationToken.None);
            }

            act.ShouldThrow <MongoConnectionException>()
            .WithInnerException <SocketException>()
            .And.ConnectionId.Should().Be(_subject.ConnectionId);

            _capturedEvents.Next().Should().BeOfType <ConnectionOpeningEvent>();
            _capturedEvents.Next().Should().BeOfType <ConnectionOpeningFailedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Exemplo n.º 2
0
        public void Setup()
        {
            _capturedEvents = new EventCapturer();
            _streamFactory  = Substitute.For <IStreamFactory>();

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

            _connectionInitializer = Substitute.For <IConnectionInitializer>();
            _connectionInitializer.InitializeConnection(null, CancellationToken.None)
            .ReturnsForAnyArgs(new ConnectionDescription(
                                   new ConnectionId(serverId),
                                   new IsMasterResult(new BsonDocument()),
                                   new BuildInfoResult(new BsonDocument("version", "2.6.3"))));
            _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);
        }
        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();
        }
Exemplo n.º 4
0
        protected async Task <IConnection> CreateConnectionAsync(CancellationToken cancellationToken)
        {
            var connection = await _connectionFactory.CreateAndConnectAsync(EndPoint, cancellationToken)
                             .ConfigureAwait(false);

            try
            {
                await _connectionInitializer.InitializeConnectionAsync(connection, cancellationToken)
                .ConfigureAwait(false);

                if (BucketName != null)
                {
                    await _connectionInitializer.SelectBucketAsync(connection, BucketName, cancellationToken)
                    .ConfigureAwait(false);
                }

                return(connection);
            }
            catch
            {
                // Be sure to cleanup the connection if bootstrap fails.
                // Use the synchronous dispose as we don't need to wait for in-flight operations to complete.
                connection.Dispose();
                throw;
            }
        }
        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 void Setup()
        {
            _capturedEvents = new EventCapturer();
            _streamFactory = Substitute.For<IStreamFactory>();

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

            _connectionInitializer = Substitute.For<IConnectionInitializer>();
            _connectionInitializer.InitializeConnection(null, CancellationToken.None)
                .ReturnsForAnyArgs(new ConnectionDescription(
                    new ConnectionId(serverId),
                    new IsMasterResult(new BsonDocument()),
                    new BuildInfoResult(new BsonDocument("version", "2.6.3"))));
            _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);
        }
        protected async Task <IConnection> CreateConnectionAsync(CancellationToken cancellationToken)
        {
            var connection = await _connectionFactory.CreateAndConnectAsync(EndPoint, cancellationToken)
                             .ConfigureAwait(false);

            await _connectionInitializer.InitializeConnectionAsync(connection, cancellationToken).ConfigureAwait(false);

            if (BucketName != null)
            {
                await _connectionInitializer.SelectBucketAsync(connection, BucketName, cancellationToken).ConfigureAwait(false);
            }

            return(connection);
        }
        public void OpenAsync_should_raise_the_correct_events_upon_failure()
        {
            var result = new TaskCompletionSource <ConnectionDescription>();

            result.SetException(new SocketException());
            _connectionInitializer.InitializeConnectionAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(result.Task);

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

            act.ShouldThrow <SocketException>();

            _listener.ReceivedWithAnyArgs().ConnectionBeforeOpening(default(ConnectionBeforeOpeningEvent));
            _listener.ReceivedWithAnyArgs().ConnectionErrorOpening(default(ConnectionErrorOpeningEvent));
            _listener.ReceivedWithAnyArgs().ConnectionFailed(default(ConnectionFailedEvent));
        }
Exemplo n.º 9
0
        public void OpenAsync_should_raise_the_correct_events_upon_failure()
        {
            var result = new TaskCompletionSource <ConnectionDescription>();

            result.SetException(new SocketException());
            _connectionInitializer.InitializeConnectionAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(result.Task);

            Func <Task> act = () => _subject.OpenAsync(CancellationToken.None);

            act.ShouldThrow <MongoConnectionException>()
            .WithInnerException <SocketException>()
            .And.ConnectionId.Should().Be(_subject.ConnectionId);

            _capturedEvents.Next().Should().BeOfType <ConnectionOpeningEvent>();
            _capturedEvents.Next().Should().BeOfType <ConnectionOpeningFailedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }