Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change RandomAccess.Write* methods to perform complete writes #55490

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,17 @@ static async Task Validate(SafeFileHandle handle, FileOptions options, bool[] sy
{
writeBuffer[0] = (byte)fileOffset;

Assert.Equal(writeBuffer.Length, syncWrite ? RandomAccess.Write(handle, writeBuffer, fileOffset) : await RandomAccess.WriteAsync(handle, writeBuffer, fileOffset));
if (syncWrite)
{
RandomAccess.Write(handle, writeBuffer, fileOffset);
}
else
{
await RandomAccess.WriteAsync(handle, writeBuffer, fileOffset);
}

Assert.Equal(writeBuffer.Length, syncRead ? RandomAccess.Read(handle, readBuffer, fileOffset) : await RandomAccess.ReadAsync(handle, readBuffer, fileOffset));

Assert.Equal(writeBuffer[0], readBuffer[0]);

fileOffset += 1;
Expand Down Expand Up @@ -116,8 +125,17 @@ static async Task Validate(SafeFileHandle handle, FileOptions options, bool[] sy
writeBuffer_1[0] = (byte)fileOffset;
writeBuffer_2[0] = (byte)(fileOffset+1);

Assert.Equal(writeBuffer_1.Length + writeBuffer_2.Length, syncWrite ? RandomAccess.Write(handle, writeBuffers, fileOffset) : await RandomAccess.WriteAsync(handle, writeBuffers, fileOffset));
if (syncWrite)
{
RandomAccess.Write(handle, writeBuffers, fileOffset);
}
else
{
await RandomAccess.WriteAsync(handle, writeBuffers, fileOffset);
}

Assert.Equal(writeBuffer_1.Length + writeBuffer_2.Length, syncRead ? RandomAccess.Read(handle, readBuffers, fileOffset) : await RandomAccess.ReadAsync(handle, readBuffers, fileOffset));

Assert.Equal(writeBuffer_1[0], readBuffer_1[0]);
Assert.Equal(writeBuffer_2[0], readBuffer_2[0]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,16 @@ public async Task WriteUsingSingleBuffer(bool async)
int take = Math.Min(content.Length - total, bufferSize);
content.AsSpan(total, take).CopyTo(buffer.GetSpan());

total += async
? await RandomAccess.WriteAsync(handle, buffer.Memory, fileOffset: total)
: RandomAccess.Write(handle, buffer.GetSpan(), fileOffset: total);
if (async)
{
await RandomAccess.WriteAsync(handle, buffer.Memory, fileOffset: total);
}
else
{
RandomAccess.Write(handle, buffer.GetSpan(), fileOffset: total);
}

total += buffer.Memory.Length;
}
}

Expand Down Expand Up @@ -154,9 +161,16 @@ public async Task WriteAsyncUsingMultipleBuffers(bool async)
content.AsSpan((int)total, bufferSize).CopyTo(buffer_1.GetSpan());
content.AsSpan((int)total + bufferSize, bufferSize).CopyTo(buffer_2.GetSpan());

total += async
? await RandomAccess.WriteAsync(handle, buffers, fileOffset: total)
: RandomAccess.Write(handle, buffers, fileOffset: total);
if (async)
{
await RandomAccess.WriteAsync(handle, buffers, fileOffset: total);
}
else
{
RandomAccess.Write(handle, buffers, fileOffset: total);
}

total += buffer_1.Memory.Length + buffer_2.Memory.Length;
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/libraries/System.IO.FileSystem/tests/RandomAccess/Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ namespace System.IO.Tests
public class RandomAccess_Write : RandomAccess_Base<int>
{
protected override int MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
=> RandomAccess.Write(handle, bytes, fileOffset);
{
RandomAccess.Write(handle, bytes, fileOffset);
return bytes?.Length ?? 0;
}

[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
Expand All @@ -24,11 +27,11 @@ public void ThrowsOnReadAccess(FileOptions options)

[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
public void WriteUsingEmptyBufferReturnsZero(FileOptions options)
public void WriteUsingEmptyBufferReturns(FileOptions options)
{
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(0, RandomAccess.Write(handle, Array.Empty<byte>(), fileOffset: 0));
RandomAccess.Write(handle, Array.Empty<byte>(), fileOffset: 0);
}
}

Expand All @@ -41,7 +44,7 @@ public void CanUseStackAllocatedMemory(FileOptions options)

using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(stackAllocated.Length, RandomAccess.Write(handle, stackAllocated, fileOffset: 0));
RandomAccess.Write(handle, stackAllocated, fileOffset: 0);
}

Assert.Equal(stackAllocated.ToArray(), File.ReadAllBytes(filePath));
Expand All @@ -58,17 +61,14 @@ public void WritesBytesFromGivenBufferToGivenFileAtGivenOffset(FileOptions optio
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, options))
{
int total = 0;
int current = 0;

while (total != fileSize)
{
Span<byte> buffer = content.AsSpan(total, Math.Min(content.Length - total, fileSize / 4));

current = RandomAccess.Write(handle, buffer, fileOffset: total);

Assert.InRange(current, 0, buffer.Length);
RandomAccess.Write(handle, buffer, fileOffset: total);

total += current;
total += buffer.Length;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace System.IO.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[SkipOnPlatform(TestPlatforms.Browser, "async file IO is not supported on browser")]
public class RandomAccess_WriteAsync : RandomAccess_Base<ValueTask<int>>
public class RandomAccess_WriteAsync : RandomAccess_Base<ValueTask>
{
protected override ValueTask<int> MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
protected override ValueTask MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
=> RandomAccess.WriteAsync(handle, bytes, fileOffset);

[Theory]
Expand Down Expand Up @@ -44,11 +44,11 @@ public async Task ThrowsOnReadAccess(FileOptions options)

[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
public async Task WriteUsingEmptyBufferReturnsZeroAsync(FileOptions options)
public async Task WriteUsingEmptyBufferReturnsAsync(FileOptions options)
{
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(0, await RandomAccess.WriteAsync(handle, Array.Empty<byte>(), fileOffset: 0));
await RandomAccess.WriteAsync(handle, Array.Empty<byte>(), fileOffset: 0);
}
}

Expand All @@ -63,17 +63,14 @@ public async Task WritesBytesFromGivenBufferToGivenFileAtGivenOffsetAsync(FileOp
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, options))
{
int total = 0;
int current = 0;

while (total != fileSize)
{
Memory<byte> buffer = content.AsMemory(total, Math.Min(content.Length - total, fileSize / 4));

current = await RandomAccess.WriteAsync(handle, buffer, fileOffset: total);
await RandomAccess.WriteAsync(handle, buffer, fileOffset: total);

Assert.InRange(current, 0, buffer.Length);

total += current;
total += buffer.Length;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ namespace System.IO.Tests
public class RandomAccess_WriteGather : RandomAccess_Base<long>
{
protected override long MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
=> RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { bytes }, fileOffset);
{
RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { bytes }, fileOffset);
return bytes?.Length ?? 0;
}

[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
Expand All @@ -36,11 +39,11 @@ public void ThrowsOnReadAccess(FileOptions options)

[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
public void WriteUsingEmptyBufferReturnsZero(FileOptions options)
public void WriteUsingEmptyBufferReturns(FileOptions options)
{
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(0, RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0));
RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0);
}
}

Expand All @@ -55,15 +58,14 @@ public void WritesBytesFromGivenBuffersToGivenFileAtGivenOffset(FileOptions opti
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, options))
{
long total = 0;
long current = 0;

while (total != fileSize)
{
int firstBufferLength = (int)Math.Min(content.Length - total, fileSize / 4);
Memory<byte> buffer_1 = content.AsMemory((int)total, firstBufferLength);
Memory<byte> buffer_2 = content.AsMemory((int)total + firstBufferLength);

current = RandomAccess.Write(
RandomAccess.Write(
handle,
new ReadOnlyMemory<byte>[]
{
Expand All @@ -73,9 +75,7 @@ public void WritesBytesFromGivenBuffersToGivenFileAtGivenOffset(FileOptions opti
},
fileOffset: total);

Assert.InRange(current, 0, buffer_1.Length + buffer_2.Length);

total += current;
total += buffer_1.Length + buffer_2.Length;
}
}

Expand All @@ -94,7 +94,7 @@ public void DuplicatedBufferDuplicatesContent(FileOptions options)

using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(repeatCount, RandomAccess.Write(handle, buffers, fileOffset: 0));
RandomAccess.Write(handle, buffers, fileOffset: 0);
}

byte[] actualContent = File.ReadAllBytes(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace System.IO.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[SkipOnPlatform(TestPlatforms.Browser, "async file IO is not supported on browser")]
public class RandomAccess_WriteGatherAsync : RandomAccess_Base<ValueTask<long>>
public class RandomAccess_WriteGatherAsync : RandomAccess_Base<ValueTask>
{
protected override ValueTask<long> MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
protected override ValueTask MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
=> RandomAccess.WriteAsync(handle, new ReadOnlyMemory<byte>[] { bytes }, fileOffset);

[Theory]
Expand Down Expand Up @@ -56,11 +56,11 @@ public async Task ThrowsOnReadAccess(FileOptions options)

[Theory]
[MemberData(nameof(GetSyncAsyncOptions))]
public async Task WriteUsingEmptyBufferReturnsZeroAsync(FileOptions options)
public async Task WriteUsingEmptyBufferReturnsAsync(FileOptions options)
{
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(0, await RandomAccess.WriteAsync(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0));
await RandomAccess.WriteAsync(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0);
}
}

Expand All @@ -75,15 +75,14 @@ public async Task WritesBytesFromGivenBufferToGivenFileAtGivenOffsetAsync(FileOp
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, options))
{
long total = 0;
long current = 0;

while (total != fileSize)
{
int firstBufferLength = (int)Math.Min(content.Length - total, fileSize / 4);
Memory<byte> buffer_1 = content.AsMemory((int)total, firstBufferLength);
Memory<byte> buffer_2 = content.AsMemory((int)total + firstBufferLength);

current = await RandomAccess.WriteAsync(
await RandomAccess.WriteAsync(
handle,
new ReadOnlyMemory<byte>[]
{
Expand All @@ -93,9 +92,7 @@ public async Task WritesBytesFromGivenBufferToGivenFileAtGivenOffsetAsync(FileOp
},
fileOffset: total);

Assert.InRange(current, 0, buffer_1.Length + buffer_2.Length);

total += current;
total += buffer_1.Length + buffer_2.Length;
}
}

Expand All @@ -114,7 +111,7 @@ public async Task DuplicatedBufferDuplicatesContentAsync(FileOptions options)

using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options))
{
Assert.Equal(repeatCount, await RandomAccess.WriteAsync(handle, buffers, fileOffset: 0));
await RandomAccess.WriteAsync(handle, buffers, fileOffset: 0);
}

byte[] actualContent = File.ReadAllBytes(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ internal static Exception GetIOError(int errorCode, string? path)

public ValueTaskSourceStatus GetStatus(short token) => _source.GetStatus(token);
public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _source.OnCompleted(continuation, state, token, flags);
void IValueTaskSource.GetResult(short token) => GetResultAndRelease(token);
int IValueTaskSource<int>.GetResult(short token) => GetResultAndRelease(token);

private int GetResultAndRelease(short token)
void IValueTaskSource.GetResult(short token) => GetResult(token);
public int GetResult(short token)
{
try
{
Expand Down
Loading