Ejemplo n.º 1
0
 public PooledMemory(IMemoryOwner <byte> owner, TestMemoryPool pool)
 {
     _owner          = owner;
     _pool           = pool;
     _leaser         = Environment.StackTrace;
     _referenceCount = 1;
 }
Ejemplo n.º 2
0
        public async Task DefaultReaderSchedulerRunsInline()
        {
            using (var pool = new TestMemoryPool())
            {
                var pipe = new Pipe(new PipeOptions(pool));

                var id = 0;

                Func <Task> doRead = async() => {
                    ReadResult result = await pipe.Reader.ReadAsync();

                    Assert.Equal(Thread.CurrentThread.ManagedThreadId, id);

                    pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                    pipe.Reader.Complete();
                };

                Task reading = doRead();

                id = Thread.CurrentThread.ManagedThreadId;

                PipeWriter buffer = pipe.Writer;
                buffer.Write(Encoding.UTF8.GetBytes("Hello World"));
                await buffer.FlushAsync();

                pipe.Writer.Complete();

                await reading;
            }
        }
        public async Task ReadAsyncCallbackRunsOnReaderScheduler()
        {
            using (var pool = new TestMemoryPool())
            {
                using (var scheduler = new ThreadScheduler())
                {
                    var pipe = new Pipe(new PipeOptions(pool, scheduler, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));

                    Func <Task> doRead = async() =>
                    {
                        Assert.False(Thread.CurrentThread.IsThreadPoolThread, "We started on the thread pool");

                        ReadResult result = await pipe.Reader.ReadAsync();

                        Assert.Equal(Thread.CurrentThread.ManagedThreadId, scheduler.Thread.ManagedThreadId);

                        pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                        pipe.Reader.Complete();
                    };

                    Task reading = ExecuteOnNonThreadPoolThread(doRead);

                    PipeWriter buffer = pipe.Writer;
                    buffer.Write(Encoding.UTF8.GetBytes("Hello World"));
                    await buffer.FlushAsync();

                    await reading;
                }
            }
        }
Ejemplo n.º 4
0
        public async Task MultiSegmentWritesUntilFailure()
        {
            using (var pool = new TestMemoryPool())
            {
                var pipe = new Pipe(s_testOptions);
                pipe.Writer.WriteEmpty(pool.MaxBufferSize);
                pipe.Writer.WriteEmpty(pool.MaxBufferSize);
                pipe.Writer.WriteEmpty(pool.MaxBufferSize);
                await pipe.Writer.FlushAsync();

                pipe.Writer.Complete();

                var stream = new ThrowAfterNWritesStream(2);
                try
                {
                    await pipe.Reader.CopyToAsync(stream);

                    Assert.True(false, $"CopyToAsync should have failed, wrote {stream.Writes} times.");
                }
                catch (InvalidOperationException)
                {
                }

                Assert.Equal(2, stream.Writes);

                ReadResult result = await pipe.Reader.ReadAsync();

                Assert.Equal(4096, result.Buffer.Length);
                pipe.Reader.Complete();
            }
        }
Ejemplo n.º 5
0
        public async Task ReadAsyncCallbackRunsOnReaderScheduler()
        {
            using (var pool = new TestMemoryPool())
            {
                using (var scheduler = new ThreadScheduler())
                {
                    var pipe = new Pipe(new PipeOptions(pool, scheduler));

                    Func <Task> doRead = async() => {
                        int oid = Thread.CurrentThread.ManagedThreadId;

                        ReadResult result = await pipe.Reader.ReadAsync();

                        Assert.NotEqual(oid, Thread.CurrentThread.ManagedThreadId);

                        Assert.Equal(Thread.CurrentThread.ManagedThreadId, scheduler.Thread.ManagedThreadId);

                        pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                        pipe.Reader.Complete();
                    };

                    Task reading = doRead();

                    PipeWriter buffer = pipe.Writer;
                    buffer.Write(Encoding.UTF8.GetBytes("Hello World"));
                    await buffer.FlushAsync();

                    await reading;
                }
            }
        }
Ejemplo n.º 6
0
 protected StreamPipeTest()
 {
     Pool   = new TestMemoryPool();
     Stream = new MemoryStream();
     Writer = new StreamPipeWriter(Stream, MinimumSegmentSize, Pool);
     Reader = new StreamPipeReader(Stream, new StreamPipeReaderOptions(MinimumSegmentSize, minimumReadThreshold: 256, Pool));
 }
