Example #1
0
        public void PeepingTomStreamTest(int originalSize, int chunkSizeToRead, int offset, JsonOperationContext context)
        {
            var bytes = new byte[originalSize];

            for (var i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)((i % 26) + 'a');
            }

            using (var stream = new MemoryStream())
                using (var peeping = new PeepingTomStream(stream, context))
                {
                    stream.Write(bytes, 0, originalSize);
                    stream.Flush();
                    stream.Position = 0;

                    var totalRead = 0;
                    do
                    {
                        int read;
                        do
                        {
                            var buffer = new byte[originalSize + offset];
                            read       = peeping.Read(buffer, offset, chunkSizeToRead);
                            totalRead += read;
                            Assert.True(read <= chunkSizeToRead);
                        } while (read != 0);
                    } while (totalRead < originalSize);

                    Assert.Equal(originalSize, totalRead);
                    var peepWindow = peeping.PeepInReadStream();
                    var length     = peepWindow.Length;

                    Assert.True(length <= PeepingTomStream.BufferWindowSize);
                    Assert.True(length >= 0);

                    var expectedLength = originalSize < PeepingTomStream.BufferWindowSize ? originalSize : PeepingTomStream.BufferWindowSize;

                    if (expectedLength != length)
                    {
                        var expected = System.Text.Encoding.UTF8.GetString(bytes, bytes.Length - expectedLength, expectedLength);
                        var actual   = System.Text.Encoding.UTF8.GetString(peepWindow);
                        Assert.Equal(expected, actual);
                    }
                    Assert.Equal(expectedLength, length);

                    for (var i = 0; i < peepWindow.Length; i++)
                    {
                        var expectedByte = (byte)(((originalSize - peepWindow.Length + i) % 26) + 'a');
                        if (expectedByte != peepWindow[i])
                        {
                            Assert.Equal(expectedByte, peepWindow[i]);
                        }
                    }
                }
        }
 public ReplayTransactionsException(string message, PeepingTomStream peepingTomStream)
     : base(message)
 {
     try
     {
         Context = Encodings.Utf8.GetString(peepingTomStream.PeepInReadStream());
     }
     catch (Exception e)
     {
         Context = "Failed to generated peepedWindow: " + e;
     }
 }
Example #3
0
        private static string GetPeepingTomBufferAsString(PeepingTomStream peepingTomStream)
        {
            string peepedWindow;

            try
            {
                peepedWindow = Encodings.Utf8.GetString(peepingTomStream.PeepInReadStream());
            }
            catch (Exception e)
            {
                peepedWindow = "Failed to generated peepedWindow: " + e;
            }
            return(peepedWindow);
        }
Example #4
0
        private static void PeepingTomStreamTest(int originalSize, int chunkSizeToRead, int offset, int?seed, JsonOperationContext context, CancellationToken token)
        {
            try
            {
                var bytes = new byte[originalSize];
                for (var i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)((i % 26) + 'a');
                }

                using (var stream = new MemoryStream())
                    using (context.GetMemoryBuffer(out var memoryBuffer))
                    {
                        // fill the buffer with garbage
                        var random = seed == null ? new Random() : new Random(seed.Value);
                        random.NextBytes(memoryBuffer.Memory.Memory.Span);

                        using (var peeping = new PeepingTomStream(stream, memoryBuffer))
                        {
                            stream.Write(bytes, 0, originalSize);
                            stream.Flush();
                            stream.Position = 0;

                            var totalRead = 0;
                            do
                            {
                                token.ThrowIfCancellationRequested();

                                int read = -1;
                                do
                                {
                                    token.ThrowIfCancellationRequested();

                                    var buffer = new Span <byte>(new byte[originalSize + offset]);
                                    read       = peeping.Read(buffer.Slice(offset, chunkSizeToRead));
                                    totalRead += read;
                                    Assert.True(read <= chunkSizeToRead);
                                } while (read != 0);
                            } while (totalRead < originalSize);

                            Assert.Equal(originalSize, totalRead);

                            var peepWindow = peeping.PeepInReadStream();

                            var length = peepWindow.Length;

                            Assert.True(length <= PeepingTomStream.BufferWindowSize);
                            Assert.True(length >= 0);

                            var expectedLength = originalSize < PeepingTomStream.BufferWindowSize ? originalSize : PeepingTomStream.BufferWindowSize;

                            if (expectedLength != length)
                            {
                                var expected = System.Text.Encoding.UTF8.GetString(bytes, bytes.Length - expectedLength, expectedLength);
                                var actual   = System.Text.Encoding.UTF8.GetString(peepWindow);
                                Assert.Equal(expected, actual);
                            }
                            Assert.Equal(expectedLength, length);

                            for (var i = 0; i < peepWindow.Length; i++)
                            {
                                token.ThrowIfCancellationRequested();

                                try
                                {
                                    var expectedByte = (byte)(((originalSize - peepWindow.Length + i) % 26) + 'a');
                                    if (expectedByte != peepWindow[i])
                                    {
                                        Assert.Equal(expectedByte, peepWindow[i]);
                                    }
                                }
                                catch (Exception e)
                                {
                                    throw new InvalidOperationException("Failure at index: " + i, e);
                                }
                            }
                        }
                    }
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Failed with originalSize {originalSize}, chunkSizeToRead {chunkSizeToRead},  offset {offset}", e);
            }
        }