Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Jun 3, 2024
1 parent 3ed17cf commit 08a5d13
Showing 1 changed file with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class HeidiConfigurationServiceTests
{
private const string FILENAME = "heidi.portable.txt";
private const string PATH = "C:\\heidi\\";
private readonly string _fullFilename = Path.Combine(PATH, FILENAME);
private readonly TimeSpan _initTimeout = TimeSpan.FromSeconds(30);

private readonly IFileSystem _fileSystem;
private readonly IHeidiPortableConfigReader _configReader;
Expand Down Expand Up @@ -55,7 +57,7 @@ public HeidiConfigurationServiceTests()
A.CallTo(() => _fileSystem.FileSystemWatcher.New(A<string>._, A<string>._)).Returns(_fileWatcher);
A.CallTo(() => _heidiSettings.ConfigFilename).Returns(FILENAME);
A.CallTo(() => _heidiSettings.ConfigPath).Returns(PATH);
A.CallTo(() => _fileSystem.File.Exists(Path.Combine(PATH, FILENAME))).Returns(true);
A.CallTo(() => _fileSystem.File.Exists(_fullFilename)).Returns(true);

A.CallTo(() => _repository.Remotes)
.Returns(new List<Remote>
Expand Down Expand Up @@ -123,11 +125,11 @@ public static IEnumerable<object[]> CtorNullArguments
var hre = A.Dummy<IHeidiRepositoryExtractor>();
var hs = A.Dummy<IHeidiSettings>();

yield return new object[] { l, fs, hpcr, hre, null!, };
yield return new object[] { l, fs, hpcr, null!, hs, };
yield return new object[] { l, fs, null!, hre, hs, };
yield return new object[] { l, null!, hpcr, hre, hs, };
yield return new object[] { null!, fs, hpcr, hre, hs, };
yield return [l, fs, hpcr, hre, null!,];
yield return [l, fs, hpcr, null!, hs,];
yield return [l, fs, null!, hre, hs,];
yield return [l, null!, hpcr, hre, hs,];
yield return [null!, fs, hpcr, hre, hs,];
}
}

Expand All @@ -153,13 +155,13 @@ internal void Ctor_ShouldThrow_WhenArgumentIsNull(
public async Task GetAllDatabases_ShouldReturnDatabases_WhenInitializedCompleted()
{
// arrange
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME)))
A.CallTo(() => _configReader.ParseAsync(_fullFilename))
.Returns(Task.FromResult(_heidiConfigurationResult));
var mre = new ManualResetEvent(false);
_sut.ConfigurationUpdated += (_, _) => mre.Set();

await _sut.InitializeAsync();
mre.WaitOne();
mre.WaitOne(_initTimeout);

// act
ImmutableArray<HeidiSingleDatabaseConfiguration> result = _sut.GetAllDatabases();
Expand All @@ -174,8 +176,8 @@ public async Task GetByRepository_ShouldReturnDatabasesForSpecificRepository_Whe
// arrange
var mre = new ManualResetEvent(false);
_sut.ConfigurationUpdated += (_, _) => mre.Set();
A.CallTo(() => _fileSystem.File.Exists(Path.Combine(PATH, FILENAME))).Returns(true);
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME)))
A.CallTo(() => _fileSystem.File.Exists(_fullFilename)).Returns(true);
A.CallTo(() => _configReader.ParseAsync(_fullFilename))
.Returns(Task.FromResult(_heidiConfigurationResult));

RepoHeidi? aRef;
Expand All @@ -191,17 +193,17 @@ public async Task GetByRepository_ShouldReturnDatabasesForSpecificRepository_Whe
})
.AssignsOutAndRefParametersLazily((HeidiSingleDatabaseConfiguration config, RepoHeidi? output) =>
{
var matches = _heidis.Where(x => x.HeidiKey.Equals(config.Key)).ToArray();
RepoHeidi[] matches = _heidis.Where(x => x.HeidiKey.Equals(config.Key)).ToArray();
if (matches.Length > 0)
{
return new object[] { matches.First(), };
return [matches.First(),];
}
return new object[] { null!, };
return [null!,];
});

await _sut.InitializeAsync();
mre.WaitOne(TimeSpan.FromSeconds(2));
mre.WaitOne(_initTimeout);

// act
IEnumerable<RepositoryHeidiConfiguration> result = _sut.GetByRepository(_repository);
Expand All @@ -217,20 +219,20 @@ public async Task GetByRepository_ShouldReturnEmpty_WhenInitializationIsFinished
{
// arrange
A.CallTo(() => _repository.Remotes)
.Returns(new List<Remote>
{
.Returns(
[
new("bb", "http://github.com/a/b.git"),
new(originKey, "http://github.com/coenm/Abcd.git"),
});
]);

var mre = new ManualResetEvent(false);
_sut.ConfigurationUpdated += (_, _) => mre.Set();
A.CallTo(() => _fileSystem.File.Exists(Path.Combine(PATH, FILENAME))).Returns(true);
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME)))
A.CallTo(() => _fileSystem.File.Exists(_fullFilename)).Returns(true);
A.CallTo(() => _configReader.ParseAsync(_fullFilename))
.Returns(Task.FromResult(_heidiConfigurationResult));

