Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
update and add tests. minor corrections to support the scenarios defi…
Browse files Browse the repository at this point in the history
…ned in the tests
  • Loading branch information
Miguel Alho authored and MiguelAlho committed Dec 3, 2018
1 parent d7b715b commit 70ff2dc
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 52 deletions.
12 changes: 7 additions & 5 deletions src/Serilog.Sinks.Loggly/Sinks/Loggly/FileBufferDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ public void MoveBookmarkForward()
//move to the NEXT file and delete the current one
//there is no garantee the the current one is the first, so we need to make
//sure we start reading the next one based on the current on
var currentBookMarkedFileInFileSet = Array.FindIndex(fileSet, f => f == _currentBookmark.File);
var currentBookMarkedFileInFileSet = _currentBookmark != null
? Array.FindIndex(fileSet, f => f == _currentBookmark.File)
: -1;

// not sure this can occur, but if for some reason the file is no longer in the list
// we should start from the beginning, maybe; being a bit defensive here due to
Expand All @@ -173,16 +175,16 @@ public void MoveBookmarkForward()
}
else
{
//if index of current is not the last , use next; otherwise use none
//if index of current is not the last , use next; otherwise preserve current
_currentBookmark = currentBookMarkedFileInFileSet <= fileSet.Length - 2
? new FileSetPosition(0, fileSet[currentBookMarkedFileInFileSet + 1])
: null;
: _currentBookmark;
}

