Skip to content
Draft
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
15 changes: 13 additions & 2 deletions test/SeqCli.Tests/Forwarder/Filesystem/InMemoryStoreDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,26 @@ public InMemoryStoreFile Create(string name, Span<byte> contents)

public override bool TryDelete(string name)
{
var existing = _files[name];

if (!existing.CanDelete())
{
throw new InvalidOperationException($"Cannot delete {name} with active handles");
}

return _files.Remove(name);
}

public override InMemoryStoreFile Replace(string toReplace, string replaceWith)
{
_files[toReplace] = _files[replaceWith];
_files.Remove(replaceWith);

return _files[toReplace];
if (!TryDelete(toReplace))
{
throw new InvalidOperationException($"Failed to replace {toReplace} with {replaceWith}");
}

return _files[replaceWith];
}

public override IEnumerable<(string Name, StoreFile File)> List(Func<string, bool> predicate)
Expand Down
27 changes: 27 additions & 0 deletions test/SeqCli.Tests/Forwarder/Filesystem/InMemoryStoreFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class InMemoryStoreFile : StoreFile
{
public byte[] Contents { get; private set; } = Array.Empty<byte>();

int _activeReaders;
int _activeWriters;

public override bool TryGetLength([NotNullWhen(true)] out long? length)
{
length = Contents.Length;
Expand All @@ -29,12 +32,36 @@ public void Append(Span<byte> incoming)
public override bool TryOpenRead(long length, [NotNullWhen(true)] out StoreFileReader? reader)
{
reader = new InMemoryStoreFileReader(this, (int)length);
_activeReaders++;

return true;
}

internal void CloseReader()
{
_activeReaders--;
}

public override bool TryOpenAppend([NotNullWhen(true)] out StoreFileAppender? appender)
{
if (_activeWriters != 0)
{
throw new InvalidOperationException("Attempt to open multiple appenders to the same file");
}

appender = new InMemoryStoreFileAppender(this);
_activeWriters++;

return true;
}

internal void CloseAppender()
{
_activeWriters--;
}

internal bool CanDelete()
{
return _activeReaders == 0 && _activeWriters == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public override void Sync()

public override void Dispose()
{
_storeFile.CloseAppender();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public override long CopyTo(Span<byte> buffer, long from = 0, long? length = nul

public override void Dispose()
{
_storeFile.CloseReader();
}
}
Loading