await _sut.InitializeAsync();
mre.WaitOne(TimeSpan.FromSeconds(2));
mre.WaitOne(_initTimeout);

// act
IEnumerable<RepositoryHeidiConfiguration> result = _sut.GetByRepository(_repository);
Expand All @@ -249,12 +251,12 @@ public async Task GetByKey_ShouldReturnEmpty_WhenKeyIsNullOrEmpty(string? key)
// arrange
var mre = new ManualResetEvent(false);
_sut.ConfigurationUpdated += (_, _) => mre.Set();
A.CallTo(() => _fileSystem.File.Exists(Path.Combine(PATH, FILENAME))).Returns(true);
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME)))
A.CallTo(() => _fileSystem.File.Exists(_fullFilename)).Returns(true);
A.CallTo(() => _configReader.ParseAsync(_fullFilename))
.Returns(Task.FromResult(_heidiConfigurationResult));

await _sut.InitializeAsync();
mre.WaitOne(TimeSpan.FromSeconds(2));
mre.WaitOne(_initTimeout);

// act
IEnumerable<RepositoryHeidiConfiguration> result = _sut.GetByKey(key!);
Expand All @@ -266,21 +268,20 @@ public async Task GetByKey_ShouldReturnEmpty_WhenKeyIsNullOrEmpty(string? key)
[Fact]
public async Task InitializeAsync_ShouldReadHeidiFile_WhenFileExists()
{
var fullFilename = Path.Combine(PATH, FILENAME);
var mre = new ManualResetEvent(false);
_sut.ConfigurationUpdated += (_, _) => mre.Set();
A.CallTo(() => _fileSystem.File.Exists(fullFilename)).Returns(true);
A.CallTo(() => _configReader.ParseAsync(fullFilename))
A.CallTo(() => _fileSystem.File.Exists(_fullFilename)).Returns(true);
A.CallTo(() => _configReader.ParseAsync(_fullFilename))
.Returns(Task.FromResult(_heidiConfigurationResult));

// act
await _sut.InitializeAsync();
var updated = mre.WaitOne(TimeSpan.FromSeconds(5));
var updated = mre.WaitOne(_initTimeout);

// assert
updated.Should().BeTrue("we expect an event to happen.");
A.CallTo(() => _fileSystem.FileSystemWatcher.New(A<string>._, A<string>._)).MustHaveHappened();
A.CallTo(() => _configReader.ParseAsync(fullFilename)).MustHaveHappenedOnceExactly();
A.CallTo(() => _configReader.ParseAsync(_fullFilename)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand Down Expand Up @@ -324,13 +325,12 @@ public async Task Events_ShouldNotBeProcessed_WhenWrongFilename()

// act
_changeEventDummyFileSystemWatcher.Change(PATH, FILENAME + "dummy");
await Task.Delay(5000);
await Task.Delay(TimeSpan.FromSeconds(10));

// assert
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME))).MustNotHaveHappened();
A.CallTo(() => _configReader.ParseAsync(_fullFilename)).MustNotHaveHappened();
}

// fragille test, failed once on build server
[Fact]
public async Task Events_ShouldBeBundled_WhenHappeningWithinWindow()
{
Expand All @@ -343,7 +343,7 @@ public async Task Events_ShouldBeBundled_WhenHappeningWithinWindow()
Task.FromResult(_heidiConfigurationResult));

await _sut.InitializeAsync();
mre.WaitOne(TimeSpan.FromSeconds(1));
mre.WaitOne(_initTimeout);
Fake.ClearRecordedCalls(_configReader);

// act
Expand All @@ -353,10 +353,10 @@ public async Task Events_ShouldBeBundled_WhenHappeningWithinWindow()
await Task.Delay(100);
_changeEventDummyFileSystemWatcher.Change(PATH, FILENAME);

await Task.Delay(5000);
await Task.Delay(TimeSpan.FromSeconds(10));

// assert
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME))).MustHaveHappenedOnceExactly();
A.CallTo(() => _configReader.ParseAsync(_fullFilename)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -369,18 +369,18 @@ public async Task Dispose_ShouldRemoveSubscription()
.Returns(Task.FromResult(new List<HeidiSingleDatabaseConfiguration>()));

await _sut.InitializeAsync();
mre.WaitOne(TimeSpan.FromSeconds(1));
mre.WaitOne(_initTimeout);

Fake.ClearRecordedCalls(_configReader);

// act
_sut.Dispose();

_changeEventDummyFileSystemWatcher.Change(PATH, FILENAME);
await Task.Delay(5000);
await Task.Delay(TimeSpan.FromSeconds(10));

// assert
A.CallTo(() => _configReader.ParseAsync(Path.Combine(PATH, FILENAME))).MustNotHaveHappened();
A.CallTo(() => _configReader.ParseAsync(_fullFilename)).MustNotHaveHappened();
}

[Fact]
Expand Down

0 comments on commit 08a5d13

Please sign in to comment.