public async Task CanAppendSelfWhileNotEmpty()
        {
            byte[] chunk = new byte[512];
            new Random().NextBytes(chunk);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();

                for (int i = 0; i < 20; i++)
                {
                    output.Write(chunk);
                }
                var readable = output.AsReadableBuffer();
                Assert.Equal(512 * 20, readable.Length);

                output.Append(readable);
                Assert.Equal(512 * 20, readable.Length);

                readable = output.AsReadableBuffer();
                Assert.Equal(2 * 512 * 20, readable.Length);

                await output.FlushAsync();
            }
        }
        public async Task WriteLargeDataBinary(int length)
        {
            byte[] data = new byte[length];
            new Random(length).NextBytes(data);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Write(data);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();

                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();

                    var input = result.Buffer;
                    if (input.Length == 0)
                    {
                        break;
                    }

                    Assert.True(input.Equals(new Span <byte>(data, offset, input.Length)));
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
        public async Task WriteLargeDataTextUtf8(int length)
        {
            string data = new string('#', length);

            FillRandomStringData(data, length);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Append(data, TextEncoding.Utf8);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();

                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();

                    var input = result.Buffer;
                    if (input.Length == 0)
                    {
                        break;
                    }

                    string s = ReadableBufferExtensions.GetUtf8String(input);
                    Assert.Equal(data.Substring(offset, input.Length), s);
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
Exemplo n.º 4
0
        public async Task WriteLargeDataBinary(int length)
        {
            byte[] data = new byte[length];
            new Random(length).NextBytes(data);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Write(data);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();
                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();
                    var input = result.Buffer;
                    if (input.Length == 0) break;

                    Assert.True(input.Equals(new Span<byte>(data, offset, input.Length)));
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
Exemplo n.º 5
0
 public async Task CanWriteNothingToBuffer()
 {
     using (var memoryPool = new MemoryPool())
     {
         var readerWriter = new PipelineReaderWriter(memoryPool);
         var buffer = readerWriter.Alloc();
         buffer.Advance(0); // doing nothing, the hard way
         await buffer.FlushAsync();
     }
 }
 public async Task CanWriteNothingToBuffer()
 {
     using (var memoryPool = new MemoryPool())
     {
         var readerWriter = new PipelineReaderWriter(memoryPool);
         var buffer       = readerWriter.Alloc();
         buffer.Advance(0); // doing nothing, the hard way
         await buffer.FlushAsync();
     }
 }
Exemplo n.º 7
0
        public UvTcpConnection(UvThread thread, UvTcpHandle handle)
        {
            _thread = thread;
            _handle = handle;

            _input  = _thread.PipelineFactory.Create();
            _output = _thread.PipelineFactory.Create();

            StartReading();
            _sendingTask = ProcessWrites();
        }
Exemplo n.º 8
0
        public UvTcpConnection(UvThread thread, UvTcpHandle handle)
        {
            _thread = thread;
            _handle = handle;

            _input = _thread.PipelineFactory.Create();
            _output = _thread.PipelineFactory.Create();

            StartReading();
            _sendingTask = ProcessWrites();
        }
        public async Task CanAppendSelfWhileEmpty()
        { // not really an expectation; just an accepted caveat
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output   = readerWriter.Alloc();
                var readable = output.AsReadableBuffer();
                output.Append(readable);
                Assert.Equal(0, output.AsReadableBuffer().Length);

                await output.FlushAsync();
            }
        }
Exemplo n.º 10
0
        public async Task CanWriteUInt64ToBuffer(ulong value, string valueAsString)
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);
                var buffer = readerWriter.Alloc();
                buffer.Append(value, EncodingData.InvariantUtf8);
                await buffer.FlushAsync();

                var result = await readerWriter.ReadAsync();
                var inputBuffer = result.Buffer;

                Assert.Equal(valueAsString, inputBuffer.GetUtf8String());
            }
        }
Exemplo n.º 11
0
        public async Task CanWriteUInt64ToBuffer(ulong value, string valueAsString)
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);
                var buffer       = readerWriter.Alloc();
                buffer.Append(value, EncodingData.InvariantUtf8);
                await buffer.FlushAsync();

                var result = await readerWriter.ReadAsync();

                var inputBuffer = result.Buffer;

                Assert.Equal(valueAsString, inputBuffer.GetUtf8String());
            }
        }