Ejemplo n.º 7
0
 public void StreamPipeWriterOptions_Ctor_Roundtrip()
 {
     using (var pool = new TestMemoryPool())
     {
         var options = new StreamPipeWriterOptions(pool: pool, minimumBufferSize: 1234, leaveOpen: true);
         Assert.Same(pool, options.Pool);
         Assert.Equal(1234, options.MinimumBufferSize);
         Assert.True(options.LeaveOpen);
     }
 }
        public async Task DefaultWriterSchedulerRunsOnSynchronizationContext()
        {
            SynchronizationContext previous = SynchronizationContext.Current;
            var sc = new CustomSynchronizationContext();

            try
            {
                SynchronizationContext.SetSynchronizationContext(sc);

                using (var pool = new TestMemoryPool())
                {
                    var pipe = new Pipe(
                        new PipeOptions(
                            pool,
                            resumeWriterThreshold: 32,
                            pauseWriterThreshold: 64
                            ));

                    Func <Task> doWrite = async() =>
                    {
                        PipeWriter writableBuffer          = pipe.Writer.WriteEmpty(64);
                        ValueTask <FlushResult> flushAsync = writableBuffer.FlushAsync();

                        Assert.False(flushAsync.IsCompleted);

                        await flushAsync;

                        pipe.Writer.Complete();

                        Assert.Same(SynchronizationContext.Current, sc);
                    };

                    Task writing = doWrite();

                    // Don't run on our bogus sync context
                    ReadResult result = await pipe.Reader.ReadAsync().ConfigureAwait(false);

                    pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                    Assert.Equal(1, sc.Callbacks.Count);
                    sc.Callbacks[0].Item1(sc.Callbacks[0].Item2);

                    pipe.Reader.Complete();

                    // Don't run on our bogus sync context
                    await writing.ConfigureAwait(false);
                }
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(previous);
            }
        }
Ejemplo n.º 9
0
        public async Task FlushCallbackRunsOnWriterScheduler()
        {
            using (var pool = new TestMemoryPool())
            {
                using (var scheduler = new ThreadScheduler())
                {
                    var pipe = new Pipe(
                        new PipeOptions(
                            pool,
                            resumeWriterThreshold: 32,
                            pauseWriterThreshold: 64,
                            readerScheduler: PipeScheduler.Inline,
                            writerScheduler: scheduler,
                            useSynchronizationContext: false));

                    PipeWriter writableBuffer          = pipe.Writer.WriteEmpty(64);
                    ValueTask <FlushResult> flushAsync = writableBuffer.FlushAsync();

                    Assert.False(flushAsync.IsCompleted);

                    Func <Task> doWrite = async() =>
                    {
                        int oid = Thread.CurrentThread.ManagedThreadId;

                        Assert.False(Thread.CurrentThread.IsThreadPoolThread, "We started on the thread pool");

                        await flushAsync;

                        Assert.NotEqual(oid, Thread.CurrentThread.ManagedThreadId);

                        pipe.Writer.Complete();

                        Assert.Equal(Thread.CurrentThread.ManagedThreadId, scheduler.Thread.ManagedThreadId);
                    };

                    Task writing = ExecuteOnNonThreadPoolThread(doWrite);

                    ReadResult result = await pipe.Reader.ReadAsync();

                    pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                    pipe.Reader.Complete();

                    await writing;
                }
            }
        }
Ejemplo n.º 10
0
        public async Task FlushCallbackRunsOnWriterScheduler()
        {
            using (var pool = new TestMemoryPool())
            {
                using (var scheduler = new ThreadScheduler())
                {
                    var pipe = new Pipe(
                        new PipeOptions(
                            pool,
                            resumeWriterThreshold: 32,
                            pauseWriterThreshold: 64,
                            writerScheduler: scheduler));

                    PipeWriter writableBuffer            = pipe.Writer.WriteEmpty(64);
                    PipeAwaiter <FlushResult> flushAsync = writableBuffer.FlushAsync();

                    Assert.False(flushAsync.IsCompleted);

                    Func <Task> doWrite = async() => {
                        int oid = Thread.CurrentThread.ManagedThreadId;

                        await flushAsync;

                        Assert.NotEqual(oid, Thread.CurrentThread.ManagedThreadId);

                        pipe.Writer.Complete();

                        Assert.Equal(Thread.CurrentThread.ManagedThreadId, scheduler.Thread.ManagedThreadId);
                    };

                    Task writing = doWrite();

                    ReadResult result = await pipe.Reader.ReadAsync();

                    pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                    pipe.Reader.Complete();

                    await writing;
                }
            }
        }
        public async Task MultiSegmentWritesWorks()
        {
            using (var pool = new TestMemoryPool())
            {
                var pipe = new Pipe(new PipeOptions(pool: pool, readerScheduler: PipeScheduler.Inline));
                pipe.Writer.WriteEmpty(4096);
                pipe.Writer.WriteEmpty(4096);
                pipe.Writer.WriteEmpty(4096);
                await pipe.Writer.FlushAsync();

                pipe.Writer.Complete();

                var stream = new MemoryStream();
                await pipe.Reader.CopyToAsync(stream);

                pipe.Reader.Complete();

                Assert.Equal(4096 * 3, stream.Length);
            }
        }
