Exception() public static method

Records any exception which is thrown by the given code.
public static Exception ( Assert code ) : Exception
code Assert The code which may thrown an exception.
return System.Exception
コード例 #1
0
            public void ShouldThrowExceptionIfProtocolIsNotSupported()
            {
                var ex = Record.Exception(() => new SocketClient(new Uri("http://localhost:1234"), null));

                ex.Should().BeOfType <NotSupportedException>();
                ex.Message.Should().Be("Unsupported protocol: http");
            }
コード例 #2
0
            public void ShouldThrowArgumentNullExceptionIfSocketClientIsNull()
            {
                var exception = Record.Exception(() => new SocketConnection(null, AuthTokens.None, Logger));

                exception.Should().NotBeNull();
                exception.Should().BeOfType <ArgumentNullException>();
            }
コード例 #3
0
            public void ShouldDisposeConnectionOnNewBeginTxIfBeginTxFailed()
            {
                // Given
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.IsOpen).Returns(true);
                var calls = 0;

                mockConn.Setup(x => x.Run("BEGIN", null, null, true))
                .Callback(() =>
                {
                    // only throw exception on the first beginTx call
                    calls++;
                    if (calls == 1)
                    {
                        throw new IOException("Triggered an error when beginTx");
                    }
                });
                var session = NewSession(mockConn.Object);

                Record.Exception(() => session.BeginTransaction()).Should().BeOfType <IOException>();

                // When
                session.BeginTransaction();

                // Then
                mockConn.Verify(x => x.Close(), Times.Once);
            }
コード例 #4
0
        public void ShouldThrowIfSummaryBuilderIsNull()
        {
            var exc = Record.Exception(() => new RunResponseHandler(new Mock <IResultStreamBuilder>().Object, null));

            exc.Should().BeOfType <ArgumentNullException>().Which
            .ParamName.Should().Be("summaryBuilder");
        }
コード例 #5
0
            public void ShouldDisposeConnectionOnNewBeginTxIfBeginTxFailed()
            {
                // Given
                var mockProtocol = new Mock <IBoltProtocol>();
                var mockConn     = NewMockedConnection(mockProtocol.Object);
                var calls        = 0;

                mockProtocol.Setup(x => x.BeginTransaction(It.IsAny <IConnection>(), It.IsAny <Bookmark>()))
                .Callback(() =>
                {
                    // only throw exception on the first beginTx call
                    calls++;
                    if (calls == 1)
                    {
                        throw new IOException("Triggered an error when beginTx");
                    }
                });

                var session = NewSession(mockConn.Object);

                Record.Exception(() => session.BeginTransaction()).Should().BeOfType <IOException>();

                // When
                session.BeginTransaction();

                // Then
                mockConn.Verify(x => x.Close(), Times.Once);
            }
コード例 #6
0
            public async Task ShouldStopClientAndThrowExceptionWhenProtocolErrorOccurs()
            {
                using (var harness = new SocketClientTestHarness(FakeUri))
                {
                    var messages = new IRequestMessage[]
                    {
                        new RunMessage("Any message"),
                    };

                    var messageHandler = new TestResponseHandler();

                    messageHandler.EnqueueMessage(messages[0]);
                    harness.SetupReadStream("00 00 00 01" +
                                            "00 02 b0 7e 00 00"); // read whatever message but not success

                    harness.SetupWriteStream();

                    await harness.Client.Start();

                    // force to recive an error
                    messageHandler.Error = new ClientException("Neo.ClientError.Request.Invalid", "Test Message");

                    // When
                    harness.Client.Send(messages);
                    var ex = Record.Exception(() => harness.Client.Receive(messageHandler));
                    ex.Should().BeOfType <ClientException>();

                    harness.MockTcpSocketClient.Verify(x => x.DisconnectAsync(), Times.Once);
                    harness.MockTcpSocketClient.Verify(x => x.Dispose(), Times.Once);
                }
            }
