Ejemplo n.º 1
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);
                }
            }
            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);
                }
            }
Ejemplo n.º 3
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 ^");
                }
            }
Ejemplo n.º 4
0
            public async Task ShouldSendMessagesAsExpected()
            {
                // Given
                var messages = new IRequestMessage[] { new RunMessage("Run message 1"), new RunMessage("Run message 1") };

                byte[] expectedBytes =
                {
                    0x00, 0x11, 0xB2, 0x10, 0x8D, 0x52, 0x75, 0x6E, 0x20, 0x6D, 0x65, 0x73, 0x73,
                    0x61, 0x67, 0x65, 0x20, 0x31, 0xA0, 0x00, 0x00, 0x00, 0x11, 0xB2, 0x10, 0x8D, 0x52, 0x75, 0x6E, 0x20,
                    0x6D, 0x65, 0x73, 0x73,
                    0x61, 0x67, 0x65, 0x20, 0x31, 0xA0, 0x00, 0x00
                };
                var expectedLength = expectedBytes.Length;

                expectedBytes = expectedBytes.PadRight(ChunkedOutputStream.BufferSize);

                var messageHandler = new MessageResponseHandler();

                messageHandler.EnqueueMessage(new InitMessage("MyClient/1.1", new Dictionary <string, object>()));
                var rb = new ResultBuilder();

                messageHandler.EnqueueMessage(messages[0], rb);
                messageHandler.EnqueueMessage(messages[1], rb);

                using (var harness = new SocketClientTestHarness(FakeUri, null))
                {
                    harness.SetupReadStream("00 00 00 01" +
                                            "00 03 b1 70 a0 00 00" +
                                            "00 0f b1 70  a1 86 66 69  65 6c 64 73  91 83 6e 75 6d 00 00" +
                                            "00 0f b1 70  a1 86 66 69  65 6c 64 73  91 83 6e 75 6d 00 00");
                    harness.SetupWriteStream();

                    await harness.Client.Start();

                    harness.ResetCalls();

                    // When
                    harness.Client.Send(messages);
                    harness.Client.Receive(messageHandler);

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

                    harness.VerifyWriteStreamContent(expectedBytes, expectedLength);
                }
            }
            public async Task ShouldIgnorePullAllWhenErrorHappenedDuringRun()
            {
                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.0", 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" +
                                            "00a0b17fa284636f6465d0274e656f2e436c69656e744572726f722e53746174656d656e742e496e76616c696453796e746178876d657373616765d065496e76616c696420696e707574202754273a206578706563746564203c696e69743e20286c696e6520312c20636f6c756d6e203120286f66667365743a203029290a22546869732077696c6c20636175736520612073796e746178206572726f72220a205e0000" +
                                            "00 02 b0 7e 00 00");

                    harness.SetupWriteStream();

                    await harness.Client.Start();

                    harness.ResetCalls();


                    // When
                    harness.Client.Send(messages, 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 ^");
                    messageHandler.QueueIsEmpty().Should().BeTrue();
                    messageHandler.FailureMessageCalled.Should().Be(1);
                    messageHandler.IgnoreMessageCalled.Should().Be(1);
                }
            }