Ejemplo n.º 12
0
        public async Task ReadMultipleTimesAdvanceFreesAppropriately()
        {
            var pool = new TestMemoryPool();

            CreateReader(memoryPool: pool);

            WriteByteArray(2000);

            for (var i = 0; i < 99; i++)
            {
                var readResult = await Reader.ReadAsync();

                Reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
            }

            var result = await Reader.ReadAsync();

            Reader.AdvanceTo(result.Buffer.End);
            Assert.Equal(1, pool.GetRentCount());
        }
        public async Task MultiSegmentWritesUntilFailure()
        {
            using (var pool = new TestMemoryPool())
            {
                var pipe = new Pipe(new PipeOptions(pool: pool, readerScheduler: PipeScheduler.Inline));
                pipe.Writer.WriteEmpty(4096);
                pipe.Writer.WriteEmpty(4096);
                pipe.Writer.WriteEmpty(4096);
                await pipe.Writer.FlushAsync();

                pipe.Writer.Complete();

                var stream = new ThrowAfterNWritesStream(2);
                await Assert.ThrowsAsync <InvalidOperationException>(() => pipe.Reader.CopyToAsync(stream));

                ReadResult result = await pipe.Reader.ReadAsync();

                Assert.Equal(4096, result.Buffer.Length);
                pipe.Reader.Complete();
            }
        }
Ejemplo n.º 14
0
        public async Task DefaultWriterSchedulerRunsOnThreadPool()
        {
            using (var pool = new TestMemoryPool())
            {
                var pipe = new Pipe(
                    new PipeOptions(
                        pool,
                        resumeWriterThreshold: 32,
                        pauseWriterThreshold: 64
                        ));

                PipeWriter writableBuffer            = pipe.Writer.WriteEmpty(64);
                PipeAwaiter <FlushResult> flushAsync = writableBuffer.FlushAsync();

                Assert.False(flushAsync.IsCompleted);

                var id = 0;

                Func <Task> doWrite = async() =>
                {
                    await flushAsync;

                    pipe.Writer.Complete();

                    Assert.True(Thread.CurrentThread.IsThreadPoolThread);
                };

                Task writing = doWrite();

                ReadResult result = await pipe.Reader.ReadAsync();

                id = Thread.CurrentThread.ManagedThreadId;

                pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                pipe.Reader.Complete();

                await writing;
            }
        }
        public async Task DefaultWriterSchedulerRunsOnThreadPool()
        {
            using (var pool = new TestMemoryPool())
            {
                var pipe = new Pipe(
                    new PipeOptions(
                        pool,
                        resumeWriterThreshold: 32,
                        pauseWriterThreshold: 64,
                        useSynchronizationContext: false
                        ));

                Func <Task> doWrite = async() =>
                {
                    Assert.False(Thread.CurrentThread.IsThreadPoolThread, "We started on the thread pool");

                    PipeWriter writableBuffer          = pipe.Writer.WriteEmpty(64);
                    ValueTask <FlushResult> flushAsync = writableBuffer.FlushAsync();

                    Assert.False(flushAsync.IsCompleted);

                    await flushAsync;

                    pipe.Writer.Complete();

                    Assert.True(Thread.CurrentThread.IsThreadPoolThread);
                };

                Task writing = ExecuteOnNonThreadPoolThread(doWrite);

                ReadResult result = await pipe.Reader.ReadAsync();

                pipe.Reader.AdvanceTo(result.Buffer.End, result.Buffer.End);

                pipe.Reader.Complete();

                await writing;
            }
        }
Ejemplo n.º 16
0
 public PipeResetTests()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool));
 }
Ejemplo n.º 17
0
 public BackpressureTests()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool, resumeWriterThreshold: ResumeWriterThreshold, pauseWriterThreshold: PauseWriterThreshold, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));
 }
Ejemplo n.º 18
0
 public BackpressureTests()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool, resumeWriterThreshold: 32, pauseWriterThreshold: 64));
 }
Ejemplo n.º 19
0
 public PipelineReaderWriterFacts()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));
 }
Ejemplo n.º 20
0
 public PipeCompletionCallbacksTests()
 {
     _pool = new TestMemoryPool();
 }
Ejemplo n.º 21
0
 public PipeLengthTests()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline, pauseWriterThreshold: 0, resumeWriterThreshold: 0, useSynchronizationContext: false));
 }
Ejemplo n.º 22
0
 public PipelineReaderWriterFacts()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool));
 }
Ejemplo n.º 23
0
 public BackpressureTests()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool, resumeWriterThreshold: 32, pauseWriterThreshold: 64, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline));
 }
Ejemplo n.º 24
0
 public PipeResetTests()
 {
     _pool = new TestMemoryPool();
     _pipe = new Pipe(new PipeOptions(_pool, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline));
 }