コード例 #7
0
            public async Task ShouldCreateExceptionWhenErrorReceivedFromDatabase()
            {
                using (var harness = new SocketClientTestHarness(FakeUri, null))
                {
                    var messages       = new IRequestMessage[] { new RunMessage("This will cause a syntax error") };
                    var messageHandler = new MessageResponseHandler();
                    messageHandler.EnqueueMessage(new InitMessage("MyClient/1.1", new Dictionary <string, object>()));
                    messageHandler.EnqueueMessage(messages[0], new ResultBuilder());

                    harness.SetupReadStream("00 00 00 01" +
                                            "00 03 b1 70 a0 00 00" +
                                            "00a0b17fa284636f6465d0274e656f2e436c69656e744572726f722e53746174656d656e742e496e76616c696453796e746178876d657373616765d065496e76616c696420696e707574202754273a206578706563746564203c696e69743e20286c696e6520312c20636f6c756d6e203120286f66667365743a203029290a22546869732077696c6c20636175736520612073796e746178206572726f72220a205e0000");

                    harness.SetupWriteStream();

                    await harness.Client.Start();

                    harness.ResetCalls();

                    // When
                    harness.Client.Send(messages);
                    Record.Exception(() => harness.Client.Receive(messageHandler));

                    // Then
                    harness.VerifyWriteStreamUsages(2 /*write + flush*/);

                    messageHandler.HasError.Should().BeTrue();
                    messageHandler.Error.Code.Should().Be("Neo.ClientError.Statement.InvalidSyntax");
                    messageHandler.Error.Message.Should().Be(
                        "Invalid input 'T': expected <init> (line 1, column 1 (offset: 0))\n\"This will cause a syntax error\"\n ^");
                }
            }
コード例 #8
0
            public void ShouldServiceUnavailableWhenProcedureNotFound()
            {
                // Given
                var pairs = new List <Tuple <IRequestMessage, IResponseMessage> >
                {
                    MessagePair(InitMessage(), SuccessMessage()),
                    MessagePair(new RunMessage("CALL dbms.cluster.routing.getServers"),
                                new FailureMessage("Neo.ClientError.Procedure.ProcedureNotFound", "not found")),
                    MessagePair(PullAllMessage(), new IgnoredMessage())
                };

                var messagingClient = new MockedMessagingClient(pairs);
                var conn            = SocketConnectionTests.NewSocketConnection(messagingClient.Client);

                conn.Init();

                var manager = CreateDiscoveryManager(conn, null);

                // When
                var exception = Record.Exception(() => manager.Rediscovery());

                // Then
                exception.Should().BeOfType <ServiceUnavailableException>();
                exception.Message.Should().StartWith("Error when calling `getServers` procedure: ");
                messagingClient.ClientMock.Verify(x => x.Stop(), Times.Once);
            }
コード例 #9
0
            public async Task ShouldStopClientAndThrowExceptionWhenProtocolErrorOccurs()
            {
                using (var harness = new SocketClientTestHarness(FakeUri, null))
                {
                    var messages = new IRequestMessage[]
                    {
                        new RunMessage("This will cause a syntax error"),
                        new PullAllMessage()
                    };

                    var messageHandler = new TestResponseHandler();

                    messageHandler.Register(new InitMessage("MyClient/1.1", new Dictionary <string, object>()));
                    messageHandler.Register(messages[0], new ResultBuilder());
                    messageHandler.Register(messages[1], new ResultBuilder());

                    harness.SetupReadStream("00 00 00 01" +
                                            "00 03 b1 70 a0 00 00");

                    harness.SetupWriteStream();

                    await harness.Client.Start();

                    messageHandler.Error = new ClientException("Neo.ClientError.Request.Invalid", "Test Message");

                    // When
                    var ex = Record.Exception(() => harness.Client.Send(messages, messageHandler));
                    ex.Should().BeOfType <ClientException>();

                    harness.MockTcpSocketClient.Verify(x => x.DisconnectAsync(), Times.Once);
                    harness.MockTcpSocketClient.Verify(x => x.Dispose(), Times.Once);
                }
            }
コード例 #10
0
        public void ShouldThrowIfStreamBuilderIsNull()
        {
            var summaryBuilder =
                new Mock <SummaryBuilder>(new Query("stmt"), new ServerInfo(new Uri("bolt://localhost")));
            var exc = Record.Exception(() => new RunResponseHandler(null, summaryBuilder.Object));

            exc.Should().BeOfType <ArgumentNullException>().Which
            .ParamName.Should().Be("streamBuilder");
        }
コード例 #11
0
            public void ShouldNotBeAbleToUseSessionWhileOngoingTransaction()
            {
                var mockConn = NewMockedConnection();
                var session  = NewSession(mockConn.Object);
                var tx       = session.BeginTransaction();

                var error = Record.Exception(() => session.Run("lalal"));

                error.Should().BeOfType <ClientException>();
            }
コード例 #12
0
            public void ShouldNotAllowNewTxWhileOneIsRunning()
            {
                var mockConn = NewMockedConnection();
                var session  = NewSession(mockConn.Object);

                session.BeginTransaction();
                var error = Record.Exception(() => session.BeginTransaction());

                error.Should().BeOfType <ClientException>();
            }
