You've already forked linux-packaging-mono
Imported Upstream version 5.10.0.47
Former-commit-id: d0813289fa2d35e1f8ed77530acb4fb1df441bc0
This commit is contained in:
parent
88ff76fe28
commit
e46a49ecf1
@@ -29,7 +29,7 @@ namespace System.IO.Tests
|
||||
{
|
||||
tasks[i] = stream.WriteAsync(data, 250 * i, 250);
|
||||
}
|
||||
Assert.False(tasks.All(t => t.IsCompleted));
|
||||
Assert.All(tasks, t => Assert.Equal(TaskStatus.WaitingForActivation, t.Status));
|
||||
|
||||
mcaos.Release();
|
||||
await Task.WhenAll(tasks);
|
||||
@@ -54,8 +54,10 @@ namespace System.IO.Tests
|
||||
Assert.Equal(TaskStatus.Faulted, stream.FlushAsync().Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CopyToAsyncTest_RequiresAsyncFlushingOfWrites()
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task CopyToTest_RequiresFlushingOfWrites(bool copyAsynchronously)
|
||||
{
|
||||
byte[] data = Enumerable.Range(0, 1000).Select(i => (byte)(i % 256)).ToArray();
|
||||
|
||||
@@ -70,17 +72,27 @@ namespace System.IO.Tests
|
||||
src.WriteByte(42);
|
||||
dst.WriteByte(42);
|
||||
|
||||
Task copyTask = src.CopyToAsync(dst);
|
||||
manualReleaseStream.Release();
|
||||
await copyTask;
|
||||
if (copyAsynchronously)
|
||||
{
|
||||
Task copyTask = src.CopyToAsync(dst);
|
||||
manualReleaseStream.Release();
|
||||
await copyTask;
|
||||
}
|
||||
else
|
||||
{
|
||||
manualReleaseStream.Release();
|
||||
src.CopyTo(dst);
|
||||
}
|
||||
|
||||
Assert.Equal(data, dst.ToArray());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task CopyToAsyncTest_ReadBeforeCopy_CopiesAllData(bool wrappedStreamCanSeek)
|
||||
[InlineData(false, false)]
|
||||
[InlineData(false, true)]
|
||||
[InlineData(true, false)]
|
||||
[InlineData(true, true)]
|
||||
public async Task CopyToTest_ReadBeforeCopy_CopiesAllData(bool copyAsynchronously, bool wrappedStreamCanSeek)
|
||||
{
|
||||
byte[] data = Enumerable.Range(0, 1000).Select(i => (byte)(i % 256)).ToArray();
|
||||
|
||||
@@ -94,7 +106,14 @@ namespace System.IO.Tests
|
||||
src.ReadByte();
|
||||
|
||||
var dst = new MemoryStream();
|
||||
await src.CopyToAsync(dst);
|
||||
if (copyAsynchronously)
|
||||
{
|
||||
await src.CopyToAsync(dst);
|
||||
}
|
||||
else
|
||||
{
|
||||
src.CopyTo(dst);
|
||||
}
|
||||
|
||||
var expected = new byte[data.Length - 1];
|
||||
Array.Copy(data, 1, expected, 0, expected.Length);
|
||||
@@ -245,13 +264,22 @@ namespace System.IO.Tests
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ManuallyReleaseAsyncOperationsStream : MemoryStream
|
||||
internal sealed class ManuallyReleaseAsyncOperationsStream : Stream
|
||||
{
|
||||
private readonly MemoryStream _stream = new MemoryStream();
|
||||
private readonly TaskCompletionSource<bool> _tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private bool _canSeek = true;
|
||||
|
||||
public override bool CanSeek => _canSeek;
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position { get => _stream.Position; set => _stream.Position = value; }
|
||||
|
||||
public void SetCanSeek(bool canSeek) => _canSeek = canSeek;
|
||||
|
||||
public void Release() { _tcs.SetResult(true); }
|
||||
@@ -259,38 +287,44 @@ namespace System.IO.Tests
|
||||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
await _tcs.Task;
|
||||
return await base.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
return await _stream.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
await _tcs.Task;
|
||||
await base.WriteAsync(buffer, offset, count, cancellationToken);
|
||||
await _stream.WriteAsync(buffer, offset, count, cancellationToken);
|
||||
}
|
||||
|
||||
public override async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _tcs.Task;
|
||||
await base.FlushAsync(cancellationToken);
|
||||
await _stream.FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public override void Flush() => _stream.Flush();
|
||||
public override int Read(byte[] buffer, int offset, int count) => _stream.Read(buffer, offset, count);
|
||||
public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin);
|
||||
public override void SetLength(long value) => _stream.SetLength(value);
|
||||
public override void Write(byte[] buffer, int offset, int count) => _stream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
internal sealed class ThrowsExceptionFromAsyncOperationsStream : MemoryStream
|
||||
{
|
||||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
public override int Read(byte[] buffer, int offset, int count) =>
|
||||
throw new InvalidOperationException("Exception from ReadAsync");
|
||||
}
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
public override void Write(byte[] buffer, int offset, int count) =>
|
||||
throw new InvalidOperationException("Exception from ReadAsync");
|
||||
|
||||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
|
||||
throw new InvalidOperationException("Exception from ReadAsync");
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
|
||||
throw new InvalidOperationException("Exception from WriteAsync");
|
||||
}
|
||||
|
||||
public override Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
public override Task FlushAsync(CancellationToken cancellationToken) =>
|
||||
throw new InvalidOperationException("Exception from FlushAsync");
|
||||
}
|
||||
}
|
||||
|
||||
public class BufferedStream_NS17
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace System.IO.Tests
|
||||
@@ -23,5 +24,67 @@ namespace System.IO.Tests
|
||||
var bufferedStream = new BufferedStream(new MemoryStream(), 1234);
|
||||
Assert.Equal(1234, bufferedStream.BufferSize);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1, 1)]
|
||||
[InlineData(1, 2)]
|
||||
[InlineData(1024, 4096)]
|
||||
[InlineData(4096, 4097)]
|
||||
[InlineData(4096, 1)]
|
||||
[InlineData(2047, 4096)]
|
||||
public void ReadSpan_WriteSpan_AllDataCopied(int spanSize, int bufferSize)
|
||||
{
|
||||
byte[] data = new byte[80000];
|
||||
new Random(42).NextBytes(data);
|
||||
|
||||
var result = new MemoryStream();
|
||||
using (var output = new BufferedStream(result, bufferSize))
|
||||
using (var input = new BufferedStream(new MemoryStream(data), bufferSize))
|
||||
{
|
||||
Span<byte> span = new byte[spanSize];
|
||||
int bytesRead;
|
||||
while ((bytesRead = input.Read(span)) != 0)
|
||||
{
|
||||
output.Write(span.Slice(0, bytesRead));
|
||||
}
|
||||
}
|
||||
Assert.Equal(data, result.ToArray());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1, 1)]
|
||||
[InlineData(1, 2)]
|
||||
[InlineData(1024, 4096)]
|
||||
[InlineData(4096, 4097)]
|
||||
[InlineData(4096, 1)]
|
||||
[InlineData(2047, 4096)]
|
||||
public async Task ReadMemory_WriteMemory_AllDataCopied(int spanSize, int bufferSize)
|
||||
{
|
||||
byte[] data = new byte[80000];
|
||||
new Random(42).NextBytes(data);
|
||||
|
||||
var result = new MemoryStream();
|
||||
using (var output = new BufferedStream(result, bufferSize))
|
||||
using (var input = new BufferedStream(new MemoryStream(data), bufferSize))
|
||||
{
|
||||
Memory<byte> memory = new byte[spanSize];
|
||||
int bytesRead;
|
||||
while ((bytesRead = await input.ReadAsync(memory)) != 0)
|
||||
{
|
||||
await output.WriteAsync(memory.Slice(0, bytesRead));
|
||||
}
|
||||
}
|
||||
Assert.Equal(data, result.ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadWriteMemory_Precanceled_Throws()
|
||||
{
|
||||
using (var bs = new BufferedStream(new MemoryStream()))
|
||||
{
|
||||
Assert.Equal(TaskStatus.Canceled, bs.ReadAsync(new byte[1], new CancellationToken(true)).AsTask().Status);
|
||||
Assert.Equal(TaskStatus.Canceled, bs.WriteAsync(new byte[1], new CancellationToken(true)).Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user