Skip to content

Commit

Permalink
feat: Add Flush(bool) to FileSystemStream. (#985)
Browse files Browse the repository at this point in the history
`FileSystemStream` was missing the implementation of `FileStream.Flush(bool)` which allows the caller to specify whether `Flush` should ensure all buffers are written to disk.
  • Loading branch information
gregington committed May 12, 2023
1 parent 2777be0 commit ccdc24d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public override void Flush()
InternalFlush();
}

/// <inheritdoc />
public override void Flush(bool flushToDisk)
=> InternalFlush();

/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ public void SetAccessControl(object value)
throw new ArgumentException("value must be of type `FileSecurity`");
}
}

/// <inheritdoc />
public override void Flush(bool flushToDisk)
=> fileStream.Flush(flushToDisk);
}
}
4 changes: 4 additions & 0 deletions src/TestableIO.System.IO.Abstractions/FileSystemStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public override void EndWrite(IAsyncResult asyncResult)
public override void Flush()
=> _stream.Flush();

/// <inheritDoc cref="FileStream.Flush(bool)" />
public virtual void Flush(bool flushToDisk)
=> _stream.Flush();

/// <inheritdoc cref="Stream.FlushAsync(CancellationToken)" />
public override Task FlushAsync(CancellationToken cancellationToken)
=> _stream.FlushAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,25 @@ public void MockFileStream_Flush_ShouldNotChangePosition()
Assert.AreEqual(200, stream.Position);
}
}

[Test]
public void MockFileStream_FlushBool_ShouldNotChangePosition([Values] bool flushToDisk)
{
// Arrange
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
fileSystem.AddFile(path, new MockFileData(new byte[0]));

using (var stream = fileSystem.FileInfo.New(path).OpenWrite())
{
// Act
stream.Write(new byte[400], 0, 400);
stream.Seek(200, SeekOrigin.Begin);
stream.Flush(flushToDisk);

// Assert
Assert.AreEqual(200, stream.Position);
}
}
}
}

0 comments on commit ccdc24d

Please sign in to comment.