Ejemplo n.º 1
0
        private static void Uncompress(ReusableMemoryStream uncompressed, byte[] body, int offset, int length, CompressionCodec codec)
        {
            try
            {
                if (codec == CompressionCodec.Snappy)
                {
#if NET_CORE
                    throw new NotImplementedException();
#else
                    uncompressed.SetLength(SnappyCodec.GetUncompressedLength(body, offset, length));
                    SnappyCodec.Uncompress(body, offset, length, uncompressed.GetBuffer(), 0);
#endif
                }
                else if (codec == CompressionCodec.Gzip)
                {
                    using (var compressed = new MemoryStream(body, offset, length, false))
                    {
                        using (var zipped = new GZipStream(compressed, CompressionMode.Decompress))
                        {
                            using (var tmp = uncompressed.Pool.Reserve())
                            {
                                zipped.ReusableCopyTo(uncompressed, tmp);
                            }
                        }
                    }
                }
                else // compression == CompressionCodec.Lz4
                {
                    KafkaLz4.Uncompress(uncompressed, body, offset);
                }

                uncompressed.Position = 0;
            }
            catch (Exception ex)
            {
                throw new UncompressException("Invalid compressed data.", codec, ex);
            }
        }
Ejemplo n.º 2
0
        public static void Compress(ReusableMemoryStream target, byte[] body, int count)
        {
            target.Write(FrameDescriptor, 0, FrameDescriptor.Length);

            // Blocks
            var left = count;

            while (left >= BLOCK_SIZE)
            {
                BlockCompress(target, body, count - left, BLOCK_SIZE);
                left -= BLOCK_SIZE;
            }

            // Last block if any
            if (left > 0)
            {
                BlockCompress(target, body, count - left, left);
            }

            // EndMark
            target.Write(Basics.Zero32, 0, Basics.Zero32.Length);
            target.SetLength(target.Position);
        }
Ejemplo n.º 3
0
        public void TestSendAsyncErrorSocketSendThrow()
        {
            var socket = new Mock <ISocket>();

            socket.Setup(s => s.CreateEventArgs()).Returns(() =>
            {
                var saea = new Mock <ISocketAsyncEventArgs>();
                return(saea.Object);
            });
            socket.Setup(s => s.Connected).Returns(true);
            SocketError error  = SocketError.WouldBlock;
            var         buffer = new ReusableMemoryStream(null);

            buffer.SetLength(14);
            socket.Setup(
                s =>
                s.Send(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>(), out error))
            .Throws <InvalidOperationException>();
            var connection = new Connection(new IPEndPoint(0, 0), _ => socket.Object, BPool, RPool, 1024, 1024);
            var e          = Assert.ThrowsAsync <TransportException>(async() => await connection.SendAsync(12, buffer, true));

            Assert.That(e.Error, Is.EqualTo(TransportError.WriteError));
            Assert.IsInstanceOf <InvalidOperationException>(e.InnerException);
        }
