Skip to content

Commit da7bce7

Browse files
authored
Change RandomAccess.Write* methods to be void-returning (#55490)
1 parent f787f38 commit da7bce7

15 files changed

Lines changed: 346 additions & 244 deletions

File tree

src/libraries/System.IO.FileSystem/tests/RandomAccess/Mixed.Windows.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,17 @@ static async Task Validate(SafeFileHandle handle, FileOptions options, bool[] sy
6262
{
6363
writeBuffer[0] = (byte)fileOffset;
6464

65-
Assert.Equal(writeBuffer.Length, syncWrite ? RandomAccess.Write(handle, writeBuffer, fileOffset) : await RandomAccess.WriteAsync(handle, writeBuffer, fileOffset));
65+
if (syncWrite)
66+
{
67+
RandomAccess.Write(handle, writeBuffer, fileOffset);
68+
}
69+
else
70+
{
71+
await RandomAccess.WriteAsync(handle, writeBuffer, fileOffset);
72+
}
73+
6674
Assert.Equal(writeBuffer.Length, syncRead ? RandomAccess.Read(handle, readBuffer, fileOffset) : await RandomAccess.ReadAsync(handle, readBuffer, fileOffset));
75+
6776
Assert.Equal(writeBuffer[0], readBuffer[0]);
6877

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

119-
Assert.Equal(writeBuffer_1.Length + writeBuffer_2.Length, syncWrite ? RandomAccess.Write(handle, writeBuffers, fileOffset) : await RandomAccess.WriteAsync(handle, writeBuffers, fileOffset));
128+
if (syncWrite)
129+
{
130+
RandomAccess.Write(handle, writeBuffers, fileOffset);
131+
}
132+
else
133+
{
134+
await RandomAccess.WriteAsync(handle, writeBuffers, fileOffset);
135+
}
136+
120137
Assert.Equal(writeBuffer_1.Length + writeBuffer_2.Length, syncRead ? RandomAccess.Read(handle, readBuffers, fileOffset) : await RandomAccess.ReadAsync(handle, readBuffers, fileOffset));
138+
121139
Assert.Equal(writeBuffer_1[0], readBuffer_1[0]);
122140
Assert.Equal(writeBuffer_2[0], readBuffer_2[0]);
123141

src/libraries/System.IO.FileSystem/tests/RandomAccess/NoBuffering.Windows.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ public async Task WriteUsingSingleBuffer(bool async)
118118
int take = Math.Min(content.Length - total, bufferSize);
119119
content.AsSpan(total, take).CopyTo(buffer.GetSpan());
120120

121-
total += async
122-
? await RandomAccess.WriteAsync(handle, buffer.Memory, fileOffset: total)
123-
: RandomAccess.Write(handle, buffer.GetSpan(), fileOffset: total);
121+
if (async)
122+
{
123+
await RandomAccess.WriteAsync(handle, buffer.Memory, fileOffset: total);
124+
}
125+
else
126+
{
127+
RandomAccess.Write(handle, buffer.GetSpan(), fileOffset: total);
128+
}
129+
130+
total += buffer.Memory.Length;
124131
}
125132
}
126133

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

157-
total += async
158-
? await RandomAccess.WriteAsync(handle, buffers, fileOffset: total)
159-
: RandomAccess.Write(handle, buffers, fileOffset: total);
164+
if (async)
165+
{
166+
await RandomAccess.WriteAsync(handle, buffers, fileOffset: total);
167+
}
168+
else
169+
{
170+
RandomAccess.Write(handle, buffers, fileOffset: total);
171+
}
172+
173+
total += buffer_1.Memory.Length + buffer_2.Memory.Length;
160174
}
161175
}
162176