Exemplo n.º 12
0
        internal RioTcpConnection(IntPtr socket, long connectionId, IntPtr requestQueue, RioThread rioThread, RegisteredIO rio)
        {
            _socket       = socket;
            _connectionId = connectionId;
            _rio          = rio;
            _rioThread    = rioThread;

            _input  = rioThread.PipelineFactory.Create();
            _output = rioThread.PipelineFactory.Create();

            _requestQueue = requestQueue;

            rioThread.AddConnection(connectionId, this);

            ProcessReceives();
            _sendTask = ProcessSends();
        }
Exemplo n.º 13
0
        internal RioTcpConnection(IntPtr socket, long connectionId, IntPtr requestQueue, RioThread rioThread, RegisteredIO rio)
        {
            _socket = socket;
            _connectionId = connectionId;
            _rio = rio;
            _rioThread = rioThread;

            _input = rioThread.PipelineFactory.Create();
            _output = rioThread.PipelineFactory.Create();

            _requestQueue = requestQueue;

            rioThread.AddConnection(connectionId, this);

            ProcessReceives();
            _sendTask = ProcessSends();
        }
Exemplo n.º 14
0
        internal SocketConnection(Socket socket, PipelineFactory factory)
        {
            socket.NoDelay = true;
            _socket = socket;
            if (factory == null)
            {
                _ownsFactory = true;
                factory = new PipelineFactory();
            }
            _factory = factory;

            _input = PipelineFactory.Create();
            _output = PipelineFactory.Create();

            ShutdownSocketWhenWritingCompletedAsync();
            ReceiveFromSocketAndPushToWriterAsync();
            ReadFromReaderAndWriteToSocketAsync();
        }
Exemplo n.º 15
0
        internal SocketConnection(Socket socket, PipelineFactory factory)
        {
            socket.NoDelay = true;
            _socket        = socket;
            if (factory == null)
            {
                _ownsFactory = true;
                factory      = new PipelineFactory();
            }
            _factory = factory;

            _input  = PipelineFactory.Create();
            _output = PipelineFactory.Create();

            ShutdownSocketWhenWritingCompletedAsync();
            ReceiveFromSocketAndPushToWriterAsync();
            ReadFromReaderAndWriteToSocketAsync();
        }
Exemplo n.º 16
0
        public void CanReReadDataThatHasNotBeenCommitted_LargeData()
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();

                byte[]    predictablyGibberish = new byte[512];
                const int SEED   = 1235412;
                Random    random = new Random(SEED);
                for (int i = 0; i < 50; i++)
                {
                    for (int j = 0; j < predictablyGibberish.Length; j++)
                    {
                        // doing it this way to be 100% sure about repeating the PRNG order
                        predictablyGibberish[j] = (byte)random.Next(0, 256);
                    }
                    output.Write(predictablyGibberish);
                }

                var readable = output.AsReadableBuffer();
                Assert.False(readable.IsSingleSpan);
                Assert.False(readable.IsEmpty);
                Assert.Equal(50 * 512, readable.Length);

                random = new Random(SEED);
                int correctCount = 0;
                foreach (var memory in readable)
                {
                    var span = memory.Span;
                    for (int i = 0; i < span.Length; i++)
                    {
                        if (span[i] == (byte)random.Next(0, 256))
                        {
                            correctCount++;
                        }
                    }
                }
                Assert.Equal(50 * 512, correctCount);
            }
        }
Exemplo n.º 17
0
        public void CanReReadDataThatHasNotBeenCommitted_SmallData()
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);
                var output       = readerWriter.Alloc();

                Assert.True(output.AsReadableBuffer().IsEmpty);
                Assert.Equal(0, output.AsReadableBuffer().Length);


                output.Append("hello world", TextEncoding.Utf8);
                var readable = output.AsReadableBuffer();

                // check that looks about right
                Assert.False(readable.IsEmpty);
                Assert.Equal(11, readable.Length);
                Assert.True(readable.Equals(Encoding.UTF8.GetBytes("hello world")));
                Assert.True(readable.Slice(1, 3).Equals(Encoding.UTF8.GetBytes("ell")));

                // check it all works after we write more
                output.Append("more data", TextEncoding.Utf8);

                // note that the snapshotted readable should not have changed by this
                Assert.False(readable.IsEmpty);
                Assert.Equal(11, readable.Length);
                Assert.True(readable.Equals(Encoding.UTF8.GetBytes("hello world")));
                Assert.True(readable.Slice(1, 3).Equals(Encoding.UTF8.GetBytes("ell")));

                // if we fetch it again, we can see everything
                readable = output.AsReadableBuffer();
                Assert.False(readable.IsEmpty);
                Assert.Equal(20, readable.Length);
                Assert.True(readable.Equals(Encoding.UTF8.GetBytes("hello worldmore data")));
            }
        }