コード例 #13
0
            public void ShouldNotAllowMoreStatementsInSessionWhileConnectionClosed()
            {
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.IsOpen).Returns(false);
                var session = new Session(null, null, null, mockConn.Object);

                var error = Record.Exception(() => session.Run("lalal"));

                error.Should().BeOfType <ClientException>();
            }
コード例 #14
0
            public void ShouldNotAllowMoreTransactionsInSessionWhileConnectionClosed()
            {
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.IsOpen).Returns(false);
                var session = new Session(mockConn.Object, null);

                var error = Record.Exception(() => session.BeginTransaction());

                error.Should().BeOfType <ClientException>();
            }
コード例 #15
0
            public void ShouldThrowExceptionWhenTryingToCollectBookmark()
            {
                var collector = NewSummaryCollector();
                var error     = Record.Exception(() =>
                                                 collector.CollectBookmark(new Dictionary <string, object> {
                    { "bookmark", "I shall not be here" }
                }));

                error.Should().BeOfType <NotSupportedException>();
                error.Message.Should().Contain("not get a bookmark on a result");
            }
コード例 #16
0
            public void ShouldNotAllowMoreTransactionsInSessionWhileConnectionHasUnrecoverableError()
            {
                var mockConn = new Mock <IPooledConnection>();

                mockConn.Setup(x => x.HasUnrecoverableError).Returns(true);
                var session = new Session(mockConn.Object, null);

                var error = Record.Exception(() => session.BeginTransaction());

                error.Should().BeOfType <ClientException>();
            }
コード例 #17
0
            public void ShouldNotAllowMoreStatementsInSessionWhileConnectionHasUnrecoverableError()
            {
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.HasUnrecoverableError).Returns(true);
                var session = new Session(mockConn.Object, null);

                var error = Record.Exception(() => session.Run("lalal"));

                error.Should().BeOfType <ClientException>();
            }
コード例 #18
0
            public void ShouldNotBeAbleToUseSessionWhileOngoingTransaction()
            {
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.IsOpen).Returns(true);
                var session = new Session(mockConn.Object, null);
                var tx      = session.BeginTransaction();

                var error = Record.Exception(() => session.Run("lalal"));

                error.Should().BeOfType <ClientException>();
            }
コード例 #19
0
            public void ShouldNotAllowNewTxWhileOneIsRunning()
            {
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.IsOpen).Returns(true);
                var session = new Session(mockConn.Object, null);

                session.BeginTransaction();
                var error = Record.Exception(() => session.BeginTransaction());

                error.Should().BeOfType <ClientException>();
            }
コード例 #20
0
        public void ShouldThrowIfValueIsOfWrongType()
        {
            var metadata = new Dictionary <string, object> {
                { Key, 1L }
            };
            var collector = new DatabaseInfoCollector();

            var ex = Record.Exception(() => collector.Collect(metadata));

            ex.Should().BeOfType <ProtocolException>().Which
            .Message.Should()
            .Contain($"Expected '{Key}' metadata to be of type 'string', but got 'Int64'.");
        }
コード例 #21
0
        public void ShouldThrowIfValueIsOfWrongType()
        {
            var metadata = new Dictionary <string, object> {
                { Key, true }
            };
            var collector = new CountersCollector();

            var ex = Record.Exception(() => collector.Collect(metadata));

            ex.Should().BeOfType <ProtocolException>().Which
            .Message.Should()
            .Contain($"Expected '{Key}' metadata to be of type 'IDictionary<String,Object>', but got 'Boolean'.");
        }
コード例 #22
0
            public void ShouldThrowExceptionWhenDisposingSessionMoreThanOnce()
            {
                // Given
                var mockConn = new Mock <IConnection>();
                var session  = NewSession(mockConn.Object);

                // When
                session.Dispose();
                var exception = Record.Exception(() => session.Dispose());

                // Then
                exception.Should().BeOfType <ObjectDisposedException>();
                exception.Message.Should().Contain("Failed to dispose this seesion as it has already been disposed.");
            }
コード例 #23
0
            public void ShouldDisposeConnectionIfBeginTxFailed()
            {
                var mockConn = new Mock <IConnection>();

                mockConn.Setup(x => x.IsOpen).Returns(true);
                mockConn.Setup(x => x.Run("BEGIN", null, null, true))
                .Throws(new IOException("Triggered an error when beginTx"));
                var session = NewSession(mockConn.Object);

                Record.Exception(() => session.BeginTransaction()).Should().BeOfType <IOException>();
                session.Dispose();

                mockConn.Verify(x => x.Close(), Times.Once);
            }