src/libraries/System.IO.FileSystem/tests/RandomAccess/Write.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ namespace System.IO.Tests
1010
public class RandomAccess_Write : RandomAccess_Base<int>
1111
{
1212
protected override int MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
13-
=> RandomAccess.Write(handle, bytes, fileOffset);
13+
{
14+
RandomAccess.Write(handle, bytes, fileOffset);
15+
return bytes?.Length ?? 0;
16+
}
1417

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

2528
[Theory]
2629
[MemberData(nameof(GetSyncAsyncOptions))]
27-
public void WriteUsingEmptyBufferReturnsZero(FileOptions options)
30+
public void WriteUsingEmptyBufferReturns(FileOptions options)
2831
{
2932
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
3033
{
31-
Assert.Equal(0, RandomAccess.Write(handle, Array.Empty<byte>(), fileOffset: 0));
34+
RandomAccess.Write(handle, Array.Empty<byte>(), fileOffset: 0);
3235
}
3336
}
3437

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

4245
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options))
4346
{
44-
Assert.Equal(stackAllocated.Length, RandomAccess.Write(handle, stackAllocated, fileOffset: 0));
47+
RandomAccess.Write(handle, stackAllocated, fileOffset: 0);
4548
}
4649

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

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

67-
current = RandomAccess.Write(handle, buffer, fileOffset: total);
68-
69-
Assert.InRange(current, 0, buffer.Length);
69+
RandomAccess.Write(handle, buffer, fileOffset: total);
7070

71-
total += current;
71+
total += buffer.Length;
7272
}
7373
}
7474

src/libraries/System.IO.FileSystem/tests/RandomAccess/WriteAsync.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace System.IO.Tests
1111
{
1212
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
1313
[SkipOnPlatform(TestPlatforms.Browser, "async file IO is not supported on browser")]
14-
public class RandomAccess_WriteAsync : RandomAccess_Base<ValueTask<int>>
14+
public class RandomAccess_WriteAsync : RandomAccess_Base<ValueTask>
1515
{
16-
protected override ValueTask<int> MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
16+
protected override ValueTask MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
1717
=> RandomAccess.WriteAsync(handle, bytes, fileOffset);
1818

1919
[Theory]
@@ -44,11 +44,11 @@ public async Task ThrowsOnReadAccess(FileOptions options)
4444

4545
[Theory]
4646
[MemberData(nameof(GetSyncAsyncOptions))]
47-
public async Task WriteUsingEmptyBufferReturnsZeroAsync(FileOptions options)
47+
public async Task WriteUsingEmptyBufferReturnsAsync(FileOptions options)
4848
{
4949
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
5050
{
51-
Assert.Equal(0, await RandomAccess.WriteAsync(handle, Array.Empty<byte>(), fileOffset: 0));
51+
await RandomAccess.WriteAsync(handle, Array.Empty<byte>(), fileOffset: 0);
5252
}
5353
}
5454

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

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

72-
current = await RandomAccess.WriteAsync(handle, buffer, fileOffset: total);
71+
await RandomAccess.WriteAsync(handle, buffer, fileOffset: total);
7372

74-
Assert.InRange(current, 0, buffer.Length);
75-
76-
total += current;
73+
total += buffer.Length;
7774
}
7875
}
7976

src/libraries/System.IO.FileSystem/tests/RandomAccess/WriteGather.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ namespace System.IO.Tests
1212
public class RandomAccess_WriteGather : RandomAccess_Base<long>
1313
{
1414
protected override long MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
15-
=> RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { bytes }, fileOffset);
15+
{
16+
RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { bytes }, fileOffset);
17+
return bytes?.Length ?? 0;
18+
}
1619

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

3740
[Theory]
3841
[MemberData(nameof(GetSyncAsyncOptions))]
39-
public void WriteUsingEmptyBufferReturnsZero(FileOptions options)
42+
public void WriteUsingEmptyBufferReturns(FileOptions options)
4043
{
4144
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
4245
{
43-
Assert.Equal(0, RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0));
46+
RandomAccess.Write(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0);
4447
}
4548
}
4649

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

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