Exemplo n.º 18
0
 public FileReader(PipelineReaderWriter input) : base(input)
 {
 }
Exemplo n.º 19
0
        public void CanReReadDataThatHasNotBeenCommitted_SmallData()
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);
                var output = readerWriter.Alloc();

                Assert.True(output.AsReadableBuffer().IsEmpty);
                Assert.Equal(0, output.AsReadableBuffer().Length);


                output.Append("hello world", TextEncoding.Utf8);
                var readable = output.AsReadableBuffer();

                // check that looks about right
                Assert.False(readable.IsEmpty);
                Assert.Equal(11, readable.Length);
                Assert.True(readable.Equals(Encoding.UTF8.GetBytes("hello world")));
                Assert.True(readable.Slice(1, 3).Equals(Encoding.UTF8.GetBytes("ell")));

                // check it all works after we write more
                output.Append("more data", TextEncoding.Utf8);

                // note that the snapshotted readable should not have changed by this
                Assert.False(readable.IsEmpty);
                Assert.Equal(11, readable.Length);
                Assert.True(readable.Equals(Encoding.UTF8.GetBytes("hello world")));
                Assert.True(readable.Slice(1, 3).Equals(Encoding.UTF8.GetBytes("ell")));

                // if we fetch it again, we can see everything
                readable = output.AsReadableBuffer();
                Assert.False(readable.IsEmpty);
                Assert.Equal(20, readable.Length);
                Assert.True(readable.Equals(Encoding.UTF8.GetBytes("hello worldmore data")));
            }
        }
Exemplo n.º 20
0
        public void CanReReadDataThatHasNotBeenCommitted_LargeData()
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();

                byte[] predictablyGibberish = new byte[512];
                const int SEED = 1235412;
                Random random = new Random(SEED);
                for (int i = 0; i < 50; i++)
                {
                    for (int j = 0; j < predictablyGibberish.Length; j++)
                    {
                        // doing it this way to be 100% sure about repeating the PRNG order
                        predictablyGibberish[j] = (byte)random.Next(0, 256);
                    }
                    output.Write(predictablyGibberish);
                }

                var readable = output.AsReadableBuffer();
                Assert.False(readable.IsSingleSpan);
                Assert.False(readable.IsEmpty);
                Assert.Equal(50 * 512, readable.Length);

                random = new Random(SEED);
                int correctCount = 0;
                foreach (var memory in readable)
                {
                    var span = memory.Span;
                    for (int i = 0; i < span.Length; i++)
                    {
                        if (span[i] == (byte)random.Next(0, 256)) correctCount++;
                    }
                }
                Assert.Equal(50 * 512, correctCount);
            }
        }
Exemplo n.º 21
0
        public async Task WriteLargeDataTextUtf8(int length)
        {
            string data = new string('#', length);
            FillRandomStringData(data, length);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Append(data, TextEncoding.Utf8);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();
                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();
                    var input = result.Buffer;
                    if (input.Length == 0) break;

                    string s = ReadableBufferExtensions.GetUtf8String(input);
                    Assert.Equal(data.Substring(offset, input.Length), s);
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
Exemplo n.º 22
0
        public async Task CanAppendSelfWhileEmpty()
        { // not really an expectation; just an accepted caveat
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                var readable = output.AsReadableBuffer();
                output.Append(readable);
                Assert.Equal(0, output.AsReadableBuffer().Length);

                await output.FlushAsync();
            }
        }
Exemplo n.º 23
0
 public FileReader(PipelineReaderWriter input) : base(input)
 {
 }
Exemplo n.º 24
0
        public async Task CanAppendSelfWhileNotEmpty()
        {
            byte[] chunk = new byte[512];
            new Random().NextBytes(chunk);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();

                for (int i = 0; i < 20; i++)
                {
                    output.Write(chunk);
                }
                var readable = output.AsReadableBuffer();
                Assert.Equal(512 * 20, readable.Length);

                output.Append(readable);
                Assert.Equal(512 * 20, readable.Length);

                readable = output.AsReadableBuffer();
                Assert.Equal(2 * 512 * 20, readable.Length);

                await output.FlushAsync();
            }
        }