Skip to content
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 @@ -161,6 +161,31 @@ public async Task LengthIsNotCachedAfterHandleHasBeenExposed(FileAccess fileAcce
Assert.Equal(2, stream.Length);
Assert.Equal(2, createdFromHandle.Length);
}

[Fact]
public async Task WriteByteFlushesTheBufferWhenItBecomesFull()
{
string filePath;
List<byte> writtenBytes = new List<byte>();

using (FileStream stream = (FileStream)await CreateWriteOnlyStreamCore(Array.Empty<byte>()))
{
filePath = stream.Name;

stream.WriteByte(0);
writtenBytes.Add(0);

byte[] bytes = new byte[BufferSize - 1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth putting something other than zeros in here? So it's distinct from the first byte you wrote

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really matter. What matters is that we are attempting to write an 11th character on the stream when the buffer can only hold 10 characters.

Copy link
Member

@jozkee jozkee Apr 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and the problem was that the internal buffer is not being reset/flushed when WriteByte is called when this buffer is already full.

stream.Write(bytes.AsSpan());
writtenBytes.AddRange(bytes);

stream.WriteByte(1);
writtenBytes.Add(1);
}

byte[] allBytes = File.ReadAllBytes(filePath);
Assert.Equal(writtenBytes.ToArray(), allBytes);
}
}

public class UnbufferedSyncFileStreamStandaloneConformanceTests : FileStreamStandaloneConformanceTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private void WriteByteSlow(byte value)
ClearReadBufferBeforeWrite();
EnsureBufferAllocated();
}
else if (_writePos == _bufferSize - 1)
else
{
FlushWrite();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this method should only be called when either _writePos == 0 or _writePos == _bufferSize, right?
Consider adding the assertion, but not in this PR since we want to merge it ASAP.

Suggested change
FlushWrite();
Debug.Assert(_writePos == _bufferSize);
FlushWrite();

}
Expand Down