66-
current = RandomAccess.Write(
68+
RandomAccess.Write(
6769
handle,
6870
new ReadOnlyMemory<byte>[]
6971
{
@@ -73,9 +75,7 @@ public void WritesBytesFromGivenBuffersToGivenFileAtGivenOffset(FileOptions opti
7375
},
7476
fileOffset: total);
7577

76-
Assert.InRange(current, 0, buffer_1.Length + buffer_2.Length);
77-
78-
total += current;
78+
total += buffer_1.Length + buffer_2.Length;
7979
}
8080
}
8181

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

9595
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options))
9696
{
97-
Assert.Equal(repeatCount, RandomAccess.Write(handle, buffers, fileOffset: 0));
97+
RandomAccess.Write(handle, buffers, fileOffset: 0);
9898
}
9999

100100
byte[] actualContent = File.ReadAllBytes(filePath);

src/libraries/System.IO.FileSystem/tests/RandomAccess/WriteGatherAsync.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace System.IO.Tests
1313
{
1414
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
1515
[SkipOnPlatform(TestPlatforms.Browser, "async file IO is not supported on browser")]
16-
public class RandomAccess_WriteGatherAsync : RandomAccess_Base<ValueTask<long>>
16+
public class RandomAccess_WriteGatherAsync : RandomAccess_Base<ValueTask>
1717
{
18-
protected override ValueTask<long> MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
18+
protected override ValueTask MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset)
1919
=> RandomAccess.WriteAsync(handle, new ReadOnlyMemory<byte>[] { bytes }, fileOffset);
2020

2121
[Theory]
@@ -56,11 +56,11 @@ public async Task ThrowsOnReadAccess(FileOptions options)
5656

5757
[Theory]
5858
[MemberData(nameof(GetSyncAsyncOptions))]
59-
public async Task WriteUsingEmptyBufferReturnsZeroAsync(FileOptions options)
59+
public async Task WriteUsingEmptyBufferReturnsAsync(FileOptions options)
6060
{
6161
using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options))
6262
{
63-
Assert.Equal(0, await RandomAccess.WriteAsync(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0));
63+
await RandomAccess.WriteAsync(handle, new ReadOnlyMemory<byte>[] { Array.Empty<byte>() }, fileOffset: 0);
6464
}
6565
}
6666

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

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

86-
current = await RandomAccess.WriteAsync(
85+
await RandomAccess.WriteAsync(
8786
handle,
8887
new ReadOnlyMemory<byte>[]
8988
{
@@ -93,9 +92,7 @@ public async Task WritesBytesFromGivenBufferToGivenFileAtGivenOffsetAsync(FileOp
9392
},
9493
fileOffset: total);
9594

96-
Assert.InRange(current, 0, buffer_1.Length + buffer_2.Length);
97-
98-
total += current;
95+
total += buffer_1.Length + buffer_2.Length;
9996
}
10097
}
10198

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

115112
using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options))
116113
{
117-
Assert.Equal(repeatCount, await RandomAccess.WriteAsync(handle, buffers, fileOffset: 0));
114+
await RandomAccess.WriteAsync(handle, buffers, fileOffset: 0);
118115
}
119116

120117
byte[] actualContent = File.ReadAllBytes(filePath);

src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.OverlappedValueTaskSource.Windows.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,8 @@ internal static Exception GetIOError(int errorCode, string? path)
8686

8787
public ValueTaskSourceStatus GetStatus(short token) => _source.GetStatus(token);
8888
public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _source.OnCompleted(continuation, state, token, flags);
89-
void IValueTaskSource.GetResult(short token) => GetResultAndRelease(token);
90-
int IValueTaskSource<int>.GetResult(short token) => GetResultAndRelease(token);
91-
92-
private int GetResultAndRelease(short token)
89+
void IValueTaskSource.GetResult(short token) => GetResult(token);
90+
public int GetResult(short token)
9391
{
9492
try
9593
{

0 commit comments

Comments
 (0)