コード例 #24
0
            public void ShouldProtocolErrorWhenNoRecord()
            {
                // Given
                var connMock = SetupSocketConnection(new List <object[]>());
                var manager  = CreateDiscoveryManager(connMock.Object);

                // When
                var exception = Record.Exception(() => manager.Rediscovery());

                // Then
                exception.Should().BeOfType <ProtocolException>();
                exception.Message.Should().Be("Error when parsing `getServers` result: Sequence contains no elements.");
                connMock.Verify(x => x.Close(), Times.Once);
            }
コード例 #25
0
            public void ShouldDisposeConnectionIfBeginTxFailed()
            {
                var mockProtocol = new Mock <IBoltProtocol>();
                var mockConn     = NewMockedConnection(mockProtocol.Object);

                mockProtocol.Setup(x => x.BeginTransaction(It.IsAny <IConnection>(), It.IsAny <Bookmark>()))
                .Throws(new IOException("Triggered an error when beginTx"));
                var session = NewSession(mockConn.Object);

                Record.Exception(() => session.BeginTransaction()).Should().BeOfType <IOException>();
                session.Dispose();

                mockConn.Verify(x => x.Sync(), Times.Once);
                mockConn.Verify(x => x.Close(), Times.Once);
            }
コード例 #26
0
            public void ShouldProtocolErrorWhenRecordUnparsable()
            {
                // Given
                var connMock = SetupSocketConnection(new object[] { 1 });
                var manager  = CreateDiscoveryManager(connMock.Object);

                // When
                var exception = Record.Exception(() => manager.Rediscovery());

                // Then
                exception.Should().BeOfType <ProtocolException>();
                exception.Message.Should()
                .Be("Error when parsing `getServers` result: keys (2) does not equal to values (1).");
                connMock.Verify(x => x.Close(), Times.Once);
            }
コード例 #27
0
            public void ShouldThrowExceptionWhenDisposingSessionMoreThanOnce()
            {
                // Given
                var mockConn = new Mock <IConnection>();
                var session  = new Session(mockConn.Object, null);

                // When
                session.Dispose();
                var exception = Record.Exception(() => session.Dispose());

                // Then
                exception.Should().BeOfType <InvalidOperationException>();
                exception.Message.Should().Be("Failed to dispose this seesion as it has already been disposed.");

                mockConn.Verify(x => x.Dispose(), Times.Once);
            }
コード例 #28
0
            public void ShouldThrowExceptionIfRouterIsEmpty()
            {
                // Given
                var clientMock   = new Mock <ISocketClient>();
                var recordFields = CreateGetServersResponseRecordFields(0, 2, 1);
                var manager      = CreateDiscoveryManager(SetupSocketConnection(recordFields, clientMock));

                // When
                var exception = Record.Exception(() => manager.Rediscovery());

                // Then
                manager.Readers.Count().Should().Be(1);
                manager.Writers.Count().Should().Be(2);
                manager.Routers.Count().Should().Be(0);
                exception.Should().BeOfType <ProtocolException>();
                exception.Message.Should().Contain("0 routers, 2 writers and 1 readers.");
                clientMock.Verify(x => x.Stop(), Times.Once);
            }
コード例 #29
0
            public void ShouldThrowExceptionIfReaderIsEmpty()
            {
                // Given
                var procedureReplyRecordFields = CreateGetServersResponseRecordFields(3, 1, 0);
                var connMock = SetupSocketConnection(procedureReplyRecordFields);
                var manager  = CreateDiscoveryManager(connMock.Object);

                // When
                var exception = Record.Exception(() => manager.Rediscovery());

                // Then
                manager.Readers.Count().Should().Be(0);
                manager.Writers.Count().Should().Be(1);
                manager.Routers.Count().Should().Be(3);
                exception.Should().BeOfType <ProtocolException>();
                exception.Message.Should().Contain("3 routers, 1 writers and 0 readers.");
                connMock.Verify(x => x.Close(), Times.Once);
            }
コード例 #30
0
        public void ShouldThrowIfOperatorTypeIsMissing()
        {
            var metadata = new Dictionary <string, object>
            {
                {
                    Key, new Dictionary <string, object>
                    {
                        { "args", new Dictionary <string, object>() }
                    }
                }
            };
            var collector = new PlanCollector();

            var ex = Record.Exception(() => collector.Collect(metadata));

            ex.Should().BeOfType <ProtocolException>().Which
            .Message.Should()
            .Be("Expected key 'operatorType' to be present in the dictionary, but could not find.");
        }