Ejemplo n.º 4
0
        public void TestFetchWithNoErrors(Basics.ApiVersion version)
        {
            // Prepare
            var serializer = new Mock <Node.ISerialization>();
            var connection = new Mock <IConnection>();
            var node       = new Node("Node", () => connection.Object, serializer.Object,
                                      new Configuration
            {
                TaskScheduler    = new CurrentThreadTaskScheduler(),
                ConsumeBatchSize = 1,
                Compatibility    = version == Basics.ApiVersion.V0 ? Compatibility.V0_8_2 : Compatibility.V0_10_1
            }, 1);
            var ev    = new ManualResetEvent(false);
            var corrs = new Queue <int>();

            connection.Setup(c => c.SendAsync(It.IsAny <int>(), It.IsAny <ReusableMemoryStream>(), It.IsAny <bool>()))
            .Returns((int c, ReusableMemoryStream d, bool a) =>
            {
                corrs.Enqueue(c);
                return(Success);
            })
            .Callback(() => ev.Set());
            connection.Setup(c => c.ConnectAsync()).Returns(Success);
            var message = new FetchMessage {
                Topic = "balbuzzard", Offset = 42, Partition = 1, MaxBytes = 1242
            };

            node.Fetch(message);
            ev.WaitOne();
            Assert.AreEqual(1, corrs.Count);
            int corr     = corrs.Dequeue();
            int response = 0;

            node.ResponseReceived += (n, l) =>
            {
                Assert.AreSame(node, n);
                ++response;
            };
            var acknowledgement = new CommonAcknowledgement <FetchResponse>();

            node.FetchAcknowledgement += (n, ack) =>
            {
                acknowledgement = ack;
                Assert.AreSame(node, n);
            };

            serializer.Setup(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), version))
            .Returns(new FetchResponse {
                FetchPartitionResponse = new CommonResponse <FetchPartitionResponse>
                {
                    TopicsResponse =
                        new[]
                    {
                        new TopicData <FetchPartitionResponse>
                        {
                            TopicName      = "balbuzzard",
                            PartitionsData =
                                new[]
                            {
                                new FetchPartitionResponse
                                {
                                    ErrorCode           = ErrorCode.NoError,
                                    HighWatermarkOffset = 42,
                                    Partition           = 1,
                                    Messages            =
                                        new List <ResponseMessage> {
                                        new ResponseMessage {
                                            Offset = 28, Message = new Message()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });

            bool resp = false;

            node.FetchResponseReceived += (n, c, s) =>
            {
                Assert.That(n, Is.EqualTo(node));
                Assert.That(c, Is.EqualTo(1));
                Assert.That(s, Is.EqualTo(28));
                resp = true;
            };

            // Now send a response
            var r = new ReusableMemoryStream(null);

            r.SetLength(28);
            connection.Raise(c => c.Response += null, connection.Object, corr, r);
            serializer.Verify(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), It.IsAny <Basics.ApiVersion>()), Times.Once);
            serializer.Verify(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), version));

            // Checks
            Assert.IsTrue(resp);
            Assert.AreNotEqual(default(DateTime), acknowledgement.ReceivedDate);
            Assert.AreEqual(1, acknowledgement.Response.FetchPartitionResponse.TopicsResponse.Length);
            Assert.AreEqual("balbuzzard", acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].TopicName);
            Assert.AreEqual(1, acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].PartitionsData.Count());
            var fetch = acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].PartitionsData.First();

            Assert.AreEqual(ErrorCode.NoError, fetch.ErrorCode);
            Assert.AreEqual(42, fetch.HighWatermarkOffset);
            Assert.AreEqual(1, fetch.Partition);
            Assert.AreEqual(1, fetch.Messages.Count());
            Assert.AreEqual(28, fetch.Messages.First().Offset);
            Assert.AreEqual(new Message(), fetch.Messages.First().Message); // This is not full proof but come on...
        }
Ejemplo n.º 5
0
        public async Task TestSendAsync()
        {
            var mocked = new Dictionary <ISocketAsyncEventArgs, Mock <ISocketAsyncEventArgs> >();
            var socket = new Mock <ISocket>();
            int step   = 0; // we'll do 3 async steps

            socket.Setup(s => s.CreateEventArgs()).Returns(() =>
            {
                var saea = new Mock <ISocketAsyncEventArgs>();
                saea.Setup(a => a.SocketError).Returns(SocketError.Success);
                saea.SetupProperty(a => a.UserToken);
                saea.Setup(a => a.BytesTransferred).Returns(() =>
                {
                    switch (step)
                    {
                    case 1:
                        return(4);

                    case 2:
                        return(7);

                    default:
                        return(3);
                    }
                });
                saea.Setup(a => a.Count).Returns(() =>
                {
                    switch (step)
                    {
                    case 1:
                        return(14);

                    case 2:
                        return(10);

                    default:
                        return(3);
                    }
                });
                saea.Setup(a => a.Offset).Returns(() =>
                {
                    switch (step)
                    {
                    case 1:
                        return(0);

                    case 2:
                        return(4);

                    default:
                        return(11);
                    }
                });
                mocked.Add(saea.Object, saea);
                return(saea.Object);
            });
            socket.Setup(s => s.Connected).Returns(true);
            SocketError error  = SocketError.WouldBlock;
            var         buffer = new ReusableMemoryStream(null);

            buffer.SetLength(14);
            socket.Setup(
                s =>
                s.Send(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>(), out error))
            .Returns(0);

            socket.Setup(s => s.SendAsync(It.IsAny <ISocketAsyncEventArgs>()))
            .Returns(() => ++ step == 3)    // simulate a synchronous return on step 1 and 2
            .Callback((ISocketAsyncEventArgs args) =>
            {
                if (step == 3)
                {
                    mocked[args].Raise(a => a.Completed += null, socket.Object, args);
                }
            });

            var connection = new Connection(new IPEndPoint(0, 0), _ => socket.Object, BPool, RPool, 1024, 1024);
            await connection.SendAsync(12, buffer, true);

            socket.Verify(s => s.Send(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>(), out error), Times.Once());
            socket.Verify(s => s.SendAsync(It.IsAny <ISocketAsyncEventArgs>()), Times.Exactly(3));
        }