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

The mock FileStream class does not handle shared file contents correctly. #1131

Open
BillArmstrong opened this issue Jul 11, 2024 · 1 comment
Labels
state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality

Comments

@BillArmstrong
Copy link

The source code below should write the values 0-9 to the console. Instead it writes the value 0 to the console ten times.

The code behaves as expected when using the real file system.

using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;

//var fileSystem = new FileSystem();
var fileSystem = new MockFileSystem();

var filename = fileSystem.Path.GetTempFileName();
try
{
    using var file1 = fileSystem.FileStream.New(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
    using var file2 = fileSystem.FileStream.New(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
    var buffer = new byte[4];

    for (int ix = 0; ix < 10; ix++)
    {
        file1.Position = 0;
        file1.Write(BitConverter.GetBytes(ix));
        file1.Flush();

        file2.Position = 0;
        file2.Flush();
        file2.Read(buffer);
        Console.WriteLine(BitConverter.ToInt32(buffer));
    }
}
finally
{
    fileSystem.File.Delete(filename);
}
@BillArmstrong BillArmstrong added state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality labels Jul 11, 2024
@vbreuss
Copy link
Member

vbreuss commented Jul 13, 2024

Thanks for reporting this issue. The root cause, seems to be, that the underlying MemoryStream is not updated when accessed from a parallel file handle.

@vbreuss vbreuss added state: ready to pick Issues that are ready for being worked on and removed state: needs discussion Issues that need further discussion labels Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality
Projects
None yet
Development

No branches or pull requests

2 participants