public void RevolvingBuffers_InlineAsyncDisposalInteraction()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            // Reader waits for a chunk of data
            Task read1 = buffers.GetBufferedDataAsync();

            Assert.False(read1.IsCompleted, "read1.IsCompleted");

            // Writer writes some data
            buffers.CopyDataToBuffer(data, 0, data.Length);
            Assert.True(read1.IsCompleted, "read1.IsCompleted");

            // Writer is done writing data
            Task wait = buffers.WaitForBufferEmptyAsync();

            Assert.False(wait.IsCompleted, "All data isn't finished until the reader releases the last buffer");

            // Writer will dispose as soon as reading is complete
            wait.ContinueWith(task =>
            {
                buffers.Dispose();
            }, TaskContinuationOptions.ExecuteSynchronously);

            // Reader looks for more data
            Task <ArraySegment <char> > read2 = buffers.GetBufferedDataAsync();

            Assert.True(wait.IsCompleted, "Wait should be completed now");
            Assert.True(read2.IsCompleted, "Read should also return because buffers were disposed immediately");
            AssertWithMessage.Equal("", GetString(read2.Result), "read2.Result");
        }
        public void RevolvingBuffers_GetBufferedDataAsync_ReturnsBufferedData()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            buffers.CopyDataToBuffer(data, 0, data.Length);

            // Act
            Task <ArraySegment <char> > task = buffers.GetBufferedDataAsync();

            // Assert
            TaskAssert.Completed(task, "Task should be completed immediately.");
            Assert.Equal("Hello, world!", GetString(task.Result));
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_ReturnsAfterBufferIsReleasedAsync()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            buffers.CopyDataToBuffer(data, 0, 2);
            buffers.GetBufferedData(); // Read the buffer
            Task task = buffers.WaitForBufferEmptyAsync();

            // Act
            buffers.GetBufferedDataAsync(); // Release the buffer

            // Assert
            TaskAssert.Completed(task, "Task should be completed when buffer is released.");
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_WaitsWhileThereIsStillMoreDataToReadAsync()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            buffers.CopyDataToBuffer(data, 0, data.Length);
            buffers.GetBufferedData(); // Read the buffer
            buffers.CopyDataToBuffer(data, 0, data.Length);
            Task task = buffers.WaitForBufferEmptyAsync();

            // Act
            buffers.GetBufferedDataAsync(); // Release the buffer, read the next buffer

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed while there is still more data.");
        }
        public void RevolvingBuffers_GetBufferedDataAsync_ReturnsEmptyBufferWhenDisposed()
        {
            // Arrange
            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            // Act
            Task <ArraySegment <char> > task = buffers.GetBufferedDataAsync();

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed until data is buffered.");

            // Act
            buffers.Dispose();

            // Assert
            TaskAssert.Completed(task, "Task should be completed after buffers are disposed.");
            Assert.Equal("", GetString(task.Result));
        }
Example #6
0
        /// <remarks>
        /// This function is the pump that pushes data from the buffers into an
        /// HTTP connection. It asynchronously waits on data, then asynchronously
        /// waits while the data is sent, then waits on more data, etc.
        /// </remarks>
        private static async void SendDataFromBuffersAsync(RevolvingBuffers <byte> buffer, IHttpSocketAdapter adapter)
        {
            while (true)
            {
                ArraySegment <byte> bufferToSend = await buffer.GetBufferedDataAsync();

                if (bufferToSend.Count == 0)
                {
                    break;
                }

                await adapter.WriteToRequestAsync(bufferToSend.Array, bufferToSend.Offset, bufferToSend.Count);
            }

            await adapter.CompleteRequest();

            adapter.Dispose();
        }
        public void RevolvingBuffers_GetBufferedDataAsync_WaitsForBufferedData()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            // Act
            Task <ArraySegment <char> > task = buffers.GetBufferedDataAsync();

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed until data is buffered.");

            // Act
            buffers.CopyDataToBuffer(data, 0, data.Length);

            // Assert
            TaskAssert.Completed(task, "Task should be completed after data is buffered.");
            Assert.Equal("Hello, world!", GetString(task.Result));
        }