From 08a5d13d65e08bdd27e1f298a4414dcc3d54605e Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Mon, 3 Jun 2024 14:15:09 +0200 Subject: [PATCH] improve tests --- .../HeidiConfigurationServiceTests.cs | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/tests/RepoM.Plugin.Heidi.Tests/Internal/HeidiConfigurationServiceTests.cs b/tests/RepoM.Plugin.Heidi.Tests/Internal/HeidiConfigurationServiceTests.cs index dac33c30..c2de0379 100644 --- a/tests/RepoM.Plugin.Heidi.Tests/Internal/HeidiConfigurationServiceTests.cs +++ b/tests/RepoM.Plugin.Heidi.Tests/Internal/HeidiConfigurationServiceTests.cs @@ -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; @@ -55,7 +57,7 @@ public HeidiConfigurationServiceTests() A.CallTo(() => _fileSystem.FileSystemWatcher.New(A._, A._)).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 @@ -123,11 +125,11 @@ public static IEnumerable CtorNullArguments var hre = A.Dummy(); var hs = A.Dummy(); - 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,]; } } @@ -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 result = _sut.GetAllDatabases(); @@ -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; @@ -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 result = _sut.GetByRepository(_repository); @@ -217,20 +219,20 @@ public async Task GetByRepository_ShouldReturnEmpty_WhenInitializationIsFinished { // arrange A.CallTo(() => _repository.Remotes) - .Returns(new List - { + .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 result = _sut.GetByRepository(_repository); @@ -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 result = _sut.GetByKey(key!); @@ -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._, A._)).MustHaveHappened(); - A.CallTo(() => _configReader.ParseAsync(fullFilename)).MustHaveHappenedOnceExactly(); + A.CallTo(() => _configReader.ParseAsync(_fullFilename)).MustHaveHappenedOnceExactly(); } [Fact] @@ -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() { @@ -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 @@ -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] @@ -369,7 +369,7 @@ public async Task Dispose_ShouldRemoveSubscription() .Returns(Task.FromResult(new List())); await _sut.InitializeAsync(); - mre.WaitOne(TimeSpan.FromSeconds(1)); + mre.WaitOne(_initTimeout); Fake.ClearRecordedCalls(_configReader); @@ -377,10 +377,10 @@ public async Task Dispose_ShouldRemoveSubscription() _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]