Exemplo n.º 1
0
        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 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 HelloResult(new BsonDocument("serviceId", serviceId)));

            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>();
        }