//also clear all the previous files in the set to avoid problems (and because
//they should no longer be considered); If no previous exists (index is -1);
//keep existing
for (int i = 0; i <= currentBookMarkedFileInFileSet; i++)
//keep existing; also do not reomve last file as it may be written to / locked
for (int i = 0; i <= currentBookMarkedFileInFileSet && i < fileSet.Length-1; i++)
{
_fileSystemAdapter.DeleteFile(fileSet[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,60 +530,267 @@ public class MoveBookmarkForwardCorrectly
{
public class NoRetentionLimitOnBuffer
{
readonly List<string> _deletedFiles = new List<string>();
FileSetPosition _sut;

public NoRetentionLimitOnBuffer()
public class NoPreviousBookmark
{
var bookmarkProvider = Substitute.For<IBookmarkProvider>();
bookmarkProvider
.When(x => x.UpdateBookmark(Arg.Any<FileSetPosition>()))
.Do(x => _sut = x.ArgAt<FileSetPosition>(0));

IFileSystemAdapter fsAdapter = CreateFileSystemAdapter();

var provider = new FileBufferDataProvider(
BaseBufferFileName,
fsAdapter,
bookmarkProvider,
Utf8Encoder,
BatchLimit,
EventSizeLimit,
null);

provider.MoveBookmarkForward();
readonly List<string> _deletedFiles = new List<string>();
FileSetPosition _sut;

public NoPreviousBookmark()
{
var bookmarkProvider = Substitute.For<IBookmarkProvider>();
bookmarkProvider
.When(x => x.UpdateBookmark(Arg.Any<FileSetPosition>()))
.Do(x => _sut = x.ArgAt<FileSetPosition>(0));
bookmarkProvider.GetCurrentBookmarkPosition().Returns(null as FileSetPosition);

IFileSystemAdapter fsAdapter = CreateFileSystemAdapter();

var provider = new FileBufferDataProvider(
BaseBufferFileName,
fsAdapter,
bookmarkProvider,
Utf8Encoder,
BatchLimit,
EventSizeLimit,
null);


provider.MoveBookmarkForward();
}

IFileSystemAdapter CreateFileSystemAdapter()
{
var fileSystemAdapter = Substitute.For<IFileSystemAdapter>();

//get files should return the single buffer file path in this scenario
fileSystemAdapter.GetFiles(Arg.Any<string>(), Arg.Any<string>())
.Returns(new[] {@"c:\a\file001.json", @"c:\a\file002.json", @"c:\a\file003.json"});

//files exist
fileSystemAdapter.Exists(Arg.Any<string>()).Returns(true);
fileSystemAdapter
.When(x => x.DeleteFile(Arg.Any<string>()))
.Do(x => _deletedFiles.Add(x.ArgAt<string>(0)));

return fileSystemAdapter;
}

[Fact]
public void BookmarkShouldBeAtStartOfNextFile() => Assert.Equal(0, _sut.NextLineStart);

[Fact]
public void BookmarkShouldBeAtNextFile() => Assert.Equal(@"c:\a\file001.json", _sut.File);

[Fact]
public void NoFileShouldHaveBeenDeleted() =>
Assert.Empty(_deletedFiles);
}

IFileSystemAdapter CreateFileSystemAdapter()
public class PreviousBookmarkStartedOnFirstFile
{
var fileSystemAdapter = Substitute.For<IFileSystemAdapter>();

//get files should return the single buffer file path in this scenario
fileSystemAdapter.GetFiles(Arg.Any<string>(), Arg.Any<string>())
.Returns(new[] {@"c:\a\file001.json", @"c:\a\file002.json", @"c:\a\file003.json"});

//files exist
fileSystemAdapter.Exists(Arg.Any<string>()).Returns(true);
fileSystemAdapter
.When(x => x.DeleteFile(Arg.Any<string>()))
.Do(x => _deletedFiles.Add(x.ArgAt<string>(0)));

return fileSystemAdapter;
readonly List<string> _deletedFiles = new List<string>();
FileSetPosition _sut;

public PreviousBookmarkStartedOnFirstFile()
{
var bookmarkProvider = Substitute.For<IBookmarkProvider>();
bookmarkProvider
.When(x => x.UpdateBookmark(Arg.Any<FileSetPosition>()))
.Do(x => _sut = x.ArgAt<FileSetPosition>(0));
bookmarkProvider.GetCurrentBookmarkPosition().Returns(new FileSetPosition(0, @"c:\a\file001.json"));

IFileSystemAdapter fsAdapter = CreateFileSystemAdapter();

var provider = new FileBufferDataProvider(
BaseBufferFileName,
fsAdapter,
bookmarkProvider,
Utf8Encoder,
BatchLimit,
EventSizeLimit,
null);

//force the current Bookmark to be current file:
//This unfortunately depends on the "NoPreviousBookmark" test working / passing
//some method of setting the current in the provider could also help setup
provider.MoveBookmarkForward();

//excercise the SUT
provider.MoveBookmarkForward();
}

IFileSystemAdapter CreateFileSystemAdapter()
{
var fileSystemAdapter = Substitute.For<IFileSystemAdapter>();

//get files should return the single buffer file path in this scenario
fileSystemAdapter.GetFiles(Arg.Any<string>(), Arg.Any<string>())
.Returns(new[] { @"c:\a\file001.json", @"c:\a\file002.json", @"c:\a\file003.json" });

//files exist
fileSystemAdapter.Exists(Arg.Any<string>()).Returns(true);
fileSystemAdapter
.When(x => x.DeleteFile(Arg.Any<string>()))
.Do(x => _deletedFiles.Add(x.ArgAt<string>(0)));

return fileSystemAdapter;
}

[Fact]
public void BookmarkShouldBeAtStartOfNextFile() => Assert.Equal(0, _sut.NextLineStart);

[Fact]
public void BookmarkShouldBeAtNextFile() => Assert.Equal(@"c:\a\file002.json", _sut.File);

[Fact]
public void PreviousFileShouldHaveBeenDeleted() =>
Assert.Equal(@"c:\a\file001.json", _deletedFiles.First());

[Fact]
public void SingleFileShouldHaveBeenDeleted() =>
Assert.Single(_deletedFiles);
}

[Fact]
public void BookmarkShouldBeAtStartOfNextFile() => Assert.Equal(0, _sut.NextLineStart);

[Fact]
public void BookmarkShouldBeAtNextFile() => Assert.Equal(@"c:\a\file002.json", _sut.File);

[Fact]
public void PreviousFileShouldHaveBeenDeleted() =>
Assert.Equal(@"c:\a\file001.json", _deletedFiles.First());
public class PreviousBookmarkStartedOnSecondFile
{
readonly List<string> _deletedFiles = new List<string>();
FileSetPosition _sut;

public PreviousBookmarkStartedOnSecondFile()
{
var bookmarkProvider = Substitute.For<IBookmarkProvider>();
bookmarkProvider
.When(x => x.UpdateBookmark(Arg.Any<FileSetPosition>()))
.Do(x => _sut = x.ArgAt<FileSetPosition>(0));
bookmarkProvider.GetCurrentBookmarkPosition().Returns(new FileSetPosition(0, @"c:\a\file001.json"));

IFileSystemAdapter fsAdapter = CreateFileSystemAdapter();

var provider = new FileBufferDataProvider(
BaseBufferFileName,
fsAdapter,
bookmarkProvider,
Utf8Encoder,
BatchLimit,
EventSizeLimit,
null);

//force the current Bookmark to be current file:
//This unfortunately depends on the "NoPreviousBookmark" test working / passing
//some method of setting the current in the provider could also help setup
provider.MoveBookmarkForward();
provider.MoveBookmarkForward(); //we can ignore the deletes here, for now
//but we need to reset the adapter in use
_deletedFiles.Clear();

//excercise the SUT
provider.MoveBookmarkForward();
}

IFileSystemAdapter CreateFileSystemAdapter()
{
var fileSystemAdapter = Substitute.For<IFileSystemAdapter>();

//get files should return the single buffer file path in this scenario
fileSystemAdapter.GetFiles(Arg.Any<string>(), Arg.Any<string>())
.Returns(new[] { @"c:\a\file001.json", @"c:\a\file002.json", @"c:\a\file003.json" });

//files exist
fileSystemAdapter.Exists(Arg.Any<string>()).Returns(true);
fileSystemAdapter
.When(x => x.DeleteFile(Arg.Any<string>()))
.Do(x => _deletedFiles.Add(x.ArgAt<string>(0)));

return fileSystemAdapter;
}

[Fact]
public void BookmarkShouldBeAtStartOfNextFile() => Assert.Equal(0, _sut.NextLineStart);

[Fact]
public void BookmarkShouldBeAtNextFile() => Assert.Equal(@"c:\a\file003.json", _sut.File);

[Theory]
[InlineData(@"c:\a\file001.json", 0)]
[InlineData(@"c:\a\file002.json", 1)]
public void PreviousFileShouldHaveBeenDeleted(string file, int position) =>
Assert.Equal(file, _deletedFiles[position]);

[Fact]
public void AllPreviousFilesShouldHaveBeenDeleted() =>
Assert.Equal(2, _deletedFiles.Count);
}

[Fact]
public void SingleFileShouldHaveBeenDeleted() =>
Assert.Single(_deletedFiles);
public class PreviousBookmarkStartedOnLastFile
{
readonly List<string> _deletedFiles = new List<string>();
FileSetPosition _sut;

public PreviousBookmarkStartedOnLastFile()
{
var bookmarkProvider = Substitute.For<IBookmarkProvider>();
bookmarkProvider
.When(x => x.UpdateBookmark(Arg.Any<FileSetPosition>()))
.Do(x => _sut = x.ArgAt<FileSetPosition>(0));
bookmarkProvider.GetCurrentBookmarkPosition().Returns(new FileSetPosition(0, @"c:\a\file001.json"));

IFileSystemAdapter fsAdapter = CreateFileSystemAdapter();

var provider = new FileBufferDataProvider(
BaseBufferFileName,
fsAdapter,
bookmarkProvider,
Utf8Encoder,
BatchLimit,
EventSizeLimit,
null);

//force the current Bookmark to be current file:
//This unfortunately depends on the "NoPreviousBookmark" test working / passing
//some method of setting the current in the provider could also help setup
provider.MoveBookmarkForward();
provider.MoveBookmarkForward();
provider.MoveBookmarkForward(); //we can ignore the deletes here, for now
//but we need to reset the adapter in use
_deletedFiles.Clear();

//excercise the SUT
provider.MoveBookmarkForward();
}

IFileSystemAdapter CreateFileSystemAdapter()
{
var fileSystemAdapter = Substitute.For<IFileSystemAdapter>();

//get files should return the single buffer file path in this scenario
fileSystemAdapter.GetFiles(Arg.Any<string>(), Arg.Any<string>())
.Returns(new[] { @"c:\a\file001.json", @"c:\a\file002.json", @"c:\a\file003.json" });

//files exist
fileSystemAdapter.Exists(Arg.Any<string>()).Returns(true);
fileSystemAdapter
.When(x => x.DeleteFile(Arg.Any<string>()))
.Do(x => _deletedFiles.Add(x.ArgAt<string>(0)));

return fileSystemAdapter;
}

[Fact]
public void BookmarkShouldBeAtEndOfLastFile() => Assert.Equal(0, _sut.NextLineStart); //file is empty so zero is the end in this setup

[Fact]
public void BookmarkShouldStillPointToLastFile() => Assert.Equal(@"c:\a\file003.json", _sut.File);

[Theory]
[InlineData(@"c:\a\file001.json", 0)]
[InlineData(@"c:\a\file002.json", 1)]
public void PreviousFileShouldHaveBeenDeleted(string file, int position) =>
Assert.Equal(file, _deletedFiles[position]);

[Fact]
public void AllPreviousFilesShouldHaveBeenDeleted() =>
Assert.Equal(2, _deletedFiles.Count);
}
}

public class RetentionLimitLessThenLimitNumberOfBufferFiles
Expand Down

0 comments on commit 70ff2dc

Please sign in to comment.